-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmitBagadAssoFormAction.tsx
More file actions
116 lines (102 loc) · 3.7 KB
/
Copy pathsubmitBagadAssoFormAction.tsx
File metadata and controls
116 lines (102 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"use server"
import { revalidatePath } from "next/cache"
import { render } from "react-email"
import { isDevelopment } from "std-env"
import { verifyCaptcha } from "@/components/captcha/verify"
import {
type BagadAssoFormData,
BagadAssoFormSchema
} from "@/components/public/bagadAsso/form-schema"
import prisma from "@/helpers/db"
import { sendEmail } from "@/helpers/email"
import { locationDisplayName } from "@/helpers/location"
import { captureActionError, withServerAction } from "@/lib/sentry"
import { tryCatch } from "@/lib/utils"
import NewBagadAssoTicket from "../../../emails/badagasso-ticket"
export type FormState = {
error?: string
success?: boolean
fieldErrors?: Partial<Record<keyof BagadAssoFormData, string[]>>
}
async function submitBagadAssoFormActionImpl(
_prevState: FormState | undefined,
data: BagadAssoFormData
): Promise<FormState> {
// Validate the data using Zod schema
const result = BagadAssoFormSchema.safeParse(data)
if (!result.success) {
// Extract field errors from Zod
const fieldErrors: Partial<Record<keyof BagadAssoFormData, string[]>> =
{}
for (const issue of result.error.issues) {
const field = issue.path[0] as keyof BagadAssoFormData
if (!fieldErrors[field]) {
fieldErrors[field] = []
}
fieldErrors[field].push(issue.message)
}
return {
error: "Un ou plusieurs champs sont invalides.",
fieldErrors
}
}
const validatedData = result.data
// Verify CAPTCHA in production
if (!isDevelopment) {
if (!validatedData.captchaToken) {
return { error: "Veuillez compléter le CAPTCHA." }
}
const isCaptchaValid = await verifyCaptcha(validatedData.captchaToken)
if (!isCaptchaValid) {
return {
error: "La vérification CAPTCHA a échoué. Veuillez réessayer."
}
}
}
// Create the ticket in the database
const ticket = await tryCatch(
prisma.bagadAssoTicket.create({
data: {
assocation: validatedData.associationName,
associationEmail: validatedData.associationEmail,
firstName: validatedData.referentFirstName,
lastName: validatedData.referentLastName,
position: validatedData.referentPosition,
phoneNumber: validatedData.referentPhone,
representativeEmail: validatedData.referentEmail,
eventName: validatedData.eventName,
eventType: validatedData.eventType,
eventDate: validatedData.eventDate,
eventAddr: validatedData.eventAddress,
estimatedParticipants: validatedData.eventParticipants,
equipments: validatedData.equipment
}
})
)
if (!ticket.success) {
captureActionError(ticket.error)
return {
error: "Le formulaire est incorrect. Veuillez recharger la page et réessayer."
}
}
const ticketRecord = ticket.value
// Email is best-effort: the ticket has already been persisted.
await sendEmail({
to: "evenement@fare-asso.fr",
subject: `Nouveau ticket bagad'Asso #${ticketRecord.id}`,
html: await render(
<NewBagadAssoTicket
data={{
...ticketRecord,
eventAddr: locationDisplayName(ticketRecord.eventAddr)
}}
/>
)
})
revalidatePath("/dashboard/bagadAsso")
return { success: true }
}
export default withServerAction(
"submitBagadAssoFormAction",
submitBagadAssoFormActionImpl
)