All files admin.ts

100% Statements 20/20
100% Branches 0/0
100% Functions 4/4
100% Lines 15/15

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          20x                                                                                 20x             20x 2x 2x 2x               20x 1x               1x   1x               20x 1x                           1x   1x   20x  
/**
 * Services for admin functions.
 * @packageDocumentation
 */
 
import { BaseService } from "./util";
 
/**
 * Admin record count architecture.
 */
interface AdminRecordCount {
  count: number;
}
 
/**
 * Admin user architecture.
 */
export interface AdminUser {
  userID: string;
  firstname: string;
  lastname: string;
  email: string;
  status: string;
  verified: boolean;
  approved: boolean;
  admin: boolean;
  joinTime: number;
}
 
/**
 * Admin post architecture.
 */
export interface AdminPost {
  postID: string;
  location: string;
  postUser: string;
  program: string;
  rating: number;
  approved: boolean;
  createTime: number;
  adminFavorite: string | null;
}
 
/**
 * Admin services.
 */
export class AdminService extends BaseService {
  /**
   * Get the number of records in a table.
   *
   * @param table The table name.
   * @returns The number of records in the table.
   */
  public async getRecords(table: string): Promise<number> {
    const sql = `SELECT COUNT(*) AS count FROM ${table}`;
    const rows: AdminRecordCount[] = await this.dbm.execute(sql);
    return rows[0].count;
  }
 
  /**
   * Get all users.
   *
   * @returns All users in the database.
   */
  public async getUsers(): Promise<AdminUser[]> {
    const sql = `
      SELECT
          User.id AS userID, firstname, lastname, email,
          UserStatus.name AS status, verified, approved, admin, joinTime
        FROM User
        JOIN UserStatus ON User.statusID = UserStatus.id
      ORDER BY User.joinTime;
    `;
    const rows: AdminUser[] = await this.dbm.execute(sql);
 
    return rows;
  }
 
  /**
   * Get all posts.
   *
   * @returns All posts in the database.
   */
  public async getPosts(): Promise<AdminPost[]> {
    const sql = `
      SELECT
          Post.id AS postID, location,
          CONCAT(User.firstname, ' ', User.lastname) AS postUser,
          Program.name AS program, Rating.general AS rating,
          Post.approved AS approved, Post.createTime AS createTime,
          AdminFavorites.id AS adminFavorite
        FROM Post
             JOIN User           ON Post.userID = User.id
             JOIN Program        ON Post.programID = Program.id
             JOIN Rating         ON Post.ratingID = Rating.id
        LEFT JOIN AdminFavorites ON Post.id = AdminFavorites.postID
      ORDER BY Post.createTime;
    `;
    const rows: AdminPost[] = await this.dbm.execute(sql);
 
    return rows;
  }
}