mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
places: a picture route records the origin of the picture only
The upload and the trip-file routes vouched for every field that changed between their read and their answer. A website the assistant or a collaborator changed while a large picture uploaded was then recorded as typed by the user and queued for the TREK Places API. Those writers now name the one field they write, and other changes keep the origin their own writer recorded.
This commit is contained in:
@@ -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<PlaceContribField, string | null>;
|
||||
|
||||
@@ -74,8 +74,9 @@ export class PlaceOriginsInterceptor implements NestInterceptor {
|
||||
}
|
||||
|
||||
function writerFor(route: Exclude<PlaceOriginRoute, 'import'>, 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 };
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user