All files / src/repositories flow-default-journey-point.repository.ts

66.67% Statements 12/18
0% Branches 0/10
33.33% Functions 1/3
62.5% Lines 10/16

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 861x 1x           1x 1x         1x   1x                         46x   46x 46x       46x                                                                                                    
import {Getter, inject} from '@loopback/core';
import {
  HasOneRepositoryFactory,
  Options,
  repository,
} from '@loopback/repository';
import {PersonaRepository} from '.';
import {LogChainDataSource} from '../datasources';
import {
  FlowDefaultJourneyPoint,
  FlowDefaultJourneyPointRelations,
  Persona,
} from '../models';
import {BaseCrudRepository} from './base-crud.repository.base';
 
export class FlowDefaultJourneyPointRepository extends BaseCrudRepository<
  FlowDefaultJourneyPoint,
  typeof FlowDefaultJourneyPoint.prototype.id,
  FlowDefaultJourneyPointRelations
> {
  public readonly persona: HasOneRepositoryFactory<
    Persona,
    typeof FlowDefaultJourneyPoint.prototype.id
  >;
 
  constructor(
    @inject('datasources.logchain') dataSource: LogChainDataSource,
    @repository.getter('PersonaRepository')
    protected persona_repo_getter: Getter<PersonaRepository>,
  ) {
    super(FlowDefaultJourneyPoint, dataSource);
    this.persona = this.createHasOneRepositoryFactoryFor(
      'persona',
      persona_repo_getter,
    );
    this.registerInclusionResolver('persona', this.persona.inclusionResolver);
  }
 
  /**
   * Count number of persona in the flow.
   * @param flow_default_id The default flow id.
   */
  async countNumberOfPersona(
    flow_default_id: number,
    options?: Options,
  ): Promise<number> {
    const count = await this.execute(
      `SELECT
        persona_id
      FROM
        flows_default_journey_point
      WHERE
        flow_default_id = $1
      GROUP BY
        persona_id;`,
      [flow_default_id],
      options,
    );
 
    return count.length;
  }
 
  /**
   * Override deleteById to remove journey points from group after delete.
   * @param id The flow persona id.
   * @param options The options.
   */
  async deleteById(
    id: typeof FlowDefaultJourneyPoint.prototype.id,
    options?: Options,
  ): Promise<void> {
    await super.deleteById(id, options);
 
    // remove journey points from group.
    if (options?.flow_id) {
      const sql = `
      UPDATE flows_default_group
        SET journey_points = array_remove ( journey_points, $2)
      WHERE
        flow_default_id = $1
        AND $2 = ANY ( journey_points );`;
      await this.execute(sql, [options?.flow_id, id], options);
    }
  }
}