From 5764f5ba9ad1192cabe43a54e77b0974158b6af0 Mon Sep 17 00:00:00 2001 From: Maithili Kalkar Date: Sat, 21 Feb 2026 16:04:32 -0500 Subject: [PATCH] fix: resolve merge conflicts of 1342 --- .../bmdashboard/bmOrgsController.js | 97 +++++++++++++++++++ src/models/bmdashboard/orgLocation.js | 58 +++++++++++ src/routes/bmdashboard/bmOrgLocationRouter.js | 16 +++ src/startup/routes.js | 3 +- 4 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 src/controllers/bmdashboard/bmOrgsController.js create mode 100644 src/models/bmdashboard/orgLocation.js create mode 100644 src/routes/bmdashboard/bmOrgLocationRouter.js diff --git a/src/controllers/bmdashboard/bmOrgsController.js b/src/controllers/bmdashboard/bmOrgsController.js new file mode 100644 index 000000000..b81b84e51 --- /dev/null +++ b/src/controllers/bmdashboard/bmOrgsController.js @@ -0,0 +1,97 @@ +const mongoose = require('mongoose'); + +const bmOrgsController = function () { + const Organizations = require('../../models/bmdashboard/orgLocation'); + const getAllOrgs = async (req, res) => { + try { + // query parameters + const { startDate, endDate } = req.query; + const query = {}; + + // build query if date filters provided + if (startDate || endDate) { + if (startDate) query.startDate = { $gte: new Date(startDate) }; + if (endDate) query.endDate = { $lte: new Date(endDate) }; + } + + // execute query and find org + const orgs = await Organizations.find(query) + .select('orgId name location status startDate country') + .lean() + .exec(); + + // transform data + const transformedOrgs = orgs + .filter( + (org) => + org.location && org.location.coordinates && org.location.coordinates.length === 2, + ) + .map((org) => ({ + orgId: org.orgId, + name: org.name, + latitude: org.location.coordinates[1], + longitude: org.location.coordinates[0], + status: org.status, + startDate: org.startDate, + country: org.country, + })); + + // send response + res.status(200).json({ + success: true, + count: transformedOrgs.length, + data: transformedOrgs, + }); + } catch (err) { + console.error('Error in getAllOrgs:', err); + res.status(500).json({ + success: false, + error: `Server error ${err.message}`, + }); + } + }; + + const getOrgById = async (req, res) => { + try { + const { id } = req.params; + Organizations.findOne({ orgId: id }) + .select('orgId name location status startDate') + .then((org) => { + if (!org) { + return res.status(404).json({ + success: false, + error: 'Organization not found', + }); + } + + // transformed org + const transformedOrg = { + orgId: org.orgId, + name: org.name, + latitude: org.location.coordinates[1], + longitude: org.location.coordinates[0], + status: org.status, + startDate: org.startDate, + }; + + res.status(200).json({ + success: true, + data: transformedOrg, + }); + }); + } catch (err) { + console.error('Error in getOrgById:', err); + res.status(500).json({ + success: false, + error: `Server error ${err.message}`, + }); + } + }; + + return { + getAllOrgs, + getOrgById, + }; +}; + +module.exports = bmOrgsController; diff --git a/src/models/bmdashboard/orgLocation.js b/src/models/bmdashboard/orgLocation.js new file mode 100644 index 000000000..6ec1e96c6 --- /dev/null +++ b/src/models/bmdashboard/orgLocation.js @@ -0,0 +1,58 @@ +const mongoose = require('mongoose'); + +const pointSchema = new mongoose.Schema({ + type: { + type: String, + enum: ['Point'], + required: true, + }, + coordinates: { + type: [Number], + required: true, + }, +}); +const OrgLocationSchema = new mongoose.Schema( + { + orgId: { + type: String, + required: true, + unique: true, + }, + name: { + type: String, + required: true, + }, + description: { + type: String, + required: true, + }, + status: { + type: String, + enum: ['active', 'completed', 'delayed'], + required: true, + }, + location: { + type: pointSchema, + required: true, + index: '2dsphere', + }, + country: { + type: String, + required: true, + }, + startDate: { + type: Date, + required: true, + }, + endDate: { + type: Date, + required: true, + }, + }, + { + timestamps: true, + }, +); + +// geospatial index +module.exports = mongoose.model('OrgLocation', OrgLocationSchema, 'orgLocation'); diff --git a/src/routes/bmdashboard/bmOrgLocationRouter.js b/src/routes/bmdashboard/bmOrgLocationRouter.js new file mode 100644 index 000000000..149684ce6 --- /dev/null +++ b/src/routes/bmdashboard/bmOrgLocationRouter.js @@ -0,0 +1,16 @@ +const express = require('express'); + +const routes = function () { + // initialize routes + const NewOrgRouter = express.Router(); + const orgLocationController = require('../../controllers/bmdashboard/bmOrgsController')(); + // get all organizations + NewOrgRouter.route('/orgLocation').get(orgLocationController.getAllOrgs); + + // get one organization by it's ID + NewOrgRouter.route('/orgLocation/:id').get(orgLocationController.getOrgById); + + return NewOrgRouter; +}; + +module.exports = routes; diff --git a/src/startup/routes.js b/src/startup/routes.js index 0c713f06a..bb4c470fa 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -204,7 +204,7 @@ const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')(); const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(buildingMaterial); const bmReusableRouter = require('../routes/bmdashboard/bmReusableRouter')(buildingReusable); const bmProjectRouter = require('../routes/bmdashboard/bmProjectRouter')(buildingProject); - +const bmOrgLocation = require('../routes/bmdashboard/bmOrgLocationRouter')(); const bmNewLessonRouter = require('../routes/bmdashboard/bmNewLessonRouter')(buildingNewLesson); const injuryCategoryRoutes = require('../routes/bmdashboard/injuryCategoryRouter'); const bmConsumablesRouter = require('../routes/bmdashboard/bmConsumablesRouter')( @@ -493,6 +493,7 @@ module.exports = function (app) { app.use('/api/bm/injuries', injuryCategoryRoutes); app.use('/api', toolAvailabilityRouter); app.use('/api', projectCostTrackingRouter); + app.use('/api/bm', bmOrgLocation); app.use('/api/bm', bmIssueRouter); app.use('/api/labor-cost', bmPaidLaborCostRouter); app.use('/api/bm', bmInjuryRouter);