mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
b56f335978
On a trip with dates the reorder dialog and the days sheet on a phone now offer two ways to add a day: a day without a date at the end as before, or the calendar day after the last date, named on its button, which extends the trip by one day. The new day goes right behind the last dated day, so the days without a date move one place back with their plans, and no existing day or booking changes its date. The server does it in one transaction behind a new dated flag on the day create contract, used by REST, the create_day tool and the plugin RPC alike. It moves the road trip boundaries of the days behind the new one, and announces the day with day:reordered and the grown trip with trip:updated; an insert at a position announces its trip now as well. The next date and the day limit come from one shared rule, so the button never promises a date the server would not give. On the client the add is online only and runs one at a time, so a double click cannot grow the trip by two days. A wiring test that picked the stay question by its position now finds it by its handler, since the delete question sits in front of it.
123 lines
5.1 KiB
TypeScript
123 lines
5.1 KiB
TypeScript
import { assignmentSchema } from '../assignment/assignment.schema';
|
|
|
|
import { z } from 'zod';
|
|
|
|
/**
|
|
* Day + day-note API contract — single source of truth for the
|
|
* /api/trips/:tripId/days and /api/trips/:tripId/days/:dayId/notes endpoints.
|
|
*
|
|
* Trip-scoped, both gated by the 'day_edit' permission. Served by the Nest
|
|
* DaysController / DayNotesController (server/src/nest/days/). Day rows (with
|
|
* their assignments) are wide and DB-derived, so list responses stay open. Day
|
|
* note writes are capped field by field: text 500, time 2000, icon 64, color 9.
|
|
* Time is wider than the legacy validateStringLengths middleware's 150 because
|
|
* the raw-body route it replaced already accepted more, and narrowing it now
|
|
* would reject payloads today's client is allowed to send.
|
|
*/
|
|
|
|
/**
|
|
* Day note entity (server day_notes table / dayNoteService). `sort_order` is
|
|
* SQLite REAL; `icon` defaults to a note emoji.
|
|
*/
|
|
export const dayNoteSchema = z.object({
|
|
id: z.number(),
|
|
day_id: z.number(),
|
|
trip_id: z.number().optional(),
|
|
text: z.string(),
|
|
time: z.string().nullable().optional(),
|
|
icon: z.string().nullable().optional(),
|
|
/** One of NOTE_COLORS, or null for the neutral card (#1629). */
|
|
color: z.string().nullable().optional(),
|
|
sort_order: z.number().optional(),
|
|
created_at: z.string().optional(),
|
|
});
|
|
export type DayNote = z.infer<typeof dayNoteSchema>;
|
|
|
|
/**
|
|
* Day entity as returned by the day list/get endpoints
|
|
* (server/src/services/dayService.ts -> listDays). Columns of the `days` table
|
|
* plus the embedded `assignments` and `notes_items` arrays.
|
|
*/
|
|
export const daySchema = z.object({
|
|
id: z.number(),
|
|
trip_id: z.number(),
|
|
day_number: z.number().optional(),
|
|
date: z.string().nullable().optional(),
|
|
title: z.string().nullable().optional(),
|
|
notes: z.string().nullable().optional(),
|
|
// Whole-day default route mode (#1281); per-segment leg modes override it.
|
|
default_transport_mode: z.string().nullable().optional(),
|
|
assignments: z.array(assignmentSchema).optional(),
|
|
notes_items: z.array(dayNoteSchema).optional(),
|
|
});
|
|
export type Day = z.infer<typeof daySchema>;
|
|
|
|
/** The refusal for `dated` next to `date` or `position`, the same on REST, MCP and the plugin RPC. */
|
|
export const DAY_CREATE_DATED_CONFLICT = 'dated cannot be combined with date or position';
|
|
|
|
/**
|
|
* A new day: appended at the end, slotted in at `position`, or with `dated` the
|
|
* calendar day after the trip's last date, which extends the trip by one day.
|
|
* `dated` picks its own date and place (behind the last dated day, in front of
|
|
* the days without a date), so neither can be given next to it.
|
|
*/
|
|
export const dayCreateRequestSchema = z
|
|
.object({
|
|
date: z.string().optional(),
|
|
notes: z.string().optional(),
|
|
// 1-based slot to insert a new empty day at (omit to append at the end).
|
|
position: z.number().int().positive().optional(),
|
|
dated: z.boolean().optional(),
|
|
})
|
|
.refine((body) => !body.dated || (body.position === undefined && body.date === undefined), {
|
|
message: DAY_CREATE_DATED_CONFLICT,
|
|
path: ['dated'],
|
|
});
|
|
export type DayCreateRequest = z.infer<typeof dayCreateRequestSchema>;
|
|
|
|
/** Reorder whole days: the desired full sequence of this trip's day ids. */
|
|
export const dayReorderRequestSchema = z.object({
|
|
orderedIds: z.array(z.number()),
|
|
});
|
|
export type DayReorderRequest = z.infer<typeof dayReorderRequestSchema>;
|
|
|
|
export const dayUpdateRequestSchema = z.object({
|
|
notes: z.string().optional(),
|
|
title: z.string().nullable().optional(),
|
|
});
|
|
export type DayUpdateRequest = z.infer<typeof dayUpdateRequestSchema>;
|
|
|
|
/**
|
|
* Whole-day default route mode (#1281). The client always sends
|
|
* `{ transport_mode: string | null }`; null clears the default. Kept loose (no
|
|
* enum, no max) — the legacy raw-body route accepted any string.
|
|
*/
|
|
export const dayTransportRequestSchema = z.object({
|
|
transport_mode: z.string().nullable().optional(),
|
|
});
|
|
export type DayTransportRequest = z.infer<typeof dayTransportRequestSchema>;
|
|
|
|
// `time`/`icon` accept null on the wire: the client's moveDayNote re-sends the
|
|
// entity fields (both nullable on dayNoteSchema) through create, and an explicit
|
|
// null time on update means "clear" (the legacy raw-body route accepted both).
|
|
export const dayNoteCreateRequestSchema = z.object({
|
|
text: z.string().min(1).max(500),
|
|
time: z.string().max(2000).nullable().optional(),
|
|
// A lucide name from NOTE_ICONS or a single emoji; 64 leaves room for a ZWJ
|
|
// sequence with skin-tone modifiers and still keeps the column a label.
|
|
icon: z.string().max(64).nullable().optional(),
|
|
/** One of NOTE_COLORS, or null for the neutral card (#1629). */
|
|
color: z.string().max(9).nullable().optional(),
|
|
sort_order: z.number().optional(),
|
|
});
|
|
export type DayNoteCreateRequest = z.infer<typeof dayNoteCreateRequestSchema>;
|
|
|
|
export const dayNoteUpdateRequestSchema = z.object({
|
|
text: z.string().max(500).optional(),
|
|
time: z.string().max(2000).nullable().optional(),
|
|
icon: z.string().max(64).nullable().optional(),
|
|
color: z.string().max(9).nullable().optional(),
|
|
sort_order: z.number().optional(),
|
|
});
|
|
export type DayNoteUpdateRequest = z.infer<typeof dayNoteUpdateRequestSchema>;
|