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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 20x 20x 20x 5x 5x 5x 1x 4x 7x 1x 1x 3x 3x 3x 6x 3x 3x 20x 6x 6x 6x 6x 20x 5x 5x 5x 5x 20x 5x 5x 5x 5x 20x 4x 4x 4x 4x 20x 3x 4x 2x 2x 4x 2x 1x 20x 2x 2x 4x 20x 1x 1x 2x 20x 2x 2x 1x 1x 1x 2x 2x 1x 20x | /** * Services for the email change table. * @packageDocumentation */ import { BaseService, getTime, newUniqueID, pruneEmailChangeRecord, emailChangeIDLength, } from "./util"; /** * Email change architecture. */ export interface EmailChange { id: string; userID: string; newEmail: string; createTime: number; } /** * Email change with only ID architecture. */ interface EmailChangeID { id: string; } /** * Email changing services. */ export class EmailChangeService extends BaseService { /** * Create an email change record. * * @param userID The user's ID. * @param newEmail The new email address. * @param prune Whether or not to prune the record when the time comes. * @returns The new email change record's ID. */ public async createEmailChangeRecord( userID: string, newEmail: string, Iprune: boolean = true ): Promise<string> { // Confirm that the user exists const userExists = await this.dbm.userService.userExists(userID); if (!userExists) { return null; } // Check if an email change record has already been created by the user const recordExists = await this.emailChangeRecordExistsByUserID(userID); if (recordExists) { const emailChangeID = await this.editEmailChangeRecord(userID, newEmail); return emailChangeID; } else { // Create the email change record const newEmailChangeID = await newUniqueID( this.dbm, "EmailChange", emailChangeIDLength ); const sql = ` INSERT INTO EmailChange ( id, userID, newEmail, createTime ) VALUES ( ?, ?, ?, ? ); `; const params = [newEmailChangeID, userID, newEmail, getTime()]; await this.dbm.execute(sql, params); Iif (prune) { pruneEmailChangeRecord(this.dbm, newEmailChangeID); } return newEmailChangeID; } } /** * Check if an email change record exists. * * @param emailChangeID An email change record's ID. * @returns Whether or not the email change record exists. */ public async emailChangeRecordExists( emailChangeID: string ): Promise<boolean> { const sql = `SELECT id FROM EmailChange WHERE id = ?;`; const params = [emailChangeID]; const rows: EmailChangeID[] = await this.dbm.execute(sql, params); return rows.length > 0; } /** * Check if an email change record exists for a given user. * * @param userID A user's ID. * @returns Whether or not the email change record exists. */ public async emailChangeRecordExistsByUserID( userID: string ): Promise<boolean> { const sql = `SELECT id FROM EmailChange WHERE userID = ?;`; const params = [userID]; const rows: EmailChangeID[] = await this.dbm.execute(sql, params); return rows.length > 0; } /** * Get an email change record. * * @param emailChangeID An email change record's ID. * @returns The email change record. */ public async getEmailChangeRecord( emailChangeID: string ): Promise<EmailChange> { const sql = `SELECT * FROM EmailChange WHERE id = ?;`; const params = [emailChangeID]; const rows: EmailChange[] = await this.dbm.execute(sql, params); return rows[0]; } /** * Get an email change record for a given user. * * @param emailChangeID An email change record's ID. * @returns The email change record. */ public async getEmailChangeRecordByUserID( userID: string ): Promise<EmailChange> { const sql = `SELECT * FROM EmailChange WHERE userID = ?;`; const params = [userID]; const rows: EmailChange[] = await this.dbm.execute(sql, params); return rows[0]; } /** * Edit an email change record. * * @param userID A user's ID. * @param newEmail The new email address. * @returns The existing email change record's ID. */ public async editEmailChangeRecord( userID: string, newEmail: string ): Promise<string> { const emailChangeRecord = await this.getEmailChangeRecordByUserID(userID); if (emailChangeRecord) { const sql = `UPDATE EmailChange SET newEmail = ? WHERE id = ?;`; const params = [newEmail, emailChangeRecord.id]; await this.dbm.execute(sql, params); return emailChangeRecord.id; } else { return null; } } /** * Delete an email change record. * * @param emailChangeID An email change record's ID. */ public async deleteEmailChangeRecord(emailChangeID: string): Promise<void> { const sql = `DELETE FROM EmailChange WHERE id = ?;`; const params = [emailChangeID]; await this.dbm.execute(sql, params); } /** * Delete an email change record for a given user. * * @param userID A user's ID. */ public async deleteEmailChangeRecordByUserID(userID: string): Promise<void> { const sql = `DELETE FROM EmailChange WHERE userID = ?;`; const params = [userID]; await this.dbm.execute(sql, params); } /** * Change a user's email address. * * @param emailChangeID An email change record's ID. * @returns Whether or not the email change was successful. */ public async changeEmail(emailChangeID: string): Promise<boolean> { const emailChangeRecord = await this.getEmailChangeRecord(emailChangeID); if (!emailChangeRecord) { return false; } const sql = `UPDATE User SET email = ? WHERE id = ?;`; const params = [emailChangeRecord.newEmail, emailChangeRecord.userID]; await this.dbm.execute(sql, params); await this.deleteEmailChangeRecord(emailChangeID); return true; } } |