@@ -248,54 +248,57 @@ const bmFinancialController = function (BuildingProject, BuildingMaterial, Build
248248 const getTotalProjectCost = async ( req , res ) => {
249249 try {
250250 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 ) ;
255- if ( ! project ) {
256- logger . logException ( `Project with ID ${ projectId } not found` ) ;
257- return res . status ( 404 ) . json ( { message : 'Project not found' } ) ;
258- }
251+
252+ const project = await validateAndGetProject ( projectId ) ;
253+
259254 const materialsCost = await calculateMaterialsCost ( project . _id ) ;
260255 const toolsCost = await calculateToolsCost ( project . _id ) ;
261- const laboorCost = await calculateLaborCost ( project . _id ) ;
256+ const laborCost = await calculateLaborCost ( project . _id ) ;
262257
263- const totalCost = materialsCost + toolsCost + laboorCost ;
264- res . status ( 200 ) . json ( {
265- totalCost,
266- } ) ;
258+ const totalCost = materialsCost + toolsCost + laborCost ;
259+
260+ res . status ( 200 ) . json ( { totalCost } ) ;
267261 } catch ( error ) {
268- logger . logException ( `Error fetching project cost: ${ error . message } ` ) ;
269- res . status ( 500 ) . json ( { message : 'Internal server error' } ) ;
262+ logger . logException ( error . message ) ;
263+ res . status ( error . status || 500 ) . json ( { message : error . message } ) ;
270264 }
271265 } ;
272266
273267 const getCostBreakdown = async ( req , res ) => {
274268 try {
275269 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 ) ;
280- if ( ! project ) {
281- logger . logException ( `Project with ID ${ projectId } not found` ) ;
282- return res . status ( 404 ) . json ( { message : 'Project not found' } ) ;
283- }
270+
271+ const project = await validateAndGetProject ( projectId ) ;
272+
284273 const materialsCost = await calculateMaterialsCost ( project . _id ) ;
285274 const toolsCost = await calculateToolsCost ( project . _id ) ;
286- const laboorCost = await calculateLaborCost ( project . _id ) ;
275+ const laborCost = await calculateLaborCost ( project . _id ) ;
287276
288277 res . status ( 200 ) . json ( {
289278 materialsCost,
290- equipmentCost : toolsCost ,
291- laborCost : laboorCost ,
279+ toolsCost,
280+ laborCost,
292281 } ) ;
293282 } catch ( error ) {
294- logger . logException ( `Error fetching project cost breakdown: ${ error . message } ` ) ;
295- res . status ( 500 ) . json ( { message : 'Internal server error' } ) ;
283+ logger . logException ( error . message ) ;
284+ res . status ( error . status || 500 ) . json ( { message : error . message } ) ;
296285 }
297286 } ;
298287
288+ const validateAndGetProject = async ( projectId ) => {
289+ if ( ! mongoose . Types . ObjectId . isValid ( projectId ) ) {
290+ throw { status : 400 , message : 'Invalid project ID' } ;
291+ }
292+
293+ const project = await BuildingProject . findById ( projectId ) ;
294+
295+ if ( ! project ) {
296+ throw { status : 404 , message : 'Project not found' } ;
297+ }
298+
299+ return project ;
300+ } ;
301+
299302 return {
300303 getTotalProjectCost,
301304 getCostBreakdown,
0 commit comments