feat: platform backbone and field capture complete (checkpoint)

Platform backbone (plan 2026-10-13-plattform-rueckgrat), all tasks reviewed:
- webhooks with delivery table, retries and stable delivery id
- calendar handlers; a finished task removes its Outlook event
- tasks, automations, inspections, varroa, treatments, feedings,
  harvests, colonies, wintering, stock and bottling record domain events
  in the same transaction; legacy post-commit calls removed
- row locks through one helper (lockRowForUpdate, lockRowsForShare,
  lockRowsForUpdate)
- job framework with JobRun, cli exit codes, cleanup, task templates,
  daily evaluation, hourly app-backup and the rekey job
- system API and page "Verwaltung > System" with retry
- @nestjs/schedule removed, guard tests against in-process schedulers

Field capture T3 (plan 2026-10-13-t3-erfassung-am-stand), all tasks reviewed:
- inspection photo offline without metadata
- bulk capture for treatments, feedings and varroa (backend + frontend)
- offline stand mode with service worker, offline pages and packs
- offline session, cold start and logout on shared devices

Docs: ARCHITECTURE.md assumptions and sections updated, TASKS.md.
Gates: backend lint, typecheck, 1770 unit, 420 e2e, build, no schema
drift; frontend lint, typecheck, 913 tests, build. Not rolled out.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Nick SzaunigandClaude Opus 5.5 committed 2026-10-14 11:36:38 +02:00
1 parent c01a38835a
commit cdd30cf3ee
288 files changed
+39165 -2853

No files matched your search

