All files / src/repositories country.repository.ts

39.13% Statements 9/23
0% Branches 0/6
25% Functions 1/4
33.33% Lines 7/21

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 471x 1x 1x 1x 1x   1x           19x                                                                    
import {inject} from '@loopback/core';
import {BaseCrudRepository} from '.';
import {LogChainDataSource} from '../datasources';
import {Country, CountryRelations} from '../models';
import {ErrorUtils, ValidationError} from '../utils/error';
 
export class CountryRepository extends BaseCrudRepository<
  Country,
  typeof Country.prototype.id,
  CountryRelations
> {
  constructor(@inject('datasources.logchain') dataSource: LogChainDataSource) {
    super(Country, dataSource);
  }
 
  /**
   * Ensure all country is active
   * @param ids The array id of Country
   * @returns A promise
   */
  async ensureCountryActive(ids: Array<number>): Promise<void | Error> {
    if (!ids.length) {
      throw new Error('Country is empty');
    }
    const pTasks: Promise<void | Error>[] = [];
    ids.forEach((id, idx) => {
      pTasks[idx] = (async () => {
        const {count} = await this.count({
          id: id,
          status: true,
        });
        if (!count) {
          throw new Error(`Country ${id} is not active`);
        }
      })();
    });
 
    const resl = await Promise.allSettled(pTasks);
    if (ErrorUtils.hasError(resl)) {
      const err = new ValidationError(`Please ensure the country is active`);
      err.statusCode = 400;
      err.details = ErrorUtils.buildErrorDetails(resl);
      throw err;
    }
  }
}