All files / src/repositories flow-default-action.repository.ts

52.63% Statements 10/19
0% Branches 0/30
33.33% Functions 1/3
47.06% Lines 8/17

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 1091x 1x   1x 1x 1x   1x               18x   18x                                                                                                                                                                                      
import {Getter, inject} from '@loopback/core';
import {Options, repository} from '@loopback/repository';
import {FlowDefaultReferenceRepository} from '.';
import {LogChainDataSource} from '../datasources';
import {FlowDefaultAction, FlowDefaultActionRelations} from '../models';
import {BaseCrudRepository} from './base-crud.repository.base';
 
export class FlowDefaultActionRepository extends BaseCrudRepository<
  FlowDefaultAction,
  typeof FlowDefaultAction.prototype.id,
  FlowDefaultActionRelations
> {
  constructor(
    @inject('datasources.logchain') dataSource: LogChainDataSource,
    @repository.getter('FlowDefaultReferenceRepository')
    protected flow_default_ref_getter: Getter<FlowDefaultReferenceRepository>,
  ) {
    super(FlowDefaultAction, dataSource);
  }
 
  /**
   * Find actions by journey id.
   * @param flow_id The flow id.
   * @param step_id The step id.
   */
  async findActionsByJourneyId(
    flow_id: number,
    step_id: number,
  ): Promise<FlowDefaultAction[]> {
    const actions = await this.execute(
      `SELECT
        fdc.ID,
        CASE
          WHEN fdc.form_id IS NULL THEN
            0
          ELSE fdc.form_id
        END form_id,
        CASE
          WHEN fdc.title IS NOT NULL THEN
            fdc.title
          WHEN fdc.title IS NULL THEN
            CASE
              WHEN fdc.form_id IS NOT NULL THEN
                f."name"
            END
          END form_name,
        CASE
          WHEN fdc.form_id = 0 THEN
            'Document'
          WHEN fdc.form_id <> 0 THEN
            'Form'
        END form_type,
        fdc.flow_persona_id AS flow_persona_id,
        fdp.NAME AS flow_persona_name,
        fdc.form_data
      FROM
        flows_default_action AS fdc
        LEFT JOIN forms AS f ON fdc.form_id = f.ID
        INNER JOIN flows_default_persona AS fdp ON fdp.ID = fdc.flow_persona_id
      WHERE
        fdc.flow_default_id = $1
        AND fdc.flow_default_journey_point_id = $2
      GROUP BY
        fdc.ID,
        fdp.NAME,
        f.NAME;`,
      [flow_id, step_id],
    );
    return actions as FlowDefaultAction[];
  }
 
  /**
   * Override deleteById to remove all reference documents if the journey points don't have any action.
   * @param id The flow persona id.
   * @param options The options.
   */
  async deleteById(
    id: typeof FlowDefaultAction.prototype.id,
    options?: Options,
  ): Promise<void> {
    await super.deleteById(id, options);
 
    if (options?.flow_id && options?.journey_point_id) {
      const {count: rest_action} = await this.count(
        {
          flow_default_id: options?.flow_id,
          flow_default_journey_point_id: options?.journey_point_id,
        },
        options,
      );
 
      // if current journey points have atleast on action. keep reference documents
      if (rest_action > 0) {
        return;
      }
 
      // otherwise, delete all.
      const repo = await this.flow_default_ref_getter();
      await repo.deleteAll(
        {
          flow_default_id: options?.flow_id,
          flow_default_journey_point_id: options?.journey_point_id,
        },
        options,
      );
    }
  }
}