Skip to content

Commit

Permalink
✨ Pipelines Endpoints -> Company
Browse files Browse the repository at this point in the history
  • Loading branch information
ouckah committed Apr 16, 2024
1 parent 30e2b3a commit 7934934
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
51 changes: 50 additions & 1 deletion server/controllers/companyController.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const getCompany = async (req, res) => {
}

try {
// Find the company by name in the database
// Find the company by id in the database
const company = await Company.findById(id);

// Check if the company exists
Expand All @@ -75,6 +75,24 @@ const getCompany = async (req, res) => {
}
};

const getCompanyByName = async (req, res) => {
const { name } = req.query;

try {
// Find the company by name in the database
const company = await Company.findOne({ name: name.toLowerCase() });

// Check if the company exists
if (!company) {
return res.status(404).json({ error: "Company not found." });
}

return res.status(200).json(company);
} catch (err) {
return res.status(400).json({ error: "Failed to retrieve company" });
}
};

const getCompanies = async (req, res) => {
const { query } = req.params;

Expand All @@ -91,6 +109,35 @@ const getCompanies = async (req, res) => {
}
};

const getEmployees = async (req, res) => {
const { id } = req.params;

try {
// Find the company by name in the database
const company = await Company.findById(id);

// Check if the company exists
if (!company) {
return res.status(404).json({ error: "Company not found." });
}

// Retrieve profiles of employees and interns using the arrays of object IDs
const employeeProfiles = await Profile.find({
_id: { $in: company.Employees },
});
const internProfiles = await Profile.find({
_id: { $in: company.interns },
});

// Combine employee and intern profiles into one list
const allProfiles = [...employeeProfiles, ...internProfiles];

return res.status(200).json(allProfiles);
} catch (err) {
return res.status(400).json({ error: "Failed to retrieve company" });
}
};

const updateCompany = async (req, res) => {
//update company on user registration
const { id } = req.params;
Expand Down Expand Up @@ -542,7 +589,9 @@ const deleteCompany = async (req, res) => {
module.exports = {
createCompany,
getCompany,
getCompanyByName,
getCompanies,
getEmployees,
updateCompany,
updateCompanies,
deleteCompany,
Expand Down
8 changes: 8 additions & 0 deletions server/routes/companies.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const bodyParser = require("body-parser");
const {
createCompany,
getCompany,
getCompanyByName,
getCompanies,
getEmployees,
updateCompany,
updateCompanies,
deleteCompany,
Expand All @@ -18,9 +20,15 @@ write.post("/create", bodyParser.json(), createCompany);
//get a specific company
read.get("/get/:id", getCompany);

// get a specific company by name
read.get("/getBy", getCompanyByName);

//get multiple companies based on a query
read.get("/get/companies/:query", getCompanies);

// get profiles of employees that work at company
read.get("/employees/:id", getEmployees);

//Update a specific company
// ! TEMP READ COMMAND
// TODO: change up backend to remove client changing of companies
Expand Down

0 comments on commit 7934934

Please sign in to comment.