diff --git a/src/controllers/bmdashboard/bmOrgsController.js b/src/controllers/bmdashboard/bmOrgsController.js new file mode 100644 index 000000000..a60aa843a --- /dev/null +++ b/src/controllers/bmdashboard/bmOrgsController.js @@ -0,0 +1,96 @@ +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; + let 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..1093e1a73 --- /dev/null +++ b/src/models/bmdashboard/orgLocation.js @@ -0,0 +1,55 @@ +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 df64ff6e1..776a068bb 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -171,6 +171,7 @@ const bmEquipmentRouter = require('../routes/bmdashboard/bmEquipmentRouter')(bui const buildingIssue = require('../models/bmdashboard/buildingIssue'); const bmIssueRouter = require('../routes/bmdashboard/bmIssueRouter')(buildingIssue); const bmExternalTeam = require('../routes/bmdashboard/bmExternalTeamRouter'); +const bmOrgLocation = require('../routes/bmdashboard/bmOrgLocationRouter')(); const bmActualVsPlannedCostRouter = require('../routes/bmdashboard/bmActualVsPlannedCostRouter'); const bmRentalChart = require('../routes/bmdashboard/bmRentalChartRouter')(); @@ -283,6 +284,8 @@ module.exports = function (app) { app.use('/api/slack', slackRouter); app.use('/api/accessManagement', appAccessRouter); app.use('/api/bm', bmExternalTeam); + app.use('/api/bm', bmOrgLocation); + app.use('api', bmIssueRouter); app.use('/api/bm', bmIssueRouter); app.use('/api/bm', bmActualVsPlannedCostRouter);