47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { z } from 'zod';
|
|
import { CURRENCIES } from '../common/currency';
|
|
import { couponCode } from '../checkout/checkout.schemas';
|
|
const amount = z.string().regex(/^(0|[1-9]\d{0,9})\.\d{2}$/);
|
|
const common = z.object({
|
|
code: couponCode,
|
|
campaignId: z.uuid().optional(),
|
|
campaignVersion: z.number().int().min(0).max(2147483647).optional(),
|
|
currency: z.enum(CURRENCIES),
|
|
minimumSubtotal: amount.default('0.00'),
|
|
maxUses: z.number().int().min(1).max(1000000),
|
|
perUserLimit: z.number().int().min(1).max(100),
|
|
startsAt: z.iso
|
|
.datetime({ offset: true })
|
|
.transform((value) => new Date(value)),
|
|
endsAt: z.iso
|
|
.datetime({ offset: true })
|
|
.transform((value) => new Date(value)),
|
|
});
|
|
export const couponSchema = z
|
|
.discriminatedUnion('kind', [
|
|
common
|
|
.extend({
|
|
kind: z.literal('FIXED'),
|
|
amount: amount.refine((value) => value !== '0.00'),
|
|
})
|
|
.strict(),
|
|
common
|
|
.extend({
|
|
kind: z.literal('PERCENT'),
|
|
percentBps: z.number().int().min(1).max(10000),
|
|
})
|
|
.strict(),
|
|
])
|
|
.refine((value) => value.endsAt > value.startsAt, { path: ['endsAt'] })
|
|
.refine((value) => value.perUserLimit <= value.maxUses, {
|
|
path: ['perUserLimit'],
|
|
})
|
|
.refine(
|
|
(value) =>
|
|
(value.campaignId === undefined) ===
|
|
(value.campaignVersion === undefined),
|
|
{ path: ['campaignVersion'] },
|
|
);
|
|
export const couponStatusSchema = z.object({ active: z.boolean() }).strict();
|
|
export type CouponInput = z.infer<typeof couponSchema>;
|