diff --git a/src/log.js b/src/log.js new file mode 100644 index 0000000..bc214dc --- /dev/null +++ b/src/log.js @@ -0,0 +1,17 @@ +export const log = async ({ env, user_id, tags }) => { + fetch('https://api.logsnag.com/v1/log', { + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${env.LOGSNAG_API_TOKEN}`, + }, + body: JSON.stringify({ + project: 'cinotify', + channel: 'api', + event: 'mail', + icon: '📬', + user_id, + tags, + }), + method: 'POST', + }); +}; diff --git a/src/mail.js b/src/mail.js index c11db55..eead642 100644 --- a/src/mail.js +++ b/src/mail.js @@ -1,3 +1,5 @@ +import { log } from './log'; + export const payload = ({ subject, to, type, body, attachments }) => ({ // https://docs.sendgrid.com/api-reference/mail-send/mail-send#body attachments, @@ -31,6 +33,15 @@ export const mail = async ({ env = {}, ...rest }) => { method: 'POST', body: JSON.stringify(payload({ ...rest })), }); + await log({ + env, + user_id: rest.to?.split(',')[0], + tags: { + attachments: !!rest.attachments, + contentType: rest.type, + status: response.status, + }, + }); if (response.status > 299) { throw new Error((await response.json()).errors[0].message); } diff --git a/src/mail.test.js b/src/mail.test.js index 4247d16..3f133ff 100644 --- a/src/mail.test.js +++ b/src/mail.test.js @@ -45,6 +45,27 @@ describe('mail', () => { }, ); }); + it('logs', async () => { + global.fetch.mockResolvedValueOnce({ json: () => '{}', status: 200 }); + await mail({ ...input, to: 'one@example.com,two@example.com' }); + expect(global.fetch).toHaveBeenCalledWith( + 'https://api.logsnag.com/v1/log', + expect.objectContaining({ + body: JSON.stringify({ + project: 'cinotify', + channel: 'api', + event: 'mail', + icon: '📬', + user_id: 'one@example.com', + tags: { + attachments: true, + contentType: 'text/html', + status: 200, + }, + }), + }), + ); + }); }); describe('payload', () => {