import { idSchema } from '../common/primitives.schema'; import { placeCategorySchema, placeImageUrlSchema, placeRatingToolFieldSchema, placeRatingVoteSchema, placeWebsiteSchema, } from '../place/place.schema'; import { tagSchema } from '../tag/tag.schema'; import { z } from 'zod'; /** * A saved place's status, in the order the one-tap cycle walks. 'revisit' (#2567) * is a place already visited that the user wants to go back to; it sits last so * the stored values and the order of the first three stay as they were. */ export const COLLECTION_STATUSES = ['idea', 'want', 'visited', 'revisit'] as const; export const collectionStatusSchema = z.enum(COLLECTION_STATUSES).catch('idea').default('idea'); export type CollectionStatus = (typeof COLLECTION_STATUSES)[number]; /** Whether the status means "been there": a place to revisit counts as visited wherever visits are counted. */ export function isVisitedCollectionStatus(status: CollectionStatus): boolean { return status === 'visited' || status === 'revisit'; } /** Per-member permission on a shared list. viewer = read + copy-to-trip only; * editor (default) = add + edit places; admin = full incl. delete. The owner * is always full and is not a member row. */ export const COLLECTION_ROLES = ['viewer', 'editor', 'admin'] as const; export const collectionRoleSchema = z.enum(COLLECTION_ROLES).catch('editor').default('editor'); export type CollectionRole = (typeof COLLECTION_ROLES)[number]; /** A user-added link on a list or a saved place (stored as a JSON array). */ export const collectionLinkSchema = z.object({ label: z.string().max(120).optional(), // http/https only — blocks javascript:/data: hrefs and forces an absolute link. url: z .string() .trim() .max(2000) .regex(/^https?:\/\/.+/i), }); export type CollectionLink = z.infer; export const collectionLinksSchema = z.array(collectionLinkSchema).max(30); /** A custom label defined per-collection (distinct from the instance-wide tags). * Members group and filter a list's places by these. */ export const collectionLabelSchema = z.object({ id: z.number(), collection_id: z.number(), name: z.string(), color: z.string().nullable().optional(), sort_order: z.number().optional(), }); export type CollectionLabel = z.infer; /** A saved place — assignmentPlace minus itinerary, plus status + provenance. */ export const collectionPlaceSchema = z.object({ id: z.number(), collection_id: z.number(), owner_id: z.number().optional(), saved_by: z.number().nullable().optional(), name: z.string(), description: z.string().nullable().optional(), lat: z.number().nullable().optional(), lng: z.number().nullable().optional(), address: z.string().nullable().optional(), category_id: z.number().nullable().optional(), price: z.number().nullable().optional(), currency: z.string().nullable().optional(), notes: z.string().nullable().optional(), // Deliberately open on the way out, pinned on the way in (see the save/update // request schemas below): rows written before the request-side check existed // still have to round-trip. image_url: z.string().nullable().optional(), google_place_id: z.string().nullable().optional(), google_ftid: z.string().nullable().optional(), osm_id: z.string().nullable().optional(), website: z.string().nullable().optional(), phone: z.string().nullable().optional(), status: collectionStatusSchema, source_trip_id: z.number().nullable().optional(), source_place_id: z.number().nullable().optional(), sort_order: z.number().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), links: collectionLinksSchema.optional(), category: placeCategorySchema.optional(), tags: z.array(tagSchema.partial()).optional(), /** Ids of the per-collection labels assigned to this place. */ label_ids: z.array(z.number().int()).optional(), // Collaborative ratings (#1435): every member's vote + the displayed aggregate. ratings: z.array(placeRatingVoteSchema).optional(), rating_avg: z.number().nullable().optional(), rating_count: z.number().optional(), }); export type CollectionPlace = z.infer; /** rate_collection_place: the acting user's vote on a saved place. */ export const collectionPlaceRateToolInputSchema = z.object({ placeId: idSchema, rating: placeRatingToolFieldSchema, }); export type CollectionPlaceRateToolInput = z.infer; /** * What a vote on a saved place answers: the saved place as its list shows it. * Only the fields the vote is about are pinned here; the place carries all its others. */ export const collectionPlaceRateResponseSchema = collectionPlaceSchema.pick({ id: true, collection_id: true, name: true, ratings: true, rating_avg: true, rating_count: true, }); /** Member of a shared list (mirrors vacay person rows). */ export const collectionMemberSchema = z.object({ user_id: z.number(), username: z.string(), email: z.string().optional(), avatar: z.string().nullable().optional(), status: z.enum(['pending', 'accepted']), role: collectionRoleSchema.optional(), is_owner: z.boolean().optional(), }); export type CollectionMember = z.infer; /** A list, with computed counts + membership for the current viewer. */ export const collectionSchema = z.object({ id: z.number(), owner_id: z.number(), name: z.string(), description: z.string().nullable().optional(), color: z.string().nullable().optional(), icon: z.string().nullable().optional(), cover_image: z.string().nullable().optional(), links: collectionLinksSchema.optional(), sort_order: z.number().optional(), place_count: z.number().optional(), is_owner: z.boolean().optional(), members: z.array(collectionMemberSchema).optional(), labels: z.array(collectionLabelSchema).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), }); export type Collection = z.infer; // ── Requests ────────────────────────────────────────────────────────────── export const collectionCreateRequestSchema = z.object({ name: z.string().min(1).max(120), description: z.string().max(2000).nullable().optional(), color: z .string() .regex(/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/) .optional(), icon: z.string().max(40).optional(), cover_image: z.string().max(500).nullable().optional(), links: collectionLinksSchema.optional(), }); export type CollectionCreateRequest = z.infer; export const collectionUpdateRequestSchema = collectionCreateRequestSchema.partial().extend({ sort_order: z.number().optional(), }); export type CollectionUpdateRequest = z.infer; /** Reorder the caller's lists — every visible collection id in the desired order. * No .min mirrors the legacy hand-rolled check the DTO ratchet replaced: any * array, empty included; .int() keeps a fractional id from reaching SQL. */ export const collectionReorderRequestSchema = z.object({ orderedIds: z.array(z.number().int()), }); export type CollectionReorderRequest = z.infer; /** Save a place into a list from a raw maps/manual payload (or carrying provenance). */ export const collectionSavePlaceRequestSchema = z.object({ collection_id: z.number().int(), source_place_id: z.number().int().nullable().optional(), source_trip_id: z.number().int().nullable().optional(), name: z.string().min(1), description: z.string().nullable().optional(), lat: z.number().nullable().optional(), lng: z.number().nullable().optional(), address: z.string().nullable().optional(), category_id: z.number().int().nullable().optional(), price: z.number().nullable().optional(), currency: z.string().nullable().optional(), notes: z.string().nullable().optional(), image_url: placeImageUrlSchema.nullable().optional(), google_place_id: z.string().nullable().optional(), google_ftid: z.string().nullable().optional(), osm_id: z.string().nullable().optional(), website: placeWebsiteSchema.nullable().optional(), phone: z.string().nullable().optional(), status: collectionStatusSchema.optional(), links: collectionLinksSchema.optional(), tag_ids: z.array(z.number().int()).optional(), force: z.boolean().optional(), // "add anyway" over a dedup match }); export type CollectionSavePlaceRequest = z.infer; /** DEDICATED DTO for POST /places/from-trip — the server reads the place, so no place payload. */ export const collectionSaveFromTripRequestSchema = z.object({ collection_id: z.number().int(), source_trip_id: z.number().int(), source_place_id: z.number().int(), force: z.boolean().optional(), }); export type CollectionSaveFromTripRequest = z.infer; /** Bulk: copy several selected trip places into a list at once. */ export const collectionSaveFromTripManyRequestSchema = z.object({ collection_id: z.number().int(), source_trip_id: z.number().int(), source_place_ids: z.array(z.number().int()).min(1).max(1000), force: z.boolean().optional(), }); export type CollectionSaveFromTripManyRequest = z.infer; export const collectionPlaceUpdateRequestSchema = z.object({ name: z.string().min(1).optional(), description: z.string().nullable().optional(), notes: z.string().nullable().optional(), // Editable coordinates so a place added by GPS can be corrected later (#1435). lat: z.number().nullable().optional(), lng: z.number().nullable().optional(), // Free-text address, correctable after saving (#1870): the add dialog always // offered it, the edit form did not. Deliberately uncapped, exactly like // collectionSavePlaceRequestSchema.address, so save and update stay in step. address: z.string().nullable().optional(), // .removeDefault() strips the inner .default('idea') so an ABSENT status parses to // undefined (left unchanged) instead of being injected as 'idea' (see #1437). The // .catch('idea') guard against invalid values is preserved. status: collectionStatusSchema.removeDefault().optional(), category_id: z.number().int().nullable().optional(), collection_id: z.number().int().optional(), // move to another list links: collectionLinksSchema.optional(), tag_ids: z.array(z.number().int()).optional(), // Replace the place's per-collection label assignments (omit to leave unchanged). label_ids: z.array(z.number().int()).optional(), // Custom thumbnail (#1136): null clears it (falls back to the auto-fetched photo). image_url: placeImageUrlSchema.nullable().optional(), // The fields saving accepts but editing did not (#2646), with the same checks, so // a place can be corrected in place instead of re-saved, which loses its labels, // ratings, position and id. Each one is left alone when absent; null clears it. google_place_id: z.string().nullable().optional(), google_ftid: z.string().nullable().optional(), osm_id: z.string().nullable().optional(), website: placeWebsiteSchema.nullable().optional(), phone: z.string().nullable().optional(), price: z.number().nullable().optional(), currency: z.string().nullable().optional(), }); export type CollectionPlaceUpdateRequest = z.infer; export const collectionSetStatusRequestSchema = z.object({ status: collectionStatusSchema }); export type CollectionSetStatusRequest = z.infer; /** Set one status on several saved places at once — the place dialog's "mark * visited everywhere it is saved" (#1469). Ids are collection_places ids. */ export const collectionSetStatusManyRequestSchema = z.object({ ids: z.array(z.number().int()).min(1).max(1000), status: collectionStatusSchema, }); export type CollectionSetStatusManyRequest = z.infer; /** Same, but naming the places by their TRIP ids: the server resolves each one to * the lists it is saved in. Drives the planner's bulk "mark as visited" (#1469). */ export const collectionSetStatusFromTripRequestSchema = z.object({ trip_id: z.number().int(), place_ids: z.array(z.number().int()).min(1).max(1000), status: collectionStatusSchema, }); export type CollectionSetStatusFromTripRequest = z.infer; export const collectionSetStatusManyResponseSchema = z.object({ /** Saved places whose status actually changed. */ updated: z.number(), /** Trip places that were found in at least one list (from-trip form only). */ places: z.number().optional(), }); export type CollectionSetStatusManyResponse = z.infer; /** Bulk-delete saved places. No .min mirrors the legacy hand-rolled check the * DTO ratchet replaced (same shape as placeBulkDeleteRequestSchema). */ export const collectionDeleteManyRequestSchema = z.object({ ids: z.array(z.number().int()), }); export type CollectionDeleteManyRequest = z.infer; /** * Copy one or many saved places INTO a trip (dedup precheck on server). * * A saved place's notes and tags stay in the list unless the request asks for * them (D9): a note is often private, and a tag is somebody's own. Older clients * that send neither field get the places without both. */ export const collectionCopyToTripRequestSchema = z.object({ trip_id: z.number().int(), place_ids: z.array(z.number().int()).min(1), force: z.boolean().optional(), include_notes: z .boolean() .optional() .describe("true copies each saved place's notes into the trip; left out or false, the notes stay in the list"), tag_ids: z .array(z.number().int()) .max(50) .optional() .describe( 'Ids of tags the saved places carry that should come along. Each becomes a tag of the trip, reusing a trip tag of the same name; ids a place does not carry are ignored. Left out, no tags are copied', ), label_ids: z .array(z.number().int()) .max(50) .optional() .describe( "PLC-21: ids of the list's tags (its labels) the saved places carry that should come along. Each becomes a tag of the trip, reusing a trip tag of the same name; ids a place does not carry are ignored. Left out, no list tags are copied", ), }); export type CollectionCopyToTripRequest = z.infer; /** What a copy into a trip did: how many went in, the ones the trip had, and each new trip place by its source. */ export const collectionCopyToTripResponseSchema = z.object({ copied: z.number(), skipped: z.array(z.object({ id: z.number(), name: z.string() })), created: z.array(z.object({ id: z.number(), source_id: z.number() })), }); export type CollectionCopyToTripResponse = z.infer; /** The most places one reorder names; well past the size lists grow to. */ export const MAX_COLLECTION_REORDER_PLACES = 10000; /** * A list's places in the order its members want them (PLC-03). Every id must * be a place of that list. Places it leaves out (saved by someone else since * the list was loaded) keep their relative order and follow the named ones. */ export const collectionPlacesReorderRequestSchema = z.object({ orderedIds: z .array(z.number().int()) .min(1) .max(MAX_COLLECTION_REORDER_PLACES) .describe("The list's place ids (collection place ids) in the new order, first to last"), }); export type CollectionPlacesReorderRequest = z.infer; /** Saved places and labels asked for by their ids, across every list the caller can see (get_collection_items). */ export const collectionItemsRequestSchema = z.object({ placeIds: z.array(idSchema).max(50).optional(), labelIds: z.array(idSchema).max(20).optional(), }); export type CollectionItemsRequest = z.infer; export const COLLECTION_PLACES_PAGE_DEFAULT = 100; export const COLLECTION_PLACES_PAGE_MAX = 500; /** * One page of a list's places for an MCP client (list_collection_places, #2647): * get_collection answers a long list as one block a client cuts in the middle. * Pages follow the list's own order and continue from a cursor rather than an * offset, so labelling the places of one page while filtering on `unlabeled` * cannot make the next page skip any. */ export const collectionPlacesPageRequestSchema = z.object({ collectionId: idSchema, cursor: z .string() .max(200) .optional() .describe('Where to go on: the next_cursor of the page before. Leave it out for the first page.'), limit: z .number() .int() .min(1) .max(COLLECTION_PLACES_PAGE_MAX) .optional() .describe( `How many places at most (default ${COLLECTION_PLACES_PAGE_DEFAULT}, at most ${COLLECTION_PLACES_PAGE_MAX})`, ), status: z.enum(COLLECTION_STATUSES).optional().describe('Only places with this status'), labelId: idSchema.optional().describe('Only places carrying this label of the list'), unlabeled: z.boolean().optional().describe('true: only places without any label. Not together with labelId.'), }); export type CollectionPlacesPageRequest = z.infer; // Fusion invitations. user_id is NUMERIC ONLY — the UI always sends an id from availableUsers. export const collectionInviteRequestSchema = z.object({ collection_id: z.number().int(), user_id: z.number().int(), role: collectionRoleSchema.optional(), }); export type CollectionInviteRequest = z.infer; export const collectionInviteActionRequestSchema = z.object({ collection_id: z.number().int() }); export type CollectionInviteActionRequest = z.infer; export const collectionInviteCancelRequestSchema = z.object({ collection_id: z.number().int(), user_id: z.number().int(), }); export type CollectionInviteCancelRequest = z.infer; /** Owner removes an ALREADY-ACCEPTED member (kick). */ export const collectionRemoveMemberRequestSchema = z.object({ collection_id: z.number().int(), user_id: z.number().int(), }); export type CollectionRemoveMemberRequest = z.infer; /** Owner changes an accepted member's permission role. */ export const collectionSetMemberRoleRequestSchema = z.object({ collection_id: z.number().int(), user_id: z.number().int(), role: collectionRoleSchema, }); export type CollectionSetMemberRoleRequest = z.infer; // ── Labels ────────────────────────────────────────────────────────────────── const labelColorSchema = z.string().regex(/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/); /** Create a custom label in a list. */ export const collectionLabelCreateRequestSchema = z.object({ collection_id: z.number().int(), name: z.string().trim().min(1).max(60), color: labelColorSchema.optional(), }); export type CollectionLabelCreateRequest = z.infer; /** Rename / recolor a label (all fields optional). */ export const collectionLabelUpdateRequestSchema = z.object({ name: z.string().trim().min(1).max(60).optional(), color: labelColorSchema.optional(), sort_order: z.number().optional(), }); export type CollectionLabelUpdateRequest = z.infer; /** Bulk add or remove one/several labels across many selected places. */ export const collectionLabelAssignRequestSchema = z.object({ label_ids: z.array(z.number().int()).min(1).max(50), place_ids: z.array(z.number().int()).min(1).max(1000), }); export type CollectionLabelAssignRequest = z.infer; // ── Responses ───────────────────────────────────────────────────────────── export const collectionListResponseSchema = z.object({ collections: z.array(collectionSchema), // `from` is DERIVED from collections.owner_id JOIN users (sendInvite is owner-only, so the // inviter is always the list owner — collection_members has no invited_by column). incomingInvites: z.array( z.object({ collection_id: z.number(), name: z.string(), from: z.object({ id: z.number(), username: z.string() }), }), ), }); export type CollectionListResponse = z.infer; export const collectionDetailResponseSchema = z.object({ collection: collectionSchema, places: z.array(collectionPlaceSchema), }); export type CollectionDetailResponse = z.infer; /** * The asked-for places and labels with the lists they sit in: each list's * size, colour, labels and people (names and roles, no addresses), and * whether the caller may delete places from it (its owner or an admin). An id * the caller cannot see is left out, never refused. */ const collectionItemsListSchema = collectionSchema .pick({ id: true, name: true, color: true, place_count: true, is_owner: true, labels: true }) .extend({ can_delete: z.boolean(), members: z.array( collectionMemberSchema.pick({ user_id: true, username: true, status: true, role: true, is_owner: true }), ), }); export const collectionItemsResponseSchema = z.object({ lists: z.array(collectionItemsListSchema), places: z.array(collectionPlaceSchema), labels: z.array(collectionLabelSchema), }); export type CollectionItemsResponse = z.infer; /** Dedup outcome envelope reused by save + copy (matches placeService dedup UX). */ export const collectionSaveResultSchema = z.object({ place: collectionPlaceSchema.optional(), duplicate: z.boolean().optional(), duplicateOf: z.object({ id: z.number(), name: z.string() }).nullable().optional(), }); export type CollectionSaveResult = z.infer; /** Library-wide "is this place already saved anywhere I can see?" lookup (inspector indicator). */ export const collectionMembershipSchema = z.object({ saved: z.boolean(), lists: z.array( z.object({ collection_id: z.number(), name: z.string(), /** The list's colour, for its dot; older servers leave it out. */ color: z.string().nullable().optional(), place_id: z.number(), /** Per-list status, so the picker can show and change it without a second round trip (#1469). */ status: collectionStatusSchema, /** False for a list the viewer may read but not edit — its status pill stays read-only. */ can_edit: z.boolean().default(true), }), ), }); export type CollectionMembership = z.infer; /** One trip place as offered by the import preview. `already_in_list` is the SAME dedup * verdict the bulk import applies (name, or coordinates within tolerance), resolved on the * server so the dialog can never disagree with what the write path then does. `scheduled` * is false for a place no day holds — the ones a trip left behind, which is exactly what * the import is for, so the dialog pre-selects them. */ export const collectionImportablePlaceSchema = z.object({ place_id: z.number(), name: z.string(), address: z.string().nullable(), lat: z.number().nullable(), lng: z.number().nullable(), category_id: z.number().nullable(), image_url: z.string().nullable(), already_in_list: z.boolean(), scheduled: z.boolean(), day_number: z.number().nullable(), date: z.string().nullable(), }); export type CollectionImportablePlace = z.infer; export const collectionImportablesResponseSchema = z.object({ places: z.array(collectionImportablePlaceSchema), }); export type CollectionImportablesResponse = z.infer;