All files session.ts

96.83% Statements 61/63
71.43% Branches 10/14
100% Functions 10/10
96.3% Lines 52/54

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          20x                                                                       20x               20x   3x   3x           3x             3x 3x 6x   3x       3x                 20x 5x 5x 5x   5x                 20x 2x 2x 2x   2x               20x 1x 1x 2x                 20x 2x 2x 2x   2x               20x 16x 16x 32x                 20x 3x 3x 3x   3x                 20x 1x   1x 1x 1x   1x               20x   1x   1x 1x 2x   1x       20x  
/**
 * Services for the session table.
 * @packageDocumentation
 */
 
import {
  BaseService,
  getTime,
  newUniqueID,
  pruneSession,
  sessionIDLength,
} from "./util";
import { User } from "./user";
 
/**
 * Session architecture.
 */
export interface Session {
  id: string;
  userID: string;
  createTime: number;
  updateTime: number;
}
 
/**
 * Session with only ID architecture.
 */
interface SessionID {
  id: string;
}
 
/**
 * Session with only user ID architecture.
 */
interface SessionUserID {
  userID: string;
}
 
/**
 * Session services.
 */
export class SessionService extends BaseService {
  /**
   * Create a session.
   *
   * @param userID The ID of the user associated with the session.
   * @param prune Whether or not to prune the session when the time comes.
   * @returns The new session's ID.
   */
  public async createSession(
    userID: string,
    Iprune: boolean = true
  ): Promise<string> {
    const newSessionID = await newUniqueID(
      this.dbm,
      "Session",
      sessionIDLength
    );
 
    const sql = `
      INSERT INTO Session (
        id, userID, createTime, updateTime
      ) VALUES (
        ?, ?, ?, ?
      );
    `;
    const now = getTime();
    const params = [newSessionID, userID, now, now];
    await this.dbm.execute(sql, params);
 
    Iif (prune) {
      pruneSession(this.dbm, newSessionID);
    }
 
    return newSessionID;
  }
 
  /**
   * Check if a session exists.
   *
   * @param sessionID A session's ID.
   * @returns Whether or not the session exists.
   */
  public async sessionExists(sessionID: string): Promise<boolean> {
    const sql = `SELECT id FROM Session WHERE id = ?;`;
    const params = [sessionID];
    const rows: SessionID[] = await this.dbm.execute(sql, params);
 
    return rows.length > 0;
  }
 
  /**
   * Get a session.
   *
   * @param sessionID A session's ID.
   * @returns The session.
   */
  public async getSession(sessionID: string): Promise<Session> {
    const sql = `SELECT * FROM Session WHERE id = ?;`;
    const params = [sessionID];
    const rows: Session[] = await this.dbm.execute(sql, params);
 
    return rows[0];
  }
 
  /**
   * Delete a session.
   *
   * @param sessionID A session's ID.
   */
  public async deleteSession(sessionID: string): Promise<void> {
    const sql = `DELETE FROM Session WHERE id = ?;`;
    const params = [sessionID];
    await this.dbm.execute(sql, params);
  }
 
  /**
   * Get all of a user's sessions.
   *
   * @param userID A user's ID.
   * @returns A list of all sessions associated with the user.
   */
  public async getUserSessions(userID: string): Promise<Session[]> {
    const sql = `SELECT * FROM Session WHERE userID = ? ORDER BY createTime;`;
    const params = [userID];
    const rows: Session[] = await this.dbm.execute(sql, params);
 
    return rows;
  }
 
  /**
   * Delete all of a user's sessions.
   *
   * @param userID A user's ID.
   */
  public async deleteUserSessions(userID: string): Promise<void> {
    const sql = `DELETE FROM Session WHERE userID = ?;`;
    const params = [userID];
    await this.dbm.execute(sql, params);
  }
 
  /**
   * Get a user ID by session ID.
   *
   * @param sessionID A session's ID.
   * @returns The ID of the user associated with the session.
   */
  public async getUserIDBySessionID(sessionID: string): Promise<string> {
    const sql = `SELECT userID from Session WHERE id = ?;`;
    const params = [sessionID];
    const rows: SessionUserID[] = await this.dbm.execute(sql, params);
 
    return rows[0]?.userID;
  }
 
  /**
   * Get a user by session ID.
   *
   * @param sessionID A session's ID.
   * @returns The user associated with the session.
   */
  public async getUserBySessionID(sessionID: string): Promise<User> {
    const userID = await this.getUserIDBySessionID(sessionID);
 
    const sql = `SELECT * FROM User WHERE id = ?;`;
    const params = [userID];
    const rows: User[] = await this.dbm.execute(sql, params);
 
    return rows[0];
  }
 
  /**
   * Update the timestamp at which the session ID was last used.
   *
   * @param sessionID A session's ID.
   */
  public async updateSession(
    sessionID: string,
    Iprune: boolean = true
  ): Promise<void> {
    const sql = `UPDATE Session SET updateTime = ? WHERE id = ?;`;
    const params = [getTime(), sessionID];
    await this.dbm.execute(sql, params);
 
    Iif (prune) {
      pruneSession(this.dbm, sessionID);
    }
  }
}