From 18867d41b15e77846212ba0a2ce4c0ac97f3bd54 Mon Sep 17 00:00:00 2001 From: Miguel Teixeira Date: Wed, 21 Oct 2026 19:10:00 +0100 Subject: [PATCH 1/2] fix(archive,ingest): abort cleanly when an event vanishes mid-pipeline (BIT-31203) A hashing/EXIF/preview pipeline (ingest, archive indexing) can take long enough that the event it was told to file a photo under gets deleted or put away in the meantime. Prisma then threw a raw foreign key violation (photos_event_id_fkey, cloud_folders_event_id_fkey) instead of recovering: the archive indexer retried until it failed the file, and setFolderEvent crashed instead of rejecting cleanly. Both now treat the missing event the same way the rest of the codebase already treats "no event": ingest() retries the create with eventId: null (the photo goes to "Sem evento", same as an explicit no-event upload), and setFolderEvent() turns the race into the same ArchiveRejected("event_not_found") it already throws when the event is missing up front. Paperclip-Issue: BIT-31203 --- apps/api/src/archive/service.ts | 10 +++++- apps/api/src/ingest/service.ts | 61 +++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/apps/api/src/archive/service.ts b/apps/api/src/archive/service.ts index 5e9e802..378040e 100644 --- a/apps/api/src/archive/service.ts +++ b/apps/api/src/archive/service.ts @@ -867,7 +867,15 @@ export class ArchiveService { eventId = await this.eventForFixture(folder.source, fixture); } const previous = folder.eventId; - const updated = await db.cloudFolder.update({ where: { id: folderId }, data: { eventId, matchedBy } }); + let updated: CloudFolder; + try { + updated = await db.cloudFolder.update({ where: { id: folderId }, data: { eventId, matchedBy } }); + } catch (err) { + // The event just checked above was deleted in the gap before this write landed: same outcome as it + // never having been found, a clean rejection instead of a raw foreign key error bubbling up. + if ((err as { code?: string }).code === "P2003") throw new ArchiveRejected("event_not_found"); + throw err; + } const files = await db.cloudFile.findMany({ where: { folderId, photoId: { not: null } }, select: { photoId: true } }); const ids = files.map((f) => f.photoId!); if (ids.length) { diff --git a/apps/api/src/ingest/service.ts b/apps/api/src/ingest/service.ts index ff189bd..acf0574 100644 --- a/apps/api/src/ingest/service.ts +++ b/apps/api/src/ingest/service.ts @@ -163,7 +163,7 @@ export class IngestService { const at = capturedAt ?? new Date(); // Archive imports name their own event. A file with no camera time, or a time outside that event's window, // must not fall through onto whatever is live today. - const eventId = origin.inbox + const requestedEventId = origin.inbox ? null : origin.held && origin.eventId ? origin.eventId @@ -175,39 +175,48 @@ export class IngestService { await storage.adopt(file, storageKey); let photo; + const data = { + organizationId: orgId, + eventId: requestedEventId, + uploaderId: origin.userId, + source: origin.source, + originalName: origin.originalName, + sha256, + byteSize: origin.original?.size ?? size, + storageKey, + originalStored: !origin.original, + capturedAt: capturedAt ?? null, + cameraMake: camera.cameraMake ?? null, + cameraModel: camera.cameraModel ?? null, + cameraSerial: camera.cameraSerial ?? null, + lens: camera.lens ?? null, + focalLength: camera.focalLength ?? null, + aperture: camera.aperture ?? null, + exposureTime: camera.exposureTime ?? null, + iso: camera.iso ?? null, + ...(camera.extra && Object.keys(camera.extra).length && { exif: camera.extra }), + ...(Object.keys(embedded).length && { embeddedIptc: embedded }), + }; try { - photo = await db.photo.create({ - data: { - organizationId: orgId, - eventId, - uploaderId: origin.userId, - source: origin.source, - originalName: origin.originalName, - sha256, - byteSize: origin.original?.size ?? size, - storageKey, - originalStored: !origin.original, - capturedAt: capturedAt ?? null, - cameraMake: camera.cameraMake ?? null, - cameraModel: camera.cameraModel ?? null, - cameraSerial: camera.cameraSerial ?? null, - lens: camera.lens ?? null, - focalLength: camera.focalLength ?? null, - aperture: camera.aperture ?? null, - exposureTime: camera.exposureTime ?? null, - iso: camera.iso ?? null, - ...(camera.extra && Object.keys(camera.extra).length && { exif: camera.extra }), - ...(Object.keys(embedded).length && { embeddedIptc: embedded }), - }, - }); + photo = await db.photo.create({ data }); } catch (err) { // Two copies of the same file finishing at once: the other one won. if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === "P2002") { const winner = await db.photo.findUniqueOrThrow({ where: { organizationId_sha256: { organizationId: orgId, sha256 } } }); return { status: "duplicate", photoId: winner.id }; } - throw err; + // The event picked a moment ago (archive match, FTP target, the day's routing) was put away or deleted + // while this photo was being prepared (hashing, EXIF, the file's move into storage can take a while): + // same outcome as no event being open, the photo goes to "Sem evento" instead of failing outright. + if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === "P2003" && requestedEventId) { + log.info({ eventId: requestedEventId }, "ingest: event was gone by the time the photo was ready, filed under no event"); + photo = await db.photo.create({ data: { ...data, eventId: null } }); + } else { + throw err; + } } + // The event actually stored on the photo: the same id asked for, or `null` when it vanished just above. + const eventId = photo.eventId; let outcome: IngestOutcome; try { -- 2.47.3 From a744a62f4a29051899d2fe5b805b1759432994ed Mon Sep 17 00:00:00 2001 From: spark Date: Thu, 22 Oct 2026 00:02:13 +0100 Subject: [PATCH 2/2] Expect the club kind in the archive directory test 5eb5e95 (#113) made /v1/archive/:id/directory return each team with kind (club, national or other), but test/archive.test.ts still expected teams without it, so "reads teams from the stored directory and hides games the viewer cannot see" fails on main and on every PR merged with it. ABC de Braga and Sporting CP are clubs; the expectations now say so. --- apps/api/test/archive.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/api/test/archive.test.ts b/apps/api/test/archive.test.ts index de135e4..0d290ae 100644 --- a/apps/api/test/archive.test.ts +++ b/apps/api/test/archive.test.ts @@ -1416,8 +1416,8 @@ describe("archive from a Nextcloud", () => { expect((await get(owner, `/v1/archive/${source.id}/directory`)).json()).toEqual({ ready: true, teams: [ - { name: "ABC de Braga", galleries: 1 }, - { name: "Sporting CP", galleries: 1 }, + { name: "ABC de Braga", galleries: 1, kind: "club" }, + { name: "Sporting CP", galleries: 1, kind: "club" }, ], }); const none = await signIn("ninguem"); @@ -1425,7 +1425,7 @@ describe("archive from a Nextcloud", () => { await ctx.db.archiveClubAlias.create({ data: { sourceId: source.id, aliasKey: "abc de braga", alias: "ABC de Braga", name: "Sporting CP" } }); await worker.markDirectoryDirty(source.id); expect(await worker.rebuildDue()).toBe(true); - expect((await get(owner, `/v1/archive/${source.id}/directory`)).json()).toEqual({ ready: true, teams: [{ name: "Sporting CP", galleries: 1 }] }); + expect((await get(owner, `/v1/archive/${source.id}/directory`)).json()).toEqual({ ready: true, teams: [{ name: "Sporting CP", galleries: 1, kind: "club" }] }); expect(await worker.rebuildDue()).toBe(false); const games = await get(owner, `/v1/archive/${source.id}/galleries?team=${encodeURIComponent("Sporting CP")}`); expect(games.statusCode).toBe(200); -- 2.47.3