Skip to content

Commit 500d9e0

Browse files
Merge pull request #1550 from OneCommunityGlobal/Vamsi_Krishna_Create_a_stacked_horizontal_bar_graph_for_tools_stoppage_reason
Aayush taking over for Vamsi krishna creates a stacked horizontal bar graph for tools stoppage reason
2 parents afb8969 + d0153f8 commit 500d9e0

4 files changed

Lines changed: 216 additions & 0 deletions

File tree

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
const { ObjectId } = require('mongoose').Types;
2+
3+
const toolStoppageReasonController = function (ToolStoppageReason) {
4+
const stoppageReasonFields = ['usedForLifetime', 'damaged', 'lost'];
5+
6+
const createPercentageProjection = (field) => ({
7+
$cond: [
8+
{ $gt: ['$total', 0] },
9+
{
10+
$round: [
11+
{
12+
$multiply: [{ $divide: [`$${field}`, '$total'] }, 100],
13+
},
14+
2,
15+
],
16+
},
17+
0,
18+
],
19+
});
20+
21+
const buildProjectDateMatchStage = (projectId, startDate, endDate) => {
22+
const matchStage = {
23+
projectId: new ObjectId(projectId),
24+
};
25+
26+
if (startDate || endDate) {
27+
matchStage.date = {};
28+
29+
if (startDate) {
30+
matchStage.date.$gte = new Date(startDate);
31+
}
32+
33+
if (endDate) {
34+
matchStage.date.$lte = new Date(endDate);
35+
}
36+
}
37+
38+
return matchStage;
39+
};
40+
41+
const buildProjectIdAggregation = (collectionName, projectNameField) => [
42+
{
43+
$group: {
44+
_id: '$projectId',
45+
},
46+
},
47+
{
48+
$lookup: {
49+
from: collectionName,
50+
localField: '_id',
51+
foreignField: '_id',
52+
as: 'projectDetails',
53+
},
54+
},
55+
{
56+
$project: {
57+
_id: 1,
58+
projectName: { $arrayElemAt: [`$projectDetails.${projectNameField}`, 0] },
59+
},
60+
},
61+
{
62+
$sort: { projectName: 1 },
63+
},
64+
];
65+
66+
const getToolsStoppageReason = async (req, res) => {
67+
try {
68+
const { id: projectId } = req.params;
69+
const { startDate, endDate } = req.query;
70+
71+
if (!ObjectId.isValid(projectId)) {
72+
return res.status(400).json({ error: 'Invalid project ID format' });
73+
}
74+
75+
const matchStage = buildProjectDateMatchStage(projectId, startDate, endDate);
76+
77+
const groupSums = stoppageReasonFields.reduce(
78+
(accumulator, field) => ({
79+
...accumulator,
80+
[field]: { $sum: `$${field}` },
81+
}),
82+
{},
83+
);
84+
85+
const percentageProjection = stoppageReasonFields.reduce(
86+
(accumulator, field) => ({
87+
...accumulator,
88+
[field]: createPercentageProjection(field),
89+
}),
90+
{},
91+
);
92+
93+
const results = await ToolStoppageReason.aggregate([
94+
{ $match: matchStage },
95+
{
96+
$group: {
97+
_id: '$toolName',
98+
...groupSums,
99+
},
100+
},
101+
{
102+
$addFields: {
103+
total: {
104+
$add: stoppageReasonFields.map((field) => `$${field}`),
105+
},
106+
},
107+
},
108+
{
109+
$project: {
110+
_id: 0,
111+
toolName: '$_id',
112+
...percentageProjection,
113+
},
114+
},
115+
{ $sort: { toolName: 1 } },
116+
]);
117+
118+
return res.json(results);
119+
} catch (error) {
120+
console.error('Error fetching tools stoppage reason:', error);
121+
return res.status(500).json({ error: 'Internal server error' });
122+
}
123+
};
124+
125+
const getUniqueProjectIds = async (req, res) => {
126+
try {
127+
const results = await ToolStoppageReason.aggregate(
128+
buildProjectIdAggregation('buildingProjects', 'name'),
129+
);
130+
131+
const formattedResults = results.map((item) => ({
132+
projectId: item._id,
133+
projectName: item.projectName || 'Unknown Project',
134+
}));
135+
136+
return res.json(formattedResults);
137+
} catch (error) {
138+
console.error('Error fetching unique project IDs:', error);
139+
return res.status(500).json({ error: 'Internal server error' });
140+
}
141+
};
142+
143+
return {
144+
getToolsStoppageReason,
145+
getUniqueProjectIds,
146+
};
147+
};
148+
149+
module.exports = toolStoppageReasonController;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const mongoose = require('mongoose');
2+
3+
const { Schema } = mongoose;
4+
5+
const toolsStoppageReasonSchema = new Schema(
6+
{
7+
projectId: {
8+
type: Schema.Types.ObjectId,
9+
ref: 'buildingProject',
10+
required: true,
11+
},
12+
toolName: {
13+
type: String,
14+
required: true,
15+
},
16+
usedForLifetime: {
17+
type: Number,
18+
required: true,
19+
min: 0,
20+
},
21+
damaged: {
22+
type: Number,
23+
required: true,
24+
min: 0,
25+
},
26+
lost: {
27+
type: Number,
28+
required: true,
29+
min: 0,
30+
},
31+
date: {
32+
type: Date,
33+
default: Date.now,
34+
},
35+
},
36+
{ collection: 'toolStoppageReason' },
37+
);
38+
39+
module.exports = mongoose.model('toolStoppageReason', toolsStoppageReasonSchema);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const express = require('express');
2+
3+
const routes = function (ToolStoppageReason) {
4+
const bmToolStoppageReasonRouter = express.Router();
5+
const controller = require('../../controllers/bmdashboard/bmToolStoppageReasonController')(
6+
ToolStoppageReason,
7+
);
8+
9+
// GET /api/bm/projects/:id/tools-stoppage-reason
10+
bmToolStoppageReasonRouter
11+
.route('/bm/projects/:id/tools-stoppage-reason')
12+
.get(controller.getToolsStoppageReason);
13+
14+
// GET /api/bm/tools-stoppage-reason/projects
15+
bmToolStoppageReasonRouter
16+
.route('/bm/tools-stoppage-reason/projects')
17+
.get(controller.getUniqueProjectIds);
18+
19+
return bmToolStoppageReasonRouter;
20+
};
21+
22+
module.exports = routes;

