@@ -176,15 +176,45 @@ async function updateWorkPeriods (payload) {
176176 const workPeriodsToRemove = _ . differenceBy ( workPeriods , newWorkPeriods , 'startDate' )
177177 // find which workperiods should be created
178178 const workPeriodsToAdd = _ . differenceBy ( newWorkPeriods , workPeriods , 'startDate' )
179- // find which workperiods' daysWorked propery should be updated
180- let workPeriodsToUpdate = _ . intersectionBy ( newWorkPeriods , workPeriods , 'startDate' )
181- // find which workperiods' daysWorked property is preset and exceeds the possible maximum
182- workPeriodsToUpdate = _ . differenceWith ( workPeriodsToUpdate , workPeriods , ( a , b ) => b . startDate === a . startDate && _ . defaultTo ( b . daysWorked , a . daysWorked ) <= a . daysWorked )
183- // include id
184- workPeriodsToUpdate = _ . map ( workPeriodsToUpdate , wpu => {
185- wpu . id = _ . filter ( workPeriods , [ 'startDate' , wpu . startDate ] ) [ 0 ] . id
186- return wpu
187- } )
179+ // find which workperiods' daysWorked property should be evaluated for changes
180+ const IntersectedWorkPeriods = _ . intersectionBy ( newWorkPeriods , workPeriods , 'startDate' )
181+ let workPeriodsToUpdate = [ ]
182+ if ( IntersectedWorkPeriods . length > 0 ) {
183+ // We only need check for first and last ones of intersected workPeriods
184+ // The ones at the middle won't be updated and their daysWorked value will stay the same
185+ if ( payload . options . oldValue . startDate !== payload . value . startDate ) {
186+ const firstWeek = _ . minBy ( IntersectedWorkPeriods , 'startDate' )
187+ const originalFirstWeek = _ . find ( workPeriods , [ 'startDate' , firstWeek . startDate ] )
188+ // recalculate daysWorked for the first week of existent workPeriods
189+ if ( firstWeek . startDate === _ . minBy ( workPeriods , 'startDate' ) . startDate ) {
190+ workPeriodsToUpdate . push ( _ . assign ( firstWeek , { id : originalFirstWeek . id } ) )
191+ // if first of intersected workPeriods is not the first one of existent workPeriods
192+ // we only check if it's daysWorked exceeds the possible maximum
193+ } else if ( originalFirstWeek . daysWorked > firstWeek . daysWorked ) {
194+ workPeriodsToUpdate . push ( _ . assign ( firstWeek , { id : originalFirstWeek . id } ) )
195+ }
196+ }
197+ if ( payload . options . oldValue . endDate !== payload . value . endDate ) {
198+ const lastWeek = _ . maxBy ( IntersectedWorkPeriods , 'startDate' )
199+ const originalLastWeek = _ . find ( workPeriods , [ 'startDate' , lastWeek . startDate ] )
200+ // recalculate daysWorked for the last week of existent workPeriods
201+ if ( lastWeek . startDate === _ . maxBy ( workPeriods , 'startDate' ) . startDate ) {
202+ workPeriodsToUpdate . push ( _ . assign ( lastWeek , { id : originalLastWeek . id } ) )
203+ // if last of intersected workPeriods is not the last one of existent workPeriods
204+ // we only check if it's daysWorked exceeds the possible maximum
205+ } else if ( originalLastWeek . daysWorked > lastWeek . daysWorked ) {
206+ workPeriodsToUpdate . push ( _ . assign ( lastWeek , { id : originalLastWeek . id } ) )
207+ }
208+ }
209+ }
210+ // if intersected WP count is 1, this can result to duplicated WorkPeriods.
211+ // We should choose the one with higher daysWorked because, it's more likely
212+ // the WP we applied "first/last one of existent WPs" logic above.
213+ if ( workPeriodsToUpdate . length === 2 ) {
214+ if ( workPeriodsToUpdate [ 0 ] . startDate === workPeriodsToUpdate [ 1 ] . startDate ) {
215+ workPeriodsToUpdate = [ _ . maxBy ( workPeriodsToUpdate , 'daysWorked' ) ]
216+ }
217+ }
188218 if ( workPeriodsToRemove . length === 0 && workPeriodsToAdd . length === 0 && workPeriodsToUpdate . length === 0 ) {
189219 logger . debug ( {
190220 component : 'ResourceBookingEventHandler' ,
@@ -256,14 +286,16 @@ async function deleteWorkPeriods (payload) {
256286 * @returns {undefined }
257287 */
258288async function _createWorkPeriods ( periods , resourceBookingId ) {
259- await Promise . all ( _ . map ( periods , async period => await WorkPeriodService . createWorkPeriod ( helper . getAuditM2Muser ( ) ,
260- {
261- resourceBookingId : resourceBookingId ,
262- startDate : period . startDate ,
263- endDate : period . endDate ,
264- daysWorked : null ,
265- paymentStatus : 'pending'
266- } ) ) )
289+ for ( const period of periods ) {
290+ await WorkPeriodService . createWorkPeriod ( helper . getAuditM2Muser ( ) ,
291+ {
292+ resourceBookingId : resourceBookingId ,
293+ startDate : period . startDate ,
294+ endDate : period . endDate ,
295+ daysWorked : period . daysWorked ,
296+ paymentStatus : 'pending'
297+ } )
298+ }
267299}
268300
269301/**
@@ -272,11 +304,13 @@ async function _createWorkPeriods (periods, resourceBookingId) {
272304 * @returns {undefined }
273305 */
274306async function _updateWorkPeriods ( periods ) {
275- await Promise . all ( _ . map ( periods , async period => await WorkPeriodService . partiallyUpdateWorkPeriod ( helper . getAuditM2Muser ( ) ,
276- period . id ,
277- {
278- daysWorked : period . daysWorked
279- } ) ) )
307+ for ( const period of periods ) {
308+ await WorkPeriodService . partiallyUpdateWorkPeriod ( helper . getAuditM2Muser ( ) ,
309+ period . id ,
310+ {
311+ daysWorked : period . daysWorked
312+ } )
313+ }
280314}
281315
282316/**
@@ -285,8 +319,9 @@ async function _updateWorkPeriods (periods) {
285319 * @returns {undefined }
286320 */
287321async function _deleteWorkPeriods ( workPeriods ) {
288- await Promise . all ( _ . map ( workPeriods ,
289- async workPeriod => await WorkPeriodService . deleteWorkPeriod ( helper . getAuditM2Muser ( ) , workPeriod . id ) ) )
322+ for ( const period of workPeriods ) {
323+ await WorkPeriodService . deleteWorkPeriod ( helper . getAuditM2Muser ( ) , period . id )
324+ }
290325}
291326
292327/**
0 commit comments