All files verify.ts

98.39% Statements 61/62
90.91% Branches 20/22
100% Functions 7/7
98.04% Lines 50/51

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          20x                                                     20x               20x   11x     11x   11x 2x       9x 9x 9x   9x 2x       7x   7x             7x 14x   7x       7x                 20x 5x 5x 5x   5x                 20x 8x 8x 8x   8x               20x 6x 6x 12x               20x 3x   4x 2x       3x 2x     4x                   20x 4x   4x 1x     3x   3x 2x     2x 2x   1x   20x  
/**
 * Services for the verify table.
 * @packageDocumentation
 */
 
import {
  BaseService,
  getTime,
  newUniqueID,
  pruneVerifyRecord,
  verifyIDLength,
} from "./util";
 
/**
 * Verify architecture.
 */
export interface Verify {
  id: string;
  email: string;
  createTime: number;
}
 
/**
 * Verify with only ID architecture.
 */
interface VerifyID {
  id: string;
}
 
/**
 * Verification services.
 */
export class VerifyService extends BaseService {
  /**
   * Create a user verification record.
   *
   * @param email The email address associated with the user's account.
   * @param prune Whether or not to prune the record when the time comes.
   * @returns The new verification record's ID.
   */
  public async createVerifyRecord(
    email: string,
    Iprune: boolean = true
  ): Promise<string> {
    // Confirm that the email address does not exist
    const emailUnused = await this.dbm.userService.uniqueEmail(email);
 
    if (!emailUnused) {
      return null;
    }
 
    // Check that no verification record has already been created
    let sql = `SELECT id FROM Verify WHERE email = ?;`;
    let params: any[] = [email];
    let rows: VerifyID[] = await this.dbm.execute(sql, params);
 
    if (rows.length > 0) {
      return null;
    }
 
    // Create the verification record
    const newVerifyID = await newUniqueID(this.dbm, "Verify", verifyIDLength);
 
    sql = `
      INSERT INTO Verify (
        id, email, createTime
      ) VALUES (
        ?, ?, ?
      );
    `;
    params = [newVerifyID, email, getTime()];
    await this.dbm.execute(sql, params);
 
    Iif (prune) {
      pruneVerifyRecord(this.dbm, newVerifyID);
    }
 
    return newVerifyID;
  }
 
  /**
   * Check if a verification record exists.
   *
   * @param verifyID A verification record's ID.
   * @returns Whether or not the verification record exists.
   */
  public async verifyRecordExists(verifyID: string): Promise<boolean> {
    const sql = `SELECT id FROM Verify WHERE id = ?;`;
    const params = [verifyID];
    const rows: VerifyID[] = await this.dbm.execute(sql, params);
 
    return rows.length > 0;
  }
 
  /**
   * Get a verification record.
   *
   * @param verifyID A verification record's ID.
   * @returns The verification record.
   */
  public async getVerifyRecord(verifyID: string): Promise<Verify> {
    const sql = `SELECT * FROM Verify WHERE id = ?;`;
    const params = [verifyID];
    const rows: Verify[] = await this.dbm.execute(sql, params);
 
    return rows[0];
  }
 
  /**
   * Delete a verification record.
   *
   * @param verifyID A verification record's ID.
   */
  public async deleteVerifyRecord(verifyID: string): Promise<void> {
    const sql = `DELETE FROM Verify WHERE id = ?;`;
    const params = [verifyID];
    await this.dbm.execute(sql, params);
  }
 
  /**
   * Delete a verification record and the corresponding user.
   *
   * @param verifyID A verification record's ID.
   */
  public async deleteUnverifiedUser(verifyID: string): Promise<void> {
    const verifyRecord = await this.getVerifyRecord(verifyID);
 
    if (verifyRecord) {
      const user = await this.dbm.userService.getUserByEmail(
        verifyRecord.email
      );
 
      if (user && !user.verified) {
        await this.dbm.userService.deleteUser(user.id);
      }
 
      await this.deleteVerifyRecord(verifyID);
    }
  }
 
  /**
   * Verify a user.
   *
   * @param verifyID A verification record's ID.
   * @returns Whether or not the verification was successful.
   */
  public async verifyUser(verifyID: string): Promise<boolean> {
    const verifyRecord = await this.getVerifyRecord(verifyID);
 
    if (!verifyRecord) {
      return false;
    }
 
    const user = await this.dbm.userService.getUserByEmail(verifyRecord.email);
 
    if (!user) {
      return false;
    }
 
    await this.dbm.userService.setVerified(user.id);
    await this.deleteVerifyRecord(verifyID);
 
    return true;
  }
}