@@ -118,16 +118,12 @@ export default class ChangeRequestsService {
118118 await prisma . project . update ( {
119119 where : { projectId : foundCR . wbsElement . project . projectId } ,
120120 data : {
121- budget : newBudget ,
122- wbsElement : {
123- update : {
124- changes : {
125- create : change
126- }
127- }
128- }
121+ budget : newBudget
129122 }
130123 } ) ;
124+
125+ //Make the associated budget change if there was a change
126+ if ( change ) await prisma . change . create ( { data : change } ) ;
131127 } else if ( foundCR . wbsElement . workPackage ) {
132128 // get the project for the work package
133129 const wpProj = await prisma . project . findUnique ( {
@@ -167,26 +163,22 @@ export default class ChangeRequestsService {
167163 update : {
168164 where : { workPackageId : foundCR . wbsElement . workPackage . workPackageId } ,
169165 data : {
170- duration : updatedDuration ,
171- wbsElement : {
172- update : {
173- changes : {
174- create : changes [ 1 ]
175- }
176- }
177- }
178- }
179- }
180- } ,
181- wbsElement : {
182- update : {
183- changes : {
184- create : changes [ 0 ]
166+ duration : updatedDuration
185167 }
186168 }
187169 }
188170 }
189171 } ) ;
172+
173+ //Making associated changes
174+ const changePromises = changes . map ( async ( change ) => {
175+ //Checking if change is not zero so we dont make changes for zero budget or timeline impact
176+ if ( change ) {
177+ await prisma . change . create ( { data : change } ) ;
178+ }
179+ } ) ;
180+
181+ await Promise . all ( changePromises ) ;
190182 }
191183
192184 // finally update the proposed solution
@@ -298,12 +290,6 @@ export default class ChangeRequestsService {
298290 } ) ;
299291 }
300292
301- // send the creator of the cr a slack notification that their cr was reviewed
302- const creatorUserSettings = await prisma . user_Settings . findUnique ( { where : { userId : foundCR . submitterId } } ) ;
303- if ( creatorUserSettings && creatorUserSettings . slackId ) {
304- await sendSlackCRReviewedNotification ( creatorUserSettings . slackId , foundCR . crId ) ;
305- }
306-
307293 // finally we can update change request
308294 const updated = await prisma . change_Request . update ( {
309295 where : { crId } ,
@@ -316,6 +302,18 @@ export default class ChangeRequestsService {
316302 include : { activationChangeRequest : true , wbsElement : { include : { workPackage : true } } }
317303 } ) ;
318304
305+ // send the creator of the cr a slack notification that their cr was reviewed
306+ const creatorUserSettings = await prisma . user_Settings . findUnique ( { where : { userId : foundCR . submitterId } } ) ;
307+ if ( creatorUserSettings && creatorUserSettings . slackId ) {
308+ try {
309+ await sendSlackCRReviewedNotification ( creatorUserSettings . slackId , foundCR . crId ) ;
310+ } catch ( err : unknown ) {
311+ if ( err instanceof Error ) {
312+ throw new HttpException ( 500 , `Failed to send slack notification: ${ err . message } ` ) ;
313+ }
314+ }
315+ }
316+
319317 return updated . crId ;
320318 }
321319
0 commit comments