Skip to content
Snippets Groups Projects
peopleService.js 1.81 KiB
const { addAddress } = require("../repository/addressRepository");
const { addContact } = require("../repository/contactRepository");
const { addDegree } = require("../repository/degreeRepository");
const {
  addPerson,
  searchPeopleByName,
} = require("../repository/peopleRepository");

/*
Function to handle the logic for creating a person.
*/
async function createPerson(person) {
  // extract the parts of a person
  const { personInfo, degrees, addresses, contacts, involvements } = person;

  console.log("Adding person....");
  // insert the personInfo into people table
  const newPersonID = await addPerson(personInfo);
  console.log("Added person ID: ", newPersonID);

  // insert the degree information into the database
  for (const degree of degrees) {
    const addedDegree = await addDegree(degree, newPersonID);
    console.log("added degree: ", degree);
  }

  // insert the address information into the database
  for (const address of addresses) {
    const addedAddress = await addAddress(address, newPersonID);
    console.log("added address: ", address);
  }

  // insert the contact information into the database
  for (const contact of contacts) {
    const addedContact = await addContact(contact, newPersonID);
    console.log("added contact: ", contact);
  }

  // insert the involvment information into the database
  for (const involvment of involvements) {
    console.log("not adding involvements yet.");
  }

  // TODO: log database change here

  return newPersonID;
}

/*
Function to call repository and get people with a 
gicen first and last name.
*/
async function findByName(firstName, lastName) {
  // call repository function, null will be returned if no people are found
  const peopleFound = await searchPeopleByName(firstName, lastName);
  return peopleFound;
}

module.exports = { createPerson, findByName };