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

67.44% Statements 29/43
65% Branches 13/20
87.5% Functions 7/8
71.79% Lines 28/39

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 1161x 1x 1x 1x     1x                                   1x   1x     1x                 138x               8x         8x 7x     1x   1x 1x                                     1x       1x 1x 1x               1x   3x     3x     3x 13x 13x             13x 13x 13x          
import _ from 'lodash';
import DefaultHandler, { order } from './default';
import constants from '../../constants';
import log from '../../../logger';
import { Assets } from '../../../types';
 
export const schema = {
  type: 'object',
  items: {
    type: 'object',
    additionalProperties: true,
    properties: {
      trigger_id: {
        type: 'object',
        properties: {
          action_name: { type: 'string', enum: constants.ACTIONS_TRIGGERS },
          display_name: { type: 'string', default: '' },
        },
      },
    },
  },
};
 
function isActionsDisabled(err): boolean {
  const errorBody = _.get(err, 'originalError.response.body') || {};
 
  return err.statusCode === 403 && errorBody.errorCode === 'feature_not_enabled';
}
 
export default class TriggersHandler extends DefaultHandler {
  existing: {
    [key: string]: {
      action_name: string;
      display_name: string;
    };
  };
 
  constructor(options: DefaultHandler) {
    super({
      ...options,
      type: 'triggers',
      id: 'name',
    });
  }
 
  async getType(): Promise<DefaultHandler['existing']> {
    Iif (this.existing) {
      return this.existing;
    }
 
    // in case client version does not support actions
    if (!this.client.actions || typeof this.client.actions.getAllTriggers !== 'function') {
      return [];
    }
 
    const triggerBindings = {};
 
    try {
      const res = await this.client.actions.getAllTriggers();
      const triggers: string[] = _(res.triggers).map('id').uniq().value();
 
      for (let i = 0; i < triggers.length; i++) {
        const triggerId = triggers[i];
        const { bindings } = await this.client.actions.getTriggerBindings({
          trigger_id: triggerId,
        });
        if (bindings.length > 0) {
          triggerBindings[triggerId] = bindings.map((binding) => ({
            action_name: binding.action.name,
            display_name: binding.display_name,
          }));
        }
      }
 
      this.existing = triggerBindings;
      return this.existing;
    } catch (err) {
      Iif (err.statusCode === 404 || err.statusCode === 501) {
        return [];
      }
 
      Eif (isActionsDisabled(err)) {
        log.info('Skipping triggers because Actions is not enabled.');
        return {};
      }
 
      throw err;
    }
  }
 
  @order('80')
  async processChanges(assets: Assets): Promise<void> {
    // No API to delete or create triggers, we can only update.
    const { triggers } = assets;
 
    // Do nothing if not set
    Iif (!triggers) return;
 
    // Process each trigger
    await Promise.all(
      Object.entries(triggers).map(async ([name, data]) => {
        const bindings = data.map((binding) => ({
          ref: {
            type: 'action_name',
            value: binding.action_name,
          },
          display_name: binding.display_name,
        }));
        await this.client.actions.updateTriggerBindings({ trigger_id: name }, { bindings });
        this.didUpdate({ trigger_id: name });
        this.updated += 1;
      })
    );
  }
}