All files / src/tools/auth0/handlers emailTemplates.ts

89.47% Statements 34/38
50% Branches 4/8
100% Functions 11/11
91.66% Lines 33/36

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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 871x 1x     1x 22x 11x   1x                       1x   133x               5x 55x 55x 55x 55x                   55x   5x       2x 2x   2x 2x 1x 1x 1x   1x   1x 1x 1x 1x                 1x 2x     2x   2x 2x 2x          
import DefaultHandler, { order } from './default';
import constants from '../../constants';
import { Assets, Asset } from '../../../types';
 
export const supportedTemplates = constants.EMAIL_TEMPLATES_NAMES.filter((p) =>
  p.includes('.json')
).map((p) => p.replace('.json', ''));
 
export const schema = {
  type: 'array',
  items: {
    type: 'object',
    properties: {
      template: { type: 'string', enum: supportedTemplates },
      body: { type: 'string', default: '' },
    },
    required: ['template'],
  },
};
 
export default class EmailTemplateHandler extends DefaultHandler {
  constructor(options: DefaultHandler) {
    super({
      ...options,
      type: 'emailTemplates',
      identifiers: ['template'],
    });
  }
 
  async getType(): Promise<Asset> {
    const emailTemplates = await Promise.all(
      constants.EMAIL_TEMPLATES_TYPES.map(async (name) => {
        try {
          const template = await this.client.emailTemplates.get({ name });
          return template;
        } catch (err) {
          // Ignore if not found, else throw error
          if (err.statusCode !== 404) {
            throw err;
          }
        }
      })
    );
 
    const nonEmptyTemplates = emailTemplates.filter((template) => !!template);
 
    return nonEmptyTemplates;
  }
 
  async updateOrCreate(emailTemplate): Promise<void> {
    try {
      const identifierField = this.identifiers[0];
 
      const params = { name: emailTemplate[identifierField] };
      const updated = await this.client.emailTemplates.update(params, emailTemplate);
      delete updated.body;
      this.didUpdate(updated);
      this.updated += 1;
    } catch (err) {
      Eif (err.statusCode === 404) {
        // Create if it does not exist
        const created = await this.client.emailTemplates.create(emailTemplate);
        delete created.body;
        this.didCreate(created);
        this.created += 1;
      } else {
        throw err;
      }
    }
  }
 
  // Run after email provider changes
  @order('60')
  async processChanges(assets: Assets): Promise<void> {
    const { emailTemplates } = assets;
 
    // Do nothing if not set
    Iif (!emailTemplates || !emailTemplates.length) return;
 
    await Promise.all(
      emailTemplates.map(async (emailTemplate) => {
        await this.updateOrCreate(emailTemplate);
      })
    );
  }
}