All files / department department.service.ts

100% Statements 19/19
100% Branches 2/2
100% Functions 6/6
100% Lines 17/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          18x 18x   18x         18x           18x   34x 34x                   32x       32x                       2x       2x                   2x         2x 1x   1x                   1x       1x      
/**
 * Department service.
 * @packageDocumentation
 */
 
import { Injectable, Inject, forwardRef } from '@nestjs/common';
import { DBService } from '../db/db.service';
import { NBDepartment } from './department.interface';
import { ServiceException } from '../service.exception';
 
/**
 * Department table name.
 */
export const departmentTableName = 'NB_DEPARTMENT';
 
/**
 * Department table service.
 */
@Injectable()
export class DepartmentService {
  constructor(
    @Inject(forwardRef(() => DBService))
    private readonly dbService: DBService,
  ) {}
 
  /**
   * Determine whether or not a department exists.
   *
   * @param departmentID The department's ID.
   * @returns Whether or not the department exists.
   */
  public async departmentExists(departmentID: number): Promise<boolean> {
    const department = await this.dbService.getByID<NBDepartment>(
      departmentTableName,
      departmentID,
    );
    return !!department;
  }
 
  /**
   * Determine whether or not a department exists by name.
   *
   * @param departmentName The department's name.
   * @returns Whether or not the department exists.
   */
  public async departmentExistsByName(
    departmentName: string,
  ): Promise<boolean> {
    const department = await this.dbService.getByFields<NBDepartment>(
      departmentTableName,
      { name: departmentName },
    );
    return !!department;
  }
 
  /**
   * Get the name of a department.
   *
   * @param departmentID The department's ID.
   * @returns The name of the department.
   */
  public async getDepartmentName(departmentID: number): Promise<string> {
    const department = await this.dbService.getByID<NBDepartment>(
      departmentTableName,
      departmentID,
    );
 
    if (department) {
      return department.name;
    } else {
      throw new ServiceException('Department does not exist');
    }
  }
 
  /**
   * Get all departments.
   *
   * @returns All departments.
   */
  public async getDepartments(): Promise<NBDepartment[]> {
    const departments = await this.dbService.list<NBDepartment>(
      departmentTableName,
      { fieldName: 'name', sortOrder: 'ASC' },
    );
    return departments;
  }
}