Initial commit: ANOUMA website with Payload CMS, WebRTC meetings, and booking system
- Next.js 16 App Router site with the ANOUMA design system - Payload CMS (PostgreSQL) for offers, events, posts and page content - WebRTC video-call system with custom signaling server - SMTP email reminders and booking-request notifications - Customer accounts, calendar-based availability, and booking workflow
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import type { PayloadRequest } from "payload";
|
||||
|
||||
/**
|
||||
* Acquires a Postgres transaction-scoped advisory lock so that two
|
||||
* concurrent requests confirming a booking for the same day can't both pass
|
||||
* the "is this slot free?" check before either has committed (Payload's
|
||||
* default transaction isolation is READ COMMITTED, which alone would allow
|
||||
* that race). The lock is automatically released when the transaction ends.
|
||||
*
|
||||
* Must be called from inside a Payload hook/operation that already has an
|
||||
* active `req.transactionID` (true for collection create/update by default).
|
||||
*/
|
||||
export async function acquireBookingDayLock(req: PayloadRequest, dateKey: string): Promise<void> {
|
||||
const txID = req.transactionID ? String(await req.transactionID) : undefined;
|
||||
if (!txID) return; // No active transaction — nothing to lock against.
|
||||
|
||||
const adapter = req.payload.db as unknown as {
|
||||
sessions?: Record<string, { db: { execute: (query: unknown) => Promise<unknown> } }>;
|
||||
drizzle: { execute: (query: unknown) => Promise<unknown> };
|
||||
};
|
||||
const tx = adapter.sessions?.[txID]?.db ?? adapter.drizzle;
|
||||
|
||||
await tx.execute(sql`SELECT pg_advisory_xact_lock(hashtext(${dateKey}))`);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { cache } from "react";
|
||||
import { getCMS } from "@/lib/payload/getPayload";
|
||||
import type { BookingRequest } from "@/payload-types";
|
||||
|
||||
/**
|
||||
* These are called only after the caller has already verified who the
|
||||
* current customer is (see lib/auth/customer.ts) — the `where: { user }`
|
||||
* filter below is what actually scopes results to that one customer.
|
||||
*/
|
||||
export const getCustomerBookings = cache(async (customerId: number): Promise<BookingRequest[]> => {
|
||||
const payload = await getCMS();
|
||||
const result = await payload.find({
|
||||
collection: "booking-requests",
|
||||
where: { user: { equals: customerId } },
|
||||
sort: "-date",
|
||||
depth: 2,
|
||||
limit: 200,
|
||||
});
|
||||
return result.docs;
|
||||
});
|
||||
|
||||
export async function getCustomerBookingById(customerId: number, id: number): Promise<BookingRequest | null> {
|
||||
const payload = await getCMS();
|
||||
const result = await payload.find({
|
||||
collection: "booking-requests",
|
||||
where: { and: [{ id: { equals: id } }, { user: { equals: customerId } }] },
|
||||
depth: 2,
|
||||
limit: 1,
|
||||
});
|
||||
return result.docs[0] ?? null;
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import type { Payload } from "payload";
|
||||
import { WEEKDAYS } from "@/collections/Availability";
|
||||
|
||||
export type TimeSlot = { start: Date; end: Date };
|
||||
|
||||
const WEEKDAY_BY_INDEX = [
|
||||
"sunday",
|
||||
"monday",
|
||||
"tuesday",
|
||||
"wednesday",
|
||||
"thursday",
|
||||
"friday",
|
||||
"saturday",
|
||||
] as const;
|
||||
|
||||
function dateKey(date: Date): string {
|
||||
return date.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function timeOfDayMinutes(date: Date): number {
|
||||
return date.getHours() * 60 + date.getMinutes();
|
||||
}
|
||||
|
||||
function atMinutesOfDay(day: Date, minutes: number): Date {
|
||||
const d = new Date(day);
|
||||
d.setHours(0, Math.round(minutes), 0, 0);
|
||||
return d;
|
||||
}
|
||||
|
||||
function overlaps(aStart: Date, aEnd: Date, bStart: Date, bEnd: Date): boolean {
|
||||
return aStart < bEnd && bStart < aEnd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes bookable slots for one offer across a date range, from the
|
||||
* configured weekly Availability (plus per-date AvailabilityOverrides),
|
||||
* minus anything already pending/confirmed on Anna's schedule — she can
|
||||
* only do one thing at a time, regardless of which offer it's for.
|
||||
*/
|
||||
export async function getAvailableSlots(
|
||||
payload: Payload,
|
||||
args: { durationMinutes: number; from: Date; to: Date },
|
||||
): Promise<Map<string, TimeSlot[]>> {
|
||||
const { durationMinutes, from, to } = args;
|
||||
const result = new Map<string, TimeSlot[]>();
|
||||
|
||||
const [{ docs: weekly }, { docs: overrides }, { docs: busyBookings }] = await Promise.all([
|
||||
payload.find({ collection: "availability", where: { active: { equals: true } }, limit: 100 }),
|
||||
payload.find({
|
||||
collection: "availability-overrides",
|
||||
where: { and: [{ date: { greater_than_equal: from.toISOString() } }, { date: { less_than_equal: to.toISOString() } }] },
|
||||
limit: 200,
|
||||
}),
|
||||
payload.find({
|
||||
collection: "booking-requests",
|
||||
where: {
|
||||
and: [
|
||||
{ status: { in: ["pending", "confirmed"] } },
|
||||
{ date: { greater_than_equal: from.toISOString() } },
|
||||
{ date: { less_than_equal: to.toISOString() } },
|
||||
],
|
||||
},
|
||||
limit: 500,
|
||||
}),
|
||||
]);
|
||||
|
||||
const overridesByDate = new Map(overrides.map((o) => [dateKey(new Date(o.date)), o]));
|
||||
const now = new Date();
|
||||
|
||||
for (let day = new Date(from); day <= to; day = new Date(day.getTime() + 24 * 60 * 60 * 1000)) {
|
||||
const key = dateKey(day);
|
||||
const override = overridesByDate.get(key);
|
||||
|
||||
let windows: { startMinutes: number; endMinutes: number }[] = [];
|
||||
|
||||
if (override?.type === "unavailable") {
|
||||
windows = [];
|
||||
} else if (override?.type === "custom-hours" && override.startTime && override.endTime) {
|
||||
windows = [
|
||||
{
|
||||
startMinutes: timeOfDayMinutes(new Date(override.startTime)),
|
||||
endMinutes: timeOfDayMinutes(new Date(override.endTime)),
|
||||
},
|
||||
];
|
||||
} else {
|
||||
const weekdayName = WEEKDAY_BY_INDEX[day.getDay()];
|
||||
windows = weekly
|
||||
.filter((w) => w.weekday === weekdayName)
|
||||
.map((w) => ({
|
||||
startMinutes: timeOfDayMinutes(new Date(w.startTime)),
|
||||
endMinutes: timeOfDayMinutes(new Date(w.endTime)),
|
||||
}));
|
||||
}
|
||||
|
||||
const dayBusy = busyBookings
|
||||
.filter((b) => dateKey(new Date(b.date)) === key)
|
||||
.map((b) => ({ start: new Date(b.startTime), end: new Date(b.endTime) }));
|
||||
|
||||
const slots: TimeSlot[] = [];
|
||||
for (const window of windows) {
|
||||
for (let start = window.startMinutes; start + durationMinutes <= window.endMinutes; start += durationMinutes) {
|
||||
const slotStart = atMinutesOfDay(day, start);
|
||||
const slotEnd = atMinutesOfDay(day, start + durationMinutes);
|
||||
if (slotStart < now) continue;
|
||||
if (dayBusy.some((b) => overlaps(slotStart, slotEnd, b.start, b.end))) continue;
|
||||
slots.push({ start: slotStart, end: slotEnd });
|
||||
}
|
||||
}
|
||||
|
||||
if (slots.length > 0) result.set(key, slots);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export { WEEKDAYS };
|
||||
Reference in New Issue
Block a user