mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
b56f335978
On a trip with dates the reorder dialog and the days sheet on a phone now offer two ways to add a day: a day without a date at the end as before, or the calendar day after the last date, named on its button, which extends the trip by one day. The new day goes right behind the last dated day, so the days without a date move one place back with their plans, and no existing day or booking changes its date. The server does it in one transaction behind a new dated flag on the day create contract, used by REST, the create_day tool and the plugin RPC alike. It moves the road trip boundaries of the days behind the new one, and announces the day with day:reordered and the grown trip with trip:updated; an insert at a position announces its trip now as well. The next date and the day limit come from one shared rule, so the button never promises a date the server would not give. On the client the add is online only and runs one at a time, so a double click cannot grow the trip by two days. A wiring test that picked the stay question by its position now finds it by its handler, since the delete question sits in front of it.
92 lines
4.4 KiB
TypeScript
92 lines
4.4 KiB
TypeScript
import { dayCreateRequestSchema, dayUpdateRequestSchema } from '@trek/shared';
|
|
import { PluginController, PluginMethod } from '../plugins/host/rpc-kit/decorators';
|
|
import { PluginGuards } from '../plugins/host/plugin-guards.service';
|
|
import { BadParams, ForbiddenResource } from '../plugins/host/rpc-errors';
|
|
import { num, schemaMessage } from '../plugins/host/rpc-params';
|
|
import type { PluginRpcContext } from '../plugins/host/rpc-kit/types';
|
|
import { RealtimeService } from '../realtime/realtime.service';
|
|
import { DaysService, DayAppendError, type DatedDayAppend, type DaySender } from './days.service';
|
|
import { DayRemovalService, DayDeleteError, type DayRemoval } from './day-removal.service';
|
|
import type { MirrorSender } from '../accommodations/accommodations.service';
|
|
|
|
const DAY_EDIT_ACTION = 'day_edit';
|
|
|
|
/**
|
|
* The day surface a plugin may reach (#plugins). Every write scopes the day to the
|
|
* trip before touching it, so a plugin cannot edit another trip's day by naming its
|
|
* id. The itinerary half lives in assignments/itinerary.rpc.ts, because
|
|
* AssignmentsModule already imports DaysModule and the reverse would be a cycle.
|
|
*/
|
|
@PluginController()
|
|
export class DaysRpc {
|
|
constructor(
|
|
private readonly days: DaysService,
|
|
private readonly realtime: RealtimeService,
|
|
private readonly guards: PluginGuards,
|
|
private readonly removal: DayRemovalService,
|
|
) {}
|
|
|
|
@PluginMethod('days.create', { permission: 'db:write:days' })
|
|
create(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
|
|
const tripId = num(params.tripId, 'tripId');
|
|
const actor = this.guards.requireActor(ctx, 'day');
|
|
const parsed = dayCreateRequestSchema.safeParse(params.input);
|
|
if (!parsed.success) throw new BadParams(`invalid day: ${schemaMessage(parsed.error)}`);
|
|
this.guards.requireTripEdit(tripId, actor, DAY_EDIT_ACTION);
|
|
const input = parsed.data;
|
|
if (input.dated) return this.appendDated(tripId, actor, input.notes);
|
|
const day = this.days.create(tripId, input.date, input.notes);
|
|
this.realtime.broadcast(tripId, 'day:created', { day });
|
|
return day;
|
|
}
|
|
|
|
/** days.create with `dated`: the calendar day after the trip's last date, as on REST and MCP. */
|
|
private appendDated(tripId: number, actor: number, notes: string | undefined): unknown {
|
|
let append: DatedDayAppend;
|
|
try {
|
|
append = this.days.appendDated(tripId, actor, notes);
|
|
} catch (err) {
|
|
if (err instanceof DayAppendError) throw new BadParams(err.message);
|
|
throw err;
|
|
}
|
|
const send: DaySender = (event, payload) => this.realtime.broadcast(tripId, event, payload);
|
|
this.days.announceDatedAppend(append, { all: send, others: send });
|
|
return append.day;
|
|
}
|
|
|
|
@PluginMethod('days.update', { permission: 'db:write:days' })
|
|
update(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
|
|
const tripId = num(params.tripId, 'tripId');
|
|
const dayId = num(params.dayId, 'dayId');
|
|
const actor = this.guards.requireActor(ctx, 'day');
|
|
const parsed = dayUpdateRequestSchema.safeParse(params.input);
|
|
if (!parsed.success) throw new BadParams(`invalid day: ${schemaMessage(parsed.error)}`);
|
|
this.guards.requireTripEdit(tripId, actor, DAY_EDIT_ACTION);
|
|
// getDay scopes the row to the trip before the write touches it.
|
|
const current = this.days.getDay(dayId, tripId);
|
|
if (!current) throw new ForbiddenResource(`no day ${dayId} on trip ${tripId}`);
|
|
const day = this.days.update(dayId, current, parsed.data as { notes?: string; title?: string | null });
|
|
this.realtime.broadcast(tripId, 'day:updated', { day });
|
|
return day;
|
|
}
|
|
|
|
@PluginMethod('days.delete', { permission: 'db:write:days' })
|
|
delete(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
|
|
const tripId = num(params.tripId, 'tripId');
|
|
const dayId = num(params.dayId, 'dayId');
|
|
const actor = this.guards.requireActor(ctx, 'day');
|
|
this.guards.requireTripEdit(tripId, actor, DAY_EDIT_ACTION);
|
|
if (!this.days.getDay(dayId, tripId)) throw new ForbiddenResource(`no day ${dayId} on trip ${tripId}`);
|
|
let removal: DayRemoval;
|
|
try {
|
|
removal = this.removal.remove(tripId, dayId, { userId: actor });
|
|
} catch (err) {
|
|
if (err instanceof DayDeleteError) throw new BadParams(err.message);
|
|
throw err;
|
|
}
|
|
const send: MirrorSender = (event, payload) => this.realtime.broadcast(tripId, event, payload);
|
|
this.removal.announce(tripId, removal, { all: send, others: send });
|
|
return { deleted: true };
|
|
}
|
|
}
|