Merge branch 'main' into feature/booking-filters

This commit is contained in:
Peer Richelsen 2022-12-20 14:17:07 +01:00 committed by GitHub
commit 53089686c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 14 deletions

View File

@ -196,21 +196,32 @@ export async function getUserAvailability(
// Take PER_DAY and turn it into day and PER_WEEK into week etc. // Take PER_DAY and turn it into day and PER_WEEK into week etc.
const filter = limitKey.split("_")[1].toLocaleLowerCase() as "day" | "week" | "month" | "year"; const filter = limitKey.split("_")[1].toLocaleLowerCase() as "day" | "week" | "month" | "year";
let total = 0; // loop through all dates and check if we have reached the limit
for (const date of dates) {
// Get all bookings that are within the filter period let total = 0;
ourBookings.forEach((booking) => { const startDate = dayjs(date).startOf(filter);
const startDate = dayjs(booking.start).startOf(filter);
// this is parsed above with parseBookingLimit so we know it's safe. // this is parsed above with parseBookingLimit so we know it's safe.
const endDate = dayjs(startDate).endOf(filter); const endDate = dayjs(date).endOf(filter);
const bookingEventTypeId = booking.source?.split("-")[1]; for (const booking of ourBookings) {
if (dayjs(booking.start).isBetween(startDate, endDate)) total++; const bookingEventTypeId = booking.source?.split("-")[1];
// Only check OUR booking that matches the current eventTypeId if (
// we don't care about another event type in this case as we dont need to know their booking limits // Only check OUR booking that matches the current eventTypeId
if (total >= limit && bookingEventTypeId === eventType?.id?.toString()) { // we don't care about another event type in this case as we dont need to know their booking limits
bufferedBusyTimes.push({ start: startDate.toISOString(), end: endDate.toISOString() }); !(bookingEventTypeId == eventType?.id && dayjs(booking.start).isBetween(startDate, endDate))
) {
continue;
}
// increment total and check against the limit, adding a busy time if condition is met.
total++;
if (total >= limit) {
bufferedBusyTimes.push({
start: startDate.toISOString(),
end: endDate.toISOString(),
});
break;
}
} }
}); }
} }
} }

View File

@ -62,7 +62,7 @@ export async function checkLimit({
{ {
id: eventId, id: eventId,
bookings: { bookings: {
some: { every: {
startTime: { startTime: {
gte: startDate, gte: startDate,
}, },