All files / src/repositories vessel.repository.ts

89.47% Statements 17/19
0% Branches 0/12
50% Functions 1/2
88.24% Lines 15/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 681x 1x 1x 1x 1x         1x                 2x   2x   2x   2x   2x 2x 2x     2x 2x                                                                      
import { Getter, inject } from '@loopback/core';
import { BelongsToAccessor, HasOneRepositoryFactory, repository } from '@loopback/repository';
import { LogChainDataSource } from '../datasources';
import { Company, Vessel, VesselRelations, User, Country } from '../models';
import { BaseCrudRepository } from './base-crud.repository.base';
import { CompanyRepository } from './company.repository';
import { UserViewRepository } from './user.view.repository';
import { CountryRepository } from '.';
 
export class VesselRepository extends BaseCrudRepository<Vessel, typeof Vessel.prototype.id, VesselRelations> {
  public readonly company_other: BelongsToAccessor<Company, typeof Company.prototype.id>;
  public readonly company: BelongsToAccessor<Company, typeof Company.prototype.id>;
  public readonly modifier: BelongsToAccessor<User, typeof User.prototype.id>;
  public readonly pod_country: HasOneRepositoryFactory<Country, typeof Country.prototype.id>;
 
  constructor(
    @inject('datasources.logchain') dataSource: LogChainDataSource,
    @repository.getter('CompanyRepository')
    protected companyRepositoryGetter: Getter<CompanyRepository>,
    @repository.getter('UserViewRepository')
    protected userRepositoryGetter: Getter<UserViewRepository>,
    @repository.getter('CountryRepository')
    protected country_repo_getter: Getter<CountryRepository>,
  ) {
    super(Vessel, dataSource);
 
    this.company_other = this.createBelongsToAccessor('company_other', companyRepositoryGetter);
    this.company = this.createBelongsToAccessor('company', companyRepositoryGetter);
    this.modifier = this.createBelongsToAccessor('modifier', userRepositoryGetter);
 
    // POD Country
    this.pod_country = this.createHasOneRepositoryFactoryFor('pod_country', country_repo_getter);
    this.registerInclusionResolver('pod_country', this.pod_country.inclusionResolver);
  }
 
  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 vessels,
              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
          ) 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 : [];
  }
}