All files / user-interest user-interest.service.ts

100% Statements 27/27
100% Branches 4/4
100% Functions 8/8
100% Lines 25/25

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113          18x 18x 18x 18x   18x         18x           18x   34x 34x 34x 34x 34x 34x                           9x   9x 8x       8x 7x   7x         1x     1x                             3x       3x                   4x                                 13x            
/**
 * User interest service.
 * @packageDocumentation
 */
 
import { Injectable, Inject, forwardRef } from '@nestjs/common';
import { DBService } from '../db/db.service';
import { UserService } from '../user/user.service';
import { DepartmentService } from '../department/department.service';
import { NBUserInterest } from './user-interest.interface';
import { ServiceException } from '../service.exception';
 
/**
 * User interest table name.
 */
export const userInterestTableName = 'NB_USER_INTEREST';
 
/**
 * User interest table service.
 */
@Injectable()
export class UserInterestService {
  constructor(
    @Inject(forwardRef(() => DBService))
    private readonly dbService: DBService,
    @Inject(forwardRef(() => UserService))
    private readonly userService: UserService,
    @Inject(forwardRef(() => DepartmentService))
    private readonly departmentService: DepartmentService,
  ) {}
 
  /**
   * Note a user's interest in a department.
   *
   * @param userID The user's ID.
   * @param departmentID The department's ID.
   * @returns The new user interest record.
   */
  public async noteInterest(
    userID: string,
    departmentID: number,
  ): Promise<NBUserInterest> {
    const userExists = await this.userService.userExists(userID);
 
    if (userExists) {
      const departmentExists = await this.departmentService.departmentExists(
        departmentID,
      );
 
      if (departmentExists) {
        await this.dropInterest(userID, departmentID);
 
        return this.dbService.create<NBUserInterest>(userInterestTableName, {
          userID,
          departmentID,
        });
      } else {
        throw new ServiceException('Department does not exist');
      }
    } else {
      throw new ServiceException('User does not exist');
    }
  }
 
  /**
   * Determine whether or not a user is interested in a department.
   *
   * @param userID The user's ID.
   * @param departmentID The department's ID.
   * @returns Whether or not the user is interested in the department.
   */
  public async isInterested(
    userID: string,
    departmentID: number,
  ): Promise<boolean> {
    const userInterest = await this.dbService.getByFields<NBUserInterest>(
      userInterestTableName,
      { userID, departmentID },
    );
    return !!userInterest;
  }
 
  /**
   * Get all of a user's departmental interests.
   *
   * @param userID The user's ID.
   * @returns All of the user's departmental interests.
   */
  public async getUserInterests(userID: string): Promise<NBUserInterest[]> {
    return this.dbService.listByFields<NBUserInterest>(
      userInterestTableName,
      { userID },
      { fieldName: 'interestTime', sortOrder: 'ASC' },
    );
  }
 
  /**
   * Delete a user's interest in a department.
   *
   * @param userID The user's ID.
   * @param departmentID The department's ID.
   */
  public async dropInterest(
    userID: string,
    departmentID: number,
  ): Promise<void> {
    await this.dbService.deleteByFields(userInterestTableName, {
      userID,
      departmentID,
    });
  }
}