Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/controllers/bmdashboard/bmOrgsController.js
Original file line number Diff line number Diff line change
@@ -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;
55 changes: 55 additions & 0 deletions src/models/bmdashboard/orgLocation.js
Original file line number Diff line number Diff line change
@@ -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');
16 changes: 16 additions & 0 deletions src/routes/bmdashboard/bmOrgLocationRouter.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions src/startup/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')();

Expand Down Expand Up @@ -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);
Expand Down