-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmaterialUtilizationController.js
More file actions
217 lines (199 loc) · 6.02 KB
/
materialUtilizationController.js
File metadata and controls
217 lines (199 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const mongoose = require('mongoose');
const MaterialUsage = require('../models/materialUsage');
/**
* @desc Get Material Utilization data aggregated by project
* @route GET /api/materials/utilization
* @access Private
*/
const getMaterialUtilization = async (req, res) => {
const { start, end, projects, materials } = req.query;
// --- 1. Validation ---
if (!start || !end) {
return res
.status(400)
.json({ success: false, message: 'Both start and end dates are required' });
}
let startDate;
let endDate;
try {
startDate = new Date(start);
endDate = new Date(end);
if (Number.isNaN(startDate.getTime()) || Number.isNaN(endDate.getTime())) {
throw new Error('Invalid date format');
}
} catch (error) {
return res
.status(400)
.json({ success: false, message: 'Invalid date format. Please use ISO date strings.' });
}
if (startDate > endDate) {
return res.status(400).json({ success: false, message: 'Start date cannot be after end date' });
}
// --- 2. Build Initial Match Stage ---
const matchStage = {
date: {
$gte: startDate,
$lte: endDate,
},
};
// If project filters are provided, add them to the match stage
if (projects && Array.isArray(projects) && projects.length > 0) {
try {
// Convert string IDs to valid MongoDB ObjectIds
const projectObjectIds = projects.map((id) => new mongoose.Types.ObjectId(id));
matchStage.projectId = { $in: projectObjectIds };
} catch (error) {
return res
.status(400)
.json({ success: false, message: 'Invalid project ID format provided.' });
}
}
if (materials && Array.isArray(materials) && materials.length > 0) {
try {
// Convert material string IDs to ObjectIds
const materialObjectIds = materials.map((id) => new mongoose.Types.ObjectId(id));
matchStage.materialId = { $in: materialObjectIds };
} catch (error) {
return res.status(400).json({ success: false, message: 'Invalid material ID format.' });
}
}
// --- 3. Aggregation Pipeline ---
try {
const aggregationPipeline = [
// Stage 1: Filter documents by date and optional projects
{
$match: matchStage,
},
// Stage 2: Group by projectId to sum up quantities
{
$group: {
_id: '$projectId',
projectName: { $first: '$projectName' }, // Get the first project name found
totalHandled: { $sum: '$receivedQty' },
used: { $sum: '$usedQty' },
},
},
// Stage 3: Calculate unused, and handle division by zero
{
$project: {
_id: 0, // Exclude the default _id
project: '$projectName',
used: '$used',
unused: { $max: [0, { $subtract: ['$totalHandled', '$used'] }] },
totalHandled: '$totalHandled', // Pass totalHandled to the next stage
},
},
// Stage 4: Calculate percentages
{
$project: {
project: 1,
used: 1,
unused: 1,
totalHandled: 1,
// Use $cond to prevent division by zero if totalHandled is 0
usedPct: {
$cond: {
if: { $eq: ['$totalHandled', 0] },
then: 0,
else: {
$round: [
{ $multiply: [{ $divide: ['$used', '$totalHandled'] }, 100] },
1, // Round to 1 decimal place
],
},
},
},
},
},
// Stage 5: Calculate unusedPct and final formatting
{
$project: {
project: 1,
totalHandled: 1,
used: 1,
unused: 1,
usedPct: 1,
unusedPct: { $subtract: [100, '$usedPct'] },
},
},
// Stage 6: Sort by project name as requested
{
$sort: { project: 1 },
},
];
const utilizationData = await MaterialUsage.aggregate(aggregationPipeline);
if (utilizationData.length === 0) {
// Per spec, return 404 if no records found
return res
.status(404)
.json({ success: false, message: 'No material records for selected range' });
}
res.status(200).json(utilizationData); // Send the successful response
} catch (error) {
console.error('Error in Material Utilization Aggregation:', error.message);
res.status(500).json({ success: false, message: 'Server Error' });
}
};
/**
* @desc Get all distinct projects for the filter dropdown
* @route GET /api/materials/distinct-projects
* @access Private
*/
const getDistinctProjects = async (req, res) => {
try {
// This finds all unique projectId/projectName pairs
const projectData = await MaterialUsage.aggregate([
{
$group: {
_id: '$projectId',
projectName: { $first: '$projectName' },
},
},
{ $sort: { projectName: 1 } },
// Send it in the format { _id: "...", projectName: "..." }
{
$project: {
_id: 1,
projectName: 1,
},
},
]);
res.status(200).json(projectData);
} catch (error) {
console.error('Error fetching distinct projects:', error.message);
res.status(500).json({ success: false, message: 'Server Error' });
}
};
/**
* @desc Get all distinct materials for the filter dropdown
* @route GET /api/materials/distinct-materials
* @access Private
*/
const getDistinctMaterials = async (req, res) => {
try {
const materialData = await MaterialUsage.aggregate([
{
$group: {
_id: '$materialId',
materialName: { $first: '$materialName' },
},
},
{ $sort: { materialName: 1 } },
{
$project: {
_id: 1,
materialName: 1,
},
},
]);
res.status(200).json(materialData);
} catch (error) {
console.error('Error fetching distinct materials:', error.message);
res.status(500).json({ success: false, message: 'Server Error' });
}
};
module.exports = {
getMaterialUtilization,
getDistinctProjects,
getDistinctMaterials,
};