-
Notifications
You must be signed in to change notification settings - Fork 1
/
contact-form.ts
87 lines (71 loc) · 2.15 KB
/
contact-form.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
import { inspect } from 'util';
import {
uiComponent,
CreateThreadInput,
PlainClient,
PlainSDKError,
} from '@team-plain/typescript-sdk';
import type { NextApiRequest, NextApiResponse } from 'next';
import UAParser from 'ua-parser-js';
const apiKey = process.env.PLAIN_API_KEY;
if (!apiKey) {
throw new Error('PLAIN_API_KEY environment variable is not set');
}
const client = new PlainClient({
apiKey,
});
export type ResponseData = {
error: string | null;
};
export type RequestBody = {
name: string;
email: string;
title: string;
components: CreateThreadInput['components'];
labelTypeIds: string[];
priority: 0 | 1 | 2 | 3 | number;
};
function logError(err: PlainSDKError) {
// This ensures the full error is logged
console.error(inspect(err, { showHidden: false, depth: null, colors: true }));
}
/**
* The API handler, this is what accepts our contact form request and submits it to Plain
*/
export default async function handler(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
// In production validation of the request body might be necessary.
const reqBody = JSON.parse(req.body) as RequestBody;
const upsertCustomerRes = await client.upsertCustomer({
identifier: {
emailAddress: reqBody.email,
},
onCreate: {
fullName: reqBody.name,
email: {
email: reqBody.email,
isVerified: true,
},
},
onUpdate: {},
});
if (upsertCustomerRes.error) {
logError(upsertCustomerRes.error);
return res.status(500).json({ error: upsertCustomerRes.error.message });
}
console.log(`Customer upserted ${upsertCustomerRes.data.customer.id}`);
const createThreadRes = await client.createThread({
customerIdentifier: {
customerId: upsertCustomerRes.data.customer.id,
},
title: reqBody.title,
components: reqBody.components,
labelTypeIds: reqBody.labelTypeIds,
priority: reqBody.priority,
});
if (createThreadRes.error) {
logError(createThreadRes.error);
return res.status(500).json({ error: createThreadRes.error.message });
}
console.log(`Thread created ${createThreadRes.data.id}`);
res.status(200).json({ error: null });
}