mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +00:00
fix: receipts, cached places, chat previews and the Google-route tool
Saving an expense twice while its receipt is also attached to a place and a booking hit the new unique index on (file, expense): the row kept from the first save was skipped, the next spare link row was adopted into a duplicate pair, and the constraint threw inside the transaction. The whole edit rolled back with a 500, every time, so that expense could never be changed again. The adopt now skips a link the expense already holds, and a receipt named twice in one save is linked once. The offline place cache was keyed on the place id alone while it is owned and evicted per trip, so two trips over the same city took each other's rows: the second write claimed them, switching the second trip off deleted them for both, and the first trip's fingerprint still matched so nothing ever refilled them. Offline search then answered nothing for a trip whose switch read "on". Each trip keeps its own copies now, and the upgrade clears the fingerprints so the next sync refills what the rebuild dropped. The chat composer minted its object URLs inside a state updater, which React runs twice in development, so one picked image drew two thumbnails and removing one left a preview for a picture that would not be sent. File and URL are one record now and every mutator commits a finished list. import_google_maps_route wrote up to thirty places and their day assignments under trips:write, which does not imply the places:write that create_place is gated on, and it was the one non-admin write tool with no demo gate and the only tool in the repo with no annotations. All three corrected.
This commit is contained in:
@@ -639,14 +639,25 @@ export class BudgetService {
|
||||
if (data.receipt_file_ids !== undefined) {
|
||||
// The ids that survive are left linked rather than dropped and re-added,
|
||||
// so a receipt the request keeps never loses its row for an instant.
|
||||
const keep = new Set(data.receipt_file_ids.map(Number));
|
||||
// Deduplicated: the same id twice would make the second pass adopt a
|
||||
// second spare row into the pair the first one just wrote.
|
||||
const wanted = [...new Set(data.receipt_file_ids.map(Number))];
|
||||
const keep = new Set(wanted);
|
||||
this.unlinkReceipts(id, keep);
|
||||
if (data.receipt_file_ids.length > 0) {
|
||||
if (wanted.length > 0) {
|
||||
const insertLink = this.db.prepare('INSERT OR IGNORE INTO file_links (file_id, budget_item_id) VALUES (?, ?)');
|
||||
const adopt = this.db.prepare('UPDATE file_links SET budget_item_id = ? WHERE id = ?');
|
||||
for (const fid of data.receipt_file_ids) {
|
||||
for (const fid of wanted) {
|
||||
const belongs = this.db.get('SELECT id FROM trip_files WHERE id = ? AND trip_id = ? AND deleted_at IS NULL', fid, tripId);
|
||||
if (!belongs) continue;
|
||||
// Already this item's receipt: nothing to do. A file can carry several
|
||||
// link rows (one per place, one per booking), so on a second save of
|
||||
// the same expense the row kept from last time is skipped by
|
||||
// unlinkReceipts and the next spare would be adopted into a duplicate
|
||||
// (file, item) pair, which the unique index refuses. That threw inside
|
||||
// the transaction and rolled the whole expense edit back, every time.
|
||||
const already = this.db.get('SELECT 1 FROM file_links WHERE file_id = ? AND budget_item_id = ?', fid, id);
|
||||
if (already) continue;
|
||||
// A file already tied to a place or a booking gets the receipt link
|
||||
// written onto that row, because the unique index is per file and
|
||||
// item and a second row for the same pair would be refused anyway.
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { z } from 'zod';
|
||||
import { googleRouteImportSchema, googleRoutePreviewRequestSchema, type GoogleRouteImport } from '@trek/shared';
|
||||
import { McpController, Tool, TOOL_ANNOTATIONS_READONLY, ok, type McpContext } from '../../nest-mcp';
|
||||
import { McpController, Tool, TOOL_ANNOTATIONS_READONLY, TOOL_ANNOTATIONS_NON_IDEMPOTENT, ok, type McpContext } from '../../nest-mcp';
|
||||
import { ADDON_IDS } from '../../addons';
|
||||
import { AddonsService } from '../addons/addons.service';
|
||||
import { addonGate } from '../addons/addon-gate';
|
||||
import { GoogleRouteService } from './google-route.service';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { demoDenied } from '../../mcp/tools/_shared';
|
||||
|
||||
@McpController()
|
||||
export class GoogleRouteMcp {
|
||||
constructor(private readonly routes: GoogleRouteService, readonly addons: AddonsService) {}
|
||||
constructor(private readonly routes: GoogleRouteService, private readonly auth: AuthService, readonly addons: AddonsService) {}
|
||||
@Tool({ name: 'preview_google_maps_route', description: 'Read ordered stops from a Google Maps directions link. No changes are saved. Review geocoded positions and unresolved stops before importing. The exact Google road geometry is not imported.',
|
||||
inputSchema: googleRoutePreviewRequestSchema.shape, annotations: TOOL_ANNOTATIONS_READONLY,
|
||||
access: { group: 'trips', mode: 'read' }, when: addonGate(ADDON_IDS.ROADTRIP) })
|
||||
@@ -16,6 +18,14 @@ export class GoogleRouteMcp {
|
||||
|
||||
@Tool({ name: 'import_google_maps_route', description: 'Append reviewed Google Maps stops to an existing trip day in supplied order. Creates places and visits atomically. Requires place and day editing permissions. Existing visits remain. TREK calculates the road geometry; no Google route geometry is preserved.',
|
||||
inputSchema: { tripId: z.number().int().positive(), ...googleRouteImportSchema.shape },
|
||||
access: { group: 'trips', mode: 'write' }, when: addonGate(ADDON_IDS.ROADTRIP) })
|
||||
import(input: GoogleRouteImport & { tripId: number }, ctx: McpContext) { return ok(this.routes.import(input.tripId, ctx.userId, input)); }
|
||||
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
|
||||
// What the tool writes, not where the link came from: this inserts up to
|
||||
// thirty places and their day assignments, which is exactly what create_place
|
||||
// and create_and_assign_place are gated on. trips:write does not imply it, and
|
||||
// a client that withheld places:write was getting an itinerary written anyway.
|
||||
access: { group: 'places', mode: 'write' }, when: addonGate(ADDON_IDS.ROADTRIP) })
|
||||
import(input: GoogleRouteImport & { tripId: number }, ctx: McpContext) {
|
||||
if (this.auth.isDemoUser(ctx.userId)) return demoDenied();
|
||||
return ok(this.routes.import(input.tripId, ctx.userId, input));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user