import { z } from 'zod' import { isCardModelId, type CardModelId } from '@/lib/cardModels' import { CONTENT_EVENT_TYPES } from '@/lib/contentEvents' /** * C213/S32/S38 — the wire contract of the public `POST /api/content-events`. * * Three additive variants, exactly one subject per body (strict objects: an * extra key is a malformed body, never a silently ignored one): * - a published piece (`{ type, pieceSlug }`): the client never names the * internal subject id — the route resolves the slug against the published * read, so an unpublished or unknown piece writes nothing; * - a personalized card download (`{ type: 'download', subjectType: 'card', * cardModelId, stateDeputySlug? }`): the client names the public catalog keys * and the route validates both against the committed catalogs (the card model * and the state deputy), so an arbitrary id or slug writes nothing; * - a card opening (`{ type: 'abertura', subjectType: 'card', cardModelId }`, * S38): the catalogue item routed the visitor to the studio; the visitor has * not chosen a state deputy yet, so there is no `stateDeputySlug` here. * * A malformed body is refused before any query (and the beacon ignores every * response anyway). */ /** Slugs are generated by `contentPieceSlugBase` and by the deputy catalog. */ const PUBLIC_SLUG_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/ const PUBLIC_SLUG_MAX_LENGTH = 120 const publicSlug = z.string().trim().min(1).max(PUBLIC_SLUG_MAX_LENGTH).regex(PUBLIC_SLUG_PATTERN) const pieceEventRequestSchema = z.strictObject({ type: z.enum(CONTENT_EVENT_TYPES), pieceSlug: publicSlug, }) const cardDownloadEventRequestSchema = z.strictObject({ type: z.literal('download'), subjectType: z.literal('card'), cardModelId: z.custom(isCardModelId), stateDeputySlug: publicSlug.optional(), }) const cardOpeningEventRequestSchema = z.strictObject({ type: z.literal('abertura'), subjectType: z.literal('card'), cardModelId: z.custom(isCardModelId), }) export const contentEventRequestSchema = z.union([ pieceEventRequestSchema, cardDownloadEventRequestSchema, cardOpeningEventRequestSchema, ])