feat(server): adopt zod DTOs for the assignment body contracts

This commit is contained in:
jubnl
2026-07-26 20:34:06 +02:00
parent 164f5152be
commit 41b51849b7
7 changed files with 153 additions and 73 deletions
@@ -2,6 +2,7 @@ import {
assignmentCreateRequestSchema,
assignmentMoveRequestSchema,
assignmentParticipantsRequestSchema,
assignmentTransportRequestSchema,
} from './assignment.schema';
import { describe, it, expect } from 'vitest';
@@ -15,13 +16,24 @@ describe('assignmentCreateRequestSchema', () => {
});
describe('assignmentMoveRequestSchema', () => {
it('requires new_day_id; order_index optional', () => {
it('requires new_day_id; order_index optional/nullable', () => {
expect(assignmentMoveRequestSchema.safeParse({ new_day_id: 4 }).success).toBe(true);
expect(assignmentMoveRequestSchema.safeParse({ new_day_id: 4, order_index: 0 }).success).toBe(true);
// The client api sends `order_index: null` when no insert position is given.
expect(assignmentMoveRequestSchema.safeParse({ new_day_id: 4, order_index: null }).success).toBe(true);
expect(assignmentMoveRequestSchema.safeParse({}).success).toBe(false);
});
});
describe('assignmentTransportRequestSchema', () => {
it('accepts a mode, an explicit null, and an absent key (legacy `?? null`)', () => {
expect(assignmentTransportRequestSchema.safeParse({ transport_mode: 'cycling' }).success).toBe(true);
expect(assignmentTransportRequestSchema.safeParse({ transport_mode: null }).success).toBe(true);
expect(assignmentTransportRequestSchema.safeParse({}).success).toBe(true);
expect(assignmentTransportRequestSchema.safeParse({ transport_mode: 5 }).success).toBe(false);
});
});
describe('assignmentParticipantsRequestSchema', () => {
it('requires a numeric user_ids array', () => {
expect(assignmentParticipantsRequestSchema.safeParse({ user_ids: [1, 2] }).success).toBe(true);
+11 -6
View File
@@ -7,10 +7,11 @@ import { z } from 'zod';
* endpoints under /api/trips/:tripId/days/:dayId/assignments and
* /api/trips/:tripId/assignments/:id/*.
*
* Trip-scoped; mutations use the 'day_edit' permission. The legacy route
* (server/src/routes/assignments.ts, mounted on /api) wraps assignmentService.
* Assignment rows carry joined place data and are kept open in responses; the
* request schemas + the bespoke 404/400 controller messages pin the rest.
* Trip-scoped; mutations use the 'day_edit' permission. The server side is the
* DI-native AssignmentsService (server/src/nest/assignments/), validated via
* the createZodDto wrappers in assignments.dto.ts. Assignment rows carry
* joined place data and are kept open in responses; the request schemas + the
* bespoke 404 controller messages pin the rest.
*/
/**
@@ -61,7 +62,9 @@ export type AssignmentReorderRequest = z.infer<typeof assignmentReorderRequestSc
export const assignmentMoveRequestSchema = z.object({
new_day_id: z.union([z.number(), z.string()]),
order_index: z.number().optional(),
// The client api types this `number | null` (assignmentsApi.move) and the
// legacy route accepted null (`orderIndex || 0`) — keep null on the wire.
order_index: z.number().nullable().optional(),
});
export type AssignmentMoveRequest = z.infer<typeof assignmentMoveRequestSchema>;
@@ -73,7 +76,9 @@ export type AssignmentTimeRequest = z.infer<typeof assignmentTimeRequestSchema>;
/** Set the leg's travel mode (a RouteProfileKey, or null to inherit the day default). */
export const assignmentTransportRequestSchema = z.object({
transport_mode: z.string().nullable(),
// The legacy route read `body.transport_mode ?? null`, so an absent key
// behaves like an explicit null — keep it optional on the wire.
transport_mode: z.string().nullable().optional(),
});
export type AssignmentTransportRequest = z.infer<typeof assignmentTransportRequestSchema>;