mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 18:54:17 +00:00
feat(trips): support custom tags and MCP tag management
This commit is contained in:
@@ -54,6 +54,7 @@ export class Trips {
|
||||
user_id!: number;
|
||||
title!: string;
|
||||
description?: string | null;
|
||||
tags_json?: string | null;
|
||||
start_date?: string | null;
|
||||
end_date?: string | null;
|
||||
currency?: string | null = 'EUR';
|
||||
@@ -135,6 +136,7 @@ export const TripsSchema = defineEntity({
|
||||
user_id: p.integer().persist(false).index('idx_trips_user_id'),
|
||||
title: p.text(),
|
||||
description: p.text().nullable(),
|
||||
tags_json: p.text().nullable(),
|
||||
start_date: p.text().nullable(),
|
||||
end_date: p.text().nullable(),
|
||||
currency: p.text().nullable(),
|
||||
|
||||
@@ -18,4 +18,5 @@ export interface TripsTable {
|
||||
updated_at: InsertOptional<string | null>;
|
||||
reminder_sent_for: InsertOptional<string | null>;
|
||||
costs_mode: InsertOptional<string>;
|
||||
tags_json: InsertOptional<string | null>;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
import { engineDdl } from '../migration-utils';
|
||||
|
||||
export class Migration20200101046650_trips_can_carry_custom_tags extends Migration {
|
||||
override name = 'Migration20200101046650_trips_can_carry_custom_tags';
|
||||
|
||||
override up(): void {
|
||||
engineDdl(this, {
|
||||
sqlite: ['ALTER TABLE trips ADD COLUMN tags_json TEXT'],
|
||||
postgres: ['ALTER TABLE "trips" ADD COLUMN "tags_json" text'],
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { QueryFlag } from '@mikro-orm/core';
|
||||
import { tripTagNamesSchema, uniqueTripTags } from '@trek/shared';
|
||||
import type { TTripsCostsMode, Trips } from '../entities/Trips.entity';
|
||||
import { coalesceParam, currentTimestamp, ilike, nowDateOffset } from '../dialect/sql-functions';
|
||||
import type { AssertRowKeys } from './_shared/rows';
|
||||
@@ -22,11 +23,12 @@ export interface TripAccess {
|
||||
* SERVICE (TR-B's ruling), so this repository method must hand the column
|
||||
* back intact for that strip to have something to delete.
|
||||
*/
|
||||
export interface TripRawRow {
|
||||
interface TripDatabaseRow {
|
||||
id: number;
|
||||
user_id: number;
|
||||
title: string;
|
||||
description: string | null;
|
||||
tags_json: string | null;
|
||||
start_date: string | null;
|
||||
end_date: string | null;
|
||||
currency: string | null;
|
||||
@@ -40,7 +42,28 @@ export interface TripRawRow {
|
||||
costs_mode: TTripsCostsMode;
|
||||
}
|
||||
|
||||
const _tripRawRowKeys: AssertRowKeys<TripRawRow, Trips> = true;
|
||||
const _tripRawRowKeys: AssertRowKeys<TripDatabaseRow, Trips> = true;
|
||||
|
||||
export type TripRawRow = Omit<TripDatabaseRow, 'tags_json'> & { tags?: string[] };
|
||||
|
||||
function decodeTags<T extends { tags_json: string | null }>({ tags_json, ...row }: T) {
|
||||
let tags: string[] = [];
|
||||
if (tags_json) {
|
||||
try {
|
||||
const parsed = tripTagNamesSchema.safeParse(JSON.parse(tags_json));
|
||||
if (parsed.success) tags = parsed.data;
|
||||
} catch {
|
||||
// Older import tools may have written invalid JSON. Keep the trip readable.
|
||||
}
|
||||
}
|
||||
return { ...row, tags };
|
||||
}
|
||||
|
||||
function encodeTags(tags: string[] | undefined): { tags_json?: string | null } {
|
||||
if (tags === undefined) return {};
|
||||
const unique = uniqueTripTags(tags);
|
||||
return { tags_json: unique.length ? JSON.stringify(unique) : null };
|
||||
}
|
||||
|
||||
/**
|
||||
* `TRIP_SELECT`'s output row (inventory §11a) — every `TripRawRow` column
|
||||
@@ -350,8 +373,8 @@ export class TripsRepository extends TrekRepository<Trips> {
|
||||
const row = await this.qb('t')
|
||||
.select(['t.*'])
|
||||
.where('t.id = ?', [id])
|
||||
.execute<TripRawRow | undefined>('get', false);
|
||||
return row ?? null;
|
||||
.execute<TripDatabaseRow | undefined>('get', false);
|
||||
return row ? decodeTags(row) : null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -405,7 +428,7 @@ export class TripsRepository extends TrekRepository<Trips> {
|
||||
const id = toRowId(trip_id);
|
||||
if (id === null) return undefined;
|
||||
const row = await this.tripSelectQuery(user_id).where('t.id', '=', id).executeTakeFirst();
|
||||
return row as TripSelectRow | undefined;
|
||||
return row ? decodeTags(row) as TripSelectRow : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -432,7 +455,7 @@ export class TripsRepository extends TrekRepository<Trips> {
|
||||
.where('t.id', '=', id)
|
||||
.where((eb) => tripAccessExpr(eb, 't.user_id', 'm.user_id', user_id))
|
||||
.executeTakeFirst();
|
||||
return row as TripSelectRow | undefined;
|
||||
return row ? decodeTags(row) as TripSelectRow : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -447,7 +470,7 @@ export class TripsRepository extends TrekRepository<Trips> {
|
||||
.where((eb) => tripAccessExpr(eb, 't.user_id', 'm.user_id', user_id));
|
||||
if (archived !== null) query = query.where('t.is_archived', '=', archived);
|
||||
const rows = await query.orderBy('t.created_at', 'desc').execute();
|
||||
return rows as TripSelectRow[];
|
||||
return rows.map(decodeTags) as TripSelectRow[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -525,6 +548,7 @@ export class TripsRepository extends TrekRepository<Trips> {
|
||||
user_id: number;
|
||||
title: string;
|
||||
description: string | null;
|
||||
tags?: string[];
|
||||
start_date: string | null;
|
||||
end_date: string | null;
|
||||
currency: string;
|
||||
@@ -534,6 +558,7 @@ export class TripsRepository extends TrekRepository<Trips> {
|
||||
user: input.user_id,
|
||||
title: input.title,
|
||||
description: input.description,
|
||||
...encodeTags(input.tags),
|
||||
start_date: input.start_date,
|
||||
end_date: input.end_date,
|
||||
currency: input.currency,
|
||||
@@ -562,6 +587,7 @@ export class TripsRepository extends TrekRepository<Trips> {
|
||||
user_id: number;
|
||||
title: string;
|
||||
description: string | null;
|
||||
tags?: string[];
|
||||
start_date: string | null;
|
||||
end_date: string | null;
|
||||
currency: string | null;
|
||||
@@ -573,6 +599,7 @@ export class TripsRepository extends TrekRepository<Trips> {
|
||||
user: input.user_id,
|
||||
title: input.title,
|
||||
description: input.description,
|
||||
...encodeTags(input.tags),
|
||||
start_date: input.start_date,
|
||||
end_date: input.end_date,
|
||||
currency: input.currency,
|
||||
@@ -605,11 +632,12 @@ export class TripsRepository extends TrekRepository<Trips> {
|
||||
*/
|
||||
async updateTripRow(
|
||||
id: number,
|
||||
data: Pick<TripRawRow, 'title' | 'description' | 'start_date' | 'end_date' | 'is_archived' | 'cover_image' | 'costs_mode'>
|
||||
& { currency: string; reminder_days: number },
|
||||
data: Partial<Pick<TripRawRow, 'title' | 'description' | 'start_date' | 'end_date' | 'is_archived' | 'cover_image' | 'costs_mode'>
|
||||
& { currency: string; reminder_days: number; tags: string[] }>,
|
||||
): Promise<void> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
await this.nativeUpdate({ id }, { ...data, updated_at: currentTimestamp(platform) });
|
||||
const { tags, ...fields } = data;
|
||||
await this.nativeUpdate({ id }, { ...fields, ...encodeTags(tags), updated_at: currentTimestamp(platform) });
|
||||
}
|
||||
|
||||
/** TP35 (`trips.service.ts::updateCoverImage`) — `UPDATE trips SET cover_image=?, updated_at=CURRENT_TIMESTAMP WHERE id=?`. */
|
||||
@@ -910,10 +938,11 @@ export class TripsRepository extends TrekRepository<Trips> {
|
||||
* the join can never fan a `t` row out to more than one result row.
|
||||
*/
|
||||
async listOwnedOrMember(user_id: number): Promise<TripRawRow[]> {
|
||||
return await this.accessibleTripsQuery(user_id)
|
||||
const rows = await this.accessibleTripsQuery(user_id)
|
||||
.select(['t.*'])
|
||||
.orderBy({ 't.start_date': 'desc' })
|
||||
.execute<TripRawRow[]>('all', false);
|
||||
.execute<TripDatabaseRow[]>('all', false);
|
||||
return rows.map(decodeTags);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user