feat(places): tags on places, owned by the trip

A place keeps its one category, which decides its marker, and can now carry
any number of tags as extra labels to filter by.

- Tags belong to the trip (tags.trip_id): every member sees the same list and
  whoever may edit the trip's places adds, renames and deletes. Personal tags
  stay what they were.
- The place form picks them on desktop and mobile through one shared hook,
  usePlaceTags, and can add a new tag to the trip on the spot.
- The existing category filter of the places list lists the tags as well.
- Tags show on the place (inspector pills, phone place sheet); the marker is
  untouched.
- MCP: create_place, create_and_assign_place and update_place take `tags`,
  and list/create/update/delete_trip_tag manage a trip's tags.
- /api/trips/:tripId/tags and the tripTags:changed event keep every client
  of the trip in step.
This commit is contained in:
Mariano
2026-11-17 06:35:09 +00:00
committed by Maurice
parent e952586f60
commit 33d074c786
108 changed files with 3634 additions and 118 deletions
+7
View File
@@ -13,6 +13,13 @@ describe('tagSchema', () => {
}).success,
).toBe(true);
});
it('takes the trip a tag belongs to, null for a personal one', () => {
const tag = { id: 1, user_id: 5, name: 'Beach', color: '#10b981' };
expect(tagSchema.safeParse({ ...tag, trip_id: 9 }).success).toBe(true);
expect(tagSchema.safeParse({ ...tag, trip_id: null }).success).toBe(true);
expect(tagSchema.safeParse({ ...tag, trip_id: 'nine' }).success).toBe(false);
});
});
describe('createTagRequestSchema', () => {
+6
View File
@@ -11,11 +11,17 @@ import { z } from 'zod';
*
* The bespoke 400 ("Tag name is required") and 404 ("Tag not found") messages are
* reproduced in the controller so the bodies stay byte-identical.
*
* A tag can also belong to a trip (`trip_id` set, trip-tag.schema.ts). Those never
* show on /api/tags: they are the trip's, read and written under
* /api/trips/:tripId/tags.
*/
export const tagSchema = z.object({
id: z.number(),
user_id: z.number(),
/** The trip the tag belongs to; null for a personal one. Absent on the compact rows a place list carries. */
trip_id: z.number().nullable().optional(),
name: z.string(),
color: z.string(),
created_at: z.string().optional(),
+61
View File
@@ -0,0 +1,61 @@
import {
TRIP_TAG_NAME_MAX,
tripTagCreateRequestSchema,
tripTagListResponseSchema,
tripTagResponseSchema,
tripTagUpdateRequestSchema,
} from './trip-tag.schema';
import { describe, it, expect } from 'vitest';
const tag = { id: 4, user_id: 1, trip_id: 9, name: 'Monument', color: '#10b981', created_at: '2026-11-16 09:44:00' };
describe('tripTagCreateRequestSchema', () => {
it('trims the name and takes it without a colour', () => {
expect(tripTagCreateRequestSchema.parse({ name: ' Monument ' })).toEqual({ name: 'Monument' });
});
it('refuses a name that is empty once trimmed, or longer than the limit', () => {
expect(tripTagCreateRequestSchema.safeParse({}).success).toBe(false);
expect(tripTagCreateRequestSchema.safeParse({ name: ' ' }).success).toBe(false);
expect(tripTagCreateRequestSchema.safeParse({ name: 'x'.repeat(TRIP_TAG_NAME_MAX) }).success).toBe(true);
expect(tripTagCreateRequestSchema.safeParse({ name: 'x'.repeat(TRIP_TAG_NAME_MAX + 1) }).success).toBe(false);
});
it('takes a hex colour and nothing else as one', () => {
expect(tripTagCreateRequestSchema.safeParse({ name: 'Zone', color: '#0ea5e9' }).success).toBe(true);
expect(tripTagCreateRequestSchema.safeParse({ name: 'Zone', color: '#abc' }).success).toBe(true);
expect(tripTagCreateRequestSchema.safeParse({ name: 'Zone', color: 'red' }).success).toBe(false);
expect(tripTagCreateRequestSchema.safeParse({ name: 'Zone', color: 'url(x)' }).success).toBe(false);
});
it('drops keys the contract does not name', () => {
expect(tripTagCreateRequestSchema.parse({ name: 'Zone', trip_id: 3, user_id: 7 })).toEqual({ name: 'Zone' });
});
});
describe('tripTagUpdateRequestSchema', () => {
it('allows either field to be left out', () => {
expect(tripTagUpdateRequestSchema.parse({})).toEqual({});
expect(tripTagUpdateRequestSchema.parse({ color: '#f59e0b' })).toEqual({ color: '#f59e0b' });
expect(tripTagUpdateRequestSchema.parse({ name: ' Activity ' })).toEqual({ name: 'Activity' });
});
it('refuses an empty name and a colour that is not hex', () => {
expect(tripTagUpdateRequestSchema.safeParse({ name: '' }).success).toBe(false);
expect(tripTagUpdateRequestSchema.safeParse({ color: 'green' }).success).toBe(false);
});
});
describe('the trip tag responses', () => {
it('describe a tag row with its trip', () => {
expect(tripTagResponseSchema.safeParse({ tag }).success).toBe(true);
expect(tripTagListResponseSchema.safeParse({ tags: [tag, { ...tag, id: 5, name: 'Zone' }] }).success).toBe(true);
});
it('fail on a row without a name', () => {
expect(tripTagResponseSchema.safeParse({ tag: { id: 4, user_id: 1, trip_id: 9, color: '#10b981' } }).success).toBe(
false,
);
});
});
+38
View File
@@ -0,0 +1,38 @@
import { hexColorSchema } from '../place/place.schema';
import { tagSchema } from './tag.schema';
import { z } from 'zod';
/**
* Trip tag API contract, the single source of truth for /api/trips/:tripId/tags.
*
* A trip tag is a row of `tags` with `trip_id` set. It belongs to the trip rather
* than to one person, so every member reads the same list and anyone who may edit
* the trip's places (`place_edit`) may add to it, rename and delete. `user_id` keeps
* who created it. A name is unique within its trip, whatever its case.
*/
/** The longest name a trip tag takes. */
export const TRIP_TAG_NAME_MAX = 50;
/** A trip tag's name as it is stored: trimmed, never empty. */
export const tripTagNameSchema = z.string().trim().min(1).max(TRIP_TAG_NAME_MAX);
export const tripTagCreateRequestSchema = z.object({
name: tripTagNameSchema,
color: hexColorSchema.optional(),
});
export type TripTagCreateRequest = z.infer<typeof tripTagCreateRequestSchema>;
/** Both fields optional: one that is left out keeps its stored value. */
export const tripTagUpdateRequestSchema = z.object({
name: tripTagNameSchema.optional(),
color: hexColorSchema.optional(),
});
export type TripTagUpdateRequest = z.infer<typeof tripTagUpdateRequestSchema>;
export const tripTagResponseSchema = z.object({ tag: tagSchema });
export type TripTagResponse = z.infer<typeof tripTagResponseSchema>;
export const tripTagListResponseSchema = z.object({ tags: z.array(tagSchema) });
export type TripTagListResponse = z.infer<typeof tripTagListResponseSchema>;