All files / src/repositories transport.repository.ts

80% Statements 16/20
0% Branches 0/12
50% Functions 1/2
77.78% Lines 14/18

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 611x 1x   1x 1x 1x     1x                       20x   20x   20x   20x 20x   20x   20x   20x                                                      
import {Getter, inject} from '@loopback/core';
import {BelongsToAccessor, repository} from '@loopback/repository';
import {CompanyRepository, UserViewRepository} from '.';
import {LogChainDataSource} from '../datasources';
import {Company, CompanyLocation, Status, Transport, TransportRelations, User} from '../models';
import {BaseCrudRepository} from './base-crud.repository.base';
import {CompanyLocationRepository} from './company-location.repository';
 
export class TransportRepository extends BaseCrudRepository<
  Transport,
  typeof Transport.prototype.id,
  TransportRelations
> {
  public readonly company: BelongsToAccessor<Company, typeof Transport.prototype.id>;
  public readonly production: BelongsToAccessor<CompanyLocation, typeof Transport.prototype.id>;
  public readonly driver: BelongsToAccessor<User, typeof Transport.prototype.id>;
  public readonly modifier: BelongsToAccessor<User, typeof User.prototype.id>;
  constructor(
    @inject('datasources.logchain') dataSource: LogChainDataSource,
    @repository.getter('UserViewRepository')
    protected userRepositoryGetter: Getter<UserViewRepository>,
    @repository.getter('CompanyRepository')
    protected companyRepositoryGetter: Getter<CompanyRepository>,
    @repository.getter('CompanyLocationRepository')
    protected companyLocationRepository: Getter<CompanyLocationRepository>,
  ) {
    super(Transport, dataSource);
    this.company = this.createBelongsToAccessor('company', companyRepositoryGetter);
 
    this.production = this.createBelongsToAccessor('production', companyLocationRepository);
 
    this.modifier = this.createBelongsToAccessor('modifier', userRepositoryGetter);
 
    this.driver = this.createBelongsToAccessor('driver', userRepositoryGetter);
  }
 
  /**
   * Get company location ids by query
   * @param ilike_value The value to query company location
   * @returns The company location ids
   */
  async getCompanyLocationIdByQuery(ilike_value: { ilike: string;}): Promise<number[]> {
    if (!ilike_value?.ilike) {
      return [];
    }
 
    const result = await this.execute(
       `SELECT 
          array_agg(cl.id) AS company_location_ids
        FROM company_locations cl
        JOIN companies co ON co.id = cl.company_id
        WHERE co.status = $1
        AND cl."name" ILIKE '%$2%'
        AND (co."name" ILIKE '%$2%' OR co.identifier ILIKE '%$2%');`,
      [Status.Company.Active, ilike_value.ilike],
    );
 
    return result?.length ? result[0].company_location_ids : [];
  }
}