All files / src/lib/workflows schemas.ts

100% Statements 249/249
100% Branches 0/0
100% Functions 0/0
100% Lines 249/249

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 2501x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * Workflow Zod Schemas
 *
 * Type-safe validation schemas for workflow API endpoints.
 * These replace z.any() usage with proper typed schemas.
 */
 
import { z } from "zod";
 
// ============================================================================
// Step Configuration Schemas
// ============================================================================
 
const sendEmailStepConfigSchema = z.object({
  type: z.literal("SEND_EMAIL"),
  templateId: z.number().optional(),
  subject: z.string().optional(),
  body: z.string().optional(),
  variables: z.record(z.string(), z.string()).optional(),
});
 
const sendSmsStepConfigSchema = z.object({
  type: z.literal("SEND_SMS"),
  message: z.string(),
  variables: z.record(z.string(), z.string()).optional(),
});
 
const sendPushStepConfigSchema = z.object({
  type: z.literal("SEND_PUSH"),
  title: z.string(),
  body: z.string(),
  url: z.string().optional(),
});
 
const waitDelayStepConfigSchema = z.object({
  type: z.literal("WAIT_DELAY"),
  duration: z.number(),
  unit: z.enum(["seconds", "minutes", "hours", "days"]),
});
 
const waitUntilStepConfigSchema = z.object({
  type: z.literal("WAIT_UNTIL"),
  time: z.string(),
  timezone: z.string().optional(),
});
 
const conditionRuleSchema = z.object({
  field: z.string(),
  operator: z.enum([
    "equals",
    "not_equals",
    "greater_than",
    "less_than",
    "contains",
    "not_contains",
    "is_empty",
    "is_not_empty",
    "in",
    "not_in",
  ]),
  value: z.union([z.string(), z.number(), z.boolean()]),
});
 
const conditionGroupSchema = z.object({
  logic: z.enum(["AND", "OR"]),
  rules: z.array(conditionRuleSchema),
});
 
const conditionStepConfigSchema = z.object({
  type: z.literal("CONDITION"),
  conditions: z.array(conditionGroupSchema),
  trueConnectionId: z.string().optional(),
  falseConnectionId: z.string().optional(),
});
 
const abVariantSchema = z.object({
  id: z.string(),
  name: z.string(),
  percentage: z.number(),
  connectionId: z.string().optional(),
});
 
const splitAbStepConfigSchema = z.object({
  type: z.literal("SPLIT_AB"),
  variants: z.array(abVariantSchema),
});
 
const updateSegmentStepConfigSchema = z.object({
  type: z.literal("UPDATE_SEGMENT"),
  action: z.enum(["add", "remove"]),
  segmentId: z.number(),
});
 
const addPointsStepConfigSchema = z.object({
  type: z.literal("ADD_POINTS"),
  points: z.number(),
  reason: z.string().optional(),
});
 
const applyCouponStepConfigSchema = z.object({
  type: z.literal("APPLY_COUPON"),
  couponCode: z.string().optional(),
  discountType: z.enum(["percentage", "fixed"]),
  discountValue: z.number(),
  expiresIn: z.number().optional(),
});
 
const webhookStepConfigSchema = z.object({
  type: z.literal("WEBHOOK"),
  url: z.string().url(),
  method: z.enum(["GET", "POST", "PUT", "DELETE"]),
  headers: z.record(z.string(), z.string()).optional(),
  body: z.record(z.string(), z.unknown()).optional(),
});
 
const endStepConfigSchema = z.object({
  type: z.literal("END"),
  status: z.enum(["completed", "cancelled"]),
});
 
// Combined step config schema
const stepConfigSchema = z.discriminatedUnion("type", [
  sendEmailStepConfigSchema,
  sendSmsStepConfigSchema,
  sendPushStepConfigSchema,
  waitDelayStepConfigSchema,
  waitUntilStepConfigSchema,
  conditionStepConfigSchema,
  splitAbStepConfigSchema,
  updateSegmentStepConfigSchema,
  addPointsStepConfigSchema,
  applyCouponStepConfigSchema,
  webhookStepConfigSchema,
  endStepConfigSchema,
]);
 
// Full workflow step schema
export const workflowStepSchema = z.object({
  id: z.string(),
  type: z.enum([
    "SEND_EMAIL",
    "SEND_SMS",
    "SEND_PUSH",
    "WAIT_DELAY",
    "WAIT_UNTIL",
    "CONDITION",
    "SPLIT_AB",
    "UPDATE_SEGMENT",
    "ADD_POINTS",
    "APPLY_COUPON",
    "WEBHOOK",
    "END",
  ]),
  name: z.string(),
  config: stepConfigSchema,
  position: z.object({
    x: z.number(),
    y: z.number(),
  }),
  connections: z.array(z.string()),
});
 
// ============================================================================
// Trigger Configuration Schemas
// ============================================================================
 
const cartAbandonedTriggerSchema = z.object({
  delayMinutes: z.number().min(1),
});
 
const inactiveDaysTriggerSchema = z.object({
  days: z.number().min(1),
});
 
const scheduledTriggerSchema = z.object({
  schedule: z.string(), // Cron expression
  timezone: z.string().optional(),
});
 
const segmentTriggerSchema = z.object({
  segmentId: z.number(),
});
 
// Trigger config is optional and varies by trigger type
// Using a loose schema since different triggers have different configs
export const triggerConfigSchema = z
  .union([
    cartAbandonedTriggerSchema,
    inactiveDaysTriggerSchema,
    scheduledTriggerSchema,
    segmentTriggerSchema,
    z.object({}), // Empty config for triggers that don't need config
  ])
  .nullable()
  .optional();
 
// ============================================================================
// Workflow Trigger Types
// ============================================================================
 
export const workflowTriggerSchema = z.enum([
  "USER_SIGNUP",
  "FIRST_PURCHASE",
  "CART_ABANDONED",
  "BROWSE_ABANDONED",
  "ORDER_COMPLETED",
  "ORDER_SHIPPED",
  "ORDER_DELIVERED",
  "REVIEW_POSTED",
  "BIRTHDAY",
  "ANNIVERSARY",
  "POINTS_EARNED",
  "TIER_UPGRADED",
  "INACTIVE_DAYS",
  "SEGMENT_ENTERED",
  "SEGMENT_EXITED",
  "MANUAL",
  "SCHEDULED",
]);
 
// ============================================================================
// Full Workflow Schemas
// ============================================================================
 
export const workflowCreateSchema = z.object({
  name: z.string().min(1).max(100),
  description: z.string().max(500).optional(),
  trigger: workflowTriggerSchema,
  triggerConfig: triggerConfigSchema,
  steps: z.array(workflowStepSchema),
  isActive: z.boolean().default(false),
  isDraft: z.boolean().default(true),
});
 
export const workflowUpdateSchema = z.object({
  name: z.string().min(1).max(100).optional(),
  description: z.string().max(500).optional().nullable(),
  trigger: workflowTriggerSchema.optional(),
  triggerConfig: triggerConfigSchema,
  steps: z.array(workflowStepSchema).optional(),
  isActive: z.boolean().optional(),
  isDraft: z.boolean().optional(),
});
 
// Export types inferred from schemas
export type WorkflowStepInput = z.infer<typeof workflowStepSchema>;
export type TriggerConfigInput = z.infer<typeof triggerConfigSchema>;
export type WorkflowCreateInput = z.infer<typeof workflowCreateSchema>;
export type WorkflowUpdateInput = z.infer<typeof workflowUpdateSchema>;