summaryrefslogtreecommitdiff
path: root/api/contact-form.ts
blob: a1710200275516cb116a7b5545bb79788f0d19ad (plain)
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
import type { VercelRequest, VercelResponse } from '@vercel/node';
import ContactFormSubmission from '_ContactFormSubmission';
import CONTACT_DETAILS from '_contactDetails';
import { sendEmail } from '_send-email';

export default async function handler(request: VercelRequest, response: VercelResponse) {
  if (request.method !== 'POST') {
		response.status(405).send('To send an enquiry, use the POST method.');
		return;
	}

	const { recipient, name, email, subject, message }: Partial<ContactFormSubmission> = request.body;

	if (!recipient || recipient === '') {
		response.status(400).send('Expected body to identify the recipient, but none was provided.');
	} else if (!name || name === '') {
		response.status(400).send('Expected body to contain the name of the sender, but none was provided.');
	} else if (!email || email === '') {
		response.status(400).send('Expected body to contain the email of the sender, but none was provided.');
	} else if (!subject || subject === '') {
		response.status(400).send('Expected body to contain the subject of the enquiry, but none was provided.');
	} else if (!message || message === '') {
		response.status(400).send('Expected body to contain the message body, but none was provided.');
	}

	const validRecipients = Object.keys(CONTACT_DETAILS);
	if (!validRecipients.includes(recipient)) {
		response.status(400).send(
			'Invalid recipient. Recipient must be one of: '
			+ validRecipients.join(', ')
			+ '.'
		);
	}

	try {
		sendEmail({
			recipient,
			senderName: name,
			senderEmail: email,
			subject,
			body: message,
		});
	} catch (err) {
		response.status(500).send('Failed to send email: ' + err.toString());
		return;
	}

	response.status(201).send(`Sent email to ${CONTACT_DETAILS[recipient].displayName}.`)
}