-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.js
63 lines (51 loc) · 1.67 KB
/
help.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
const log = require("debug")("ak-sender");
const poll_for_rebuild = (api, poll_url, fn) =>
api
.get(poll_url)
.then(
resp =>
resp.body.finished
? fn(null, resp.body.results)
: setTimeout(() => poll_for_rebuild(api, poll_url, fn), 100)
);
const wait_for_rebuild = async (api, mailing_id) => {
const deferred_rebuild = await api.post(`mailer/${mailing_id}/rebuild`);
const poll_url = deferred_rebuild.header.location.replace(
process.env.AK_BASE,
""
);
log(
"Initiated polling for rebubild of %s – poll url is %s",
mailing_id,
poll_url
);
return await new Promise((resolve, reject) =>
poll_for_rebuild(
api,
poll_url,
(err, count) => (err ? reject(err) : resolve(count))
)
);
};
module.exports = {
hasAllKeys: (obj, keys) =>
keys.filter(key => obj[key] === undefined).length == 0,
cloneAndSend: async (api, mailing_id, subject, html) => {
log("Cloning mailing %s", mailing_id);
const cloned = await api.post(`mailer/${mailing_id}/copy`);
const split_location = cloned.header["location"].split("/");
const new_mailing_id = split_location[split_location.length - 2];
log("Copied mailing %s – got new mailing %s.", mailing_id, new_mailing_id);
const patched = await api
.patch(`mailer/${new_mailing_id}`)
.send({ subjects: [subject], html });
log(
"Patched mailing %s with subject %s, now rebuilding",
new_mailing_id,
subject
);
const targeted_count = await wait_for_rebuild(api, new_mailing_id);
const queued = await api.post(`mailer/${new_mailing_id}/queue`);
return { targeted_count, new_mailing_id };
}
};