@@ -177,7 +177,7 @@ const bmFinancialController = function (BuildingProject, BuildingMaterial, Build
177177 let laborCost = 0 ;
178178
179179 try {
180- materialsCost = await calculateMaterialsCost ( BuildingMaterial , project . _id ) ;
180+ materialsCost = await calculateMaterialsCost ( project . _id ) ;
181181 } catch ( error ) {
182182 logger . logException (
183183 `Materials cost error for project ${ project . _id } : ${ error . message } ` ,
@@ -223,7 +223,7 @@ const bmFinancialController = function (BuildingProject, BuildingMaterial, Build
223223
224224 const results = await Promise . all (
225225 projects . map ( async ( project ) => {
226- const materialsCost = await calculateMaterialsCost ( BuildingMaterial , project . _id ) ;
226+ const materialsCost = await calculateMaterialsCost ( project . _id ) ;
227227 const toolsCost = await calculateToolsCost ( project . _id ) ;
228228 const laborCost = await calculateLaborCost ( project . _id ) ;
229229 const totalCost = materialsCost + toolsCost + laborCost ;
@@ -247,12 +247,16 @@ const bmFinancialController = function (BuildingProject, BuildingMaterial, Build
247247
248248 const getTotalProjectCost = async ( req , res ) => {
249249 try {
250- const project = await BuildingProject . findById ( req . params . projectId ) ;
250+ const { projectId } = req . params ;
251+ if ( ! mongoose . Types . ObjectId . isValid ( projectId ) ) {
252+ return res . status ( 400 ) . json ( { message : 'Invalid project ID' } ) ;
253+ }
254+ const project = await BuildingProject . findById ( projectId ) ;
251255 if ( ! project ) {
252- logger . logException ( `Project with ID ${ req . params . projectId } not found` ) ;
256+ logger . logException ( `Project with ID ${ projectId } not found` ) ;
253257 return res . status ( 404 ) . json ( { message : 'Project not found' } ) ;
254258 }
255- const materialsCost = await calculateMaterialsCost ( BuildingMaterial , project . _id ) ;
259+ const materialsCost = await calculateMaterialsCost ( project . _id ) ;
256260 const toolsCost = await calculateToolsCost ( project . _id ) ;
257261 const laboorCost = await calculateLaborCost ( project . _id ) ;
258262
@@ -268,12 +272,16 @@ const bmFinancialController = function (BuildingProject, BuildingMaterial, Build
268272
269273 const getCostBreakdown = async ( req , res ) => {
270274 try {
271- const project = await BuildingProject . findById ( req . params . projectId ) ;
275+ const { projectId } = req . params ;
276+ if ( ! mongoose . Types . ObjectId . isValid ( projectId ) ) {
277+ return res . status ( 400 ) . json ( { message : 'Invalid project ID' } ) ;
278+ }
279+ const project = await BuildingProject . findById ( projectId ) ;
272280 if ( ! project ) {
273- logger . logException ( `Project with ID ${ req . params . projectId } not found` ) ;
281+ logger . logException ( `Project with ID ${ projectId } not found` ) ;
274282 return res . status ( 404 ) . json ( { message : 'Project not found' } ) ;
275283 }
276- const materialsCost = await calculateMaterialsCost ( BuildingMaterial , project . _id ) ;
284+ const materialsCost = await calculateMaterialsCost ( project . _id ) ;
277285 const toolsCost = await calculateToolsCost ( project . _id ) ;
278286 const laboorCost = await calculateLaborCost ( project . _id ) ;
279287
0 commit comments