-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
87 lines (81 loc) · 2.69 KB
/
app.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
84
85
86
87
const parsers = require("./parsers");
const autocomplete = require("./autocomplete");
const { getSGMail, getSGClient } = require("./helpers");
async function sendEmail(action, settings) {
const mail = getSGMail(settings, action.params);
const {
to, cc, bcc, from, replyTo, subject, text, html, template, dynamicTemplateData,
attachmentPaths, categories, headers, customArgs, sendAt,
} = action.params;
if (!to || !(from || template)) {
throw new Error("One of the required parameters was not provided");
}
const attachments = (
attachmentPaths ? parsers.array(attachmentPaths).map(parsers.sgAttachment) : undefined
);
const request = {
to: parsers.sgEmailAddrMulti(to),
cc: parsers.sgEmailAddrMulti(cc),
bcc: parsers.sgEmailAddrMulti(bcc),
from: parsers.sgEmailAddr(from),
replyTo: parsers.sgEmailAddr(replyTo),
subject: parsers.string(subject),
substitutionWrappers: ["{{", "}}"],
text: text || undefined,
html: html || undefined,
templateId: parsers.autocomplete(template),
dynamicTemplateData: parsers.object(dynamicTemplateData),
attachments,
categories: parsers.autocomplete(categories),
sendAt: sendAt ? Number.parseInt(parsers.autocomplete(sendAt), 10) : undefined,
headers: parsers.object(headers),
customArgs: parsers.object(customArgs),
};
// send mail
const [response] = await mail.send(request);
return { headers: response.headers, status: response.statusCode === 202 ? "processing" : "delivered" };
}
async function getEmailStats(action, settings) {
const client = getSGClient(settings, action.params);
const startDate = parsers.autocomplete(action.params.startDate);
const endDate = parsers.autocomplete(action.params.endDate);
const { limit, offset } = action.params;
return client.request({
method: "GET",
url: "/v3/stats",
qs: {
limit: parsers.number(limit),
offset: parsers.number(offset),
aggregated_by: "day",
end_date: endDate, // || (new Date().toISOString().split('T')[0]),
start_date: startDate,
},
});
}
async function getEventWebhooks(action, settings) {
const client = getSGClient(settings, action.params);
return client.request({
method: "GET",
url: "/v3/user/webhooks/event/settings",
});
}
async function getCategories(action, settings) {
const client = getSGClient(settings, action.params);
const { limit, offset } = action.params;
return client.request({
method: "GET",
url: "/v3/categories",
qs: {
limit: parsers.number(limit),
offset: parsers.number(offset),
},
});
}
module.exports = {
sendEmail,
getEmailStats,
getEventWebhooks,
getCategories,
// Autocomplete Functions
...autocomplete,
};