manicanldes-backend/src/campaigns/campaign.schemas.ts

51 lines
1.5 KiB
TypeScript

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<typeof campaignCreateSchema>;
export type CampaignPatchInput = z.infer<typeof campaignPatchSchema>;
export type CampaignQuery = z.infer<typeof campaignQuerySchema>;