diff --git a/ApiCore-Backend/src/compliance/readiness/loaders/lots.loader.spec.ts b/ApiCore-Backend/src/compliance/readiness/loaders/lots.loader.spec.ts index 4088886..677f73b 100644 --- a/ApiCore-Backend/src/compliance/readiness/loaders/lots.loader.spec.ts +++ b/ApiCore-Backend/src/compliance/readiness/loaders/lots.loader.spec.ts @@ -65,6 +65,17 @@ describe('loadLots', () => { }); }); + it('leaves out a lot whose medicine vanished between the two reads', async () => { + const { db } = fakeDb( + [lotRow({ id: 'l1' }), lotRow({ id: 'gone', medicine: null })], + [], + ); + + const { lots } = await loadLots(db); + + expect(lots.map((lot) => lot.id)).toEqual(['l1']); + }); + it('reads only active medicines', async () => { const { db } = fakeDb( [], diff --git a/ApiCore-Backend/src/compliance/readiness/loaders/lots.loader.ts b/ApiCore-Backend/src/compliance/readiness/loaders/lots.loader.ts index 659386f..844d708 100644 --- a/ApiCore-Backend/src/compliance/readiness/loaders/lots.loader.ts +++ b/ApiCore-Backend/src/compliance/readiness/loaders/lots.loader.ts @@ -57,12 +57,16 @@ export async function loadLots(db: DbClient): Promise { orderBy: [{ name: 'asc' }, { id: 'asc' }], }), ]); + // Prisma reads the medicine in a second query: a lot deleted together with + // its medicine in between comes back with medicine null (CI run 836, 500). + // Such a lot is gone, so it is left out. + const present = rows.filter((row) => (row.medicine as unknown) !== null); const totals = await lotTotals( db, - rows.map((row) => row.id), + present.map((row) => row.id), ); return { - lots: rows.map((row): ReadinessLot => ({ + lots: present.map((row): ReadinessLot => ({ id: row.id, medicineId: row.medicineId, medicineName: row.medicine.name,