|
| 1 | +import { z } from "zod"; |
| 2 | +import { widgetConfigSchema } from "./widget-config"; |
| 3 | + |
| 4 | +export const programTemplateStepKeySchema = z.enum(["brand", "reward"]); |
| 5 | +export type ProgramTemplateStepKeyType = z.infer< |
| 6 | + typeof programTemplateStepKeySchema |
| 7 | +>; |
| 8 | + |
| 9 | +// ProgramTemplateStepConfig Zod schema and type |
| 10 | +export const programTemplateStepConfigSchema = z.object({ |
| 11 | + key: programTemplateStepKeySchema, |
| 12 | + title: z.string(), |
| 13 | + description: z.string().optional(), |
| 14 | +}); |
| 15 | +export type ProgramTemplateStepConfigType = z.infer< |
| 16 | + typeof programTemplateStepConfigSchema |
| 17 | +>; |
| 18 | + |
| 19 | +// ProgramTemplateConfig Zod schema and type |
| 20 | +export const programTemplateConfigSchema = z.object({ |
| 21 | + schemaVersion: z.number(), |
| 22 | + steps: z.array(programTemplateStepConfigSchema), |
| 23 | + meta: z.record(z.unknown()).optional(), |
| 24 | +}); |
| 25 | +export type ProgramTemplateConfigType = z.infer< |
| 26 | + typeof programTemplateConfigSchema |
| 27 | +>; |
| 28 | + |
| 29 | +// --- Action Config Schemas --- |
| 30 | + |
| 31 | +// Enums for sub-configs |
| 32 | +const trackingMethodEnum = z.enum([ |
| 33 | + "unique_link", |
| 34 | + "coupon_code", |
| 35 | + "api", |
| 36 | + "manual", |
| 37 | +]); |
| 38 | +const verificationMethodEnum = z.enum(["automatic", "manual"]); |
| 39 | + |
| 40 | +// Tracking configuration schema |
| 41 | +const trackingConfigSchema = z.object({ |
| 42 | + method: trackingMethodEnum, |
| 43 | + urlSettings: z |
| 44 | + .object({ |
| 45 | + allowCustom: z.boolean().optional(), |
| 46 | + domain: z.string().url().optional(), |
| 47 | + }) |
| 48 | + .optional(), |
| 49 | +}); |
| 50 | + |
| 51 | +// Verification configuration schema |
| 52 | +const verificationConfigSchema = z.object({ |
| 53 | + method: verificationMethodEnum, |
| 54 | +}); |
| 55 | + |
| 56 | +export const RewardCurrencies = ["USD", "EUR", "GBP"] as const; |
| 57 | + |
| 58 | +export const currencySchema = z.enum(RewardCurrencies); |
| 59 | + |
| 60 | +export type CurrencyType = z.infer<typeof currencySchema>; |
| 61 | + |
| 62 | +// Simplified reward configuration for initial implementation |
| 63 | +// Only supporting cash rewards for referrers and discounts for referees |
| 64 | +export const rewardConfigSchema = z.object({ |
| 65 | + referrer: z.object({ |
| 66 | + type: z.literal("cash"), |
| 67 | + valueType: z.enum(["fixed", "percentage"]), |
| 68 | + value: z.number().positive(), |
| 69 | + currency: currencySchema, |
| 70 | + }), |
| 71 | + referee: z.object({ |
| 72 | + type: z.literal("discount"), |
| 73 | + valueType: z.enum(["fixed", "percentage"]), |
| 74 | + value: z.number().positive(), |
| 75 | + currency: currencySchema.optional(), // Only for fixed discounts |
| 76 | + minPurchaseAmount: z.number().positive().optional(), |
| 77 | + validityDays: z.number().int().positive().optional(), |
| 78 | + }), |
| 79 | +}); |
| 80 | + |
| 81 | +export type RewardConfigType = z.infer<typeof rewardConfigSchema>; |
| 82 | + |
| 83 | +// Notification schema |
| 84 | +export const notificationConfigV1Schema = z |
| 85 | + .object({ |
| 86 | + // Add notification config fields when implemented |
| 87 | + }) |
| 88 | + .optional(); |
| 89 | +export type NotificationConfigV1Type = z.infer< |
| 90 | + typeof notificationConfigV1Schema |
| 91 | +>; |
| 92 | + |
| 93 | +// Program config schema |
| 94 | +export const programConfigV1Schema = z.object({ |
| 95 | + schemaVersion: z.literal(1), |
| 96 | + actions: z.array(z.any()).optional(), // Legacy field, being phased out |
| 97 | + notification: notificationConfigV1Schema, |
| 98 | + templateConfig: programTemplateConfigSchema.optional(), |
| 99 | + widgetConfig: widgetConfigSchema, |
| 100 | +}); |
| 101 | + |
| 102 | +export type ProgramConfigV1Type = z.infer<typeof programConfigV1Schema>; |
| 103 | + |
| 104 | +export const configuredProgramTemplateSchema = z.object({ |
| 105 | + rewardConfig: rewardConfigSchema.optional(), // New reward configuration |
| 106 | +}); |
| 107 | +export type ConfiguredProgramTemplateType = z.infer< |
| 108 | + typeof configuredProgramTemplateSchema |
| 109 | +>; |
0 commit comments