Skip to content
Snippets Groups Projects
Commit 49eaaf98 authored by Federico Hurtado's avatar Federico Hurtado
Browse files

Add involvements to create/update person.

parent 961193d4
No related branches found
No related tags found
No related merge requests found
...@@ -57,12 +57,12 @@ Function to add an involvement to the involvementLookup table ...@@ -57,12 +57,12 @@ Function to add an involvement to the involvementLookup table
async function addInvolvementIfNotExists(involvementTypeId) { async function addInvolvementIfNotExists(involvementTypeId) {
console.log("Adding involvement for type ID: ", involvementTypeId); console.log("Adding involvement for type ID: ", involvementTypeId);
// Don't add involvement if it exists // Don't add involvement if it exists
const existingInvolvement = await getInvolvement(involvementTypeId); const existingInvolvement = await getInvolvement(involvementTypeId);
if (existingInvolvement) { if (existingInvolvement) {
console.log("Involvement already exists: ", involvementTypeId); console.log("Involvement already exists: ", involvementTypeId);
return; return;
} }
// Insert SQL query // Insert SQL query
const query = `INSERT INTO involvementLookup (involvementType) VALUES (?)`; const query = `INSERT INTO involvementLookup (involvementType) VALUES (?)`;
...@@ -70,31 +70,36 @@ async function addInvolvementIfNotExists(involvementTypeId) { ...@@ -70,31 +70,36 @@ async function addInvolvementIfNotExists(involvementTypeId) {
// Attempt to insert involvement into table // Attempt to insert involvement into table
try { try {
await pool.query(query, [involvementTypeId]); 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) { } catch (error) {
console.error(`Error adding involvement with type ID: ${involvementTypeId}`, error); console.error(
`Error adding involvement with type ID: ${involvementTypeId}`,
error
);
throw error; throw error;
} }
} }
async function getInvolvement(involvementType) { async function getInvolvement(involvementType) {
// SQL query to get a certain involvement // SQL query to get a certain involvement
const query = "SELECT * FROM involvementLookup WHERE involvementType = ?"; const query = "SELECT * FROM involvementLookup WHERE involvementType = ?";
try { try {
console.log("Getting involvement: ", involvementType); console.log("Getting involvement: ", involvementType);
const [rows] = await pool.query(query, [involvementType]); const [rows] = await pool.query(query, [involvementType]);
if (rows.length > 0) { if (rows.length > 0) {
console.log("Found involvement: ", involvementType); console.log("Found involvement: ", involvementType);
return rows[0]; return rows[0];
} else { } else {
console.log("Involvement not found: ", involvementType); console.log("Involvement not found: ", involvementType);
return null; return null;
} }
} catch (error) { } catch (error) {
console.error(`Error fetching involvement ${involvementType}:`, error); console.error(`Error fetching involvement ${involvementType}:`, error);
throw error; throw error;
} }
} }
async function checkInvolvementTypeTableExistence() { async function checkInvolvementTypeTableExistence() {
...@@ -123,11 +128,59 @@ async function checkInvolvementLookupTableExistence() { ...@@ -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 = { module.exports = {
checkInvolvementTypeTableExistence, checkInvolvementTypeTableExistence,
checkInvolvementLookupTableExistence, checkInvolvementLookupTableExistence,
addInvolvementTypeIfNotExists, addInvolvementTypeIfNotExists,
getInvolvementType, getInvolvementType,
addInvolvementIfNotExists, addInvolvementIfNotExists,
getInvolvement getInvolvement,
addPeopleInvolvement,
getInvolvementsForPerson,
}; };
...@@ -24,6 +24,12 @@ const { ...@@ -24,6 +24,12 @@ const {
updatePerson, updatePerson,
getPersonById, getPersonById,
} = require("../repository/peopleRepository"); } = require("../repository/peopleRepository");
const {
addPeopleInvolvement,
getInvolvementType,
getInvolvement,
getInvolvementsForPerson,
} = require("../repository/involvementRepository");
/* /*
Function to handle the logic for creating a person. Function to handle the logic for creating a person.
...@@ -55,9 +61,24 @@ async function createPerson(person) { ...@@ -55,9 +61,24 @@ async function createPerson(person) {
console.log("added contact: ", addedContact); console.log("added contact: ", addedContact);
} }
// insert the involvment information into the database // insert the involvement information into the database
for (const involvment of involvements) { for (const involvement of involvements) {
console.log("not adding involvements yet."); // 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 // TODO: log database change here
...@@ -146,7 +167,7 @@ async function getPersonFullDetails(personId) { ...@@ -146,7 +167,7 @@ async function getPersonFullDetails(personId) {
const contactInformation = await getContactsForPerson(personId); const contactInformation = await getContactsForPerson(personId);
// get the involvement information (none for now) // get the involvement information (none for now)
const involvmentInformation = []; const involvmentInformation = await getInvolvementsForPerson(personId);
// return all of the data // return all of the data
return { return {
...@@ -268,7 +289,37 @@ async function updateContactsForPerson(personId, contacts) { ...@@ -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 = { module.exports = {
createPerson, createPerson,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment