All files userStatusChange.ts

100% Statements 58/58
100% Branches 7/7
100% Functions 9/9
100% Lines 49/49

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177          20x                                                                     20x               20x       4x   4x 4x 4x   5x 3x             3x 6x   3x   1x 1x 2x   1x                   20x 5x 5x 5x   5x                 20x     5x 5x 5x   5x               20x 3x 3x 6x               20x 1x 1x 1x   1x     20x 1x                     1x   1x               20x 2x   3x 1x 1x 2x   2x                 20x 2x   20x  
/**
 * Services for the user status change table.
 * @packageDocumentation
 */
 
import { BaseService, getTime, newUniqueID } from "./util";
 
/**
 * User status change architecture.
 */
export interface UserStatusChange {
  id: string;
  userID: string;
  newStatusID: number;
  createTime: number;
}
 
/**
 * User statuus change with only ID architecture.
 */
interface UserStatusChangeID {
  id: string;
}
 
/**
 * User status change requests architecture.
 */
export interface UserStatusChangeRequest {
  userID: string;
  firstname: string;
  lastname: string;
  email: string;
  status: string;
  newStatus: string;
  requestID: string;
}
 
/**
 * User status change services.
 */
export class UserStatusChangeService extends BaseService {
  /**
   * Create a new user status change request.
   *
   * @param userID The user's ID.
   * @param newStatusID The requested new status ID.
   * @returns The new status change request's ID.
   */
  public async createStatusChangeRequest(
    userID: string,
    newStatusID: number
  ): Promise<string> {
    const requestID = await newUniqueID(this.dbm, "UserStatusChange");
 
    let sql = `SELECT id FROM UserStatusChange WHERE userID = ?;`;
    let params: any[] = [userID];
    const rows: UserStatusChangeID[] = await this.dbm.execute(sql, params);
 
    if (rows.length === 0) {
      sql = `
        INSERT INTO UserStatusChange (
          id, userID, newStatusID, createTime
        ) VALUES (
          ?, ?, ?, ?
        );
      `;
      params = [requestID, userID, newStatusID, getTime()];
      await this.dbm.execute(sql, params);
 
      return requestID;
    } else {
      sql = `UPDATE UserStatusChange SET newStatusID = ? WHERE userID = ?;`;
      params = [newStatusID, userID];
      await this.dbm.execute(sql, params);
 
      return rows[0].id;
    }
  }
 
  /**
   * Check if a user status change request exists.
   *
   * @param requestID A status change request's ID.
   * @returns Whether or not the user status change request exists.
   */
  public async statusChangeRequestExists(requestID: string): Promise<boolean> {
    const sql = `SELECT id FROM UserStatusChange WHERE id = ?;`;
    const params = [requestID];
    const rows: UserStatusChangeID[] = await this.dbm.execute(sql, params);
 
    return rows.length > 0;
  }
 
  /**
   * Get a user status change request by ID.
   *
   * @param requestID A status change request's ID.
   * @returns The user status change request.
   */
  public async getStatusChangeRequest(
    requestID: string
  ): Promise<UserStatusChange> {
    const sql = `SELECT * FROM UserStatusChange WHERE id = ?;`;
    const params = [requestID];
    const rows: UserStatusChange[] = await this.dbm.execute(sql, params);
 
    return rows[0];
  }
 
  /**
   * Delete a user status change request.
   *
   * @param requestID A status change request's ID.
   */
  public async deleteStatusChangeRequest(requestID: string): Promise<void> {
    const sql = `DELETE FROM UserStatusChange WHERE id = ?;`;
    const params = [requestID];
    await this.dbm.execute(sql, params);
  }
 
  /**
   * Get all user status change requests.
   *
   * @returns All user status change requests.
   */
  public async getStatusChangeRequests(): Promise<UserStatusChange[]> {
    const sql = `SELECT * FROM UserStatusChange;`;
    const params = [];
    const rows: UserStatusChange[] = await this.dbm.execute(sql, params);
 
    return rows;
  }
 
  public async getUserRequests(): Promise<UserStatusChangeRequest[]> {
    const sql = `
      SELECT
        User.id AS userID, firstname, lastname, email,
        us1.name AS status, us2.name AS newStatus,
        UserStatusChange.id AS requestID
      FROM UserStatusChange
      JOIN User              ON UserStatusChange.userID = User.id
      JOIN UserStatus AS us1 ON User.statusID = us1.id
      JOIN UserStatus AS us2 ON UserStatusChange.newStatusID = us2.id
      ORDER BY UserStatusChange.createTime;
    `;
    const rows: UserStatusChangeRequest[] = await this.dbm.execute(sql);
 
    return rows;
  }
 
  /**
   * Approve a user status change request.
   *
   * @param requestID A status change request's ID.
   */
  public async approveStatusChangeRequest(requestID: string): Promise<void> {
    const request = await this.getStatusChangeRequest(requestID);
 
    if (request) {
      const sql = `UPDATE User SET statusID = ? WHERE id = ?;`;
      const params = [request.newStatusID, request.userID];
      await this.dbm.execute(sql, params);
 
      await this.deleteStatusChangeRequest(requestID);
    }
  }
 
  /**
   * Deny a user status change request.
   *
   * @param requestID A status change request's ID.
   */
  public async denyStatusChangeRequest(requestID: string): Promise<void> {
    await this.deleteStatusChangeRequest(requestID);
  }
}