Fix busy for all overlap in free busy modal

Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
This commit is contained in:
Richard Steinmetz 2021-06-08 11:56:30 +02:00
parent a98e42717d
commit da4af8f25f
No known key found for this signature in database
GPG Key ID: 31BA3356F0FA2874
1 changed files with 21 additions and 6 deletions

View File

@ -119,12 +119,15 @@ export default function(organizer, attendees, resources) {
const slotsWithoutOverlap = []
if (slots.length) {
let currentSlotStart = slots[0].start
slots.forEach(() => {
const combined = findNextCombinedSlot(slots, currentSlotStart)
if (combined) {
slotsWithoutOverlap.push(combined)
currentSlotStart = combined.end
slots.forEach(slot => {
const combined = findNextCombinedSlot(slots, currentSlotStart) ?? slot
if (combined.start < currentSlotStart) {
// This slot has already been combined with a former slot
return
}
slotsWithoutOverlap.push(combined)
currentSlotStart = combined.end
})
}
console.debug('deduplicated slots', slots, slotsWithoutOverlap)
@ -149,9 +152,16 @@ export default function(organizer, attendees, resources) {
}
}
/***
* Try to combine slots after the given starting date
*
* @param {{start: Date, end: Date}[]} slots The slots to combine
* @param {Date} start Combine slots after this date
* @returns {undefined|{start: Date, end: Date}} The combined date or undefined if no overlaps were found
*/
function findNextCombinedSlot(slots, start) {
const slot = slots
.filter(slot => slot.start > start)
.filter(slot => slot.start >= start)
.reduce((combined, slot) => {
if (slot.start < combined.start) {
// This slot starts too early
@ -163,6 +173,11 @@ function findNextCombinedSlot(slots, start) {
return combined
}
if (slot.start > combined.end) {
// This slots starts after the the combined one
return combined
}
// The slot is extended
return {
start: combined.start,