From 156db09cf0c23a0599cf8088376a51368f9a1551 Mon Sep 17 00:00:00 2001 From: ReinaT5678 Date: Fri, 4 Apr 2025 14:46:40 -0700 Subject: [PATCH 1/6] backend edits --- .../bmdashboard/bmOrgsController.js | 103 ++++++++++++++++++ src/models/bmdashboard/orgLocation.js | 49 +++++++++ src/routes/bmdashboard/orgLocationRoutes.js | 14 +++ 3 files changed, 166 insertions(+) create mode 100644 src/controllers/bmdashboard/bmOrgsController.js create mode 100644 src/models/bmdashboard/orgLocation.js create mode 100644 src/routes/bmdashboard/orgLocationRoutes.js diff --git a/src/controllers/bmdashboard/bmOrgsController.js b/src/controllers/bmdashboard/bmOrgsController.js new file mode 100644 index 000000000..a03afa926 --- /dev/null +++ b/src/controllers/bmdashboard/bmOrgsController.js @@ -0,0 +1,103 @@ +const mongoose = require('mongoose'); + +const bmOrgsController = function () { + const Organizations = require('../../models/bmdashboard/orgLocation'); + + const getAllOrgs = async (req, res) => { + try { + const { startDate, endDate } = req.query; + let query = {}; + + if (startDate || endDate) { + query.startDate = {}; + if (startDate) query.startDate.$gte = new Date(startDate); + if (endDate) query.endDate = { $lte: new Date(endDate) }; + } + + Organizations + .find(query) + .select('orgId name location status startDate') + .then(orgs => { + const transformedOrgs = orgs.map(org => ({ + 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, + count: transformedOrgs.length, + data: transformedOrgs + }); + }) + .catch(error => { + console.error('Error fetching orgs:', error); + res.status(500).json({ + success: false, + error: 'Server Error' + }); + }); + } catch (err) { + console.error('Exception in getAllOrgs:', err); + res.status(500).json({ + success: false, + error: 'Server Error' + }); + } + }; + + 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' + }); + } + + 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(error => { + console.error('Error fetching org by ID:', error); + res.status(500).json({ + success: false, + error: 'Server Error' + }); + }); + } catch (err) { + console.error('Exception in getOrgById:', err); + res.status(500).json({ + success: false, + error: 'Server Error' + }); + } + }; + + 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..0100a3107 --- /dev/null +++ b/src/models/bmdashboard/orgLocation.js @@ -0,0 +1,49 @@ +const mongoose = require('mongoose'); + +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: { + type: String, + enum: ['Point'], + default: 'Point' + }, + coordinates: { + type: [Number], + required: true + } + }, + country: { + type: String, + required: true + }, + startDate: { + type: Date, + required: true + }, + endDate: { + type: Date, + required: true + } +}) + +// geospatial index +OrgLocationSchema.index({ location: '2dsphere' }); +module.exports = mongoose.model('OrgLocation', OrgLocationSchema); diff --git a/src/routes/bmdashboard/orgLocationRoutes.js b/src/routes/bmdashboard/orgLocationRoutes.js new file mode 100644 index 000000000..9672d069f --- /dev/null +++ b/src/routes/bmdashboard/orgLocationRoutes.js @@ -0,0 +1,14 @@ +const express = require("express"); + +const routes = function () { + const NewOrgRouter = express.Router(); + const orgLocationController = require('../../controllers/bmdashboard/bmOrgsController')(); + + NewOrgRouter.route('/orgLocation').get(orgLocationController.getAllOrgs); + + NewOrgRouter.route('/orgLocation/:id').get(orgLocationController.getOrgById); + + return NewOrgRouter; +}; + +module.exports = routes; From 67ad47d0a410a40b4ad42d642345baf07f1adc34 Mon Sep 17 00:00:00 2001 From: ReinaT5678 Date: Tue, 8 Apr 2025 13:41:49 -0700 Subject: [PATCH 2/6] backend routes --- .../bmdashboard/bmOrgsController.js | 107 ++++++++---------- src/models/bmdashboard/orgLocation.js | 28 +++-- ...cationRoutes.js => bmOrgLocationRouter.js} | 9 +- 3 files changed, 72 insertions(+), 72 deletions(-) rename src/routes/bmdashboard/{orgLocationRoutes.js => bmOrgLocationRouter.js} (64%) diff --git a/src/controllers/bmdashboard/bmOrgsController.js b/src/controllers/bmdashboard/bmOrgsController.js index a03afa926..6ca83fafa 100644 --- a/src/controllers/bmdashboard/bmOrgsController.js +++ b/src/controllers/bmdashboard/bmOrgsController.js @@ -1,69 +1,68 @@ const mongoose = require('mongoose'); -const bmOrgsController = function () { +const bmOrgsController = function() { const Organizations = require('../../models/bmdashboard/orgLocation'); - - const getAllOrgs = async (req, res) => { + + const getAllOrgs = async(req, res) => { try { - const { startDate, endDate } = req.query; + // query parameters + const {startDate, endDate} = req.query; let query = {}; - + + // build query if date filters provided if (startDate || endDate) { - query.startDate = {}; - if (startDate) query.startDate.$gte = new Date(startDate); - if (endDate) query.endDate = { $lte: new Date(endDate) }; + if (startDate) query.startDate = {$gte: new Date(startDate)}; + if (endDate) query.endDate = {$lte: new Date(endDate)}; } - - Organizations - .find(query) - .select('orgId name location status startDate') - .then(orgs => { - const transformedOrgs = orgs.map(org => ({ - 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, - count: transformedOrgs.length, - data: transformedOrgs - }); - }) - .catch(error => { - console.error('Error fetching orgs:', error); - res.status(500).json({ - success: false, - error: 'Server Error' - }); + + // 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('Exception in getAllOrgs:', err); + } catch(err) { res.status(500).json({ success: false, - error: 'Server Error' + error: 'Server error '+ error.message }); } }; - + const getOrgById = async (req, res) => { try { - const { id } = req.params; - + const {id} = req.params; + Organizations - .findOne({ orgId: id }) - .select('orgId name location status startDate') + .findOne({orgId: id}) + . select('ordId name location status startDate') .then(org => { if (!org) { return res.status(404).json({ - success: false, + success:false, error: 'Organization not found' }); } - + + // transformed org const transformedOrg = { orgId: org.orgId, name: org.name, @@ -72,28 +71,20 @@ const bmOrgsController = function () { status: org.status, startDate: org.startDate }; - - res.status(200).json({ - success: true, + + res.status(200).json ({ + success:true, data: transformedOrg }); }) - .catch(error => { - console.error('Error fetching org by ID:', error); - res.status(500).json({ - success: false, - error: 'Server Error' - }); - }); } catch (err) { - console.error('Exception in getOrgById:', err); - res.status(500).json({ + res.status(500).json ({ success: false, - error: 'Server Error' + error: 'Server error' }); } }; - + return { getAllOrgs, getOrgById diff --git a/src/models/bmdashboard/orgLocation.js b/src/models/bmdashboard/orgLocation.js index 0100a3107..1093e1a73 100644 --- a/src/models/bmdashboard/orgLocation.js +++ b/src/models/bmdashboard/orgLocation.js @@ -1,5 +1,16 @@ 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, @@ -20,15 +31,9 @@ const OrgLocationSchema = new mongoose.Schema({ required: true }, location: { - type: { - type: String, - enum: ['Point'], - default: 'Point' - }, - coordinates: { - type: [Number], - required: true - } + type: pointSchema, + required: true, + index: '2dsphere' }, country: { type: String, @@ -42,8 +47,9 @@ const OrgLocationSchema = new mongoose.Schema({ type: Date, required: true } +}, { + timestamps: true }) // geospatial index -OrgLocationSchema.index({ location: '2dsphere' }); -module.exports = mongoose.model('OrgLocation', OrgLocationSchema); +module.exports = mongoose.model('OrgLocation', OrgLocationSchema, 'orgLocation'); diff --git a/src/routes/bmdashboard/orgLocationRoutes.js b/src/routes/bmdashboard/bmOrgLocationRouter.js similarity index 64% rename from src/routes/bmdashboard/orgLocationRoutes.js rename to src/routes/bmdashboard/bmOrgLocationRouter.js index 9672d069f..3919a760e 100644 --- a/src/routes/bmdashboard/orgLocationRoutes.js +++ b/src/routes/bmdashboard/bmOrgLocationRouter.js @@ -1,14 +1,17 @@ -const express = require("express"); +const express = require ("express"); -const routes = function () { +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; +module.exports = routes; \ No newline at end of file From 054060735d2806e64ae0997bd18c48153cf515db Mon Sep 17 00:00:00 2001 From: ReinaT5678 Date: Thu, 17 Apr 2025 12:53:18 -0700 Subject: [PATCH 3/6] reina-create-interactive-map-showing-location-of-projects-backend --- .../bmdashboard/bmOrgsController.js | 53 ++++++++++--------- src/routes/bmdashboard/bmOrgLocationRouter.js | 1 - src/startup/routes.js | 2 + 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/controllers/bmdashboard/bmOrgsController.js b/src/controllers/bmdashboard/bmOrgsController.js index 6ca83fafa..73741568e 100644 --- a/src/controllers/bmdashboard/bmOrgsController.js +++ b/src/controllers/bmdashboard/bmOrgsController.js @@ -2,26 +2,27 @@ const mongoose = require('mongoose'); const bmOrgsController = function() { const Organizations = require('../../models/bmdashboard/orgLocation'); - const getAllOrgs = async(req, res) => { try { - // query parameters + // query parameters const {startDate, endDate} = req.query; let query = {}; - - // build query if date filters provided + + // 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 + + console.log(`Found ${orgs.length} organizations`); + + // transform data const transformedOrgs = orgs .filter(org => org.location && org.location.coordinates && org.location.coordinates.length === 2) .map(org => ({ @@ -33,16 +34,18 @@ const bmOrgsController = function() { startDate: org.startDate, country: org.country })); - // send response - res.status(200).json({ - success:true, - count:transformedOrgs.length, - data: transformedOrgs - }); + + // 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 '+ error.message + error: 'Server error ' + err.message }); } }; @@ -50,19 +53,18 @@ const bmOrgsController = function() { const getOrgById = async (req, res) => { try { const {id} = req.params; - Organizations .findOne({orgId: id}) - . select('ordId name location status startDate') + .select('orgId name location status startDate') .then(org => { if (!org) { return res.status(404).json({ - success:false, + success: false, error: 'Organization not found' }); } - - // transformed org + + // transformed org const transformedOrg = { orgId: org.orgId, name: org.name, @@ -71,16 +73,17 @@ const bmOrgsController = function() { status: org.status, startDate: org.startDate }; - - res.status(200).json ({ - success:true, + + res.status(200).json({ + success: true, data: transformedOrg }); - }) + }); } catch (err) { - res.status(500).json ({ + console.error('Error in getOrgById:', err); + res.status(500).json({ success: false, - error: 'Server error' + error: 'Server error ' + err.message }); } }; diff --git a/src/routes/bmdashboard/bmOrgLocationRouter.js b/src/routes/bmdashboard/bmOrgLocationRouter.js index 3919a760e..429ad8027 100644 --- a/src/routes/bmdashboard/bmOrgLocationRouter.js +++ b/src/routes/bmdashboard/bmOrgLocationRouter.js @@ -4,7 +4,6 @@ const routes = function() { // initialize routes const NewOrgRouter = express.Router(); const orgLocationController = require('../../controllers/bmdashboard/bmOrgsController')(); - // get all organizations NewOrgRouter.route('/orgLocation').get(orgLocationController.getAllOrgs); diff --git a/src/startup/routes.js b/src/startup/routes.js index 99b339849..04cb09be6 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -127,6 +127,7 @@ const bmToolRouter = require('../routes/bmdashboard/bmToolRouter')(buildingTool, const bmEquipmentRouter = require('../routes/bmdashboard/bmEquipmentRouter')(buildingEquipment); const bmIssueRouter = require('../routes/bmdashboard/bmIssueRouter')(buildingIssue); const bmExternalTeam = require('../routes/bmdashboard/bmExternalTeamRouter'); +const bmOrgLocation = require('../routes/bmdashboard/bmOrgLocationRouter')(); const blueSquareEmailAssignmentRouter = require('../routes/BlueSquareEmailAssignmentRouter')( blueSquareEmailAssignment, @@ -192,6 +193,7 @@ module.exports = function (app) { app.use('/api/bm', bmEquipmentRouter); app.use('/api/bm', bmConsumablesRouter); app.use('/api/bm', bmExternalTeam); + app.use('/api/bm', bmOrgLocation); app.use('api', bmIssueRouter); app.use('/api', registrationRouter); }; From dc1d5e51af7bdce885fe8999e228fd825e2ad2a9 Mon Sep 17 00:00:00 2001 From: ReinaT5678 <56940344+ReinaT5678@users.noreply.github.com> Date: Thu, 17 Apr 2025 13:08:32 -0700 Subject: [PATCH 4/6] Update bmOrgsController.js --- src/controllers/bmdashboard/bmOrgsController.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/controllers/bmdashboard/bmOrgsController.js b/src/controllers/bmdashboard/bmOrgsController.js index 73741568e..369ad4421 100644 --- a/src/controllers/bmdashboard/bmOrgsController.js +++ b/src/controllers/bmdashboard/bmOrgsController.js @@ -19,8 +19,6 @@ const bmOrgsController = function() { .select('orgId name location status startDate country') .lean() .exec(); - - console.log(`Found ${orgs.length} organizations`); // transform data const transformedOrgs = orgs From 20a87f32f90fc1dba7295dc02bc9f518df00411c Mon Sep 17 00:00:00 2001 From: ReinaT5678 Date: Mon, 21 Apr 2025 11:05:00 -0700 Subject: [PATCH 5/6] reina-create-map-showing-location-of-orgs-backend --- src/controllers/bmdashboard/bmOrgsController.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/controllers/bmdashboard/bmOrgsController.js b/src/controllers/bmdashboard/bmOrgsController.js index 73741568e..721d5ea37 100644 --- a/src/controllers/bmdashboard/bmOrgsController.js +++ b/src/controllers/bmdashboard/bmOrgsController.js @@ -19,9 +19,7 @@ const bmOrgsController = function() { .select('orgId name location status startDate country') .lean() .exec(); - - console.log(`Found ${orgs.length} organizations`); - + // transform data const transformedOrgs = orgs .filter(org => org.location && org.location.coordinates && org.location.coordinates.length === 2) From 1fbb3a45165c22876e9329b026284bc8cdd4cd98 Mon Sep 17 00:00:00 2001 From: ReinaT5678 Date: Mon, 28 Jul 2025 13:09:51 -0700 Subject: [PATCH 6/6] feat(routes): missing routes --- src/routes/bmdashboard/bmOrgLocationRouter.js | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/routes/bmdashboard/bmOrgLocationRouter.js b/src/routes/bmdashboard/bmOrgLocationRouter.js index 429ad8027..149684ce6 100644 --- a/src/routes/bmdashboard/bmOrgLocationRouter.js +++ b/src/routes/bmdashboard/bmOrgLocationRouter.js @@ -1,16 +1,16 @@ -const express = require ("express"); +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); +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); + // get one organization by it's ID + NewOrgRouter.route('/orgLocation/:id').get(orgLocationController.getOrgById); - return NewOrgRouter; + return NewOrgRouter; }; -module.exports = routes; \ No newline at end of file +module.exports = routes;