Skip to content

Commit f4b9bc6

Browse files
committed
Newsletter: schedule send
1 parent c9e8563 commit f4b9bc6

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

.github/actions/render-newsletter/dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/actions/render-newsletter/src/index.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,25 @@ async function run(options: Options) {
211211
});
212212

213213
const url = response.headers.get('location');
214-
console.log(response.status, response.statusText, response.headers, await response.text());
215214

216-
setOutput('send_date', sendAt.toISOString());
217-
setOutput('single_send_url', url);
218-
219-
return {
220-
sendAt,
221-
url
222-
};
215+
if (response.ok) {
216+
const ssend: SG.ScheduledSend = await response.json();
217+
if (ssend.status === 'draft') {
218+
console.log('Scheduling');
219+
await SG.scheduleSingleSend({ id: ssend.id, sendAt, token: options.apiKey });
220+
}
221+
222+
setOutput('send_date', sendAt.toISOString());
223+
setOutput('single_send_url', url);
224+
225+
return {
226+
sendAt,
227+
url
228+
};
229+
} else {
230+
console.error(response.status, response.statusText, response.headers, await response.text());
231+
throw new Error('Could not create newsletter');
232+
}
223233
} else {
224234
console.log(text);
225235
}

.github/actions/render-newsletter/src/sendgrid.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ import fetch from 'node-fetch';
33
export type ScheduledSend = {
44
id: string,
55
name: string,
6-
status: string,
6+
status: 'draft' | 'scheduled' | 'triggered',
77
categories: string[],
88
send_at: string,
99
created_at: string,
1010
updated_at: string,
1111
is_abtest: boolean,
1212
};
1313

14+
export type ErrorItem = { field: string, message: string, error_id: string };
15+
export type ErrorBody = {
16+
errors: ErrorItem[]
17+
};
18+
1419
const API_BASE = 'https://api.sendgrid.com/v3';
1520
type SingleSendParams = {
1621
html: string,
@@ -25,6 +30,7 @@ type SingleSendParams = {
2530
export async function singleSend(params: SingleSendParams) {
2631
const url = `${API_BASE}/marketing/singlesends` +
2732
(params.id ? `/${params.id}` : '');
33+
2834
return await fetch(url, {
2935
method: params.id ? 'PATCH' : 'POST',
3036
headers: {
@@ -47,6 +53,26 @@ export async function singleSend(params: SingleSendParams) {
4753
});
4854
}
4955

56+
type ScheduleSendParams = {
57+
id: string,
58+
sendAt: Date,
59+
token: string,
60+
};
61+
export async function scheduleSingleSend({ id, sendAt, token }: ScheduleSendParams) {
62+
const url = `${API_BASE}/marketing/singlesends/${id}/schedule`;
63+
64+
return await fetch(url, {
65+
method: 'PUT',
66+
headers: {
67+
'Authorization': `Bearer ${token}`,
68+
'Content-type': 'application/json',
69+
},
70+
body: JSON.stringify({
71+
send_at: sendAt.toISOString(),
72+
})
73+
});
74+
}
75+
5076
export async function deleteSingleSend(params: any) {
5177
const { id, token } = params;
5278
return await fetch(`${API_BASE}/marketing/singlesends/${id}`, {

0 commit comments

Comments
 (0)