From 663d059911a9639a8d53af346e6ac17003641abf Mon Sep 17 00:00:00 2001 From: kainguyen <kainguyen@vt.edu> Date: Wed, 30 Oct 2024 14:12:35 +0000 Subject: [PATCH] Populate involvement type and involvement lookup in database --- .../config/database/databaseInitializer.js | 104 ++++++++++++++ backend/repository/involvementRepository.js | 133 ++++++++++++++++++ 2 files changed, 237 insertions(+) diff --git a/backend/config/database/databaseInitializer.js b/backend/config/database/databaseInitializer.js index f3db959..d52c90f 100644 --- a/backend/config/database/databaseInitializer.js +++ b/backend/config/database/databaseInitializer.js @@ -6,6 +6,12 @@ const { addStateIfNotExists, checkTableExistence, } = require("../../repository/stateRepository"); +const { + addInvolvementTypeIfNotExists, + addInvolvementIfNotExists, + checkInvolvementTypeTableExistence, + checkInvolvementLookupTableExistence, +} = require("../../repository/involvementRepository"); /* Class responsible for populating the database @@ -133,11 +139,109 @@ class DatabaseInitializer { } } + /* + Creates all the involvement types + */ + async initializeInvolvementTypes() { + await checkInvolvementTypeTableExistence(); // Ensure the table exists + + const involvementTypes = [ + "Asian American Cadet Organization", + "Black Cadet Organization", + "Cadet Alumni Team", + "Cadet Language Organization", + "Cadets for a Cause", + "Coast Guard Auxiliary", + "Color Guard", + "Conrad Cavalry", + "Corps Marksmanship Team", + "Eagle Scout Association", + "EMT Staff", + "Esprit de Corps", + "Executive Committee", + "Fenix (Latin Cadets)", + "Gregory Guard", + "Growley Team", + "Hall Council", + "Helping Educate Regarding Orientation (HERO)", + "Historian Staff", + "Honor Court", + "Inspector General Staff", + "Men's Basketball", + "O-Course Committee", + "Ordinance Staff", + "Rappelling Tower Committee", + "Recruiting Staff", + "Sash and Saber Honor Society", + "Signal Corps", + "Skipper Crew", + "Society of American Military Engineers", + "VPI Battalion", + "VTCC Cyber Team", + "Women's Basketball", + ]; + + for (const type of involvementTypes) { + await addInvolvementTypeIfNotExists(type); // Add each involvement type + } + } + + /* + Creates all the involvement lookups directly based on the defined involvement types + */ + async initializeInvolvementLookups() { + await checkInvolvementLookupTableExistence(); // Ensure the lookup table exists + + // Define lookup entries with type IDs (these IDs should correspond to your actual involvement type IDs) + const involvementLookups = [ + { involvementTypeId: 1 }, // Asian American Cadet Organization + { involvementTypeId: 2 }, // Black Cadet Organization + { involvementTypeId: 3 }, // Cadet Alumni Team + { involvementTypeId: 4 }, // Cadet Language Organization + { involvementTypeId: 5 }, // Cadets for a Cause + { involvementTypeId: 6 }, // Coast Guard Auxiliary + { involvementTypeId: 7 }, // Color Guard + { involvementTypeId: 8 }, // Conrad Cavalry + { involvementTypeId: 9 }, // Corps Marksmanship Team + { involvementTypeId: 10 }, // Eagle Scout Association + { involvementTypeId: 11 }, // EMT Staff + { involvementTypeId: 12 }, // Esprit de Corps + { involvementTypeId: 13 }, // Executive Committee + { involvementTypeId: 14 }, // Fenix (Latin Cadets) + { involvementTypeId: 15 }, // Gregory Guard + { involvementTypeId: 16 }, // Growley Team + { involvementTypeId: 17 }, // Hall Council + { involvementTypeId: 18 }, // Helping Educate Regarding Orientation (HERO) + { involvementTypeId: 19 }, // Historian Staff + { involvementTypeId: 20 }, // Honor Court + { involvementTypeId: 21 }, // Inspector General Staff + { involvementTypeId: 22 }, // Men’s Basketball + { involvementTypeId: 23 }, // O-Course Committee + { involvementTypeId: 24 }, // Ordinance Staff + { involvementTypeId: 25 }, // Rappelling Tower Committee + { involvementTypeId: 26 }, // Recruiting Staff + { involvementTypeId: 27 }, // Sash and Saber Honor Society + { involvementTypeId: 28 }, // Signal Corps + { involvementTypeId: 29 }, // Skipper Crew + { involvementTypeId: 30 }, // Society of American Military Engineers + { involvementTypeId: 31 }, // VPI Battalion + { involvementTypeId: 32 }, // VTCC Cyber Team + { involvementTypeId: 33 }, // Women’s Basketball + ]; + + for (const entry of involvementLookups) { + await addInvolvementIfNotExists(entry.involvementTypeId); // Add entry to involvementLookup + } + } + + async initializeAll() { await this.testDBConnection(); await this.initializeRoles(); await this.initializeTestUsers(); await this.initializeStates(); + await this.initializeInvolvementTypes(); + await this.initializeInvolvementLookups(); console.log("database init completed."); } diff --git a/backend/repository/involvementRepository.js b/backend/repository/involvementRepository.js index e69de29..4951e84 100644 --- a/backend/repository/involvementRepository.js +++ b/backend/repository/involvementRepository.js @@ -0,0 +1,133 @@ +const { pool } = require("../config/database/database.config"); + +/* +Class responsible for making queries to the +involvementLookup and involvementType database. +*/ + +/* +Function to add an involvement type to the database +*/ +async function addInvolvementTypeIfNotExists(description) { + console.log("adding involvement type: ", description); + + // Don't add involvement type if it exists + const existingType = await getInvolvementType(description); + if (existingType) { + console.log("Involvement type already exists: ", description); + return; + } + + // Insert SQL query + const query = `INSERT INTO involvementType (description) VALUES (?)`; + + // Attempt to insert involvement type into table + try { + await pool.query(query, [description]); + console.log(`Successfully added involvement type: ${description}`); + } catch (error) { + console.error(`Error adding involvement type: ${description}`, error); + throw error; + } +} + +async function getInvolvementType(description) { + // SQL query to get a certain involvement type + const query = "SELECT * FROM involvementType WHERE description = ?"; + + try { + console.log("Getting involvement type: ", description); + const [rows] = await pool.query(query, [description]); + if (rows.length > 0) { + console.log("Found involvement type: ", description); + return rows[0]; + } else { + console.log("Involvement type not found: ", description); + return null; + } + } catch (error) { + console.error(`Error fetching involvement type ${description}:`, error); + throw error; + } +} + +/* +Function to add an involvement to the involvementLookup table +*/ +async function addInvolvementIfNotExists(involvementTypeId) { + console.log("Adding involvement for type ID: ", involvementTypeId); + + // Don't add involvement if it exists + const existingInvolvement = await getInvolvement(involvementTypeId); + if (existingInvolvement) { + console.log("Involvement already exists: ", involvementTypeId); + return; + } + + // Insert SQL query + const query = `INSERT INTO involvementLookup (involvementType) VALUES (?)`; + + // Attempt to insert involvement into table + try { + await pool.query(query, [involvementTypeId]); + console.log(`Successfully added involvement with type ID: ${involvementTypeId}`); + } catch (error) { + console.error(`Error adding involvement with type ID: ${involvementTypeId}`, error); + throw error; + } +} + +async function getInvolvement(involvementType) { + // SQL query to get a certain involvement + const query = "SELECT * FROM involvementLookup WHERE involvementType = ?"; + + try { + console.log("Getting involvement: ", involvementType); + const [rows] = await pool.query(query, [involvementType]); + if (rows.length > 0) { + console.log("Found involvement: ", involvementType); + return rows[0]; + } else { + console.log("Involvement not found: ", involvementType); + return null; + } + } catch (error) { + console.error(`Error fetching involvement ${involvementType}:`, error); + throw error; + } +} + +async function checkInvolvementTypeTableExistence() { + try { + const [rows] = await pool.query("SHOW TABLES LIKE 'involvementType'"); + if (rows.length > 0) { + console.log("Table `involvementType` exists"); + } else { + console.log("Table `involvementType` does not exist"); + } + } catch (error) { + console.error("Error checking involvementType table existence:", error); + } +} + +async function checkInvolvementLookupTableExistence() { + try { + const [rows] = await pool.query("SHOW TABLES LIKE 'involvementLookup'"); + if (rows.length > 0) { + console.log("Table `involvementLookup` exists"); + } else { + console.log("Table `involvementLookup` does not exist"); + } + } catch (error) { + console.error("Error checking involvementLookup table existence:", error); + } +} + +module.exports = { + checkInvolvementTypeTableExistence, + checkInvolvementLookupTableExistence, + addInvolvementTypeIfNotExists, + getInvolvementType, + addInvolvementIfNotExists, + getInvolvement +}; -- GitLab