All files / src/repositories company-location.repository.ts

63.16% Statements 12/19
0% Branches 0/2
33.33% Functions 1/3
58.82% Lines 10/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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 891x 1x         1x 1x 1x     1x                         41x   41x 41x       41x                                                                                                                  
import {Getter, inject} from '@loopback/core';
import {
  HasOneRepositoryFactory,
  Options,
  repository,
} from '@loopback/repository';
import {BaseCrudRepository, CountryRepository} from '.';
import {LogChainDataSource} from '../datasources';
import {CompanyLocation, CompanyLocationRelations, Country} from '../models';
import {ID} from '../types';
 
export class CompanyLocationRepository extends BaseCrudRepository<
  CompanyLocation,
  typeof CompanyLocation.prototype.id,
  CompanyLocationRelations
> {
  public readonly country: HasOneRepositoryFactory<
    Country,
    typeof CompanyLocation.prototype.id
  >;
 
  constructor(
    @inject('datasources.logchain') dataSource: LogChainDataSource,
    @repository.getter('CountryRepository')
    protected country_repo_getter: Getter<CountryRepository>,
  ) {
    super(CompanyLocation, dataSource);
    this.country = this.createHasOneRepositoryFactoryFor(
      'country',
      country_repo_getter,
    );
    this.registerInclusionResolver('country', this.country.inclusionResolver);
  }
 
  /**
   * Check if the location id exists with the given id and the company id
   * @param company_id The company id.
   * @param id The location.
   * @param options The options.
   */
  async existsWithCompany(
    company_id: ID,
    id: ID,
    options?: Options,
  ): Promise<boolean> {
    const sql = `
      SELECT
        COUNT(cl.ID)
      FROM
        companies AS c
        INNER JOIN company_personas cp ON c.ID = cp.company_id
        INNER JOIN company_locations cl ON cp.ID = cl.company_persona_id
      WHERE
        c.ID = $1 AND cl.ID = $2;`;
 
    const resl = await this.execute(sql, [company_id, id]);
    return resl[0].count > 0;
  }
 
  /**
   * Check if the location id exists with the given id and the company id
   * @param company_id The company id.
   * @param id The location.
   * @param options The options.
   */
  async getLocationsByCompanyId(
    company_id: ID,
    persona_id?: ID,
    options?: Options,
  ): Promise<Location[]> {
    const persona_id_condition = persona_id ? ` AND cp.persona_id = ${persona_id}` : '';
    const sql = `
      SELECT
        cl.id,
        cl.name,
        cl.google_map_link,
        cl.coordinates,
        cp.persona_id,
        cl.address
      FROM
        company_personas cp
        INNER JOIN company_locations cl ON cp.id = cl.company_persona_id
      WHERE cp.company_id = $1 ${persona_id_condition} AND status = TRUE
      ORDER BY cl.name;`;
    const resl = await this.execute(sql, [company_id], options);
    return resl as Location[];
  }
}