Repository navigation
Add one shared builder for signed paid booking rows #1829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * Shared booking date and duration rules — the single definition of how a | ||
| * booking row's `date` and `durationDays` are derived from a listing's type | ||
| * and the buyer's chosen span. Both the payment flows (paid webhook, | ||
| * store-and-refund placeholder) and the public availability/free-booking | ||
| * paths route through here so a listing's range policy can never drift between | ||
| * them. | ||
| * | ||
| * This module is pure: callers pass the listing facts and the chosen date/day | ||
| * count, it returns the two fields. No database, no settings. | ||
| */ | ||
|
|
||
| import { capacityDateFor } from "#shared/capacity-rules.ts"; | ||
| import type { ListingWithCount } from "#shared/types.ts"; | ||
| import { normalizeDurationDays } from "#shared/types.ts"; | ||
|
|
||
| /** The listing facts the row builder reads to derive a booking's date and | ||
| * duration. Narrower than the full listing so the helper stays pure over the | ||
| * fields it actually consumes. */ | ||
| export type BookingDateSource = Pick< | ||
| ListingWithCount, | ||
| "listing_type" | "duration_days" | "customisable_days" | ||
| >; | ||
|
|
||
| /** | ||
| * Shared booking-date fields (date + durationDays). | ||
| * | ||
| * Span rules, one declaration: | ||
| * - A `customisable_days` listing spans the chosen `dayCount`. | ||
| * - A non-customisable `daily` listing spans its fixed `duration_days`. | ||
| * - A `standard` listing spans one dateless day (no range column). | ||
| * | ||
| * `dayCount` defaults to `1`, and {@link normalizeDurationDays} clamps | ||
| * non-finite input back to `1`, so a genuinely optional `dayCount` (a legacy | ||
| * signed session without `day_count`, modelled as `undefined` on | ||
| * `BookingIntent`) flows through that default unchanged — missing day count | ||
| * still means one day. `capacityDateFor` drops the date for date-less | ||
| * (standard) listings whose own cap is a running total, never a per-date | ||
| * count. | ||
| */ | ||
| export const bookingDateFields = ( | ||
| listing: BookingDateSource, | ||
| date: string | null, | ||
| dayCount = 1, | ||
| ): { date: string | null; durationDays: number } => ({ | ||
| date: capacityDateFor(listing.listing_type, date), | ||
| durationDays: listing.customisable_days | ||
| ? normalizeDurationDays(dayCount) | ||
| : listing.listing_type === "daily" | ||
| ? normalizeDurationDays(listing.duration_days) | ||
| : 1, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /** | ||
| * The canonical signed paid booking rows — one representation of what a | ||
| * completed, correctly-priced order writes to `listing_attendees`, shared by | ||
| * ordinary payment completion. The later staged-checkout runtime will reach | ||
| * the same rows through the same builder, which is why the builder is pure | ||
| * over its inputs (the only impurity is the single order-token UUID the | ||
| * expansion mints, mirroring the in-memory token main's rows already share). | ||
| * | ||
| * Behaviour kept identical to main's paid webhook path: | ||
| * - one row per signed line, carrying listing, quantity, date, duration, | ||
| * package path, and paid price, in input order; | ||
| * - child allocations expand into one row per (child, parent), preserving | ||
| * total quantity and exact total paid price (the last split row absorbs | ||
| * the rounding residue) and sharing one order token; | ||
| * - a folded child is stamped with its parent's package ONLY when that parent | ||
| * books through exactly one path — mixed and standalone parent paths do | ||
| * not falsely stamp children; | ||
| * - a genuine `pricePaid: 0` (a free line that was still signed) is kept | ||
| * distinct from an omitted price, so the ledger never confuses "free" | ||
| * with "unpriced". | ||
| */ | ||
|
|
||
| import { | ||
| soleParentPackageIds, | ||
| stampChildRowPackages, | ||
| } from "#shared/booking/page-packages.ts"; | ||
| import { | ||
| type BookingDateSource, | ||
| bookingDateFields, | ||
| } from "#shared/booking-date-fields.ts"; | ||
| import type { | ||
| ChildAllocation, | ||
| ListingBooking, | ||
| } from "#shared/db/attendee-types.ts"; | ||
| import { expandChildAllocations } from "#shared/db/attendees/order-parents.ts"; | ||
|
|
||
| /** One signed paid line: the booking slot (listing id + package path), the | ||
| * quantity, the listing's date/duration facts, and the line's charged | ||
| * amount — everything {@link orderBookings} needs to build one canonical | ||
| * {@link ListingBooking} row before child-allocation expansion. | ||
| * | ||
| * The slot's `packageGroupId` is `0` for a standalone line and the package's | ||
| * group id for a line booked through a package — the same `packageGroupId 0` | ||
| * contract the existing writers expect. `pricePaid` carries the distinction | ||
| * the ledger depends on: a genuine `0` stays `0`; an `undefined` line | ||
| * carries no price and is written to no row's `pricePaid`. */ | ||
| export type SignedPaidLine = { | ||
| listingId: number; | ||
| packageGroupId: number; | ||
| quantity: number; | ||
| /** The listing facts that derive the row's `date`/`durationDays`. */ | ||
| listing: BookingDateSource; | ||
| /** The line's charged amount in minor units, or `undefined` when the line | ||
| * carries no price. */ | ||
| pricePaid?: number; | ||
| }; | ||
|
|
||
| /** Input to {@link orderBookings}: the signed paid lines plus the order's | ||
| * shared date, day count, and per-(child, parent) allocations. */ | ||
| export type OrderBookingsInput = { | ||
| lines: SignedPaidLine[]; | ||
| date: string | null; | ||
| /** Visitor-chosen day count for "customisable days" listings. Absent or 1 | ||
| * when no selected listing is customisable — modelled as optional to | ||
| * mirror the genuinely optional `BookingIntent.dayCount` (a legacy session | ||
| * without `day_count` still means one day via {@link bookingDateFields}'s | ||
| * default). Typed `number | undefined` (not bare `?: number`) so an | ||
| * intent's `dayCount` can be passed through directly under | ||
| * `exactOptionalPropertyTypes`. */ | ||
| dayCount?: number | undefined; | ||
| /** Per-(child, parent) allocations from the fold, carried through the signed | ||
| * metadata. Absent or empty for legacy/no-parent orders. Typed | ||
| * `ChildAllocation[] | undefined` so an intent's `allocations` can be | ||
| * passed through directly under `exactOptionalPropertyTypes`. */ | ||
| allocations?: ChildAllocation[] | undefined; | ||
| }; | ||
|
|
||
| /** | ||
| * Build the canonical signed paid booking rows from a validated, priced order. | ||
| * | ||
| * Each line becomes one row carrying its listing, quantity, date, duration, | ||
| * package path, and paid price; child lines expand into per-parent rows when | ||
| * allocations are present; each folded child is stamped with its parent's | ||
| * package only when that parent books through exactly one path (see | ||
| * {@link soleParentPackageIds}). Preserves input order, exact total paid price | ||
| * (the last split row absorbs the rounding residue), the `pricePaid: 0` | ||
| * versus omitted distinction, and one shared order token across every | ||
| * expanded row. | ||
| */ | ||
| export const orderBookings = (input: OrderBookingsInput): ListingBooking[] => { | ||
| const { lines, date, dayCount, allocations } = input; | ||
| const rawBookings: ListingBooking[] = lines.map((line) => ({ | ||
| listingId: line.listingId, | ||
| packageGroupId: line.packageGroupId, | ||
| quantity: line.quantity, | ||
| ...bookingDateFields(line.listing, date, dayCount), | ||
| ...(line.pricePaid !== undefined ? { pricePaid: line.pricePaid } : {}), | ||
| })); | ||
| return stampChildRowPackages( | ||
| allocations && allocations.length > 0 | ||
| ? expandChildAllocations(rawBookings, allocations) | ||
| : rawBookings, | ||
| // soleParentPackageIds reads each line's package path; a SignedPaidLine | ||
| // already carries packageGroupId (0 = standalone, dropped; N = a sole | ||
| // package, kept), so the lines can be passed directly — matching main's | ||
| // per-line package-group derivation. | ||
| soleParentPackageIds(lines), | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.