Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

2.3 KB
Raw Blame
import { hexColorSchema } from '../place/place.schema';

import { z } from 'zod';

/**
 * Category API contract — single source of truth for the /api/categories endpoints.
 *
 * Categories are the place-category palette (also the admin "Personalization"
 * surface). Reading is open to any authenticated user; create/update/delete are
 * admin-only. The legacy route (server/src/routes/categories.ts) wraps
 * services/categoryService.ts 1:1.
 *
 * The bespoke 400 ("Category name is required") and 404 ("Category not found")
 * messages are reproduced in the controller so the bodies stay byte-identical.
 */

/**
 * Note the asymmetry below: the response keeps `color: z.string()` while the
 * requests demand a hex value. Tightening the response would make an instance
 * fail on its own stored rows — the write path was unvalidated for a long time,
 * so anything could be in there. The client allow-lists on read for the same
 * reason (client/src/utils/safeColor.ts).
 */
export const categorySchema = z.object({
  id: z.number(),
  name: z.string(),
  color: z.string(),
  icon: z.string(),
  user_id: z.number().nullable().optional(),
  created_at: z.string().optional(),
});
export type Category = z.infer<typeof categorySchema>;

export const createCategoryRequestSchema = z.object({
  name: z.string().min(1),
  // Hex only: the value is interpolated into a style="…" attribute of the
  // hand-built marker HTML on both map renderers, and on the share page that
  // answers without a guard.
  color: hexColorSchema.optional(),
  icon: z.string().optional(),
});
export type CreateCategoryRequest = z.infer<typeof createCategoryRequestSchema>;

/** All fields optional — the service COALESCEs each against the stored value. */
export const updateCategoryRequestSchema = z.object({
  name: z.string().optional(),
  // Hex only: the value is interpolated into a style="…" attribute of the
  // hand-built marker HTML on both map renderers, and on the share page that
  // answers without a guard.
  color: hexColorSchema.optional(),
  icon: z.string().optional(),
});
export type UpdateCategoryRequest = z.infer<typeof updateCategoryRequestSchema>;

export const categoryListResponseSchema = z.object({
  categories: z.array(categorySchema),
});
export type CategoryListResponse = z.infer<typeof categoryListResponseSchema>;