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 { PluginGuards } from '../../nest-rpc/plugin-guards.service';
import { BadParams, ForbiddenResource } from '../../nest-rpc/rpc-errors';
import { PluginController, PluginMethod } from '../../nest-rpc/rpc-kit/decorators';
import type { PluginRpcContext } from '../../nest-rpc/rpc-kit/types';
import { schemaMessage, rowIdParam } from '../../nest-rpc/rpc-params';
import { JourneyDomainService } from '../journey/journey-domain.service';
import { RealtimeService } from '../realtime/realtime.service';
import type { OriginWriter } from './place-origins.helpers';
import { PlaceOriginsService } from './place-origins.service';
import { PlacesService } from './places.service';
import type { PlaceCreateInput, PlaceUpdateInput } from './places.service';
import {
placeCreateRequestSchema,
placeImageUrlSchema,
placeUpdateRequestSchema,
placeWebsiteSchema,
} from '@trek/shared';
const PLACE_EDIT_ACTION = 'place_edit';
/** What a plugin writes is the plugin's, never shared as a user's correction. */
const PLUGIN: OriginWriter = { kind: 'fixed', origin: 'plugin' };
/**
* The @trek/shared write schemas carry no string-length caps, so the plugin path
* mirrors the ones the REST controller adds. Without them a plugin could store a
* field the web app refuses with a 400, e.g. a 100k-character place name.
*/
const PLACE_STR_LIMITS: Record<string, number> = { name: 200, description: 2000, address: 500, notes: 2000 };
/**
* The two URL fields the REST controller pins in validateUrlFields. Same reason
* they are checked there rather than in the contract: the write schemas are open
* records, so a plugin could otherwise store a thumbnail or a homepage the web
* app refuses — and both end up somewhere that treats them as a URL.
*/
function capUrls(input: Record<string, unknown>): void {
const image = input.image_url;
if (image !== undefined && image !== null && !placeImageUrlSchema.safeParse(image).success) {
throw new BadParams(
'invalid place: image_url must be an uploaded path, a photo-proxy path, an inline image or an https URL',
);
}
const website = input.website;
if (website !== undefined && website !== null && website !== '') {
const parsed = placeWebsiteSchema.safeParse(website);
if (!parsed.success) throw new BadParams('invalid place: website must be an http or https URL');
// Stored in its parsed form, like the REST route: a bare host gains https.
input.website = parsed.data;
}
}
/**
* The place surface a plugin may reach (#plugins).
*
* Journey mirroring is best-effort on every path that triggers it, in the REST
* controller and the MCP tool alike, so a broken journey link can never turn a
* successful write into an RPC error.
*/
@PluginController()
export class PlacesRpc {
constructor(
private readonly places: PlacesService,
private readonly journey: JourneyDomainService,
private readonly realtime: RealtimeService,
private readonly guards: PluginGuards,
private readonly origins: PlaceOriginsService,
) {}
@PluginMethod('places.create', { permission: 'db:write:places' })
async create(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
const tripId = rowIdParam(params.tripId, 'tripId');
const actor = this.guards.requireActor(ctx, 'place');
const parsed = placeCreateRequestSchema.safeParse(params.input);
if (!parsed.success) throw new BadParams(`invalid place: ${schemaMessage(parsed.error)}`);
this.guards.capStrings(parsed.data as Record<string, unknown>, PLACE_STR_LIMITS);
capUrls(parsed.data as Record<string, unknown>);
await this.guards.requireTripEdit(tripId, actor, PLACE_EDIT_ACTION);
const place = await this.places.create(String(tripId), parsed.data as unknown as PlaceCreateInput);
this.realtime.broadcast(tripId, 'place:created', { place });
await this.origins.record(place.id, null, PLUGIN);
await this.mirrorJourneys(() => this.journey.onPlaceCreated(tripId, place.id));
return place;
}
@PluginMethod('places.update', { permission: 'db:write:places' })
async update(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
const tripId = rowIdParam(params.tripId, 'tripId');
const placeId = rowIdParam(params.placeId, 'placeId');
const actor = this.guards.requireActor(ctx, 'place');
const parsed = placeUpdateRequestSchema.safeParse(params.input);
if (!parsed.success) throw new BadParams(`invalid place: ${schemaMessage(parsed.error)}`);
this.guards.capStrings(parsed.data as Record<string, unknown>, PLACE_STR_LIMITS);
capUrls(parsed.data as Record<string, unknown>);
await this.guards.requireTripEdit(tripId, actor, PLACE_EDIT_ACTION);
// update is async (it may delete a superseded storage object): await it so the
// null-refusal check compares against the resolved place, not an always-truthy
// Promise, and the broadcast below carries the actual place.
const before = await this.origins.valuesBefore(placeId);
const place = await this.places.update(String(tripId), String(placeId), parsed.data as PlaceUpdateInput);
if (place === null) throw new ForbiddenResource(`no place ${placeId} on trip ${tripId}`);
this.realtime.broadcast(tripId, 'place:updated', { place });
if ('id' in place) await this.origins.record(place.id, before, PLUGIN);
await this.mirrorJourneys(() => this.journey.onPlaceUpdated(placeId));
return place;
}
@PluginMethod('places.delete', { permission: 'db:write:places' })
async delete(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
const tripId = rowIdParam(params.tripId, 'tripId');
const placeId = rowIdParam(params.placeId, 'placeId');
const actor = this.guards.requireActor(ctx, 'place');
await this.guards.requireTripEdit(tripId, actor, PLACE_EDIT_ACTION);
// Scope the id to the trip before anything else: onPlaceDeleted keys on the place
// alone, so an id belonging to a foreign trip would detach THAT trip's journey
// entries even though the delete below refuses it.
if (!(await this.places.get(String(tripId), String(placeId)))) {
throw new ForbiddenResource(`no place ${placeId} on trip ${tripId}`);
}
// Ahead of the DELETE, like the REST route and the MCP tool:
// journey_entries.source_place_id is ON DELETE SET NULL, so afterwards the hook
// finds nothing left to detach and the entries linger as orphans.
await this.mirrorJourneys(() => this.journey.onPlaceDeleted(placeId));
// The link is gone once the place is, so read it first (#1298).
const expenseIds = await this.places.linkedExpenseIds(tripId, [placeId]);
// remove is async (it deletes the place's storage object): await it so the
// refusal check sees the resolved answer, not an always-truthy Promise.
const { deleted, cancelled } = await this.places.remove(String(tripId), String(placeId));
if (!deleted) {
throw new ForbiddenResource(`no place ${placeId} on trip ${tripId}`);
}
this.realtime.broadcast(tripId, 'place:deleted', { placeId });
// A night booked at this place went with it, and took its partner booking and
// that booking's expense along. Neither is covered by place:deleted, and an
// expense linked by reservation_id is not one linkedExpenseIds finds.
for (const reservationId of cancelled.reservationIds)
this.realtime.broadcast(tripId, 'reservation:deleted', { reservationId });
for (const itemId of [...expenseIds, ...cancelled.budgetItemIds])
this.realtime.broadcast(tripId, 'budget:deleted', { itemId });
return { deleted: true };
}
/**
* Journey mirroring never fails a write that already succeeded.
*/
private async mirrorJourneys(run: () => Promise<void>): Promise<void> {
try {
await run();
} catch {
/* non-fatal */
}
}
}