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

91.66% Statements 22/24
66.66% Branches 4/6
100% Functions 8/8
100% Lines 21/21

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 611x 1x     1x                     1x       134x               5x   5x 5x 5x 5x         5x         1x     1x     1x 1x 1x 1x 1x 1x 1x 1x          
import DefaultHandler from './default';
import constants from '../../constants';
import { Assets, Asset, CalculatedChanges } from '../../../types';
 
export const schema = {
  type: 'array',
  items: {
    type: 'object',
    properties: {
      name: { type: 'string', enum: constants.GUARDIAN_FACTOR_TEMPLATES },
    },
    required: ['name'],
  },
};
 
export default class GuardianFactorTemplatesHandler extends DefaultHandler {
  existing: Asset[];
 
  constructor(options) {
    super({
      ...options,
      type: 'guardianFactorTemplates',
      id: 'name',
    });
  }
 
  async getType(): Promise<Asset[]> {
    Iif (this.existing) return this.existing;
 
    const data = await Promise.all(
      constants.GUARDIAN_FACTOR_TEMPLATES.map(async (name) => {
        const templates = await this.client.guardian.getFactorTemplates({ name });
        return { name, ...templates };
      })
    );
 
    // Filter out empty, should have more then 1 keys (name)
    return data.filter((d) => Object.keys(d).length > 1);
  }
 
  async processChanges(assets: Assets): Promise<void> {
    // No API to delete or create guardianFactorTemplates, we can only update.
    const { guardianFactorTemplates } = assets;
 
    // Do nothing if not set
    Iif (!guardianFactorTemplates || !guardianFactorTemplates.length) return;
 
    // Process each factor templates
    await Promise.all(
      guardianFactorTemplates.map(async (fatorTemplates) => {
        const data = { ...fatorTemplates };
        const params = { name: fatorTemplates.name };
        delete data.name;
        await this.client.guardian.updateFactorTemplates(params, data);
        this.didUpdate(params);
        this.updated += 1;
      })
    );
  }
}