Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion src/lib/actions/booking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,46 @@ export const createBooking = async (
message: 'Unknown error occurred. Please contact admin.',
};
}

const MSPERDAY = 1000 * 60 * 60 * 24;
const MIN_BOOKING_ADVANCE_DAYS = 7;
const MIN_BOOKING_ADVANCE_DAYS_PANTRY = 1;
if (token.isAdmin === false) {
const venue = await prisma.venue.findUnique({
where: {
id: data.venueId
},
select: {
name: true
},
}
);
const isPantry = venue?.name.toLowerCase().includes('pantry');
const minDaysRequired = isPantry ? MIN_BOOKING_ADVANCE_DAYS_PANTRY : MIN_BOOKING_ADVANCE_DAYS;
const now = new Date();
const bookingDate = new Date(data.startTime);
// ensure same timezone for rare cases of bookings
const utcNow = Date.UTC(
now.getFullYear(),
now.getMonth(),
now.getDate(),
);
const utcBookingDate = Date.UTC(
bookingDate.getFullYear(),
bookingDate.getMonth(),
bookingDate.getDate(),
);
const daysUntilBooking = Math.ceil(
(utcBookingDate - utcNow) / (MSPERDAY),
);
// if pantries from saga; elm; cendy, bookings can be made up to 24 hours prior
if (daysUntilBooking < minDaysRequired) {
const advanceMessage = isPantry ? 'Pantry bookings must be made at least 24 hours in advance.' : 'Bookings must be made at least one week in advance.';
return {
success: false,
message: advanceMessage,
};
}
}
// Ensure user has the permissions for the corresponding organisation
if (!hasOrgPerms(token, data.organisationId)) {
return {
Expand Down Expand Up @@ -162,6 +201,50 @@ export const editBooking = async (
};
}

const MSPERDAY = 1000 * 60 * 60 * 24;
const MIN_BOOKING_ADVANCE_DAYS = 7;
const MIN_BOOKING_ADVANCE_DAYS_PANTRY = 1;

if (token.isAdmin === false) {
const venue = await prisma.venue.findUnique({
where: { id: data.venueId },
select: { name: true },
});

const isPantry = venue?.name.toLowerCase().includes('pantry');
const minDaysRequired = isPantry
? MIN_BOOKING_ADVANCE_DAYS_PANTRY
: MIN_BOOKING_ADVANCE_DAYS;

const now = new Date();
const bookingDate = new Date(data.startTime);

// Ensure same timezone
const utcNow = Date.UTC(
now.getFullYear(),
now.getMonth(),
now.getDate(),
);
const utcBookingDate = Date.UTC(
bookingDate.getFullYear(),
bookingDate.getMonth(),
bookingDate.getDate(),
);

const daysUntilBooking = Math.ceil(
(utcBookingDate - utcNow) / MSPERDAY,
);

if (daysUntilBooking < minDaysRequired) {
return {
success: false,
message: isPantry
? 'Pantry bookings must be made at least 24 hours in advance.'
: 'Bookings must be made at least one week in advance.',
};
}
}

const overlapping = await prisma.booking.findFirst({
where: {
venueId: data.venueId,
Expand Down