import { z } from 'zod'; import { text } from '../common/input'; import { groupSchema } from '../catalog/catalog.schemas'; import { pageSchema } from '../identity/identity.schemas'; const timestamp = z.iso .datetime({ offset: true }) .transform((value) => new Date(value)); const fields = { title: text(160), summary: text(500, 0), collectionId: z.uuid(), startsAt: timestamp, endsAt: timestamp, }; const version = z.number().int().min(0).max(2147483647); export const campaignCreateSchema = z .object({ ...fields, summary: fields.summary.default(''), slug: groupSchema.shape.slug, idempotencyKey: z.uuid(), }) .strict() .refine((value) => value.endsAt > value.startsAt, { message: 'End must follow start', path: ['endsAt'], }); export const campaignPatchSchema = z .object(fields) .partial() .extend({ version, }) .strict() .refine( (value) => Object.keys(value).some((key) => key !== 'version'), 'Provide at least one field', ); export const campaignPublicationSchema = z.object({ version }).strict(); export const campaignCancellationSchema = z .object({ reason: text(500, 3) }) .strict(); export const campaignQuerySchema = pageSchema .extend({ state: z.enum(['DRAFT', 'PUBLISHED', 'CANCELLED']).optional() }) .strict(); export const campaignSlugSchema = groupSchema.shape.slug; export type CampaignCreateInput = z.infer; export type CampaignPatchInput = z.infer; export type CampaignQuery = z.infer;