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

91.42% Statements 32/35
76.92% Branches 10/13
100% Functions 10/10
96.96% Lines 32/33

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 116 117 1181x 1x 1x     1x                           7x                       1x   142x               10x           8x 3x 3x 3x 3x       5x       14x 14x           14x       9x     9x                 9x 9x         9x 7x 2x           5x     9x         1x 9x     9x   9x   9x   9x      
import DefaultAPIHandler, { order } from './default';
import constants from '../../constants';
import { filterExcluded, getEnabledClients } from '../../utils';
import { CalculatedChanges, Assets } from '../../../types';
 
export const schema = {
  type: 'array',
  items: {
    type: 'object',
    properties: {
      strategy: { type: 'string', enum: ['auth0'], default: 'auth0' },
      name: { type: 'string' },
      options: {
        type: 'object',
        properties: {
          customScripts: {
            type: 'object',
            properties: {
              ...constants.DATABASE_SCRIPTS.reduce(
                (o, script) => ({ ...o, [script]: { type: 'string' } }),
                {}
              ),
            },
          },
        },
      },
    },
    required: ['name'],
  },
};
 
export default class DatabaseHandler extends DefaultAPIHandler {
  constructor(config: DefaultAPIHandler) {
    super({
      ...config,
      type: 'databases',
      stripUpdateFields: ['strategy', 'name'],
    });
  }
 
  objString(db) {
    return super.objString({ name: db.name, id: db.id });
  }
 
  getClientFN(fn: 'create' | 'delete' | 'getAll' | 'update'): Function {
    // Override this as a database is actually a connection but we are treating them as a different object
    // If we going to update database, we need to get current options first
    if (fn === 'update') {
      return (params, payload) =>
        this.client.connections.get(params).then((connection) => {
          payload.options = { ...connection.options, ...payload.options };
          return this.client.connections.update(params, payload);
        });
    }
 
    return this.client.connections[fn].bind(this.client.connections);
  }
 
  async getType() {
    Iif (this.existing) return this.existing;
    this.existing = this.client.connections.getAll({
      strategy: 'auth0',
      paginate: true,
      include_totals: true,
    });
 
    return this.existing;
  }
 
  async calcChanges(assets: Assets): Promise<CalculatedChanges> {
    const { databases } = assets;
 
    // Do nothing if not set
    Iif (!databases)
      return {
        del: [],
        create: [],
        update: [],
        conflicts: [],
      };
 
    // Convert enabled_clients by name to the id
    const clients = await this.client.clients.getAll({ paginate: true, include_totals: true });
    const existingDatabasesConnections = await this.client.connections.getAll({
      strategy: 'auth0',
      paginate: true,
      include_totals: true,
    });
    const formatted = databases.map((db) => {
      if (db.enabled_clients) {
        return {
          ...db,
          enabled_clients: getEnabledClients(assets, db, existingDatabasesConnections, clients),
        };
      }
 
      return db;
    });
 
    return super.calcChanges({ ...assets, databases: formatted });
  }
 
  // Run after clients are updated so we can convert all the enabled_clients names to id's
  @order('60')
  async processChanges(assets: Assets) {
    const { databases } = assets;
 
    // Do nothing if not set
    Iif (!databases) return;
 
    const excludedConnections: string[] = (assets.exclude && assets.exclude.databases) || [];
 
    const changes = await this.calcChanges(assets);
 
    await super.processChanges(assets, filterExcluded(changes, excludedConnections));
  }
}