feat(booking): API réservation + availability + lib métier
Récupéré du workspace Backend (3 fichiers, 406 lignes) : - src/lib/booking.ts : logique métier réservation - src/app/api/bookings/route.ts : POST/GET bookings - src/app/api/carbets/[carbetId]/availability/route.ts : calendrier dispo Le schéma Booking/Availability était déjà dans main.
This commit is contained in:
parent
9f3eda8bb4
commit
0de034022a
3 changed files with 406 additions and 0 deletions
50
src/lib/booking.ts
Normal file
50
src/lib/booking.ts
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import { UserRole } from "@/generated/prisma/enums";
|
||||
|
||||
export const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
export function parseIsoDate(value: unknown): Date | null {
|
||||
if (typeof value !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.getTime())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return date;
|
||||
}
|
||||
|
||||
export function normalizeUtcDayStart(date: Date): Date {
|
||||
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
|
||||
}
|
||||
|
||||
export function hasOverlap(startA: Date, endA: Date, startB: Date, endB: Date): boolean {
|
||||
return startA < endB && endA > startB;
|
||||
}
|
||||
|
||||
export function enumerateUtcDays(start: Date, end: Date): Date[] {
|
||||
const days: Date[] = [];
|
||||
const cursor = normalizeUtcDayStart(start);
|
||||
const limit = normalizeUtcDayStart(end);
|
||||
|
||||
while (cursor < limit) {
|
||||
days.push(new Date(cursor));
|
||||
cursor.setUTCDate(cursor.getUTCDate() + 1);
|
||||
}
|
||||
|
||||
return days;
|
||||
}
|
||||
|
||||
export function isCeUserRole(role?: UserRole): boolean {
|
||||
return role === UserRole.CE_MANAGER || role === UserRole.CE_MEMBER;
|
||||
}
|
||||
|
||||
export function isWeekendUtcDay(date: Date): boolean {
|
||||
const day = date.getUTCDay();
|
||||
return day === 0 || day === 6;
|
||||
}
|
||||
|
||||
export function isPublicAllowedByDefaultPolicy(day: Date): boolean {
|
||||
return !isWeekendUtcDay(day);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue