mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
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:
@@ -3,6 +3,7 @@ import { TagsRepository } from '../repositories/Tags.repository';
|
||||
import { DbTimestampType } from '../types';
|
||||
import { CollectionPlaces } from './CollectionPlaces.entity';
|
||||
import { Places } from './Places.entity';
|
||||
import { Trips } from './Trips.entity';
|
||||
import { Users } from './Users.entity';
|
||||
|
||||
export class Tags {
|
||||
@@ -13,6 +14,8 @@ export class Tags {
|
||||
name!: string;
|
||||
color?: string | null = '#10b981';
|
||||
created_at?: string | null;
|
||||
trip?: Ref<Trips> | null;
|
||||
trip_id?: number | null;
|
||||
collection_place_tags_inverse = new Collection<CollectionPlaces>(this);
|
||||
place_tags_inverse = new Collection<Places>(this);
|
||||
}
|
||||
@@ -27,6 +30,8 @@ export const TagsSchema = defineEntity({
|
||||
name: p.text(),
|
||||
color: p.text().nullable(),
|
||||
created_at: p.type(DbTimestampType).nullable().defaultRaw(`CURRENT_TIMESTAMP`),
|
||||
trip: () => p.manyToOne(Trips).ref().deleteRule('cascade').nullable().hidden().index('idx_tags_trip_id'),
|
||||
trip_id: p.integer().nullable().persist(false).index('idx_tags_trip_id'),
|
||||
collection_place_tags_inverse: () => p.manyToMany(CollectionPlaces).mappedBy('collection_place_tags').hidden(),
|
||||
place_tags_inverse: () => p.manyToMany(Places).mappedBy('place_tags').hidden(),
|
||||
},
|
||||
|
||||
@@ -31,6 +31,7 @@ import { Reservations } from './Reservations.entity';
|
||||
import { RoadtripDayBoundaries } from './RoadtripDayBoundaries.entity';
|
||||
import { RoadtripPreferences } from './RoadtripPreferences.entity';
|
||||
import { ShareTokens } from './ShareTokens.entity';
|
||||
import { Tags } from './Tags.entity';
|
||||
import { TodoCategoryAssignees } from './TodoCategoryAssignees.entity';
|
||||
import { TodoItems } from './TodoItems.entity';
|
||||
import { TourDrafts } from './TourDrafts.entity';
|
||||
@@ -93,6 +94,7 @@ export class Trips {
|
||||
roadtrip_day_boundaries_collection = new Collection<RoadtripDayBoundaries>(this);
|
||||
roadtrip_preferences_collection = new Collection<RoadtripPreferences>(this);
|
||||
share_tokens_collection = new Collection<ShareTokens>(this);
|
||||
tags_collection = new Collection<Tags>(this);
|
||||
todo_category_assignees_collection = new Collection<TodoCategoryAssignees>(this);
|
||||
todo_items_collection = new Collection<TodoItems>(this);
|
||||
tour_drafts_collection = new Collection<TourDrafts>(this);
|
||||
@@ -171,6 +173,7 @@ export const TripsSchema = defineEntity({
|
||||
roadtrip_day_boundaries_collection: () => p.oneToMany(RoadtripDayBoundaries).mappedBy('trip').hidden(),
|
||||
roadtrip_preferences_collection: () => p.oneToMany(RoadtripPreferences).mappedBy('trip').hidden(),
|
||||
share_tokens_collection: () => p.oneToMany(ShareTokens).mappedBy('trip').hidden(),
|
||||
tags_collection: () => p.oneToMany(Tags).mappedBy('trip').hidden(),
|
||||
todo_category_assignees_collection: () => p.oneToMany(TodoCategoryAssignees).mappedBy('trip').hidden(),
|
||||
todo_items_collection: () => p.oneToMany(TodoItems).mappedBy('trip').hidden(),
|
||||
tour_drafts_collection: () => p.oneToMany(TourDrafts).mappedBy('trip').hidden(),
|
||||
|
||||
@@ -8,4 +8,5 @@ export interface TagsTable {
|
||||
name: string;
|
||||
color: InsertOptional<string | null>;
|
||||
created_at: InsertOptional<string | null>;
|
||||
trip_id: InsertOptional<number | null>;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { addColumnIfMissing } from '../migration-utils';
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* A tag can belong to a trip. Every tag used to be one person's: a place of a
|
||||
* shared trip carried labels only their owner could list, pick again or rename,
|
||||
* and a co-traveller had to make their own tag of the same name.
|
||||
*
|
||||
* A tag with `trip_id` set is the trip's own. Every member reads it, anyone who
|
||||
* may edit the trip's places may change it, and it goes when the trip goes.
|
||||
* `user_id` keeps who created it. The column stays null on every existing row,
|
||||
* which leaves those what they were: personal tags.
|
||||
*/
|
||||
export class Migration20200101046440_a_trip_owns_its_tags extends Migration {
|
||||
override name = 'Migration20200101046440_a_trip_owns_its_tags';
|
||||
|
||||
override async up(): Promise<void> {
|
||||
await addColumnIfMissing(this, 'tags', 'trip_id', 'trip_id INTEGER REFERENCES trips(id) ON DELETE CASCADE');
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS idx_tags_trip_id ON tags (trip_id)');
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,14 @@ import { findOwnedByUser, listForOwner } from './_shared/owned-lookup';
|
||||
import { toRow, type AssertRowKeys } from './_shared/rows';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
|
||||
/** A `tags` row as the API emits it. */
|
||||
/** A `tags` row as the API emits it. `trip_id` is null on a personal tag and names the trip on one of its own. */
|
||||
export interface TagRow {
|
||||
id: number;
|
||||
user_id: number;
|
||||
name: string;
|
||||
color: string | null;
|
||||
created_at: string | null;
|
||||
trip_id: number | null;
|
||||
}
|
||||
|
||||
const _tagRowKeys: AssertRowKeys<TagRow, Tags> = true;
|
||||
@@ -45,6 +46,9 @@ export interface TagForPlaceRow {
|
||||
*/
|
||||
type PlaceTagsKyselyDB = Pick<DB, 'place_tags'>;
|
||||
|
||||
/** `tags` alone, for the reads that name their columns. */
|
||||
type TagsKyselyDB = Pick<DB, 'tags'>;
|
||||
|
||||
/**
|
||||
* `place_tags` joined to `places` for TP46 (`trips.service.ts::copy`'s
|
||||
* place-tags read). The `trip_id` the caller passes stays `number | string`
|
||||
@@ -97,11 +101,18 @@ export class TagsRepository extends TrekRepository<Tags> {
|
||||
* `id` and the `defaultRaw` `created_at` the same way a legacy
|
||||
* INSERT-then-reselect pair did.
|
||||
*/
|
||||
async createTag(input: { user_id: number; name: string; color: string }): Promise<TagRow> {
|
||||
async createTag(input: {
|
||||
user_id: number;
|
||||
name: string;
|
||||
color: string | null;
|
||||
/** The trip the tag is for; left out, it is `user_id`'s personal one. */
|
||||
trip_id?: number;
|
||||
}): Promise<TagRow> {
|
||||
const id = await this.insert({
|
||||
user: input.user_id,
|
||||
name: input.name,
|
||||
color: input.color,
|
||||
...(input.trip_id === undefined ? {} : { trip: input.trip_id }),
|
||||
});
|
||||
const inserted = await this.findOne({ id });
|
||||
if (!inserted) {
|
||||
@@ -193,22 +204,39 @@ export class TagsRepository extends TrekRepository<Tags> {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* PL2 (`PlacesService.tagsOnTrip`) — `` SELECT id, user_id FROM tags WHERE
|
||||
* id IN (${…}) ``, the ownership half of tag scoping: the caller
|
||||
* (`tagsOnTrip`) intersects these rows' `user_id` against the trip's
|
||||
* roster, and an id belonging to nobody on the roster drops silently.
|
||||
* `$in` for the dynamic list (rule 17b).
|
||||
* PL2 (`PlacesService.tagsOnTrip`) — `` SELECT id, user_id, trip_id FROM tags
|
||||
* WHERE id IN (${…}) ``, what decides whether a trip may put a tag on a
|
||||
* place: the caller keeps a trip's own tag for that trip and a personal one
|
||||
* (`trip_id` null) while its `user_id` is on the roster, and every other id
|
||||
* drops silently.
|
||||
*/
|
||||
async findByIds(ids: number[]): Promise<{ id: number; user_id: number }[]> {
|
||||
async findByIds(ids: number[]): Promise<{ id: number; user_id: number; trip_id: number | null }[]> {
|
||||
if (ids.length === 0) return [];
|
||||
// `t.user` (unaliased, not separately joined) resolves to the physical
|
||||
// `user_id` column and comes back keyed as `user_id` on the raw driver
|
||||
// row — `TripsRepository.findAccessible`'s `.select(['t.id', 't.user',
|
||||
// 't.currency'])` establishes the same shape.
|
||||
return this.qb('t')
|
||||
.select(['t.id', 't.user'])
|
||||
.where({ id: { $in: ids } })
|
||||
.execute<{ id: number; user_id: number }[]>('all', false);
|
||||
return await this.kysely<TagsKyselyDB>()
|
||||
.selectFrom('tags')
|
||||
.select(['id', 'user_id', 'trip_id'])
|
||||
.where('id', 'in', ids)
|
||||
.execute();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// A trip's own tags (`trip_id` set): one list for everybody on the trip.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** `SELECT * FROM tags WHERE trip_id = ? ORDER BY name ASC`: the trip's own tags, whoever created them. */
|
||||
async listByTrip(tripId: number): Promise<TagRow[]> {
|
||||
const tags = await this.find({ trip: tripId }, { orderBy: { name: 'asc' } });
|
||||
return tags.map((tag) => toRow(tag) as TagRow);
|
||||
}
|
||||
|
||||
/**
|
||||
* `SELECT * FROM tags WHERE id = ? AND trip_id = ?`: one tag of the trip, or
|
||||
* null for a personal tag and for another trip's, so a write scoped to a trip
|
||||
* cannot reach either.
|
||||
*/
|
||||
async findByIdAndTrip(id: number, tripId: number): Promise<TagRow | null> {
|
||||
const tag = await this.findOne({ id, trip: tripId });
|
||||
return tag ? (toRow(tag) as TagRow) : null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user