-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
83 lines (68 loc) · 3.17 KB
/
index.js
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
import { Resend } from "resend";
const addBlockquoteMarkers = msg => msg.split('\n').map(line => `> ${line}`).join('\n');
async function getFacultyEmail(contactId) {
const resp = await fetch('https://raw.githubusercontent.com/nusmodifications/nusmods/master/website/src/data/facultyEmail.json');
const data = await resp.json();
const contact = data.find(contact => contact.id === contactId);
return contact ? contact.email : null;
}
/**
* @typedef {Object} Env
*/
export default {
/**
* @param {Request} request
* @param {Env} env
* @param {ExecutionContext} ctx
* @returns {Promise<Response>}
*/
async fetch(request, env, _) {
const respHeaders = new Headers();
respHeaders.set('Access-Control-Allow-Origin', '*');
respHeaders.set('Access-Control-Allow-Methods', 'POST, OPTIONS');
respHeaders.set('Access-Control-Allow-Headers', '*');
respHeaders.set('Allow', 'POST, OPTIONS');
if (request.method === "OPTIONS") {
return new Response(null, { status: 204, headers: respHeaders });
}
if (request.method !== "POST" || request.body === null) {
return new Response(null, { status: 400, headers: respHeaders });
}
const parsedBody = await request.json();
if (parsedBody === null) {
return new Response(null, { status: 400, headers: respHeaders });
}
const { name, contactId, moduleCode, replyTo, message, matricNumber, debug = false } = parsedBody;
const facultyEmail = await getFacultyEmail(contactId);
let debugMessage = '';
let email;
let cc = [];
if (debug) {
debugMessage = `This is a debug email. If this was in production this would have been sent to <${facultyEmail}>.
========================\n`;
email = '[email protected]';
} else {
email = facultyEmail;
cc = ['[email protected]', replyTo];
}
const moduleUrl = `https://nusmods.com/modules/${moduleCode}`;
const resend = new Resend(env.RESEND_API_KEY);
const { error } = await resend.emails.send({
from: '[email protected]',
to: email,
cc,
reply_to: `${name} <${replyTo}>`,
subject: `[NUSMods] Enquiry/issue about ${moduleCode} on NUSMods from ${name} (${matricNumber})`,
text: `${debugMessage}Hello,
${name} (${matricNumber}) reported the following issue with ${moduleCode} (${moduleUrl}) on NUSMods. Since NUSMods obtains its information directly from the Registrar's Office, we hope you can help check that the information is correct and update it if necessary.
${addBlockquoteMarkers(message)}
Please reply directly to this email to reply to the student. You can also reply to [email protected] (which is cc'd on this email) if you believe the issue is with NUSMods itself. If you have already made changes to the module, please note that it may take up to 24 hours to be reflected on NUSMods.
Regards,
The NUSMods Team`,
});
if (error) {
throw error;
}
return new Response(null, { status: 202, headers: respHeaders });
}
}