Repository navigation
Import/export a listing or group as JSON, with cross-entity name uniqueness #1511
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 22 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
2cd63c4
Enforce cross-entity name uniqueness for listings and groups
claude e461f3a
Add catalog transfer engine: export/import a listing or group as JSON
claude 6fc6cbd
Wire catalog import/export into the admin UI and routes
claude 40cb032
Mutation-harden the catalog-transfer modules
claude 0e219bd
Cover the new export/import UI links and the entity-pages refactor
claude b9a8962
Move import-only membership writers out of shared db files
claude 20881f1
Lock editor access to catalog import/export
claude 7799f3c
Close pre-existing mutation gaps exposed by the name-uniqueness change
claude b1b1b37
Address Codex review and close catalog-transfer coverage gaps
claude ef82482
Fix CI: alias-rule violation and duplicate-name test fixtures
claude a04e8af
Harden catalog import/export per second Codex review
claude 449223f
Batch parent-edge import; enforce name uniqueness in bulk group dupli…
claude 434c693
Unify listing field-value validation across form, API, and import
claude e349b9b
Scope import field validation to the schema; fix datetime regex anchor
claude 853da9e
Import: clear non-package overrides, reject hidden-package parents, d…
claude c7f359f
Batch group-compat validation so many-group imports stay under the N+…
claude c062e72
Fix many-group import test; reject bad datetime offsets; batch packag…
claude 6c94583
Reject fractional datetimes without a seconds component on import
claude 37e0c2a
Reject unsafe-integer prices in catalog imports
claude 7e91714
Reject package day-price overrides for spans a member doesn't offer o…
claude d73f2de
Format catalog-transfer-packages test (biome)
claude a60d936
Run child add-on reachability check on imported parent edges
claude 475e1c9
Make export tolerant of unexportable rows; filter over-duration day p…
claude 823e698
Cover the default-duration branch of the day-price filter
claude 159139c
Dedupe the day-price import tests via a shared helper
claude 0d2c463
Merge remote-tracking branch 'origin/main' into claude/listing-group-…
claude 6a5c6dd
Carry bookable_alone through catalog transfers
claude ccba2f5
Drop the now-dead group-siblings fallback in listing validation
claude b985942
Exempt bookable-alone children from the import add-on reachability check
claude 6b91c00
Merge remote-tracking branch 'origin/main' into claude/listing-group-…
claude 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| /** | ||
| * Build the id-free JSON export for one listing or group (see schema.ts). | ||
| * | ||
| * The exporter reads the decrypted stored row and its related facets — group | ||
| * memberships (with package overrides), parent listings, and per-day package | ||
| * overrides — and renders every cross-reference by name. Prices come straight | ||
| * off the listing columns (`unit_price`/`day_prices`); the derived | ||
| * `listing_prices` mirror rows are re-synced from those on import, so they are | ||
| * not exported separately. | ||
| */ | ||
|
|
||
| import * as v from "valibot"; | ||
| import { mapNotNullish } from "#fp"; | ||
| import { | ||
| getAllGroupNames, | ||
| getGroupPackagePrices, | ||
| groupsTable, | ||
| } from "#shared/db/groups.ts"; | ||
| import { getParentIds } from "#shared/db/listing-parents.ts"; | ||
| import { | ||
| getGroupDayPrices, | ||
| getGroupDayPricesByGroupIds, | ||
| } from "#shared/db/listing-prices.ts"; | ||
| import { | ||
| getListingNamesByIds, | ||
| getStoredListingWithCount, | ||
| listingsTable, | ||
| } from "#shared/db/listings.ts"; | ||
| import type { AdminLevel } from "#shared/types.ts"; | ||
| import { getListingGroupMemberships } from "./membership.ts"; | ||
| import { | ||
| CATALOG_TRANSFER_VERSION, | ||
| GroupDataSchema, | ||
| type GroupMember, | ||
| type GroupTransfer, | ||
| ListingDataSchema, | ||
| type ListingMembership, | ||
| type ListingTransfer, | ||
| } from "./schema.ts"; | ||
|
|
||
| /** Listing columns that never travel: the id/slug/timestamp columns (an import | ||
| * mints fresh ones) and the image/attachment columns (deliberately out of | ||
| * scope). Named in snake_case for {@link listingsTable.rowToInput}. */ | ||
| const LISTING_EXPORT_EXCLUDED = [ | ||
| "created", | ||
| "slug", | ||
| "slug_index", | ||
| "image_url", | ||
| "attachment_url", | ||
| "attachment_name", | ||
| ] as const; | ||
|
|
||
| /** `webhook_url` receives attendee PII, so — like the edit form — it is hidden | ||
| * from an editor; an editor's export must not reveal a URL they can't read. */ | ||
| const EDITOR_EXPORT_EXCLUDED = [ | ||
| ...LISTING_EXPORT_EXCLUDED, | ||
| "webhook_url", | ||
| ] as const; | ||
|
|
||
| /** Group columns that never travel — the slug pair (regenerated on import). */ | ||
| const GROUP_EXPORT_EXCLUDED = ["slug", "slug_index"] as const; | ||
|
|
||
| /** Convert a per-day override map to the JSON record shape, or undefined when | ||
| * there are no overrides (so an empty map is omitted from the blob). */ | ||
| const dayPricesToRecord = ( | ||
| dayPrices: ReadonlyMap<number, number> | undefined, | ||
| ): Record<string, number> | undefined => { | ||
| if (!dayPrices || dayPrices.size === 0) return undefined; | ||
| const record: Record<string, number> = {}; | ||
| for (const [day, price] of dayPrices) record[String(day)] = price; | ||
| return record; | ||
| }; | ||
|
|
||
| /** The package-override fields shared by both membership views, each omitted at | ||
| * its neutral default (no price override, quantity 1, no per-day overrides) so a | ||
| * plain membership serialises to just its name reference. */ | ||
| const overrideFields = ( | ||
| packagePrice: number | null, | ||
| quantity: number, | ||
| dayPrices: ReadonlyMap<number, number> | undefined, | ||
| ): { | ||
| packagePrice?: number; | ||
| quantity?: number; | ||
| dayPrices?: Record<string, number>; | ||
| } => { | ||
| const record = dayPricesToRecord(dayPrices); | ||
| return { | ||
| ...(packagePrice === null ? {} : { packagePrice }), | ||
| ...(quantity === 1 ? {} : { quantity }), | ||
| ...(record ? { dayPrices: record } : {}), | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Build the JSON export for the listing with `id`, or null when it does not | ||
| * exist. Reads the *stored* row (no operator defaults overlaid) so a re-import | ||
| * preserves the listing's own columns. | ||
| */ | ||
| export const exportListing = async ( | ||
| id: number, | ||
| adminLevel?: AdminLevel, | ||
| ): Promise<ListingTransfer | null> => { | ||
| const listing = await getStoredListingWithCount(id); | ||
| if (!listing) return null; | ||
|
|
||
| const [memberships, groupNames, parentIds] = await Promise.all([ | ||
| getListingGroupMemberships(id), | ||
| getAllGroupNames(), | ||
| getParentIds(id), | ||
| ]); | ||
| const groupDayPrices = await getGroupDayPricesByGroupIds( | ||
| memberships.map((m) => m.group_id), | ||
| ); | ||
|
|
||
| // Every membership row references an existing group (FK), and `groupNames` | ||
| // covers all groups, so the name lookup always resolves. | ||
| const groups: ListingMembership[] = memberships.map((m) => ({ | ||
| group: groupNames.get(m.group_id)!, | ||
| ...overrideFields( | ||
| m.package_price, | ||
| m.quantity, | ||
| groupDayPrices.get(m.group_id)?.get(id), | ||
| ), | ||
| })); | ||
|
|
||
| const parentNames = await getListingNamesByIds(parentIds); | ||
| const parents = mapNotNullish((parentId: number) => | ||
| parentNames.get(parentId), | ||
| )(parentIds); | ||
|
|
||
| return { | ||
| groups, | ||
| kind: "listing", | ||
| listing: v.parse( | ||
| ListingDataSchema, | ||
|
stefan-burke marked this conversation as resolved.
Resolved
|
||
| listingsTable.rowToInput( | ||
| listing, | ||
| adminLevel === "editor" | ||
| ? EDITOR_EXPORT_EXCLUDED | ||
| : LISTING_EXPORT_EXCLUDED, | ||
| ), | ||
| ), | ||
| parents, | ||
| version: CATALOG_TRANSFER_VERSION, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Build the JSON export for the group with `id`, or null when it does not | ||
| * exist. Includes every member listing (by name) with its package override, | ||
| * quantity, and per-day overrides. | ||
| */ | ||
| export const exportGroup = async ( | ||
| id: number, | ||
| ): Promise<GroupTransfer | null> => { | ||
| const group = await groupsTable.findById(id); | ||
| if (!group) return null; | ||
|
|
||
| const rows = await getGroupPackagePrices(id); | ||
| const [listingNames, dayPrices] = await Promise.all([ | ||
| getListingNamesByIds(rows.map((r) => r.listing_id)), | ||
| getGroupDayPrices(id), | ||
| ]); | ||
|
|
||
| // Every package row references an existing listing (FK), and `listingNames` | ||
| // covers exactly those ids, so the name lookup always resolves. | ||
| const members: GroupMember[] = rows.map((row) => ({ | ||
| listing: listingNames.get(row.listing_id)!, | ||
| ...overrideFields( | ||
| row.package_price, | ||
| row.quantity, | ||
| dayPrices.get(row.listing_id), | ||
| ), | ||
| })); | ||
|
|
||
| return { | ||
| group: v.parse( | ||
| GroupDataSchema, | ||
| groupsTable.rowToInput(group, GROUP_EXPORT_EXCLUDED), | ||
| ), | ||
| kind: "group", | ||
| members, | ||
| version: CATALOG_TRANSFER_VERSION, | ||
| }; | ||
| }; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When exporting a listing that is itself a parent, the blob only records the listings this row is offered under, not the required children it offers. Re-importing that parent therefore creates a standalone listing with no child selector, so buyers can book the parent alone and the add-on structure is lost; include child references in the transfer or reject/export-warn for parent listings.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is by design. The transfer format records a listing's parents (what it's offered under), not the children it offers, because children are themselves separate listings — the format is single-entity, and every cross-reference (parents, group memberships, group members) is a by-name reference to an entity that already exists, never an embedded subtree. The add-on structure is preserved by exporting/importing each child, which references this parent by name on its own import (parents must already exist, which is exactly why the import resolves parent references rather than creating them). Exporting a lone parent yielding a standalone listing until its children are imported is the intended boundary, consistent with how memberships and parents are all handled.
Happy to revisit if you'd rather the format embed child references, but that's a format-scope change I'd want your call on rather than make unilaterally.
Generated by Claude Code