src/startup/routes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const userPermissionChangeLog = require('../models/userPermissionChangeLog');
8080
const mapLocations = require('../models/mapLocation');
8181
const buildingProject = require('../models/bmdashboard/buildingProject');
8282
const buildingNewLesson = require('../models/bmdashboard/buildingNewLesson');
83+
const buildingToolStoppageReason = require('../models/bmdashboard/buildingToolsStoppage');
8384
const metIssue = require('../models/bmdashboard/metIssue');
8485
const projectStatus = require('../models/bmdashboard/project');
8586

@@ -283,6 +284,9 @@ const bmIssueRouter = require('../routes/bmdashboard/bmIssueRouter')(buildingIss
283284
const bmInjuryRouter = require('../routes/bmdashboard/bmInjuryRouter')(injujrySeverity);
284285

285286
const bmExternalTeam = require('../routes/bmdashboard/bmExternalTeamRouter');
287+
const bmToolStoppageReasonRouter = require('../routes/bmdashboard/bmToolStoppageReasonRouter')(
288+
buildingToolStoppageReason,
289+
);
286290
const bmActualVsPlannedCostRouter = require('../routes/bmdashboard/bmActualVsPlannedCostRouter');
287291
const bmRentalChart = require('../routes/bmdashboard/bmRentalChartRouter')();
288292
const bmToolsReturnedLateRouter = require('../routes/bmdashboard/bmToolsReturnedLateRouter')();
@@ -553,6 +557,8 @@ module.exports = function (app) {
553557
app.use('/api/slack', slackRouter);
554558
app.use('/api/accessManagement', appAccessRouter);
555559
app.use('/api/bm', bmExternalTeam);
560+
//app.use('api', bmIssueRouter);
561+
app.use('/api', bmToolStoppageReasonRouter);
556562
app.use('/api', costBreakdownRouter);
557563
app.use('/api', toolAvailabilityRouter);
558564
app.use('/api', toolUtilizationRouter);

0 commit comments

Comments
 (0)