All files / src/repositories loading.repository.ts

62.96% Statements 17/27
0% Branches 0/4
20% Functions 1/5
65.22% Lines 15/23

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 721x 1x 1x 1x 1x       1x                     20x   20x   20x   20x 20x 20x 20x 20x 20x                                                                                  
import {Getter, inject} from '@loopback/core';
import {BelongsToAccessor, repository} from '@loopback/repository';
import {LogChainDataSource} from '../datasources';
import {Company, Loading, LoadingRelations, User} from '../models';
import {BaseCrudRepository} from './base-crud.repository.base';
import {CompanyRepository} from './company.repository';
import {UserViewRepository} from './user.view.repository';
 
export class LoadingRepository extends BaseCrudRepository<Loading, typeof Loading.prototype.id, LoadingRelations> {
  public readonly company: BelongsToAccessor<Company, typeof Company.prototype.id>;
  public readonly delivered_company: BelongsToAccessor<Company, typeof Company.prototype.id>;
  public readonly modifier: BelongsToAccessor<User, typeof User.prototype.id>;
  public readonly delivered: BelongsToAccessor<User, typeof User.prototype.id>;
  public readonly supervise: BelongsToAccessor<User, typeof User.prototype.id>;
  public readonly company_other: BelongsToAccessor<Company, typeof Company.prototype.id>;
 
  constructor(
    @inject('datasources.logchain') dataSource: LogChainDataSource,
    @repository.getter('CompanyRepository')
    protected companyRepositoryGetter: Getter<CompanyRepository>,
    @repository.getter('UserViewRepository')
    protected userRepositoryGetter: Getter<UserViewRepository>,
  ) {
    super(Loading, dataSource);
 
    this.company = this.createBelongsToAccessor('company', companyRepositoryGetter);
    this.delivered_company = this.createBelongsToAccessor('delivered_company', companyRepositoryGetter);
    this.modifier = this.createBelongsToAccessor('modifier', userRepositoryGetter);
    this.delivered = this.createBelongsToAccessor('delivered', userRepositoryGetter);
    this.supervise = this.createBelongsToAccessor('supervise', userRepositoryGetter);
    this.company_other = this.createBelongsToAccessor('company_other', companyRepositoryGetter);
  }
 
  /**
   * Update container no.
   *
   * @param {number} flow_id - ID of flow
   * @param {string} container_no - container no
   * @return {Promise<void>} no return value
   */
  async updateContainerNo(flow_id: number, old_container_no: string, new_container_no: string): Promise<void> {
    await this.execute(`
    SELECT update_container_no_for_flow($1, $2, $3)
    `, [flow_id, old_container_no, new_container_no]);
  }
 
  /**
   * Validate duplicated container no.
   *
   * @param {number} flow_id - ID of flow
   * @param {string} old_container_no - old_container_no
   * @param {string} new_container_no - new_container_no
   * @return {Promise<boolean>} return boolean value
   */
  async validateDuplicatedContainerNo(flow_id: number, old_container_no: string, new_container_no: string): Promise<boolean> {
    const result = await this.execute(`
    SELECT needed_info->>'container_no' AS container_no
    FROM flows_persona 
    WHERE flow_id = $1 
    AND needed_info->>'container_no' IS NOT NULL;
    `, [flow_id]);
    if (result.length !== 0) {
      const containers = JSON.parse(result[0].container_no);
      const oldIndex = containers.findIndex((container: string) => container === old_container_no);
      const newIndex = containers.findIndex((container: string) => container === new_container_no);
      return newIndex !== -1 && (oldIndex !== newIndex) as boolean;
    }
 
    return false;
  }
}