The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
import { currentTimestampKysely } from '../dialect/kysely-functions';
import type { ShareTokens } from '../entities/ShareTokens.entity';
import type { DB } from '../kysely/db';
import { TrekRepository } from './_shared/trek-repository';
/**
* `share_tokens` — public trip-share links (Plan 3h Task 6). `trip_id` and
* `created_by` are `persist(false)` shadow mirrors of the `trip`/`createdByRef`
* relations (`ShareTokens.entity.ts`), so every READ here goes through
* `this.db_()` (Kysely), never a bare `qb().select(['trip_id', ...])` — the
* `CollectionPlacesRepository`/`DayAssignmentsRepository.assignmentWithPlaceSelect`
* class-docstring trap applies here too. WRITES go through the MikroORM
* entity API (`insert`/`nativeUpdate`/`nativeDelete`), using the entity's own
* relation property names (`trip`/`createdByRef`) for the two FK columns —
* the `DocumentConnectionsRepository.insertConnection` precedent.
*/
export interface ShareTokenRow {
id: number;
trip_id: number;
token: string;
created_by: number;
share_map: number | null;
share_bookings: number | null;
share_packing: number | null;
share_budget: number | null;
share_collab: number | null;
created_at: string | null;
expires_at: string | null;
share_travel_only: number;
share_hide_images: number;
}
type ShareTokensKyselyDB = Pick<DB, 'share_tokens'>;
export class ShareTokensRepository extends TrekRepository<ShareTokens> {
private db_() {
return this.kysely<ShareTokensKyselyDB>();
}
/**
* SH1 (`createOrUpdate`, TX) — `SELECT token FROM share_tokens WHERE
* trip_id = ?`.
*/
async findTokenByTrip(trip_id: number | string): Promise<{ token: string } | undefined> {
return await this.db_()
.selectFrom('share_tokens')
.select('token')
.where('trip_id', '=', trip_id as number)
.executeTakeFirst();
}
/**
* SH2 (`createOrUpdate`, TX, existing row) — `UPDATE share_tokens SET
* share_map=?, share_bookings=?, share_packing=?, share_budget=?,
* share_collab=?, share_travel_only=?, share_hide_images=?, expires_at=?
* WHERE trip_id=?`.
*/
async updateFlagsByTrip(
trip_id: number | string,
data: {
share_map: number;
share_bookings: number;
share_packing: number;
share_budget: number;
share_collab: number;
share_travel_only: number;
share_hide_images: number;
expires_at: string;
},
): Promise<void> {
await this.nativeUpdate({ trip: trip_id as number }, data);
}
/**
* SH3 (`createOrUpdate`, TX, new row) — `INSERT INTO share_tokens
* (trip_id, token, created_by, share_map, share_bookings, share_packing,
* share_budget, share_collab, share_travel_only, share_hide_images,
* expires_at) VALUES (...)`. `token` is
* generated by the caller (`crypto.randomBytes(24)`), passed straight
* through.
*/
async insertNew(data: {
trip_id: number | string;
token: string;
created_by: number;
share_map: number;
share_bookings: number;
share_packing: number;
share_budget: number;
share_collab: number;
share_travel_only: number;
share_hide_images: number;
expires_at: string;
}): Promise<void> {
await this.insert({
trip: data.trip_id as number,
token: data.token,
createdByRef: data.created_by,
share_map: data.share_map,
share_bookings: data.share_bookings,
share_packing: data.share_packing,
share_budget: data.share_budget,
share_collab: data.share_collab,
share_travel_only: data.share_travel_only,
share_hide_images: data.share_hide_images,
expires_at: data.expires_at,
});
}
/** SH4 (`get`) — `SELECT * FROM share_tokens WHERE trip_id = ?`. */
async findRawByTrip(trip_id: number | string): Promise<ShareTokenRow | undefined> {
return await this.db_()
.selectFrom('share_tokens')
.selectAll()
.where('trip_id', '=', trip_id as number)
.executeTakeFirst();
}
/**
* SH5 (`remove`) — `DELETE FROM share_tokens WHERE trip_id = ?`.
* **DISTINCT from `deleteByCreator` (UC6)** — a different predicate on the
* same table, never conflated into one method with an optional parameter.
*/
async deleteByTrip(trip_id: number | string): Promise<void> {
await this.nativeDelete({ trip: trip_id as number });
}
/**
* The `token = ?` and `(expires_at IS NULL OR expires_at > <now>)`
* predicate SH6, SH17 and `platform.routes.ts`'s R1-converted pre-init
* `servePhoto` fallback ALL share — built exactly once here, never
* re-derived at any of the three call sites (R5). `token` is compared
* with a plain `=` — no `LIKE`, no case-folding, ever (R5's own
* "the opposite of rule 18" framing). `currentTimestampKysely` (not a
* hand-written `datetime('now')` literal): SQLite's bare `CURRENT_TIMESTAMP`
* keyword renders in the SAME `YYYY-MM-DD HH:MM:SS` UTC text `datetime('now')`
* does, so reusing the existing helper is byte-equivalent to the legacy
* predicate, not an approximation of it.
*/
private validTokenQuery(token: string) {
const platform = this.getEntityManager().getPlatform();
return this.db_()
.selectFrom('share_tokens')
.where('token', '=', token)
.where((eb) => eb.or([eb('expires_at', 'is', null), eb('expires_at', '>', currentTimestampKysely(platform))]));
}
/** SH6 (`getSharedTripData`) — the full row, valid-token predicate above. */
async findValidByToken(token: string): Promise<ShareTokenRow | undefined> {
return await this.validTokenQuery(token).selectAll().executeTakeFirst();
}
/**
* SH17 (`getSharedPlacePhotoKey`) — `trip_id, share_map,
* share_hide_images` only, SAME valid-token predicate as SH6/R1.
*/
async findTripAndShareMapByToken(
token: string,
): Promise<{ trip_id: number; share_map: number | null; share_hide_images: number } | undefined> {
return await this.validTokenQuery(token).select(['trip_id', 'share_map', 'share_hide_images']).executeTakeFirst();
}
/**
* R1 (`platform.routes.ts`'s pre-init `servePhoto` share-token fallback,
* SV-PLATFORM1) — `trip_id` only, the SAME valid-token predicate SH6/SH17
* use, built once here and reused, not re-derived at the pre-init call
* site.
*/
async findTripIdByToken(token: string): Promise<{ trip_id: number } | undefined> {
return await this.validTokenQuery(token).select(['trip_id']).executeTakeFirst();
}
/**
* UC6 (`user-cleanup.service.ts`'s GDPR erasure) — `DELETE FROM
* share_tokens WHERE created_by = ?`. **DISTINCT from `deleteByTrip`
* (SH5)** — deletes every link a user CREATED, across every trip,
* regardless of who owns those trips, not the links on one trip.
*/
async deleteByCreator(userId: number): Promise<void> {
await this.nativeDelete({ createdByRef: userId });
}
}