| 1 | import { z } from 'zod' |
| 2 | |
| 3 | export const CUSTOM_FIELD_TYPES = |
| 4 | 'text', |
| 5 | 'number', |
| 6 | 'select', |
| 7 | 'checkbox', |
| 8 | 'date', |
| 9 | 'datetime', |
| 10 | 'time', |
| 11 | ] as const |
| 12 | |
| 13 | export type CustomFieldType |
| 14 | |
| 15 | export const TEMPORAL_TYPES = |
| 16 | |
| 17 | export function isTemporalType(t |
| 18 | return t === |
| 19 | } |
| 20 | |
| 21 | export const CUSTOM_FIELD_TYPE_LABELS: |
| 22 | text: 'Text', |
| 23 | number: 'Number', |
| 24 | select: 'Dropdown', |
| 25 | checkbox: 'Checkbox', |
| 26 | date: 'Date', |
| 27 | datetime: 'Date & time', |
| 28 | time: 'Time', |
| 29 | } |
| 30 | |
| 31 | export interface CustomFieldConfig |
| 32 | max?: number |
| 33 | maxLength?: number |
| 34 | min?: number |
| 35 | } |
| 36 | |
| 37 | export interface CustomFieldDefinition |
| 38 | config: CustomFieldConfig |
| 39 | createdAt: Date |
| 40 | defaultValue: string |
| 41 | id: string |
| 42 | label: string |
| 43 | options: string[] |
| 44 | organizationId: string |
| 45 | projectId: string |
| 46 | required: boolean |
| 47 | type: CustomFieldType |
| 48 | visibleToClient: boolean |
| 49 | } |
| 50 | |
| 51 | const labelSchema = z |
| 52 | |
| 53 | const configSchema = z |
| 54 | .object({ |
| 55 | maxLength: z.number |
| 56 | min: z.number |
| 57 | max: z.number |
| 58 | }) |
| 59 | .refine((c) |
| 60 | message: 'Min must be less than or equal to Max', |
| 61 | path: ['min'], |
| 62 | }) |
| 63 | .nullable() |
| 64 | .optional() |
| 65 | |
| 66 | const DATE_RE = / |
| 67 | const TIME_RE = / |
| 68 | |
| 69 | export function isValidDateString(s |
| 70 | const m = |
| 71 | if (!m) |
| 72 | return false |
| 73 | } |
| 74 | const year = |
| 75 | const month = |
| 76 | const day = |
| 77 | if (month < |
| 78 | return false |
| 79 | } |
| 80 | const daysInMonth = |
| 81 | return day <= |
| 82 | } |
| 83 | |
| 84 | export function isValidTimeString(s |
| 85 | return TIME_RE.test |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Parse a YYYY-MM-DD string as a local-time Date (midnight in the runtime's |
| 90 | * timezone). `new Date("YYYY-MM-DD")` parses as UTC, which can shift the day |
| 91 | * for users west of UTC. |
| 92 | */ |
| 93 | export function parseDateOnlyAsLocal(s |
| 94 | const m = |
| 95 | if (!m) |
| 96 | return null |
| 97 | } |
| 98 | const year = |
| 99 | const month = |
| 100 | const day = |
| 101 | if (!isValidDateString(s |
| 102 | return null |
| 103 | } |
| 104 | return new Date |
| 105 | } |
| 106 | |
| 107 | export function formatLocalDateOnly(date |
| 108 | const y = |
| 109 | const m = |
| 110 | const d = |
| 111 | return `${y} |
| 112 | } |
| 113 | |
| 114 | function validateDefinitionCrossFields( |
| 115 | data: { |
| 116 | type: CustomFieldType |
| 117 | options?: string |
| 118 | defaultValue?: string |
| 119 | config?: CustomFieldConfig |
| 120 | }, |
| 121 | ctx: z |
| 122 | ) { |
| 123 | if (data.config |
| 124 | if (data.type |
| 125 | ctx.addIssue({ |
| 126 | code: 'custom', |
| 127 | path: ['config', |
| 128 | message: 'maxLength is only allowed for text fields.', |
| 129 | }) |
| 130 | } |
| 131 | if (data.type |
| 132 | ctx.addIssue({ |
| 133 | code: 'custom', |
| 134 | path: ['config', |
| 135 | message: 'min is only allowed for number fields.', |
| 136 | }) |
| 137 | } |
| 138 | if (data.type |
| 139 | ctx.addIssue({ |
| 140 | code: 'custom', |
| 141 | path: ['config', |
| 142 | message: 'max is only allowed for number fields.', |
| 143 | }) |
| 144 | } |
| 145 | } |
| 146 | if (data.type |
| 147 | if (!data.options |
| 148 | ctx.addIssue({ |
| 149 | code: 'custom', |
| 150 | path: ['options'], |
| 151 | message: 'At least one option is required for a select field.', |
| 152 | }) |
| 153 | return |
| 154 | } |
| 155 | const set = |
| 156 | if (set.size |
| 157 | ctx.addIssue({ |
| 158 | code: 'custom', |
| 159 | path: ['options'], |
| 160 | message: 'Options must be unique.', |
| 161 | }) |
| 162 | } |
| 163 | if ( |
| 164 | data.defaultValue != |
| 165 | !data.options.includes |
| 166 | ) { |
| 167 | ctx.addIssue({ |
| 168 | code: 'custom', |
| 169 | path: ['defaultValue'], |
| 170 | message: 'Default must be one of the configured options.', |
| 171 | }) |
| 172 | } |
| 173 | } |
| 174 | if (data.type |
| 175 | ctx.addIssue({ |
| 176 | code: 'custom', |
| 177 | path: ['options'], |
| 178 | message: 'Options are only allowed for select fields.', |
| 179 | }) |
| 180 | } |
| 181 | if ( |
| 182 | data.type === |
| 183 | data.defaultValue != |
| 184 | data.defaultValue !== |
| 185 | !isValidDateString(data.defaultValue |
| 186 | ) { |
| 187 | ctx.addIssue({ |
| 188 | code: 'custom', |
| 189 | path: ['defaultValue'], |
| 190 | message: "Default must be a valid YYYY-MM-DD date or 'today'.", |
| 191 | }) |
| 192 | } |
| 193 | if ( |
| 194 | data.type === |
| 195 | data.defaultValue != |
| 196 | data.defaultValue !== |
| 197 | !isValidTimeString(data.defaultValue |
| 198 | ) { |
| 199 | ctx.addIssue({ |
| 200 | code: 'custom', |
| 201 | path: ['defaultValue'], |
| 202 | message: "Default must be a valid HH:MM time or 'now'.", |
| 203 | }) |
| 204 | } |
| 205 | if ( |
| 206 | data.type === |
| 207 | data.defaultValue != |
| 208 | data.defaultValue !== |
| 209 | Number.isNaN(Date |
| 210 | ) { |
| 211 | ctx.addIssue({ |
| 212 | code: 'custom', |
| 213 | path: ['defaultValue'], |
| 214 | message: "Default must be a valid datetime or 'now'.", |
| 215 | }) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | export const customFieldDefinitionCreateSchema = |
| 220 | .object({ |
| 221 | label: labelSchema, |
| 222 | type: z.enum |
| 223 | required: z.boolean |
| 224 | visibleToClient: z.boolean |
| 225 | defaultValue: z.string |
| 226 | options: z.array |
| 227 | config: configSchema, |
| 228 | }) |
| 229 | .superRefine(validateDefinitionCrossFields) |
| 230 | |
| 231 | export const customFieldDefinitionUpdateSchema = |
| 232 | .object({ |
| 233 | label: labelSchema, |
| 234 | type: z.enum |
| 235 | required: z.boolean |
| 236 | visibleToClient: z.boolean |
| 237 | defaultValue: z.string |
| 238 | options: z.array |
| 239 | config: configSchema, |
| 240 | }) |
| 241 | .superRefine(validateDefinitionCrossFields) |
| 242 | |
| 243 | export function resolveDefault( |
| 244 | def: Pick |
| 245 | now: Date |
| 246 | ): unknown { |
| 247 | if (def.defaultValue |
| 248 | return def.type |
| 249 | } |
| 250 | if (def.type |
| 251 | return formatLocalDateOnly(now |
| 252 | } |
| 253 | if (def.type |
| 254 | const h = |
| 255 | const m = |
| 256 | return `${h} |
| 257 | } |
| 258 | if (def.type |
| 259 | return now.toISOString |
| 260 | } |
| 261 | if (def.type |
| 262 | return def.defaultValue |
| 263 | } |
| 264 | if (def.type |
| 265 | const n = |
| 266 | return Number.isFinite |
| 267 | } |
| 268 | return def.defaultValue |
| 269 | } |
| 270 | |
| 271 | function valueSchemaFor(def: |
| 272 | switch (def.type |
| 273 | case 'text': { |
| 274 | let s = |
| 275 | if (def.config |
| 276 | s = s. |
| 277 | } else { |
| 278 | s = s. |
| 279 | } |
| 280 | return def.required |
| 281 | } |
| 282 | case 'number': { |
| 283 | let n = |
| 284 | if (def.config |
| 285 | n = n. |
| 286 | } |
| 287 | if (def.config |
| 288 | n = n. |
| 289 | } |
| 290 | return def.required |
| 291 | } |
| 292 | case 'select': { |
| 293 | const opts = |
| 294 | if (opts.length |
| 295 | return z.never |
| 296 | } |
| 297 | const e = |
| 298 | return def.required |
| 299 | } |
| 300 | case 'checkbox': |
| 301 | return z.boolean |
| 302 | case 'date': { |
| 303 | const s = |
| 304 | .string() |
| 305 | .refine(isValidDateString, |
| 306 | return def.required |
| 307 | ? s.min( |
| 308 | : s.optional() |
| 309 | } |
| 310 | case 'time': { |
| 311 | const s = |
| 312 | .string() |
| 313 | .refine(isValidTimeString, |
| 314 | return def.required |
| 315 | ? s.min( |
| 316 | : s.optional() |
| 317 | } |
| 318 | case 'datetime': { |
| 319 | const s = |
| 320 | message: 'Invalid datetime', |
| 321 | }) |
| 322 | return def.required |
| 323 | } |
| 324 | default: |
| 325 | return z.unknown |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | export function buildCustomValuesSchema(defs |
| 330 | const shape: |
| 331 | for (const def |
| 332 | shape[def.id |
| 333 | } |
| 334 | return z.object |
| 335 | const out: |
| 336 | for (const def |
| 337 | const v = |
| 338 | if (v != |
| 339 | out[def.id |
| 340 | } |
| 341 | } |
| 342 | return out |
| 343 | }) |
| 344 | } |
| 345 | |
| 346 | // ---------- display helpers ---------- |
| 347 | |
| 348 | export function normalizeCustomFieldLabel(label |
| 349 | return label.trim |
| 350 | } |
| 351 | |
| 352 | export function formatCustomFieldValue( |
| 353 | def: CustomFieldDefinition |
| 354 | raw: unknown, |
| 355 | timeZone?: string |
| 356 | ): string | null |
| 357 | if (raw == |
| 358 | return null |
| 359 | } |
| 360 | switch (def.type |
| 361 | case 'checkbox': |
| 362 | return raw ? |
| 363 | case 'date': { |
| 364 | const local = |
| 365 | if (!local) |
| 366 | return String(raw |
| 367 | } |
| 368 | return local.toLocaleDateString |
| 369 | year: 'numeric', |
| 370 | month: 'short', |
| 371 | day: 'numeric', |
| 372 | }) |
| 373 | } |
| 374 | case 'datetime': { |
| 375 | const d = |
| 376 | if (Number.isNaN |
| 377 | return String(raw |
| 378 | } |
| 379 | const resolvedTimeZone = |
| 380 | return d.toLocaleString |
| 381 | year: 'numeric', |
| 382 | month: 'short', |
| 383 | day: 'numeric', |
| 384 | hour: 'numeric', |
| 385 | minute: '2-digit', |
| 386 | timeZone: resolvedTimeZone, |
| 387 | }) |
| 388 | } |
| 389 | default: |
| 390 | return String(raw |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // ---------- editor-form helpers ---------- |
| 395 | |
| 396 | export interface CustomFieldFormValues |
| 397 | config?: CustomFieldConfig |
| 398 | defaultValue?: string |
| 399 | label: string |
| 400 | options?: { |
| 401 | required: boolean |
| 402 | type: CustomFieldType |
| 403 | useCurrent: boolean |
| 404 | visibleToClient: boolean |
| 405 | } |
| 406 | |
| 407 | export interface CustomFieldInitial |
| 408 | config?: CustomFieldConfig |
| 409 | defaultValue?: string |
| 410 | label?: string |
| 411 | options?: string |
| 412 | required?: boolean |
| 413 | type?: CustomFieldType |
| 414 | visibleToClient?: boolean |
| 415 | } |
| 416 | |
| 417 | export function toCustomFieldFormValues( |
| 418 | initial?: CustomFieldInitial |
| 419 | ): CustomFieldFormValues { |
| 420 | const def = |
| 421 | const type = |
| 422 | const isSentinel = |
| 423 | return { |
| 424 | label: initial?.label |
| 425 | type, |
| 426 | required: initial?.required |
| 427 | visibleToClient: initial?.visibleToClient |
| 428 | defaultValue: isSentinel ? |
| 429 | options: (initial?.options |
| 430 | config: initial?.config |
| 431 | useCurrent: isSentinel, |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | export interface CustomFieldSubmitPayload |
| 436 | config: CustomFieldConfig |
| 437 | defaultValue: string |
| 438 | label: string |
| 439 | options: string[] |
| 440 | required: boolean |
| 441 | type: CustomFieldType |
| 442 | visibleToClient: boolean |
| 443 | } |
| 444 | |
| 445 | export function toSubmittableCustomFieldPayload( |
| 446 | values: CustomFieldFormValues |
| 447 | ): CustomFieldSubmitPayload { |
| 448 | let defaultValue: |
| 449 | if (values.useCurrent |
| 450 | defaultValue = values. |
| 451 | } else if (values |
| 452 | defaultValue = values. |
| 453 | } |
| 454 | |
| 455 | return { |
| 456 | label: values.label |
| 457 | type: values.type |
| 458 | required: values.required |
| 459 | visibleToClient: values.visibleToClient |
| 460 | defaultValue, |
| 461 | options: |
| 462 | values.type === |
| 463 | ? (values.options |
| 464 | : null, |
| 465 | config: values.config |
| 466 | } |
| 467 | } |