+259
View File
@@ -12,6 +12,15 @@ interface VarroaMeasurementBody {
calculatedValue: number | null;
}
interface BulkVarroaBody {
bulkId: string;
records: (VarroaMeasurementBody & {
miteCount: number | null;
durationMinutes: number | null;
assessment: string | null;
})[];
}
describe('Varroa measurements (e2e)', () => {
let app: INestApplication<App>;
let prisma: PrismaService;
@@ -92,6 +101,19 @@ describe('Varroa measurements (e2e)', () => {
});
afterAll(async () => {
const measurementIds = (
await prisma.varroaMeasurement.findMany({
where: { colonyId },
select: { id: true },
})
).map((measurement) => measurement.id);
// Deliveries go with their event (ON DELETE CASCADE).
await prisma.domainEvent.deleteMany({
where: {
aggregateType: 'VarroaMeasurement',
aggregateId: { in: measurementIds },
},
});
await prisma.varroaMeasurement.deleteMany({ where: { colonyId } });
await prisma.colony.deleteMany({ where: { id: colonyId } });
await prisma.apiary.deleteMany({ where: { id: apiaryId } });
@@ -136,6 +158,30 @@ describe('Varroa measurements (e2e)', () => {
measurementId = created.id;
expect(measurementId).toEqual(expect.any(String));
expect(created.calculatedValue).toBe(4);
// Committed with the measurement and its audit entry; the threshold
// rule and the varroa.measured webhook follow from the event.
await expect(
prisma.auditLog.count({
where: {
entityType: 'VarroaMeasurement',
entityId: measurementId,
action: 'CREATE',
userId: beekeeperId,
},
}),
).resolves.toBe(1);
const events = await prisma.domainEvent.findMany({
where: { aggregateType: 'VarroaMeasurement', aggregateId: measurementId },
});
expect(events).toEqual([
expect.objectContaining({
type: 'VarroaMeasured',
actorUserId: beekeeperId,
payload: { colonyId },
status: 'PENDING',
}),
]);
});
it('recomputes mites/day when the count is corrected', async () => {
@@ -153,6 +199,28 @@ describe('Varroa measurements (e2e)', () => {
})
.expect(200);
expect((response.body as VarroaMeasurementBody).calculatedValue).toBe(10);
// A corrected count can cross the threshold: the rule re-evaluates it
// from VarroaCorrected.
await expect(
prisma.auditLog.count({
where: {
entityType: 'VarroaMeasurement',
entityId: measurementId,
action: 'UPDATE',
userId: beekeeperId,
},
}),
).resolves.toBe(1);
await expect(
prisma.domainEvent.findFirst({
where: { aggregateId: measurementId, type: 'VarroaCorrected' },
}),
).resolves.toMatchObject({
aggregateType: 'VarroaMeasurement',
actorUserId: beekeeperId,
payload: { colonyId },
});
});
it('lists measurements for the colony', async () => {
@@ -187,4 +255,195 @@ describe('Varroa measurements (e2e)', () => {
it('rejects a role without any varroa permission', () =>
thresholdBody(noVarroaToken).expect(403));
});
describe('POST /varroa-measurements/bulk', () => {
const suffix = Date.now();
const shared = {
date: '2026-09-10',
method: 'NATURAL_MITE_FALL',
durationMinutes: 4320,
notes: 'Windel nach Ameisensäure',
};
let bulkColonyIds: string[];
let endedColonyId: string;
const postBulk = (token: string, body: object) =>
request(app.getHttpServer())
.post('/api/v1/varroa-measurements/bulk')
.set('Authorization', `Bearer ${token}`)
.send(body);
const storedMeasurements = () =>
prisma.varroaMeasurement.count({
where: { colonyId: { in: [...bulkColonyIds, endedColonyId] } },
});
beforeAll(async () => {
const colonyAt = async (
code: string,
status: 'ACTIVE' | 'MERGED' = 'ACTIVE',
) =>
(
await prisma.colony.create({
data: {
code: `${code}-${suffix}`,
apiaryId,
colonyType: 'PRODUCTION_COLONY',
status,
},
})
).id;
bulkColonyIds = [await colonyAt('VB-1'), await colonyAt('VB-2')];
endedColonyId = await colonyAt('VB-X', 'MERGED');
});
afterAll(async () => {
const colonyIds = [...bulkColonyIds, endedColonyId];
const measurementIds = (
await prisma.varroaMeasurement.findMany({
where: { colonyId: { in: colonyIds } },
select: { id: true },
})
).map((measurement) => measurement.id);
await prisma.domainEvent.deleteMany({
where: {
aggregateType: 'VarroaMeasurement',
aggregateId: { in: measurementIds },
},
});
await prisma.varroaMeasurement.deleteMany({
where: { colonyId: { in: colonyIds } },
});
await prisma.colony.deleteMany({ where: { id: { in: colonyIds } } });
});
it('bulk derives mites per day per entry', async () => {
const response = await postBulk(beekeeperToken, {
...shared,
entries: [
{ colonyId: bulkColonyIds[0], miteCount: 30, assessment: 'hoch' },
{ colonyId: bulkColonyIds[1], miteCount: 6 },
],
}).expect(201);
const { bulkId, records } = response.body as BulkVarroaBody;
expect(
records.map((record) => ({
colonyId: record.colonyId,
miteCount: record.miteCount,
durationMinutes: record.durationMinutes,
calculatedValue: record.calculatedValue,
assessment: record.assessment,
})),
).toEqual([
{
colonyId: bulkColonyIds[0],
miteCount: 30,
durationMinutes: 4320,
calculatedValue: 10,
assessment: 'hoch',
},
{
colonyId: bulkColonyIds[1],
miteCount: 6,
durationMinutes: 4320,
calculatedValue: 2,
assessment: null,
},
]);
expect(
await prisma.domainEvent.count({
where: {
type: 'VarroaMeasured',
payload: { path: ['bulkId'], equals: bulkId },
},
}),
).toBe(2);
expect(
await prisma.auditLog.count({
where: {
entityType: 'VarroaMeasurement',
action: 'CREATE',
userId: beekeeperId,
entityId: { in: records.map((record) => record.id) },
},
}),
).toBe(2);
});
it('bulk rejects duplicate colonyId in entries', async () => {
const before = await storedMeasurements();
const response = await postBulk(beekeeperToken, {
...shared,
entries: [
{ colonyId: bulkColonyIds[0], miteCount: 3 },
{ colonyId: bulkColonyIds[0], miteCount: 4 },
],
}).expect(400);
expect(JSON.stringify(response.body)).toContain(
'entries enthält ein Volk doppelt',
);
expect(await storedMeasurements()).toBe(before);
});
it('bulk rejects the whole batch when one colony has ended and names it', async () => {
const before = await storedMeasurements();
const response = await postBulk(beekeeperToken, {
...shared,
entries: [
{ colonyId: bulkColonyIds[0], miteCount: 3 },
{ colonyId: endedColonyId, miteCount: 4 },
],
}).expect(400);
expect(
(response.body as { invalidColonyIds: string[] }).invalidColonyIds,
).toEqual([endedColonyId]);
expect(await storedMeasurements()).toBe(before);
});
it.each([
['no entry', {}, () => []],
[
'a shared mite count instead of one per entry',
{ miteCount: 5 },
() => [{ colonyId: bulkColonyIds[0] }],
],
[
'an invalid entry',
{},
() => [{ colonyId: bulkColonyIds[0], miteCount: -1 }],
],
])('bulk rejects %s', (_label, extra: object, entries: () => object[]) =>
postBulk(beekeeperToken, {
...shared,
...extra,
entries: entries(),
}).expect(400),
);
it('bulk rejects an empty colonyId in entries as invalid input, not as an unknown colony', async () => {
const response = await postBulk(beekeeperToken, {
...shared,
entries: [
{ colonyId: bulkColonyIds[0], miteCount: 3 },
{ colonyId: '', miteCount: 4 },
],
}).expect(400);
expect(JSON.stringify(response.body)).toContain(
'entries.1.colonyId enthält eine leere Volk-ID',
);
expect(response.body).not.toHaveProperty('invalidColonyIds');
});
it('bulk rejects a role without varroa:create', () =>
postBulk(staffToken, {
...shared,
entries: [{ colonyId: bulkColonyIds[0], miteCount: 1 }],
}).expect(403));
});
});