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