All files / libs/tools/plugin/src/executors/brevo-send-templates executor.ts

0% Statements 0/61
0% Branches 0/1
0% Functions 0/1
0% Lines 0/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                           
import fs from 'node:fs';

import { BrevoClient } from '@getbrevo/brevo';
import { Logger } from '@nestjs/common';
import { type Executor } from '@nx/devkit';

import { assert } from '@amalia/ext/typescript';

interface BrevoMailsFile {
  token: string;

  mails: {
    template: number;
    email: string;
    params?: Record<string, unknown>;
  }[];
}

/**
 * Create a file at project root called brevo-mails.json then past that in it:
 *
 * {
 *   "token": "< TOKEN >",
 *   "mails": [
 *     {"template": 25, "email": "< MAIL >", "params": { "notificationLink": "https://app.amalia.io/< WHATEVER >"}}
 *   ]
 * }
 *
 * Add one line per message.
 */

export const executor: Executor = async () => {
  const mailsToSend = JSON.parse(fs.readFileSync('brevo-mails.json').toString()) as BrevoMailsFile;

  assert(mailsToSend.token, 'Missing token');

  const client = new BrevoClient({ apiKey: mailsToSend.token });

  for (const mail of mailsToSend.mails) {
    await client.transactionalEmails.sendTransacEmail({
      to: [{ email: mail.email, name: mail.email }],
      templateId: mail.template,
      ...(mail.params
        ? {
            params: {
              URL: 'app.amalia.io',
              ...mail.params,
            },
          }
        : {}),
    });

    new Logger('MailSenderExecutor').log(`Sent mail to ${mail.email}`);
  }

  return {
    success: true,
  };
};

export default executor;