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
+4
View File
@@ -191,6 +191,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'نباتي صرف',
'places.details.fact.internetAccess': 'واي فاي',
'places.categoryCreateError': 'فشل إنشاء الفئة',
'places.tags': 'الوسوم',
'places.newTag': 'وسم جديد',
'places.addTag': 'إضافة وسم',
'places.tagCreateError': 'فشل إنشاء الوسم',
'places.nameRequired': 'يرجى إدخال اسم',
'places.saveError': 'فشل الحفظ',
'places.createExpenseHint': 'يحفظ المكان ثم يفتح محرر التكاليف.',
+4
View File
@@ -171,6 +171,10 @@ const places: TranslationStrings = {
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Kateqoriya yaratmaq mümkün olmadı',
'places.tags': 'Etiketlər',
'places.newTag': 'Yeni etiket',
'places.addTag': 'Etiket əlavə et',
'places.tagCreateError': 'Etiket yaratmaq mümkün olmadı',
'places.nameRequired': 'Ad daxil edin',
'places.saveError': 'Yadda saxlamaq mümkün olmadı',
'places.createExpenseHint': 'Məkanı yadda saxlayır, sonra Xərclər redaktorunu açır.',
+4
View File
@@ -159,6 +159,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegano',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Falha ao criar categoria',
'places.tags': 'Etiquetas',
'places.newTag': 'Nova etiqueta',
'places.addTag': 'Adicionar etiqueta',
'places.tagCreateError': 'Falha ao criar etiqueta',
'places.nameRequired': 'Digite um nome',
'places.saveError': 'Falha ao salvar',
'places.createExpenseHint': 'Salva o local e depois abre o editor de despesas.',
+4
View File
@@ -157,6 +157,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegà',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': "No s'ha pogut crear la categoria",
'places.tags': 'Etiquetes',
'places.newTag': 'Etiqueta nova',
'places.addTag': 'Afegeix etiqueta',
'places.tagCreateError': "No s'ha pogut crear l'etiqueta",
'places.nameRequired': 'Introdueix un nom',
'places.saveError': "No s'ha pogut desar",
'places.createExpenseHint': 'Desa el lloc i després obre l’editor de despeses.',
+4
View File
@@ -167,6 +167,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Veganské',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Nepodařilo se vytvořit kategorii',
'places.tags': 'Štítky',
'places.newTag': 'Nový štítek',
'places.addTag': 'Přidat štítek',
'places.tagCreateError': 'Nepodařilo se vytvořit štítek',
'places.nameRequired': 'Prosím zadejte název',
'places.saveError': 'Uložení se nezdařilo',
'places.createExpenseHint': 'Uloží místo a poté otevře editor nákladů.',
+4
View File
@@ -160,6 +160,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegan', // same-as-en
'places.details.fact.internetAccess': 'WLAN',
'places.categoryCreateError': 'Fehler beim Erstellen der Kategorie',
'places.tags': 'Tags', // same-as-en
'places.newTag': 'Neuer Tag',
'places.addTag': 'Tag hinzufügen',
'places.tagCreateError': 'Fehler beim Erstellen des Tags',
'places.nameRequired': 'Bitte einen Namen eingeben',
'places.saveError': 'Fehler beim Speichern',
'places.createExpenseHint': 'Speichert den Ort und öffnet dann den Kosten-Editor.',
+4
View File
@@ -166,6 +166,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegan',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Failed to create category',
'places.tags': 'Tags',
'places.newTag': 'New tag',
'places.addTag': 'Add tag',
'places.tagCreateError': 'Failed to create tag',
'places.nameRequired': 'Please enter a name',
'places.saveError': 'Failed to save',
'places.createExpenseHint': 'Saves the place, then opens the Costs editor.',
+4
View File
@@ -160,6 +160,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegano',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'No se pudo crear la categoría',
'places.tags': 'Etiquetas',
'places.newTag': 'Nueva etiqueta',
'places.addTag': 'Añadir etiqueta',
'places.tagCreateError': 'No se pudo crear la etiqueta',
'places.nameRequired': 'Introduce un nombre',
'places.saveError': 'No se pudo guardar',
'places.createExpenseHint': 'Guarda el lugar y luego abre el editor de gastos.',
+4
View File
@@ -160,6 +160,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegantoit',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Kategooria loomine ebaõnnestus',
'places.tags': 'Sildid',
'places.newTag': 'Uus silt',
'places.addTag': 'Lisa silt',
'places.tagCreateError': 'Sildi loomine ebaõnnestus',
'places.nameRequired': 'Sisesta nimi',
'places.saveError': 'Salvestamine ebaõnnestus',
'places.createExpenseHint': 'Salvestab koha ja avab seejärel kulude muutmise vaate.',
+4
View File
@@ -160,6 +160,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Végan',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Impossible de créer la catégorie',
'places.tags': 'Étiquettes',
'places.newTag': 'Nouvelle étiquette',
'places.addTag': 'Ajouter une étiquette',
'places.tagCreateError': "Impossible de créer l'étiquette",
'places.nameRequired': 'Veuillez saisir un nom',
'places.saveError': "Échec de l'enregistrement",
'places.createExpenseHint': 'Enregistre le lieu, puis ouvre l’éditeur de dépenses.',
+4
View File
@@ -161,6 +161,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegan',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Αποτυχία δημιουργίας κατηγορίας',
'places.tags': 'Ετικέτες',
'places.newTag': 'Νέα ετικέτα',
'places.addTag': 'Προσθήκη ετικέτας',
'places.tagCreateError': 'Αποτυχία δημιουργίας ετικέτας',
'places.nameRequired': 'Παρακαλώ εισαγάγετε ένα όνομα',
'places.saveError': 'Αποτυχία αποθήκευσης',
'places.createExpenseHint': 'Αποθηκεύει την τοποθεσία και ανοίγει τον επεξεργαστή εξόδων.',
+4
View File
@@ -166,6 +166,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'טבעוני',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'יצירת הקטגוריה נכשלה',
'places.tags': 'תגיות',
'places.newTag': 'תגית חדשה',
'places.addTag': 'הוספת תגית',
'places.tagCreateError': 'יצירת התגית נכשלה',
'places.nameRequired': 'הזינו שם',
'places.saveError': 'השמירה נכשלה',
'places.createExpenseHint': 'שומר את המקום ואז פותח את עורך ההוצאות.',
+4
View File
@@ -160,6 +160,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegán',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Nem sikerült létrehozni a kategóriát',
'places.tags': 'Címkék',
'places.newTag': 'Új címke',
'places.addTag': 'Címke hozzáadása',
'places.tagCreateError': 'Nem sikerült létrehozni a címkét',
'places.nameRequired': 'Kérjük, adj meg egy nevet',
'places.saveError': 'Nem sikerült menteni',
'places.createExpenseHint': 'Menti a helyet, majd megnyitja a költségszerkesztőt.',
+4
View File
@@ -150,6 +150,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegan', // same-as-en
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Gagal membuat kategori',
'places.tags': 'Tag',
'places.newTag': 'Tag baru',
'places.addTag': 'Tambah tag',
'places.tagCreateError': 'Gagal membuat tag',
'places.nameRequired': 'Harap masukkan nama',
'places.saveError': 'Gagal menyimpan',
'places.createExpenseHint': 'Menyimpan tempat, lalu membuka editor biaya.',
+4
View File
@@ -159,6 +159,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegano',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Impossibile creare la categoria',
'places.tags': 'Tag',
'places.newTag': 'Nuovo tag',
'places.addTag': 'Aggiungi tag',
'places.tagCreateError': 'Impossibile creare il tag',
'places.nameRequired': 'Inserisci un nome',
'places.saveError': 'Impossibile salvare',
'places.createExpenseHint': 'Salva il luogo e poi apre l’editor dei costi.',
+4
View File
@@ -151,6 +151,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'ヴィーガン',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'カテゴリの作成に失敗しました',
'places.tags': 'タグ',
'places.newTag': '新しいタグ',
'places.addTag': 'タグを追加',
'places.tagCreateError': 'タグの作成に失敗しました',
'places.nameRequired': '名前を入力してください',
'places.saveError': '保存に失敗しました',
'places.createExpenseHint': '場所を保存してから費用エディタを開きます。',
+4
View File
@@ -150,6 +150,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': '비건',
'places.details.fact.internetAccess': '와이파이',
'places.categoryCreateError': '카테고리 생성 실패',
'places.tags': '태그',
'places.newTag': '새 태그',
'places.addTag': '태그 추가',
'places.tagCreateError': '태그 생성 실패',
'places.nameRequired': '이름을 입력하세요',
'places.saveError': '저장 실패',
'places.createExpenseHint': '장소를 저장한 뒤 비용 편집기를 엽니다.',
+4
View File
@@ -159,6 +159,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Veganistisch',
'places.details.fact.internetAccess': 'Wifi',
'places.categoryCreateError': 'Categorie aanmaken mislukt',
'places.tags': 'Tags', // same-as-en
'places.newTag': 'Nieuwe tag',
'places.addTag': 'Tag toevoegen',
'places.tagCreateError': 'Tag aanmaken mislukt',
'places.nameRequired': 'Voer een naam in',
'places.saveError': 'Opslaan mislukt',
'places.createExpenseHint': 'Slaat de plaats op en opent daarna de kosten-editor.',
+4
View File
@@ -159,6 +159,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Wegańskie',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Nie udało się utworzyć kategorii',
'places.tags': 'Tagi',
'places.newTag': 'Nowy tag',
'places.addTag': 'Dodaj tag',
'places.tagCreateError': 'Nie udało się utworzyć tagu',
'places.nameRequired': 'Proszę podać nazwę',
'places.saveError': 'Nie udało się zapisać',
'places.createExpenseHint': 'Zapisuje miejsce i otwiera edytor kosztów.',
+4
View File
@@ -175,6 +175,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Веганское',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Не удалось создать категорию',
'places.tags': 'Теги',
'places.newTag': 'Новый тег',
'places.addTag': 'Добавить тег',
'places.tagCreateError': 'Не удалось создать тег',
'places.nameRequired': 'Введите название',
'places.saveError': 'Ошибка сохранения',
'places.createExpenseHint': 'Сохраняет место и открывает редактор расходов.',
+4
View File
@@ -168,6 +168,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegánske',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Nepodarilo sa vytvoriť kategóriu',
'places.tags': 'Štítky',
'places.newTag': 'Nový štítok',
'places.addTag': 'Pridať štítok',
'places.tagCreateError': 'Nepodarilo sa vytvoriť štítok',
'places.nameRequired': 'Zadajte prosím názov',
'places.saveError': 'Uloženie zlyhalo',
'places.createExpenseHint': 'Uloží miesto a potom otvorí editor nákladov.',
+4
View File
@@ -159,6 +159,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Veganskt',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Det gick inte att skapa kategorin',
'places.tags': 'Taggar',
'places.newTag': 'Ny tagg',
'places.addTag': 'Lägg till tagg',
'places.tagCreateError': 'Det gick inte att skapa taggen',
'places.nameRequired': 'Ange ett namn',
'places.saveError': 'Det gick inte att spara',
'places.createExpenseHint': 'Sparar platsen och öppnar sedan kostnadsredigeraren.',
+4
View File
@@ -142,6 +142,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'วีแกน',
'places.details.fact.internetAccess': 'อินเตอร์เน็ตไร้สาย',
'places.categoryCreateError': 'ไม่สามารถสร้างหมวดหมู่ได้',
'places.tags': 'แท็ก',
'places.newTag': 'แท็กใหม่',
'places.addTag': 'เพิ่มแท็ก',
'places.tagCreateError': 'ไม่สามารถสร้างแท็กได้',
'places.nameRequired': 'กรุณากรอกชื่อ',
'places.saveError': 'บันทึกไม่สำเร็จ',
'places.createExpenseHint': 'บันทึกสถานที่ จากนั้นเปิดตัวแก้ไขต้นทุน',
+4
View File
@@ -161,6 +161,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Vegan', // same-as-en
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Kategori oluşturulamadı',
'places.tags': 'Etiketler',
'places.newTag': 'Yeni etiket',
'places.addTag': 'Etiket ekle',
'places.tagCreateError': 'Etiket oluşturulamadı',
'places.nameRequired': 'Lütfen bir ad girin',
'places.saveError': 'Kaydedilemedi',
'places.createExpenseHint': 'Yeri kaydeder, sonra masraf düzenleyicisini açar.',
+4
View File
@@ -175,6 +175,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Веганське',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Не вдалося створити категорію',
'places.tags': 'Теги',
'places.newTag': 'Новий тег',
'places.addTag': 'Додати тег',
'places.tagCreateError': 'Не вдалося створити тег',
'places.nameRequired': 'Введіть назву',
'places.saveError': 'Помилка збереження',
'places.createExpenseHint': 'Зберігає місце й відкриває редактор витрат.',
+4
View File
@@ -151,6 +151,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': 'Thuần chay',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': 'Không tạo được danh mục',
'places.tags': 'Thẻ',
'places.newTag': 'Thẻ mới',
'places.addTag': 'Thêm thẻ',
'places.tagCreateError': 'Không tạo được thẻ',
'places.nameRequired': 'Vui lòng nhập tên',
'places.saveError': 'Không lưu được',
'places.createExpenseHint': 'Lưu địa điểm rồi mở trình sửa chi phí.',
+4
View File
@@ -149,6 +149,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': '純素',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': '建立分類失敗',
'places.tags': '標籤',
'places.newTag': '新增標籤',
'places.addTag': '新增標籤',
'places.tagCreateError': '建立標籤失敗',
'places.nameRequired': '請輸入名稱',
'places.saveError': '儲存失敗',
'places.createExpenseHint': '先儲存地點,然後開啟費用編輯器。',
+4
View File
@@ -149,6 +149,10 @@ const places: TranslationStrings = {
'places.details.fact.vegan': '纯素',
'places.details.fact.internetAccess': 'Wi-Fi',
'places.categoryCreateError': '创建分类失败',
'places.tags': '标签',
'places.newTag': '新建标签',
'places.addTag': '添加标签',
'places.tagCreateError': '创建标签失败',
'places.nameRequired': '请输入名称',
'places.saveError': '保存失败',
'places.createExpenseHint': '先保存地点,然后打开费用编辑器。',
+1
View File
@@ -30,6 +30,7 @@ export * from './maps/place-shadow.schema';
export * from './maps/route-usage.schema';
export * from './category/category.schema';
export * from './tag/tag.schema';
export * from './tag/trip-tag.schema';
export * from './notification/notification.schema';
export * from './memories/memories.schema';
export * from './atlas/atlas.schema';
+27
View File
@@ -3,6 +3,7 @@ import {
placeBulkDeleteRequestSchema,
placeImportListRequestSchema,
placeSchema,
placeTagIdsSchema,
placeUpdateRequestSchema,
placeWebsiteSchema,
} from './place.schema';
@@ -128,3 +129,29 @@ describe('placeWebsiteSchema (#2483)', () => {
expect(shape.properties?.website).toEqual({ type: 'string', maxLength: 500 });
});
});
describe('placeTagIdsSchema', () => {
it('SHARED-PLACE-040: takes tag ids, none at all included, and nothing that is not an id', () => {
expect(placeTagIdsSchema.safeParse([3, 8]).success).toBe(true);
// An empty list is how a place loses its tags.
expect(placeTagIdsSchema.safeParse([]).success).toBe(true);
expect(placeTagIdsSchema.safeParse([0]).success).toBe(false);
expect(placeTagIdsSchema.safeParse(['3']).success).toBe(false);
expect(placeTagIdsSchema.safeParse([{ id: 3 }]).success).toBe(false);
});
it('SHARED-PLACE-041: stops at fifty tags on one place', () => {
const ids = Array.from({ length: 51 }, (_, i) => i + 1);
expect(placeTagIdsSchema.safeParse(ids.slice(0, 50)).success).toBe(true);
expect(placeTagIdsSchema.safeParse(ids).success).toBe(false);
});
it('SHARED-PLACE-042: the tool schema an MCP client reads is a plain list of integers', () => {
const shape = z.toJSONSchema(z.object({ tags: placeTagIdsSchema.optional() }), { io: 'input' });
expect(shape.properties?.tags).toMatchObject({
type: 'array',
maxItems: 50,
items: { type: 'integer', exclusiveMinimum: 0 },
});
});
});
+8
View File
@@ -1,3 +1,4 @@
import { idSchema } from '../common/primitives.schema';
import { tagSchema } from '../tag/tag.schema';
import { placeFieldOriginsSchema } from './place-contrib.schema';
import { placeEmailField, placeOpeningHoursField } from './place-hours';
@@ -345,6 +346,13 @@ export const placeImportMapRequestSchema = z.object({
});
export type PlaceImportMapRequest = z.infer<typeof placeImportMapRequestSchema>;
/**
* A place's tags as a write names them: tag ids, and always the whole set, since an
* update replaces what the place had. The REST bodies above are open records and read
* the key as it comes; the MCP place tools take it through this schema.
*/
export const placeTagIdsSchema = z.array(idSchema).max(50);
/** Query filters for the place list. */
export const placeListQuerySchema = z.object({
search: z.string().optional(),
+5 -2
View File
@@ -19,6 +19,7 @@ const FIXTURES: Record<TrekWsEventName, Record<string, unknown>> = {
'place:created': { place: { id: 3, name: 'Louvre' } },
'place:updated': { place: { id: 3, name: 'Louvre' } },
'place:deleted': { placeId: 3 },
'tripTags:changed': {},
'tours:changed': { placeIds: [3] },
'tours:visits': { assignmentId: 9, visits: [{ assignment_id: 9, station_key: 'abcdef', status: 'visited' }] },
'assignment:created': { assignment: { id: 9, day_id: 2 } },
@@ -219,9 +220,11 @@ describe('@trek/shared realtime event registry', () => {
// writes that send them follow.
// 42nd and 43rd user event: mailInbox:changed and mailInbox:settings, the
// inbox of forwarded booking mails and its settings card.
expect(TREK_WS_TRIP_EVENT_NAMES).toHaveLength(80);
// 81st trip event: tripTags:changed, so a tag a member adds to the trip,
// renames or deletes shows the same for everyone on it.
expect(TREK_WS_TRIP_EVENT_NAMES).toHaveLength(81);
expect(TREK_WS_USER_EVENT_NAMES).toHaveLength(43);
expect(TREK_WS_EVENT_NAMES).toHaveLength(123);
expect(TREK_WS_EVENT_NAMES).toHaveLength(124);
});
it('WSEVT-REG-002: every name is domain:action shaped and outside the reserved plugin: namespace', () => {
+3
View File
@@ -65,6 +65,9 @@ export const TREK_WS_EVENTS = {
'place:created': { scope: 'trip', payload: z.object({ place: entity }) },
'place:updated': { scope: 'trip', payload: z.object({ place: entity }) },
'place:deleted': { scope: 'trip', payload: z.object({ placeId: id }) },
// The trip's own tags changed (one added, renamed, recoloured or deleted): every
// client reads the list again and takes the change over into the places it holds.
'tripTags:changed': { scope: 'trip', payload: empty },
// Invalidate the trip's Tours list; affected ids let peers exclude new Tours immediately.
'tours:changed': { scope: 'trip', payload: z.object({ placeIds: idList.optional() }) },
// CW-03: the stations of a city walk visited or skipped on one of its days, all of them as they are now.
+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>;