From 49eaaf98ca96864d5f0f196a53ee97210b5f38d0 Mon Sep 17 00:00:00 2001 From: Federico Hurtado <fed_home@Federicos-Mac-mini.local> Date: Wed, 30 Oct 2024 13:28:53 -0400 Subject: [PATCH] Add involvements to create/update person. --- backend/repository/involvementRepository.js | 89 ++++++++++++++++----- backend/service/peopleService.js | 61 ++++++++++++-- 2 files changed, 127 insertions(+), 23 deletions(-) diff --git a/backend/repository/involvementRepository.js b/backend/repository/involvementRepository.js index 4951e84..058f3dd 100644 --- a/backend/repository/involvementRepository.js +++ b/backend/repository/involvementRepository.js @@ -57,12 +57,12 @@ 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; - } + // 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 (?)`; @@ -70,31 +70,36 @@ async function addInvolvementIfNotExists(involvementTypeId) { // Attempt to insert involvement into table try { await pool.query(query, [involvementTypeId]); - console.log(`Successfully added involvement with type ID: ${involvementTypeId}`); + console.log( + `Successfully added involvement with type ID: ${involvementTypeId}` + ); } catch (error) { - console.error(`Error adding involvement with type ID: ${involvementTypeId}`, 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 = ?"; + // SQL query to get a certain involvement + const query = "SELECT * FROM involvementLookup WHERE involvementType = ?"; - try { + 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]; + console.log("Found involvement: ", involvementType); + return rows[0]; } else { - console.log("Involvement not found: ", involvementType); - return null; + console.log("Involvement not found: ", involvementType); + return null; } - } catch (error) { + } catch (error) { console.error(`Error fetching involvement ${involvementType}:`, error); throw error; - } + } } async function checkInvolvementTypeTableExistence() { @@ -123,11 +128,59 @@ async function checkInvolvementLookupTableExistence() { } } +async function addPeopleInvolvement(personId, involvementId) { + const query = ` + INSERT INTO peopleXInvolvement (peopleId, involvementId) + VALUES (?, ?) + ON DUPLICATE KEY UPDATE peopleId = peopleId; + `; + + try { + await pool.query(query, [personId, involvementId]); + console.log( + `Linked involvement ${involvementId} to person ID: ${personId}` + ); + } catch (error) { + console.log( + `Error linking involvement ${involvementId} to person ID: ${personId}: `, + error + ); + } +} + +/* +Function to get all involvement names associated with a person by their ID. +*/ +async function getInvolvementsForPerson(personId) { + const query = ` + SELECT it.description + FROM peopleXInvolvement px + JOIN involvementLookup il ON px.involvementId = il.involvementId + JOIN involvementType it ON il.involvementType = it.involvementTypeId + WHERE px.peopleId = ? + `; + + try { + const [rows] = await pool.query(query, [personId]); + const involvementNames = rows.map((row) => row.description); + console.log(`Involvements for person ID ${personId}:`, involvementNames); + return involvementNames; + } catch (error) { + console.error( + `Error fetching involvements for person ID ${personId}:`, + error + ); + throw error; + } +} + module.exports = { checkInvolvementTypeTableExistence, checkInvolvementLookupTableExistence, addInvolvementTypeIfNotExists, getInvolvementType, addInvolvementIfNotExists, - getInvolvement + getInvolvement, + addPeopleInvolvement, + getInvolvementsForPerson, }; diff --git a/backend/service/peopleService.js b/backend/service/peopleService.js index 10b760c..04844b3 100644 --- a/backend/service/peopleService.js +++ b/backend/service/peopleService.js @@ -24,6 +24,12 @@ const { updatePerson, getPersonById, } = require("../repository/peopleRepository"); +const { + addPeopleInvolvement, + getInvolvementType, + getInvolvement, + getInvolvementsForPerson, +} = require("../repository/involvementRepository"); /* Function to handle the logic for creating a person. @@ -55,9 +61,24 @@ async function createPerson(person) { console.log("added contact: ", addedContact); } - // insert the involvment information into the database - for (const involvment of involvements) { - console.log("not adding involvements yet."); + // insert the involvement information into the database + for (const involvement of involvements) { + // retrieve involvement Type + const involvementType = await getInvolvementType(involvement); + + if (involvementType) { + // get the invovelment id and link to person + const involvement = await getInvolvement( + involvementType.involvementTypeId + ); + if (involvement) { + await addPeopleInvolvement(newPersonID, involvement.involvementId); + } else { + console.log("invovlement not found"); + } + } else { + console.log("involvement does not exist: ", involvement); + } } // TODO: log database change here @@ -146,7 +167,7 @@ async function getPersonFullDetails(personId) { const contactInformation = await getContactsForPerson(personId); // get the involvement information (none for now) - const involvmentInformation = []; + const involvmentInformation = await getInvolvementsForPerson(personId); // return all of the data return { @@ -268,7 +289,37 @@ async function updateContactsForPerson(personId, contacts) { } } -async function updateInvolvementsForPerson(personId, involevements) {} +async function updateInvolvementsForPerson(personId, involevements) { + // Step 1: Retrieve the current involvements for the person + const currentInvolvements = await getInvolvementsForPerson(personId); + + console.log("current involvements: ", currentInvolvements); + + const newInvolvementIds = []; + for (const involvement of involevements) { + // get the involvement type for each invovlement + const involvementType = await getInvolvementType(involvement); + + if (involvementType) { + // get the invovelment id and link to person + const involvement = await getInvolvement( + involvementType.involvementTypeId + ); + if (involvement) { + console.log("invovlement id: ", involvement.involvementId); + newInvolvementIds.push(involvement.involvementId); + + if (!currentInvolvements.includes(involvement)) { + await addPeopleInvolvement(personId, involvement.involvementId); + } else { + console.log("involvement already here!"); + } + } else { + console.log("invovlement not found"); + } + } + } +} module.exports = { createPerson, -- GitLab