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

90% Statements 18/20
66.66% Branches 4/6
100% Functions 5/5
100% Lines 18/18

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 531x 1x     1x                     1x       134x               5x 5x 5x         1x     1x     1x 1x 1x 1x 1x 1x 1x 1x          
import DefaultHandler from './default';
import constants from '../../constants';
import { Asset, Assets } from '../../../types';
 
export const schema = {
  type: 'array',
  items: {
    type: 'object',
    properties: {
      name: { type: 'string', enum: constants.GUARDIAN_FACTORS },
    },
    required: ['name'],
  },
};
 
export default class GuardianFactorsHandler extends DefaultHandler {
  existing: Asset[];
 
  constructor(options: DefaultHandler) {
    super({
      ...options,
      type: 'guardianFactors',
      id: 'name',
    });
  }
 
  async getType(): Promise<Asset[]> {
    Iif (this.existing) return this.existing;
    this.existing = await this.client.guardian.getFactors();
    return this.existing;
  }
 
  async processChanges(assets: Assets): Promise<void> {
    // No API to delete or create guardianFactors, we can only update.
    const { guardianFactors } = assets;
 
    // Do nothing if not set
    Iif (!guardianFactors || !guardianFactors.length) return;
 
    // Process each factor
    await Promise.all(
      guardianFactors.map(async (factor) => {
        const data = { ...factor };
        const params = { name: factor.name };
        delete data.name;
        await this.client.guardian.updateFactor(params, data);
        this.didUpdate(params);
        this.updated += 1;
      })
    );
  }
}