feat: T4 wave 5 - colony end cleanup, withdrawal chain, next steps, inspection takeover UI

- Colony end releases the queen in the same transaction; open
  suggestions become obsolete and a CLEANUP suggestion lists open
  tasks and the queen; apply-cleanup validates evidence and blocks
  reactivated colonies
- Withdrawal chain: plausibility warnings on harvest and bottling,
  conflict notifications also when a treatment is entered or corrected
  after the harvest (naming bottled lots), withdrawal spans in the
  calendar and M365 sync, Berlin-day end events
- Next 3 steps per colony and the "Heute" endpoint with ranking and an
  explicit merge table by intent
- Inspection-triggered rule suggestions follow the colony's latest
  inspection (resolve when the finding is gone, refresh when it stays)
- Frontend: colony card takeover in the inspection form, feed status as
  condition, occupied frames 0-40; legacy offline entries keep syncing
- Notifications and preferences are deleted with their user

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 16:28:07 +02:00
1 parent 02eb16924e
commit ef33e5549b
122 files changed
+10707 -504

No files matched your search

+210 -9
View File
@@ -29,6 +29,7 @@ describe('Automation rules (e2e)', () => {
let staffToken: string;
let apiaryId: string;
let colonyId: string;
const followColonyIds: string[] = [];
let varroaRuleId: string;
let swarmRuleId: string;
@@ -112,13 +113,17 @@ describe('Automation rules (e2e)', () => {
// Rule suggestions and the rules' own entries are written without a
// user - they are not covered by the userId cleanup below. Deliveries go
// with their event (ON DELETE CASCADE).
const colonyIds = [colonyId, ...followColonyIds];
const suggestionIds = (
await prisma.suggestion.findMany({
where: { colonyId },
select: { id: true },
})
).map((suggestion) => suggestion.id);
const byColony = { where: { colonyId }, select: { id: true } };
const byColony = {
where: { colonyId: { in: colonyIds } },
select: { id: true },
};
const recordIds = [
...(await prisma.inspection.findMany(byColony)),
...(await prisma.varroaMeasurement.findMany(byColony)),
@@ -127,10 +132,10 @@ describe('Automation rules (e2e)', () => {
where: {
OR: [
{ entityType: 'Suggestion', entityId: { in: suggestionIds } },
{
...colonyIds.map((id) => ({
entityType: 'AutomationRule',
afterData: { path: ['relatedId'], equals: colonyId },
},
afterData: { path: ['relatedId'], equals: id },
})),
],
},
});
@@ -143,11 +148,19 @@ describe('Automation rules (e2e)', () => {
await prisma.notification.deleteMany({
where: { relatedType: 'Suggestion', relatedId: { in: suggestionIds } },
});
await prisma.suggestion.deleteMany({ where: { colonyId } });
await prisma.varroaMeasurement.deleteMany({ where: { colonyId } });
await prisma.swarmControlCheck.deleteMany({ where: { colonyId } });
await prisma.inspection.deleteMany({ where: { colonyId } });
await prisma.colony.deleteMany({ where: { id: colonyId } });
await prisma.suggestion.deleteMany({
where: { colonyId: { in: colonyIds } },
});
await prisma.varroaMeasurement.deleteMany({
where: { colonyId: { in: colonyIds } },
});
await prisma.swarmControlCheck.deleteMany({
where: { colonyId: { in: colonyIds } },
});
await prisma.inspection.deleteMany({
where: { colonyId: { in: colonyIds } },
});
await prisma.colony.deleteMany({ where: { id: { in: colonyIds } } });
await prisma.apiary.deleteMany({ where: { id: apiaryId } });
await prisma.refreshToken.deleteMany({
where: { userId: { in: [adminId, staffId] } },
@@ -448,6 +461,194 @@ describe('Automation rules (e2e)', () => {
expect(resolved).toMatchObject({ status: 'RESOLVED', dedupeKey: null });
});
describe('an open inspection suggestion follows newer inspections', () => {
const ANCHOR_DAY = '2026-05-05T00:00:00.000Z';
async function freshColony(): Promise<string> {
const colony = await prisma.colony.create({
data: {
code: `AUTO-F-${Date.now()}-${followColonyIds.length}`,
apiaryId,
colonyType: 'PRODUCTION_COLONY',
},
});
followColonyIds.push(colony.id);
return colony.id;
}
async function inspectOn(
forColony: string,
date: string,
body: object,
): Promise<string> {
const response = await request(app.getHttpServer())
.post('/api/v1/inspections')
.set('Authorization', `Bearer ${staffToken}`)
.send({ colonyId: forColony, date, ...body })
.expect(201);
const id = (response.body as { id: string }).id;
await drainDomainEvents(app, [id]);
return id;
}
const queenlessOf = (forColony: string) =>
prisma.suggestion.findMany({
where: { colonyId: forColony, ruleKey: 'QUEENLESS_NO_EGGS' },
});
const queenless = { queenSeen: false, eggsPresent: false };
const clean = { queenSeen: true, eggsPresent: true };
it('a newer clean inspection resolves it', async () => {
const forColony = await freshColony();
await inspectOn(forColony, ANCHOR_DAY, queenless);
expect(await queenlessOf(forColony)).toMatchObject([{ status: 'OPEN' }]);
await inspectOn(forColony, '2026-05-12T00:00:00.000Z', clean);
expect(await queenlessOf(forColony)).toMatchObject([
{ status: 'RESOLVED', dedupeKey: null },
]);
});
it('a newer inspection with the finding keeps one OPEN suggestion with updated evidence', async () => {
const forColony = await freshColony();
await inspectOn(forColony, ANCHOR_DAY, queenless);
const newerId = await inspectOn(
forColony,
'2026-05-12T00:00:00.000Z',
queenless,
);
const suggestions = await queenlessOf(forColony);
expect(suggestions).toHaveLength(1);
expect(suggestions[0]).toMatchObject({
status: 'OPEN',
evidence: { inspectionId: newerId, inspectedOn: '2026-05-12' },
});
});
it('a backdated inspection changes nothing', async () => {
const forColony = await freshColony();
await inspectOn(forColony, ANCHOR_DAY, queenless);
const [before] = await queenlessOf(forColony);
await inspectOn(forColony, '2026-04-20T00:00:00.000Z', clean);
const [after] = await queenlessOf(forColony);
expect(after).toMatchObject({
status: 'OPEN',
evidence: before.evidence,
});
});
it('correcting the inspection of the finding keeps it OPEN while the finding remains', async () => {
const forColony = await freshColony();
const id = await inspectOn(forColony, ANCHOR_DAY, queenless);
await request(app.getHttpServer())
.patch(`/api/v1/inspections/${id}`)
.set('Authorization', `Bearer ${staffToken}`)
.send({ queenSeen: false, eggsPresent: false, notes: 'korrigiert' })
.expect(200);
await drainDomainEvents(app, [id]);
expect(await queenlessOf(forColony)).toMatchObject([
{ status: 'OPEN', evidence: { inspectionId: id } },
]);
});
it('correcting an older inspection than the latest changes nothing', async () => {
const forColony = await freshColony();
const olderId = await inspectOn(forColony, ANCHOR_DAY, queenless);
await inspectOn(forColony, '2026-05-12T00:00:00.000Z', queenless);
const [before] = await queenlessOf(forColony);
await request(app.getHttpServer())
.patch(`/api/v1/inspections/${olderId}`)
.set('Authorization', `Bearer ${staffToken}`)
.send({ queenSeen: true, eggsPresent: true })
.expect(200);
await drainDomainEvents(app, [olderId]);
const [after] = await queenlessOf(forColony);
expect(after).toMatchObject({
status: 'OPEN',
evidence: before.evidence,
});
});
it('a late retry of an older inspection after a newer clean one proposes nothing', async () => {
const forColony = await freshColony();
const olderId = await inspectOn(forColony, ANCHOR_DAY, queenless);
await inspectOn(forColony, '2026-05-12T00:00:00.000Z', clean);
expect(await queenlessOf(forColony)).toMatchObject([
{ status: 'RESOLVED' },
]);
// The delivery of the older inspection runs again.
await prisma.domainEvent.updateMany({
where: { aggregateId: olderId },
data: { status: 'PENDING', processedAt: null, claimedAt: null },
});
await prisma.domainEventDelivery.updateMany({
where: {
handler: 'automation.inspection-rules',
event: { aggregateId: olderId },
},
data: { status: 'PENDING', attempts: 0, completedAt: null },
});
await drainDomainEvents(app, [olderId]);
expect(await queenlessOf(forColony)).toMatchObject([
{ status: 'RESOLVED' },
]);
});
it('the feed rule follows newer inspections the same way', async () => {
const forColony = await freshColony();
await inspectOn(forColony, ANCHOR_DAY, {
...clean,
feedStatus: 'CRITICAL',
});
const feedOf = () =>
prisma.suggestion.findMany({
where: { colonyId: forColony, ruleKey: 'FEED_STATUS_CRITICAL' },
});
expect(await feedOf()).toMatchObject([{ status: 'OPEN' }]);
const stillId = await inspectOn(forColony, '2026-05-12T00:00:00.000Z', {
...clean,
feedStatus: 'CRITICAL',
});
expect(await feedOf()).toMatchObject([
{ status: 'OPEN', evidence: { inspectionId: stillId } },
]);
await inspectOn(forColony, '2026-05-19T00:00:00.000Z', {
...clean,
feedStatus: 'GOOD',
});
expect(await feedOf()).toMatchObject([{ status: 'RESOLVED' }]);
});
it('correcting the inspection of the finding resolves it', async () => {
const forColony = await freshColony();
const id = await inspectOn(forColony, ANCHOR_DAY, queenless);
await request(app.getHttpServer())
.patch(`/api/v1/inspections/${id}`)
.set('Authorization', `Bearer ${staffToken}`)
.send({ queenSeen: true, eggsPresent: true })
.expect(200);
await drainDomainEvents(app, [id]);
expect(await queenlessOf(forColony)).toMatchObject([
{ status: 'RESOLVED' },
]);
});
});
it('lets an admin clear a rule config, which falls back to its defaults', async () => {
const before = await prisma.automationRule.findUniqueOrThrow({
where: { id: varroaRuleId },