diff --git a/server/src/nest/places/place-origins.helpers.ts b/server/src/nest/places/place-origins.helpers.ts index 53bad4627..6e941ff0e 100644 --- a/server/src/nest/places/place-origins.helpers.ts +++ b/server/src/nest/places/place-origins.helpers.ts @@ -9,9 +9,14 @@ import { type PlaceFieldOrigins, } from '@trek/shared'; -/** Who wrote a place: a client reporting per field, or a path that decides the origin of all it changed. */ +/** + * Who wrote a place: a client reporting per field, or a path that decides the + * origin of all it changed. A path that writes one field only names it in + * `field`, so a value another writer changed meanwhile keeps its own origin. + */ export type OriginWriter = - { kind: 'client'; claims: PlaceFieldOrigins | undefined } | { kind: 'fixed'; origin: PlaceFieldOrigin }; + | { kind: 'client'; claims: PlaceFieldOrigins | undefined } + | { kind: 'fixed'; origin: PlaceFieldOrigin; field?: PlaceContribField }; /** The shareable values of a place, one per field, null where it holds none. */ export type OriginSnapshot = Record; diff --git a/server/src/nest/places/place-origins.interceptor.ts b/server/src/nest/places/place-origins.interceptor.ts index e81666f57..d9d83db92 100644 --- a/server/src/nest/places/place-origins.interceptor.ts +++ b/server/src/nest/places/place-origins.interceptor.ts @@ -74,8 +74,9 @@ export class PlaceOriginsInterceptor implements NestInterceptor { } function writerFor(route: Exclude, body: unknown): OriginWriter { - if (route === 'upload') return { kind: 'fixed', origin: 'user' }; - if (route === 'attachment') return { kind: 'fixed', origin: 'attachment' }; + // A picture route writes the picture only; whatever else changed while it ran keeps its own origin. + if (route === 'upload') return { kind: 'fixed', origin: 'user', field: 'image' }; + if (route === 'attachment') return { kind: 'fixed', origin: 'attachment', field: 'image' }; // The pipe has refused a malformed body before the handler ran; this reads the same field again. const parsed = placeFieldOriginsSchema.safeParse((body as { fieldOrigins?: unknown } | null)?.fieldOrigins); return { kind: 'client', claims: parsed.success ? parsed.data : undefined }; diff --git a/server/src/nest/places/place-origins.service.ts b/server/src/nest/places/place-origins.service.ts index 1c0dbf805..2f0515ee9 100644 --- a/server/src/nest/places/place-origins.service.ts +++ b/server/src/nest/places/place-origins.service.ts @@ -100,7 +100,9 @@ export class PlaceOriginsService { const row = await this.origins.placeValues(placeId); if (!row) return; const after = snapshotOf(row); - const changed = changedFields(before, after); + const changed = changedFields(before, after).filter( + (field) => writer.kind !== 'fixed' || writer.field === undefined || writer.field === field, + ); if (changed.length === 0) return; const providers = needsProviders(writer, changed) ? await this.providerValues(row) : []; const updatedAt = dbNow(); diff --git a/server/src/nest/places/places.mcp.ts b/server/src/nest/places/places.mcp.ts index 8d4e7e2ab..b668e375f 100644 --- a/server/src/nest/places/places.mcp.ts +++ b/server/src/nest/places/places.mcp.ts @@ -265,7 +265,7 @@ export class PlacesMcp { if (result === 'too_large') return errorResult('That image is too large.'); if (!result || isUpdateConflict(result)) return errorResult('Place not found.'); this.guards.safeBroadcast(tripId, 'place:updated', { place: result }); - await this.origins.record(result.id, before, { kind: 'fixed', origin: 'attachment' }); + await this.origins.record(result.id, before, { kind: 'fixed', origin: 'attachment', field: 'image' }); await this.places.onUpdated(result.id); return ok({ place: result }); } diff --git a/server/tests/unit/nest/places/place-origins.interceptor.test.ts b/server/tests/unit/nest/places/place-origins.interceptor.test.ts index e1ea5d9fb..9779c6863 100644 --- a/server/tests/unit/nest/places/place-origins.interceptor.test.ts +++ b/server/tests/unit/nest/places/place-origins.interceptor.test.ts @@ -76,9 +76,9 @@ describe('PlaceOriginsInterceptor', () => { it('POI-003: the upload is the user own picture, a trip file the attachment', async () => { await run('upload', { params: { id: '3' } }, { place: { id: 3 } }); - expect(origins.record).toHaveBeenLastCalledWith(3, BEFORE, { kind: 'fixed', origin: 'user' }); + expect(origins.record).toHaveBeenLastCalledWith(3, BEFORE, { kind: 'fixed', origin: 'user', field: 'image' }); await run('fromFile', { params: { id: '3' } }, { place: { id: 3 } }); - expect(origins.record).toHaveBeenLastCalledWith(3, BEFORE, { kind: 'fixed', origin: 'attachment' }); + expect(origins.record).toHaveBeenLastCalledWith(3, BEFORE, { kind: 'fixed', origin: 'attachment', field: 'image' }); }); it('POI-004: an import records the places it answered with, in one pass', async () => { diff --git a/server/tests/unit/nest/places/place-origins.service.test.ts b/server/tests/unit/nest/places/place-origins.service.test.ts index ba384260e..fdc3ace2e 100644 --- a/server/tests/unit/nest/places/place-origins.service.test.ts +++ b/server/tests/unit/nest/places/place-origins.service.test.ts @@ -138,6 +138,21 @@ describe('an update', () => { expect((await origins(id)).website).toEqual({ origin: 'agent', value_hash: valueHash('https://agent.example') }); }); + it('POS-015: a picture route records the picture only; a value another writer changed meanwhile keeps its origin', async () => { + const id = await place({ website: 'https://mine.example' }); + await service().record(id, null, { kind: 'fixed', origin: 'agent' }); + const before = await service().valuesBefore(id); + // The assistant changes the website while the picture uploads. + await updateRows(orm(), Places, { id }, { website: 'https://agent.example', image_url: '/uploads/places/b.jpg' }); + await service().record(id, before, { kind: 'fixed', origin: 'user', field: 'image' }); + const rows = await origins(id); + expect(rows.image).toEqual({ origin: 'user', value_hash: valueHash('/uploads/places/b.jpg') }); + expect(rows.website?.origin).toBe('agent'); + expect(contrib.enqueue).toHaveBeenLastCalledWith( + expect.objectContaining({ values: [{ field: 'image', value: '/uploads/places/b.jpg' }], dropped: [] }), + ); + }); + it('POS-013: nothing changed, nothing is recorded or queued', async () => { const id = await place({ website: 'https://same.example' }); const before = await service().valuesBefore(id); diff --git a/server/tests/unit/nest/places/places.mcp.origins.test.ts b/server/tests/unit/nest/places/places.mcp.origins.test.ts index d24128bd1..1eef8fd0c 100644 --- a/server/tests/unit/nest/places/places.mcp.origins.test.ts +++ b/server/tests/unit/nest/places/places.mcp.origins.test.ts @@ -56,7 +56,7 @@ describe('PlacesMcp origin tracking', () => { it('PMO-003: a trip file made the picture is an attachment', async () => { await mcp.setPlaceImageFromFile({ tripId: 1, placeId: 8, fileId: 4 }, ctx); - expect(origins.record).toHaveBeenCalledWith(8, BEFORE, { kind: 'fixed', origin: 'attachment' }); + expect(origins.record).toHaveBeenCalledWith(8, BEFORE, { kind: 'fixed', origin: 'attachment', field: 'image' }); }); it('PMO-004: a bulk update records every place it changed as the agent', async () => {