All files userStatus.ts

100% Statements 22/22
100% Branches 4/4
100% Functions 4/4
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          20x                                                     20x           20x 1x 1x   1x                 20x 7x 7x 7x   7x                 20x 3x 3x 3x   3x   20x  
/**
 * Services for the user status table.
 * @packageDocumentation
 */
 
import { BaseService } from "./util";
 
/**
 * User status architecture.
 */
export interface UserStatus {
  id: number;
  name: string;
}
 
/**
 * User status with only ID architecture.
 */
interface UserStatusID {
  id: number;
}
 
/**
 * User status with only name architecture.
 */
interface UserStatusName {
  name: string;
}
 
/**
 * User status services.
 */
export class UserStatusService extends BaseService {
  /**
   * Get all user statuses.
   *
   * @returns A list of all user statuses.
   */
  public async getStatuses(): Promise<UserStatus[]> {
    const sql = `SELECT * FROM UserStatus ORDER BY id;`;
    const rows: UserStatus[] = await this.dbm.execute(sql);
 
    return rows;
  }
 
  /**
   * Get the name of a user status by ID.
   *
   * @param statusID A status's ID.
   * @returns The status's name.
   */
  public async getStatusName(statusID: number): Promise<string> {
    const sql = `SELECT name FROM UserStatus WHERE id = ?;`;
    const params = [statusID];
    const rows: UserStatusName[] = await this.dbm.execute(sql, params);
 
    return rows[0]?.name;
  }
 
  /**
   * Check if a user status is valid.
   *
   * @param statusID A status's ID.
   * @returns Whether or not the status is valid.
   */
  public async validStatus(statusID: number): Promise<boolean> {
    const sql = `SELECT id FROM UserStatus WHERE id = ?;`;
    const params = [statusID];
    const rows: UserStatusID[] = await this.dbm.execute(sql, params);
 
    return rows.length > 0;
  }
}