Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion src/features/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ const handleCheckAvailability = withActiveEvent(async (request, event) => {
const quantity = Math.max(1, Number.isNaN(parsed) ? 1 : parsed);
const date = url.searchParams.get("date") || undefined;
return apiResponse({
available: await hasAvailableSpots(event.id, quantity, date),
available: await hasAvailableSpots(
event.id,
quantity,
date,
event.duration_days,
),
});
});

Expand Down
13 changes: 10 additions & 3 deletions src/features/api/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import {
capacityErrorFormatter,
isRegistrationClosed,
} from "#routes/format.ts";
import { ensureAllBookings } from "#routes/public/ticket-payment.ts";
import {
bookingDateFields,
ensureAllBookings,
} from "#routes/public/ticket-payment.ts";
import { getFromEmailIfConfigured } from "#routes/public/ticket-routes.ts";
import {
htmlResponse,
Expand Down Expand Up @@ -502,17 +505,21 @@ type CreatedAttendee = Extract<

type CreatedEntry = { attendee: CreatedAttendee; event: EventWithCount };

/** Create the attendee plus per-event bookings atomically. */
/**
* Create the attendee plus per-event bookings atomically.
* durationDays is event-scoped and re-read here at finalize time so the
* stored range always matches the event's current duration policy.
*/
const createAttendeeForSession = async (
session: ValidatedPaymentSession,
intent: BookingIntent,
validatedItems: ValidatedItem[],
): Promise<{ ok: true; entries: CreatedEntry[] } | PaymentResult> => {
const bookings = validatedItems.map(({ item, event }) => ({
date: event.event_type === "daily" ? intent.date : null,
eventId: item.e,
pricePaid: item.p,
quantity: item.q,
...bookingDateFields(event, intent.date),
}));

const result = await createAttendeeAtomic({
Expand Down
22 changes: 20 additions & 2 deletions src/features/public/ticket-payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ export const checkAvailability = (
date,
);

/**
* Shared booking-date fields (date + durationDays). Keeps the payment and
* webhook flows aligned: both read duration from the event at insert time.
*/
export const bookingDateFields = (
event: Pick<TicketEvent["event"], "event_type" | "duration_days">,
date: string | null,
): { date: string | null; durationDays: number } => ({
date: event.event_type === "daily" ? date : null,
durationDays:
event.event_type === "daily" ? Math.max(1, event.duration_days) : 1,
});

/** Build registration items from events and quantities */
export const buildRegistrationItems = (
events: TicketEvent[],
Expand Down Expand Up @@ -171,11 +184,16 @@ export const handlePaymentFlow = (
const buildBookings = (
selected: EventQty[],
date: string | null,
): { eventId: number; quantity: number; date: string | null }[] =>
): {
eventId: number;
quantity: number;
date: string | null;
durationDays: number;
}[] =>
selected.map(({ event, qty }) => ({
date: event.event_type === "daily" ? date : null,
eventId: event.id,
quantity: qty,
...bookingDateFields(event, date),
}));

/**
Expand Down
16 changes: 14 additions & 2 deletions src/shared/booking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ export const processBooking = async (
(customUnitPrice !== undefined && customUnitPrice > 0 && paymentsEnabled);

if (needsPayment) {
const available = await hasAvailableSpots(event.id, quantity, date);
const available = await hasAvailableSpots(
event.id,
quantity,
date,
event.duration_days,
);
if (!available) return { type: "sold_out" };

// Provider is guaranteed to exist when isPaymentsEnabled() is true
Expand Down Expand Up @@ -84,7 +89,14 @@ export const processBooking = async (
// Free event — create attendee atomically
const result = await createAttendeeAtomic({
...contact,
bookings: [{ date, eventId: event.id, quantity }],
bookings: [
{
date,
durationDays: event.duration_days,
eventId: event.id,
quantity,
},
],
});

if (!result.success) {
Expand Down
Loading
Loading