All files suspended.ts

97.96% Statements 48/49
75% Branches 6/8
100% Functions 7/7
97.56% Lines 40/41

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          20x                                                               20x                 20x     2x   2x   2x 2x 2x   3x 1x             1x 2x   2x   1x       1x   1x                   20x 2x 2x 2x   2x                 20x 2x 2x 2x   2x               20x 2x 2x 4x                 20x 7x 7x 7x   7x               20x 1x                 1x   1x   20x  
/**
 * Services for the suspended table.
 * @packageDocumentation
 */
 
import { BaseService, getTime, newUniqueID, pruneSuspension } from "./util";
import { User } from "./user";
 
/**
 * Suspended architecture.
 */
export interface Suspended {
  id: string;
  userID: string;
  suspendedUntil: number;
  createTime: number;
}
 
/**
 * Suspended with only ID architecture.
 */
interface SuspendedID {
  id: string;
}
 
/**
 * Suspended user architecture.
 */
export interface SuspendedUser extends User {
  status: string;
  suspensionID: string;
  suspendedUntil: number;
}
 
/**
 * Suspended services.
 */
export class SuspendedService extends BaseService {
  /**
   * Suspend a user's account.
   *
   * @param userID A user's ID.
   * @param until The time at which the account will no longer be suspended.
   * @param prune Whether or not to prune the suspension when the time comes.
   * @return The new suspension record's ID.
   */
  public async suspendUser(
    userID: string,
    until: number,
    Iprune: boolean = true
  ): Promise<string> {
    const suspensionID = await newUniqueID(this.dbm, "UserStatusChange");
 
    let sql = `SELECT id FROM Suspended WHERE userID = ?;`;
    let params: any[] = [userID];
    const rows: SuspendedID[] = await this.dbm.execute(sql, params);
 
    if (rows.length === 0) {
      sql = `
        INSERT INTO Suspended (
          id, userID, suspendedUntil, createTime
        ) VALUES (
          ?, ?, ?, ?
        );
      `;
      params = [suspensionID, userID, until, getTime()];
      await this.dbm.execute(sql, params);
 
      await this.dbm.sessionService.deleteUserSessions(userID);
 
      Iif (prune) {
        pruneSuspension(this.dbm, suspensionID);
      }
 
      return suspensionID;
    } else {
      return null;
    }
  }
 
  /**
   * Check if a suspension record exists.
   *
   * @param suspensionID A suspension record's ID.
   * @returns Whether or not the suuspension record exists.
   */
  public async suspensionExists(suspensionID: string): Promise<boolean> {
    const sql = `SELECT id FROM Suspended WHERE id = ?;`;
    const params = [suspensionID];
    const rows: SuspendedID[] = await this.dbm.execute(sql, params);
 
    return rows.length > 0;
  }
 
  /**
   * Get a suspension record.
   *
   * @param suspensionID A suspension record's ID.
   * @returns The suspension record.
   */
  public async getSuspension(suspensionID: string): Promise<Suspended> {
    const sql = `SELECT * FROM Suspended WHERE id = ?;`;
    const params = [suspensionID];
    const rows: Suspended[] = await this.dbm.execute(sql, params);
 
    return rows[0];
  }
 
  /**
   * Delete a suspension record.
   *
   * @param suspensionID A suspension record's ID.
   */
  public async deleteSuspension(suspensionID: string): Promise<void> {
    const sql = `DELETE FROM Suspended WHERE id = ?;`;
    const params = [suspensionID];
    await this.dbm.execute(sql, params);
  }
 
  /**
   * Check whether or not a user's account has been suspended.
   *
   * @param userID A user's ID.
   * @returns Whether or not the user's account has been suspended.
   */
  public async userIsSuspended(userID: string): Promise<boolean> {
    const sql = `SELECT id FROM Suspended WHERE userID = ?;`;
    const params = [userID];
    const rows: SuspendedID[] = await this.dbm.execute(sql, params);
 
    return rows.length > 0;
  }
 
  /**
   * Get all suspended users.
   *
   * @returns All suspended users
   */
  public async suspendedUsers(): Promise<SuspendedUser[]> {
    const sql = `
      SELECT
          User.*, UserStatus.name AS status, Suspended.id AS suspensionID,
          suspendedUntil
        FROM User
        JOIN Suspended  ON User.id = Suspended.userID
        JOIN UserStatus ON User.statusID = UserStatus.id
      ORDER BY Suspended.suspendedUntil;
    `;
    const rows: SuspendedUser[] = await this.dbm.execute(sql);
 
    return rows;
  }
}