Skip to content
Open
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
97 changes: 97 additions & 0 deletions src/controllers/bmdashboard/bmOrgsController.js
Original file line number Diff line number Diff line change
@@ -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;
58 changes: 58 additions & 0 deletions src/models/bmdashboard/orgLocation.js
Original file line number Diff line number Diff line change
@@ -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');
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: 2 additions & 1 deletion src/startup/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')(
Expand Down Expand Up @@ -507,6 +507,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);
Expand Down