-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.ts
158 lines (142 loc) · 4.34 KB
/
actions.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use server';
import { captureException } from '@sentry/nextjs';
import type { LibraryResponse, SendEmailV3_1 } from 'node-mailjet';
import {
INVALID_EMAIL_ERROR_CODE,
INVALID_ERROR_CODES,
mailjetClient,
} from '@/plugins/mailjet';
import { isEmailValid } from '@/utils/is-email-valid';
import {
ContactFormFields,
ContactFormResponseMessage,
ContactFormState,
} from './types';
export async function sendMessage(
_prevState: ContactFormState,
formData: FormData,
): Promise<ContactFormState> {
const {
firstName,
lastName,
email,
company,
projectType,
description,
honeypot,
}: ContactFormFields & { honeypot?: string } = {
firstName: formData.get('firstName') as string,
lastName: formData.get('lastName') as string,
email: formData.get('email') as string,
company: formData.get('company') as string,
projectType: formData.getAll('projectType').join(', '),
description: formData.get('description') as string,
honeypot: formData.get('address') as string,
};
// Honeypot field is filled in, so this is likely a bot.
if (honeypot) {
return {
message: ContactFormResponseMessage.FAILED,
success: false,
};
}
if (!firstName || !lastName || !email || !description) {
const emailIsValid = isEmailValid(email);
const emailValidationMessage = emailIsValid
? ''
: 'Please check that you have entered a valid email address.';
return {
data: {
firstName,
lastName,
email,
company,
projectType,
description,
},
message: ContactFormResponseMessage.INVALID,
success: false,
invalidFieldMessages: {
firstName: firstName ? '' : 'First Name is required.',
lastName: lastName ? '' : 'Last Name is required.',
email: email ? emailValidationMessage : 'Email is required.',
description: description ? '' : 'Message is required.',
},
};
}
const messageBody: SendEmailV3_1.Body = {
Messages: [
{
From: {
Email: 'no-reply@charlesharwood.dev',
Name: 'charlesharwood.dev',
},
To: [
{
Email: process.env.EMAIL_TO_ADDRESS as string,
Name: 'Charles Harwood',
},
],
ReplyTo: {
Email: email,
Name: `${firstName} ${lastName}`,
},
Subject: 'New enquiry from charlesharwood.dev',
HTMLPart: `<h1>You've received a new enquiry from https://charlesharwood.dev:</h1><p>First Name: ${firstName}</p><p>Last Name: ${lastName}</p><p>Email: ${email}</p><p>Company: ${company}</p><p>Project Type: ${projectType}</p><p>Message: ${description}</p>`,
TextPart: `First Name: ${firstName}\nLast Name: ${lastName}\nEmail: ${email}\nCompany: ${company}\nProject Type: ${projectType}\nMessage: ${description}`,
},
],
};
try {
const response: LibraryResponse<SendEmailV3_1.Response> =
await mailjetClient
.post('send', { version: 'v3.1' })
.request(messageBody);
const data = response.body.Messages[0];
if (data.Status === 'success') {
return {
message: ContactFormResponseMessage.SUCCESS,
success: true,
};
}
if (data.Errors.length > 0) {
if (
data.Errors.some(
(error) => error.ErrorCode === INVALID_EMAIL_ERROR_CODE,
)
) {
return {
message: ContactFormResponseMessage.INVALID_EMAIL,
success: false,
};
}
if (
data.Errors.some((error) => INVALID_ERROR_CODES.has(error.ErrorCode))
) {
return {
message: ContactFormResponseMessage.INVALID,
success: false,
};
}
}
// Capture data response errors that haven't been handled above and send to Sentry.
captureException(JSON.stringify(data.Errors));
// Throw any other error codes out to the catch block for generic failure messaging.
throw new Error(ContactFormResponseMessage.FAILED);
} catch (error) {
// Capture unhandled exceptions and send to Sentry.
if (
error instanceof Error &&
error.message !== ContactFormResponseMessage.FAILED
) {
captureException(error);
}
return {
message:
error instanceof Error
? error.message
: ContactFormResponseMessage.FAILED,
success: false,
};
}
}