From 25eae4a5464e568aa655a617fc86f66acca9fb4b Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Wed, 18 Jan 2023 19:26:07 +0000 Subject: [PATCH] find indexes instead of just one (#6547) --- packages/lib/slots.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/lib/slots.ts b/packages/lib/slots.ts index c9344acd3b..81a00b43fb 100644 --- a/packages/lib/slots.ts +++ b/packages/lib/slots.ts @@ -118,6 +118,12 @@ function buildSlots({ return slots; } +function fromIndex(cb: (val: T, i: number, a: T[]) => boolean, index: number) { + return function (e: T, i: number, a: T[]) { + return i >= index && cb(e, i, a); + }; +} + const getSlots = ({ inviteeDate, frequency, @@ -203,14 +209,25 @@ const getSlots = ({ startTime: override.start.getUTCHours() * 60 + override.start.getUTCMinutes(), endTime: override.end.getUTCHours() * 60 + override.end.getUTCMinutes(), })); + // unset all working hours that relate to this user availability override overrides.forEach((override) => { - const index = computedLocalAvailability.findIndex( - (a) => !a.userIds?.length || (override.userIds[0] && a.userIds?.includes(override.userIds[0])) - ); - if (index >= 0) { - computedLocalAvailability[index] = override; + let i = -1; + const indexes: number[] = []; + while ( + (i = computedLocalAvailability.findIndex( + fromIndex( + (a) => !a.userIds?.length || (!!override.userIds[0] && a.userIds?.includes(override.userIds[0])), + i + 1 + ) + )) != -1 + ) { + indexes.push(i); } + // work backwards as splice modifies the original array. + indexes.reverse().forEach((idx) => computedLocalAvailability.splice(idx, 1)); }); + // and push all overrides as new computed availability + computedLocalAvailability.push(...overrides); } return buildSlots({