All files / feedback feedback.service.ts

100% Statements 34/34
100% Branches 10/10
100% Functions 10/10
100% Lines 32/32

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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139          18x 18x 18x 18x   18x         18x           18x   34x 34x 34x 34x 34x 34x                           6x       6x   6x 5x   5x 4x 3x         1x         1x     1x                     2x       2x                   2x         2x 1x   1x                         7x                     3x                       3x      
/**
 * Feedback service.
 * @packageDocumentation
 */
 
import { Injectable, Inject, forwardRef } from '@nestjs/common';
import { DBService } from '../db/db.service';
import { ResourceService } from '../resource/resource.service';
import { UserService } from '../user/user.service';
import { NBFeedback } from './feedback.interface';
import { ServiceException } from '../service.exception';
 
/**
 * Feedback table name.
 */
export const feedbackTableName = 'NB_FEEDBACK';
 
/**
 * Feedback table service.
 */
@Injectable()
export class FeedbackService {
  constructor(
    @Inject(forwardRef(() => DBService))
    private readonly dbService: DBService,
    @Inject(forwardRef(() => ResourceService))
    private readonly resourceService: ResourceService,
    @Inject(forwardRef(() => UserService))
    private readonly userService: UserService,
  ) {}
 
  /**
   * Send feedback on the application.
   *
   * @param userID The user's ID.
   * @param feedback The feedback.
   * @returns The new feedback record.
   */
  public async sendFeedback(
    userID: string,
    feedback: string,
  ): Promise<NBFeedback> {
    const feedbackMaxLength = await this.resourceService.getResource<number>(
      'FEEDBACK_MAX_LENGTH',
    );
 
    const userExists = await this.userService.userExists(userID);
 
    if (userExists) {
      const userFeedback = await this.getUserFeedback(userID);
 
      if (userFeedback === undefined) {
        if (feedback.length >= 1 && feedback.length <= feedbackMaxLength) {
          return this.dbService.create<NBFeedback>(feedbackTableName, {
            userID,
            feedback,
          });
        } else {
          throw new ServiceException(
            `Feedback must be between 1 and ${feedbackMaxLength} characters`,
          );
        }
      } else {
        throw new ServiceException('User has already provided feedback');
      }
    } else {
      throw new ServiceException('User does not exist');
    }
  }
 
  /**
   * Determine whether or not a feedback record exists.
   *
   * @param feedbackID The feedback ID.
   * @returns Whether or not the feedback record exists.
   */
  public async feedbackExists(feedbackID: string): Promise<boolean> {
    const feedback = await this.dbService.getByID<NBFeedback>(
      feedbackTableName,
      feedbackID,
    );
    return !!feedback;
  }
 
  /**
   * Get a feedback record.
   *
   * @param feedbackID The feedback ID.
   * @returns The feedback record.
   */
  public async getFeedback(feedbackID: string): Promise<NBFeedback> {
    const feedback = await this.dbService.getByID<NBFeedback>(
      feedbackTableName,
      feedbackID,
    );
 
    if (feedback) {
      return feedback;
    } else {
      throw new ServiceException('Feedback does not exist');
    }
  }
 
  /**
   * Get feedback provided by a user.
   *
   * @param userID The user's ID.
   * @returns The feedback provided by the user, or undefined if no feedback provided.
   */
  public async getUserFeedback(
    userID: string,
  ): Promise<NBFeedback | undefined> {
    return this.dbService.getByFields<NBFeedback>(feedbackTableName, {
      userID,
    });
  }
 
  /**
   * Get all user feedback.
   *
   * @returns All user feedback.
   */
  public async getAllFeedback(): Promise<NBFeedback[]> {
    return this.dbService.list<NBFeedback>(feedbackTableName, {
      fieldName: 'submitTime',
      sortOrder: 'ASC',
    });
  }
 
  /**
   * Delete a feedback record.
   *
   * @param feedbackID The feedback ID.
   */
  public async deleteFeedback(feedbackID: string): Promise<void> {
    await this.dbService.deleteByID(feedbackTableName, feedbackID);
  }
}