All files / src/repositories cargo.repository.ts

86.67% Statements 13/15
0% Branches 0/12
50% Functions 1/2
84.62% Lines 11/13

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 641x 1x 1x 1x 1x       1x             2x   2x   2x   2x   2x                                                                                
import { Getter, inject } from '@loopback/core';
import { BelongsToAccessor, repository } from '@loopback/repository';
import { LogChainDataSource } from '../datasources';
import { Company, Cargo, CargoRelations, User } from '../models';
import { BaseCrudRepository } from './base-crud.repository.base';
import { CompanyRepository } from './company.repository';
import { UserViewRepository } from './user.view.repository';
 
export class CargoRepository extends BaseCrudRepository<Cargo, typeof Cargo.prototype.id, CargoRelations> {
  public readonly company: BelongsToAccessor<Company, typeof Company.prototype.id>;
  public readonly modifier: BelongsToAccessor<User, typeof User.prototype.id>;
 
  constructor(
    @inject('datasources.logchain') dataSource: LogChainDataSource,
    @repository.getter('CompanyRepository')
    protected companyRepositoryGetter: Getter<CompanyRepository>,
    @repository.getter('UserViewRepository')
    protected userRepositoryGetter: Getter<UserViewRepository>,
  ) {
    super(Cargo, dataSource);
 
    this.company = this.createBelongsToAccessor('company', companyRepositoryGetter);
 
    this.modifier = this.createBelongsToAccessor('modifier', userRepositoryGetter);
  }
 
  async getDefaultContainers(flow_id: number, action_type = 0, journey_point_id = 0): Promise<string[]> {
    const containers = await this.execute(
      `SELECT subtract_jsonb_arrays((needed_info->>'container_no')::jsonb , COALESCE(
        (
          SELECT jsonb_agg(DISTINCT container_no) AS containers FROM (
            SELECT container_no,
              COUNT(id) total_actions,
              COUNT(CASE WHEN status_name IN ('Completed','Cancelled') THEN 1 ELSE NULL END) cancelled_actions 
            FROM actions_data_view 
            WHERE flow_id = $1 AND container_no <> ''
            GROUP BY container_no
            UNION
            SELECT 
              value->>'container_no' AS container_no, 
              1 AS total_actions,
              1 AS cancelled_actions
            FROM cargoes,
              jsonb_array_elements(containers) AS elements
            WHERE flow_id = $1 AND status IN (2,3) 
            AND hidden = FALSE 
            AND company_id = $2 
            AND action_type = $3 
            AND journey_point_id = $4
            AND (cancelled_action IS NULL OR cancelled_action <> 3)
          ) result_data 
          WHERE cancelled_actions = total_actions
        ), '[""]'::jsonb
      )
      ) AS containers
      FROM flows_persona WHERE flow_id = $1 AND needed_info->>'container_no' IS NOT NULL 
      LIMIT 1;`,
      [flow_id, this.current_user?.company_id, action_type, journey_point_id],
    );
 
    return containers?.length ? containers[0].containers : [];
  }
}