Skip to content
Snippets Groups Projects
databaseInitializer.js 2.91 KiB
const { addUser, findByUsername } = require("../../repository/userRepository");
const {
  addRoleIfNotExists,
} = require("../../repository/rolesPermissionRepository");
const {
  addStateIfNotExists,
  checkTableExistence,
} = require("../../repository/stateRepository");

/*
Class responsible for populating the database 
when the server starts with necessary lookup tables and 
users.
*/
class DatabaseInitializer {
  constructor(pool) {
    this.pool = pool;
  }

  /*
  Method for ensuring database is accessible.
  */
  async testDBConnection() {
    try {
      const [results] = await this.pool.query("SELECT 1");
      console.log("Database connection test successful. Results:", results);
    } catch (err) {
      console.error("Error testing database connection:", err);
      throw err;
    }
  }

  /*
  Method for creating the possible user roles and 
  their descriptions.
  */
  async initializeRoles() {
    console.log("initializing roles...");

    try {
      await addRoleIfNotExists(1, "guest", "Guest access");
      await addRoleIfNotExists(2, "admin", "Admin access");
      console.log("Roles initialized successfully.");
    } catch (error) {
      console.log("error initializing roles: ", error);
      throw error;
    }
  }

  /*
  Creates all of us as test users with admin roles.
  */
  async initializeTestUsers() {
    const testUsers = ["fhurtado14", "laylah", "sushant20", "kainguyen"];
    for (const user of testUsers) {
      try {
        const foundUser = await findByUsername(user);
        if (foundUser) {
          console.log("user already exists: ", user);
          continue;
        }

        // add user with admin privilages
        await addUser(user, 2);
        console.log("successfully added test user: ", user);
      } catch (error) {
        console.log("Error adding test user: ", user);
        console.log("Error was: ", error);
      }
    }
  }

  /*
  Creates all the states
  */
  async initializeStates() {
    checkTableExistence();

    const states = [
      "AL",
      "AK",
      "AZ",
      "AR",
      "CA",
      "CO",
      "CT",
      "DE",
      "FL",
      "GA",
      "HI",
      "ID",
      "IL",
      "IN",
      "IA",
      "KS",
      "KY",
      "LA",
      "ME",
      "MD",
      "MA",
      "MI",
      "MN",
      "MS",
      "MO",
      "MT",
      "NE",
      "NV",
      "NH",
      "NJ",
      "NM",
      "NY",
      "NC",
      "ND",
      "OH",
      "OK",
      "OR",
      "PA",
      "RI",
      "SC",
      "SD",
      "TN",
      "TX",
      "UT",
      "VT",
      "VA",
      "WA",
      "WV",
      "WI",
      "WY",
    ];
    for (const state of states) {
      await addStateIfNotExists(state);
    }
  }

  async initializeAll() {
    await this.testDBConnection();
    await this.initializeRoles();
    await this.initializeTestUsers();
    await this.initializeStates();

    console.log("database init completed.");
  }
}

module.exports = DatabaseInitializer;