From 453992160bf350c4dc789131f2b26238703d7405 Mon Sep 17 00:00:00 2001 From: Hricha Mehra Date: Sun, 3 Sep 2023 11:34:18 +0530 Subject: [PATCH 1/5] added files --- controller/organization.js | 60 ++++++++++++++++++++++++++++++++++++++ routes/organization.js | 10 +++++++ services/organization.js | 40 +++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 controller/organization.js create mode 100644 routes/organization.js create mode 100644 services/organization.js diff --git a/controller/organization.js b/controller/organization.js new file mode 100644 index 00000000..f0b6effc --- /dev/null +++ b/controller/organization.js @@ -0,0 +1,60 @@ +import { + addNewOrganization, deleteOrganizationById, updateOrganizationById, getOrganization, + } from "#services/organization"; + import { logger } from "#util"; + + async function addOrganization(req, res) { + const { + parent, startDate, name, accreditation, + } = req.body; + try { + const organization = await addNewOrganization( parent, startDate, name, accreditation,); + res.json({ res: `added organization${organization.name}` }); + } catch (error) { + logger.error("Error while inserting", error); + res.status(500); + res.json({ err: "Error while inserting in DB" }); + } + } + async function deleteOrganization(req, res) { + const { organizationId } = req.params; + try { + await deleteOrganizationById(accredationId); + res.json({ res: "Orgaization deleted successfully" }); + } catch (error) { + logger.error("Error while deleting", error); + res.status(500); + res.json({ err: "Error while deleting from DB" }); + } + } + + async function updateOrganization(req, res) { + const { + id, ...data + } = req.body; + + try { + await updateOrganizationById(id, data); + res.json({ res: "organization updated" }); + } catch (error) { + logger.error("Error while inserting", error); + res.status(500); + res.json({ err: "Error while inserting in DB" }); + } + } + + async function showOrganization(req, res) { + try { + const organization = await getOrganization(req.query); + return res.json({ res: organization }); + } catch (error) { + logger.error("Error while fetching", error); + res.status(500); + return res.json({ err: "Error while fetching the data" }); + } + } + + export default { + addOrganization, updateOrganization, deleteOrganization, showOrganization, + }; + \ No newline at end of file diff --git a/routes/organization.js b/routes/organization.js new file mode 100644 index 00000000..1d532dd6 --- /dev/null +++ b/routes/organization.js @@ -0,0 +1,10 @@ +import express from "express"; +import organizationController from "#controller/organization"; + +const router = express.Router(); +router.get("/list", organizationController.showOrganization); +router.post("/add", organizationController.addOrganization); +router.delete("/delete/:organizationId", organizationController.deleteOrganization); +router.post("/update", organizationController.updateOrganization); + +export default router; diff --git a/services/organization.js b/services/organization.js new file mode 100644 index 00000000..b1d3d155 --- /dev/null +++ b/services/organization.js @@ -0,0 +1,40 @@ +import Organization from "#models/organization"; +import databaseError from "#error/database"; + +export async function addNewOrganization(parent, startDate, name, accreditation) { + const newOrganization = await Organization.create({ + parent, startDate, name, accreditation, + }); + if (newOrganization.name === name) { + return newOrganization; + } + throw new databaseError.DataEntryError("Add Organization"); +} + +export async function getOrganizations(filter) { + const organization = await Organization.read(filter); + if (organization) { + return organization; + } + throw new databaseError.DataNotFoundError("Organization"); +} + +export async function deleteOrganizationById(accredationId) { + const deleted = await Organization.remove({ _id: accredationId }); + if (deleted) { + return deleted; + } + throw new databaseError.DataDeleteError("Organization"); +} + +export async function updateOrganizationById(id, data) { + const updated = await Organization.update({ _id: id }, data); + if (updated) { + return updated; + } + throw new databaseError.DataEntryError("Organization"); +} + +export default { + deleteOrganizationById, addNewOrganization, updateOrganizationById, +}; From 6e50d4e58acbe4081db551516f79c51c0acd0984 Mon Sep 17 00:00:00 2001 From: Hricha Mehra Date: Sun, 3 Sep 2023 12:00:42 +0530 Subject: [PATCH 2/5] added oragnization to app.js --- app.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 769f3985..d83390b8 100644 --- a/app.js +++ b/app.js @@ -10,6 +10,7 @@ import usersRouter from "#routes/users"; import authRouter from "#routes/auth"; import accreditationRouter from "#routes/accreditation"; import infrastructureRouter from "#routes/infrastructure"; +import organizationRouter from "#routes/organization"; import { identifyUser } from "#middleware/identifyUser"; const app = express(); @@ -33,5 +34,5 @@ app.use("/users", usersRouter); app.use("/auth", authRouter); app.use("/accreditation", accreditationRouter); app.use("/infrastructure", infrastructureRouter); - +app.use("/organization", organizationRouter); export default app; From 86f4f7e65466c368cec4a780cf67b11a83bca65b Mon Sep 17 00:00:00 2001 From: Hricha Mehra Date: Sun, 3 Sep 2023 19:25:09 +0530 Subject: [PATCH 3/5] made suggested changes --- controller/organization.js | 8 ++++---- services/organization.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/controller/organization.js b/controller/organization.js index f0b6effc..422c3921 100644 --- a/controller/organization.js +++ b/controller/organization.js @@ -1,5 +1,5 @@ import { - addNewOrganization, deleteOrganizationById, updateOrganizationById, getOrganization, + addNewOrganization, deleteOrganizationById, updateOrganizationById, getOrganizations, } from "#services/organization"; import { logger } from "#util"; @@ -19,8 +19,8 @@ import { async function deleteOrganization(req, res) { const { organizationId } = req.params; try { - await deleteOrganizationById(accredationId); - res.json({ res: "Orgaization deleted successfully" }); + await deleteOrganizationById(organizationId); + res.json({ res: "Organization deleted successfully" }); } catch (error) { logger.error("Error while deleting", error); res.status(500); @@ -45,7 +45,7 @@ import { async function showOrganization(req, res) { try { - const organization = await getOrganization(req.query); + const organization = await getOrganizations(req.query); return res.json({ res: organization }); } catch (error) { logger.error("Error while fetching", error); diff --git a/services/organization.js b/services/organization.js index b1d3d155..d34a6a19 100644 --- a/services/organization.js +++ b/services/organization.js @@ -19,7 +19,7 @@ export async function getOrganizations(filter) { throw new databaseError.DataNotFoundError("Organization"); } -export async function deleteOrganizationById(accredationId) { +export async function deleteOrganizationById(organizationId) { const deleted = await Organization.remove({ _id: accredationId }); if (deleted) { return deleted; From 20f6e3cd8b9bfaa962c55c53ef36759152be8fc2 Mon Sep 17 00:00:00 2001 From: Hricha Mehra Date: Sun, 3 Sep 2023 19:27:14 +0530 Subject: [PATCH 4/5] made suggested changes --- services/organization.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/organization.js b/services/organization.js index d34a6a19..221e0b87 100644 --- a/services/organization.js +++ b/services/organization.js @@ -20,7 +20,7 @@ export async function getOrganizations(filter) { } export async function deleteOrganizationById(organizationId) { - const deleted = await Organization.remove({ _id: accredationId }); + const deleted = await Organization.remove({ _id: organizationId }); if (deleted) { return deleted; } From 1af530e5fd8eb3fc830ec2e9811d83577014c0a3 Mon Sep 17 00:00:00 2001 From: Hricha Mehra Date: Thu, 7 Sep 2023 11:43:13 +0530 Subject: [PATCH 5/5] made suggested changes(exported get organization) --- services/organization.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/organization.js b/services/organization.js index 221e0b87..f4d4e359 100644 --- a/services/organization.js +++ b/services/organization.js @@ -36,5 +36,5 @@ export async function updateOrganizationById(id, data) { } export default { - deleteOrganizationById, addNewOrganization, updateOrganizationById, + deleteOrganizationById, addNewOrganization, updateOrganizationById,getOrganizations, };