All files / src/repositories flow-default-persona.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 86 87 88 89 90 91 921x 1x           1x 1x           1x   1x                         18x   18x 18x       18x                                                                                                              
import {Getter, inject} from '@loopback/core';
import {
  HasOneRepositoryFactory,
  Options,
  repository,
} from '@loopback/repository';
import {PersonaRepository} from '.';
import {LogChainDataSource} from '../datasources';
import {
  FlowDefaultJourneyPoint,
  FlowDefaultPersona,
  FlowDefaultPersonaRelations,
  Persona,
} from '../models';
import {BaseCrudRepository} from './base-crud.repository.base';
 
export class FlowDefaultPersonaRepository extends BaseCrudRepository<
  FlowDefaultPersona,
  typeof FlowDefaultPersona.prototype.id,
  FlowDefaultPersonaRelations
> {
  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(FlowDefaultPersona, dataSource);
    this.persona = this.createHasOneRepositoryFactoryFor(
      'persona',
      persona_repo_getter,
    );
    this.registerInclusionResolver('persona', this.persona.inclusionResolver);
  }
 
  /**
   * Find users by role rights.
   * @param feature_id The feature id.
   * @param right The right access.
   */
  async groupingPersonaByFlowId(flow_default_id: number) {
    const flow_persona = await this.execute(
      `SELECT
        FORMAT (
          '%s (%s)',
          per.title,
          COUNT ( fdp.persona_id )
        ) title,
        fdp.persona_id,
        CAST (COUNT ( fdp.persona_id ) as INTEGER)
      FROM
        flows_default_persona AS fdp
      INNER JOIN personas AS per ON fdp.persona_id = per."id"
      WHERE
        fdp.flow_default_id = $1
      GROUP BY
        fdp.persona_id,
        per.title;`,
      [flow_default_id],
    );
 
    return flow_persona as {title: string; persona_id: number; count: number}[];
  }
 
  /**
   * Override deleteById to remove persona from group after delete.
   * @param id The flow persona id.
   * @param options The options.
   */
  async deleteById(
    id: typeof FlowDefaultPersona.prototype.id,
    options?: Options,
  ): Promise<void> {
    await super.deleteById(id, options);
 
    // remove personas from group.
    if (options?.flow_id) {
      const sql = `
        UPDATE flows_default_group
          SET flow_personas = array_remove ( flow_personas, $2)
        WHERE
          flow_default_id = $1
        AND $2 = ANY ( flow_personas );`;
      await this.execute(sql, [options?.flow_id, id], options);
    }
  }
}