Files
pn-new-crm/src/lib/validators/berths.ts

123 lines
5.1 KiB
TypeScript
Raw Normal View History

import { z } from 'zod';
import { BERTH_STATUSES } from '@/lib/constants';
import { baseListQuerySchema } from '@/lib/api/route-helpers';
// ─── Create Berth ────────────────────────────────────────────────────────────
export const createBerthSchema = z.object({
mooringNumber: z.string().min(1),
area: z.string().min(1),
lengthFt: z.coerce.number().optional(),
lengthM: z.coerce.number().optional(),
widthFt: z.coerce.number().optional(),
widthM: z.coerce.number().optional(),
draftFt: z.coerce.number().optional(),
draftM: z.coerce.number().optional(),
price: z.coerce.number().optional(),
priceCurrency: z.string().optional(),
status: z.enum(BERTH_STATUSES).default('available'),
tenureType: z.enum(['permanent', 'fixed_term']).optional(),
mooringType: z.string().optional(),
feat(berths): full NocoDB field parity, numeric types, sales edit access Aligns the berths schema with the 117 production rows in NocoDB and exposes every field for editing via the BerthForm sheet. Schema (migration 0020): - power_capacity / voltage / nominal_boat_size / nominal_boat_size_m: text -> numeric (NocoDB stores plain numbers; text was wrong shape and broke filter/sort) - ADD status_override_mode text (1/117 legacy rows have a value; carried forward for parity but not yet wired into the UI) - USING NULLIF(TRIM(...), '')::numeric so legacy whitespace and empty strings convert cleanly Validator + service: - updateBerthSchema / createBerthSchema use z.coerce.number() for the four numeric fields - berths.service stringifies numeric values for Drizzle's numeric type Form (src/components/berths/berth-form.tsx): - adds: nominal boat size (ft/m), water depth (ft/m) + "is minimum" flag, side pontoon, cleat type/capacity, bollard type/capacity, bow facing - converts to typed selects (with NocoDB option lists in src/lib/constants): area, side pontoon, mooring type, cleat type/capacity, bollard type/capacity, access - power capacity / voltage become numeric inputs (with kW / V hints) Permissions (seed.ts + dev DB): - sales_manager and sales_agent: berths.edit false -> true ("sales will sometimes have to update these and I cannot be the only one") - super_admin / director already had it; viewer stays read-only - dev DB updated in-place via UPDATE roles ... jsonb_set Verification: - pnpm exec vitest run: 858/858 passing - pnpm exec tsc --noEmit: same 36 errors as baseline (all pre-existing on feat/mobile-foundation, none introduced) - lint clean Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 15:30:32 +02:00
powerCapacity: z.coerce.number().optional(), // kW
voltage: z.coerce.number().optional(), // V at 60Hz
access: z.string().optional(),
bowFacing: z.string().optional(),
sidePontoon: z.string().optional(),
});
export type CreateBerthInput = z.infer<typeof createBerthSchema>;
// ─── Update Berth ─────────────────────────────────────────────────────────────
export const updateBerthSchema = z.object({
area: z.string().optional(),
lengthFt: z.coerce.number().optional(),
lengthM: z.coerce.number().optional(),
widthFt: z.coerce.number().optional(),
widthM: z.coerce.number().optional(),
draftFt: z.coerce.number().optional(),
draftM: z.coerce.number().optional(),
widthIsMinimum: z.boolean().optional(),
feat(berths): full NocoDB field parity, numeric types, sales edit access Aligns the berths schema with the 117 production rows in NocoDB and exposes every field for editing via the BerthForm sheet. Schema (migration 0020): - power_capacity / voltage / nominal_boat_size / nominal_boat_size_m: text -> numeric (NocoDB stores plain numbers; text was wrong shape and broke filter/sort) - ADD status_override_mode text (1/117 legacy rows have a value; carried forward for parity but not yet wired into the UI) - USING NULLIF(TRIM(...), '')::numeric so legacy whitespace and empty strings convert cleanly Validator + service: - updateBerthSchema / createBerthSchema use z.coerce.number() for the four numeric fields - berths.service stringifies numeric values for Drizzle's numeric type Form (src/components/berths/berth-form.tsx): - adds: nominal boat size (ft/m), water depth (ft/m) + "is minimum" flag, side pontoon, cleat type/capacity, bollard type/capacity, bow facing - converts to typed selects (with NocoDB option lists in src/lib/constants): area, side pontoon, mooring type, cleat type/capacity, bollard type/capacity, access - power capacity / voltage become numeric inputs (with kW / V hints) Permissions (seed.ts + dev DB): - sales_manager and sales_agent: berths.edit false -> true ("sales will sometimes have to update these and I cannot be the only one") - super_admin / director already had it; viewer stays read-only - dev DB updated in-place via UPDATE roles ... jsonb_set Verification: - pnpm exec vitest run: 858/858 passing - pnpm exec tsc --noEmit: same 36 errors as baseline (all pre-existing on feat/mobile-foundation, none introduced) - lint clean Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 15:30:32 +02:00
nominalBoatSize: z.coerce.number().optional(), // ft
nominalBoatSizeM: z.coerce.number().optional(), // m
waterDepth: z.coerce.number().optional(),
waterDepthM: z.coerce.number().optional(),
waterDepthIsMinimum: z.boolean().optional(),
sidePontoon: z.string().optional(),
feat(berths): full NocoDB field parity, numeric types, sales edit access Aligns the berths schema with the 117 production rows in NocoDB and exposes every field for editing via the BerthForm sheet. Schema (migration 0020): - power_capacity / voltage / nominal_boat_size / nominal_boat_size_m: text -> numeric (NocoDB stores plain numbers; text was wrong shape and broke filter/sort) - ADD status_override_mode text (1/117 legacy rows have a value; carried forward for parity but not yet wired into the UI) - USING NULLIF(TRIM(...), '')::numeric so legacy whitespace and empty strings convert cleanly Validator + service: - updateBerthSchema / createBerthSchema use z.coerce.number() for the four numeric fields - berths.service stringifies numeric values for Drizzle's numeric type Form (src/components/berths/berth-form.tsx): - adds: nominal boat size (ft/m), water depth (ft/m) + "is minimum" flag, side pontoon, cleat type/capacity, bollard type/capacity, bow facing - converts to typed selects (with NocoDB option lists in src/lib/constants): area, side pontoon, mooring type, cleat type/capacity, bollard type/capacity, access - power capacity / voltage become numeric inputs (with kW / V hints) Permissions (seed.ts + dev DB): - sales_manager and sales_agent: berths.edit false -> true ("sales will sometimes have to update these and I cannot be the only one") - super_admin / director already had it; viewer stays read-only - dev DB updated in-place via UPDATE roles ... jsonb_set Verification: - pnpm exec vitest run: 858/858 passing - pnpm exec tsc --noEmit: same 36 errors as baseline (all pre-existing on feat/mobile-foundation, none introduced) - lint clean Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 15:30:32 +02:00
powerCapacity: z.coerce.number().optional(), // kW
voltage: z.coerce.number().optional(), // V at 60Hz
mooringType: z.string().optional(),
cleatType: z.string().optional(),
cleatCapacity: z.string().optional(),
bollardType: z.string().optional(),
bollardCapacity: z.string().optional(),
access: z.string().optional(),
price: z.coerce.number().optional(),
priceCurrency: z.string().optional(),
bowFacing: z.string().optional(),
berthApproved: z.boolean().optional(),
tenureType: z.enum(['permanent', 'fixed_term']).optional(),
tenureYears: z.coerce.number().int().optional(),
tenureStartDate: z.string().optional(),
tenureEndDate: z.string().optional(),
});
export type UpdateBerthInput = z.infer<typeof updateBerthSchema>;
// ─── Update Berth Status ──────────────────────────────────────────────────────
export const updateBerthStatusSchema = z.object({
status: z.enum(BERTH_STATUSES),
reason: z.string().min(1, 'Reason is required'),
});
export type UpdateBerthStatusInput = z.infer<typeof updateBerthStatusSchema>;
// ─── List Berths ──────────────────────────────────────────────────────────────
export const listBerthsSchema = baseListQuerySchema.extend({
status: z.enum(BERTH_STATUSES).optional(),
area: z.string().optional(),
minLength: z.coerce.number().optional(),
maxLength: z.coerce.number().optional(),
minPrice: z.coerce.number().optional(),
maxPrice: z.coerce.number().optional(),
tenureType: z.enum(['permanent', 'fixed_term']).optional(),
tagIds: z
.string()
.optional()
.transform((v) => (v ? v.split(',') : undefined)),
});
export type ListBerthsQuery = z.infer<typeof listBerthsSchema>;
// ─── Add Maintenance Log ──────────────────────────────────────────────────────
export const addMaintenanceLogSchema = z.object({
category: z.enum(['routine', 'repair', 'inspection', 'upgrade']),
description: z.string().min(1),
cost: z.coerce.number().optional(),
costCurrency: z.string().optional(),
responsibleParty: z.string().optional(),
performedDate: z.string().min(1, 'Performed date is required'),
photoFileIds: z.array(z.string()).optional(),
});
export type AddMaintenanceLogInput = z.infer<typeof addMaintenanceLogSchema>;
// ─── Update Waiting List ──────────────────────────────────────────────────────
export const updateWaitingListSchema = z.object({
entries: z.array(
z.object({
clientId: z.string(),
position: z.number().int().min(1),
priority: z.enum(['normal', 'high']).optional(),
notifyPref: z.enum(['email', 'in_app', 'both']).optional(),
notes: z.string().optional(),
}),
),
});
export type UpdateWaitingListInput = z.infer<typeof updateWaitingListSchema>;