feat: T4 wave 4 - rules suggest, takeover, chains, autopilot A1-A3, suggestions page, varroa bands UI
- Automation rules propose suggestions instead of creating tasks; notify per rule config; corrected varroa measurements refresh or resolve their suggestion - Inspection values can be taken over into the colony card (occupiedFrames 0-40, strength deprecated); warning counts use the same visibility rule as the notification list - Biotech chains: swarm check after queen cells, queen acceptance check, split schedules (raise own, queen cell, queen) - Season autopilot core with A1 inspection due, A2 varroa measurement, A3 recheck after treatment; colony sync and daily step - Frontend: suggestions page with groups, bulk accept, undo, paging, focus management and permission gating - Frontend: varroa traffic light from seasonal bands, sample size, one range request for the colony chart - Notifications: mark-read answers 404 for rows the user may no longer see Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
64a2b7dad9
commit
02eb16924e
142 files changed
+11847
-1251
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import type { AnyDomainEventEnvelope } from '../domain-events/domain-event-types';
|
||||
import { OnDomainEvent } from '../domain-events/on-domain-event.decorator';
|
||||
import { isLivingColonyStatus } from '../colonies/colony-status';
|
||||
import { Prisma } from '../../generated/prisma';
|
||||
import { StockService } from '../stock/stock.service';
|
||||
import { AutomationService } from './automation.service';
|
||||
@@ -10,9 +11,10 @@ type Tx = Prisma.TransactionClient;
|
||||
/**
|
||||
* Evaluates the automation rules on domain events. Pure DB writer: it reads
|
||||
* the record the event is about through the delivery transaction and lets
|
||||
* AutomationService write task, warning and audit entries in it. A record
|
||||
* deleted before the event was dispatched is skipped. Errors propagate, so
|
||||
* the delivery is rolled back as a whole and retried.
|
||||
* AutomationService write suggestion, notification and audit entries in it.
|
||||
* A record deleted before the event was dispatched, or of a colony that
|
||||
* ended, is skipped. Errors propagate, so the delivery is rolled back as a
|
||||
* whole and retried.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AutomationEventHandlers {
|
||||
@@ -21,6 +23,18 @@ export class AutomationEventHandlers {
|
||||
private readonly stock: StockService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Fast path only: proposeForRuleIn re-checks the colony under its lock.
|
||||
* Whether the colony still lives (a colony that ended takes no proposals).
|
||||
*/
|
||||
private async isLivingColonyIn(tx: Tx, colonyId: string): Promise<boolean> {
|
||||
const colony = await tx.colony.findUnique({
|
||||
where: { id: colonyId },
|
||||
select: { status: true },
|
||||
});
|
||||
return colony !== null && isLivingColonyStatus(colony.status);
|
||||
}
|
||||
|
||||
@OnDomainEvent('InspectionRecorded', 'automation.inspection-rules')
|
||||
async inspectionRules(
|
||||
event: AnyDomainEventEnvelope<'InspectionRecorded'>,
|
||||
@@ -29,14 +43,17 @@ export class AutomationEventHandlers {
|
||||
const inspection = await tx.inspection.findUnique({
|
||||
where: { id: event.aggregateId },
|
||||
});
|
||||
if (inspection === null) {
|
||||
if (
|
||||
inspection === null ||
|
||||
!(await this.isLivingColonyIn(tx, inspection.colonyId))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
await this.automation.evaluateInspectionIn(tx, inspection);
|
||||
}
|
||||
|
||||
// A correction can lift a count over the threshold, so it is evaluated
|
||||
// like a new measurement; the open-task check keeps it to one task.
|
||||
// like a new measurement; the open-suggestion check keeps it to one.
|
||||
@OnDomainEvent(
|
||||
['VarroaMeasured', 'VarroaCorrected'],
|
||||
'automation.varroa-threshold',
|
||||
@@ -48,7 +65,10 @@ export class AutomationEventHandlers {
|
||||
const measurement = await tx.varroaMeasurement.findUnique({
|
||||
where: { id: event.aggregateId },
|
||||
});
|
||||
if (measurement === null) {
|
||||
if (
|
||||
measurement === null ||
|
||||
!(await this.isLivingColonyIn(tx, measurement.colonyId))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
await this.automation.evaluateVarroaMeasurementIn(tx, measurement);
|
||||
@@ -62,7 +82,10 @@ export class AutomationEventHandlers {
|
||||
const treatment = await tx.treatment.findUnique({
|
||||
where: { id: event.aggregateId },
|
||||
});
|
||||
if (treatment === null) {
|
||||
if (
|
||||
treatment === null ||
|
||||
!(await this.isLivingColonyIn(tx, treatment.colonyId))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
await this.automation.evaluateTreatmentIn(tx, treatment);
|
||||
@@ -76,7 +99,10 @@ export class AutomationEventHandlers {
|
||||
const feeding = await tx.feeding.findUnique({
|
||||
where: { id: event.aggregateId },
|
||||
});
|
||||
if (feeding === null) {
|
||||
if (
|
||||
feeding === null ||
|
||||
!(await this.isLivingColonyIn(tx, feeding.colonyId))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
await this.automation.evaluateFeedingIn(tx, feeding);
|
||||
@@ -89,18 +115,15 @@ export class AutomationEventHandlers {
|
||||
event: AnyDomainEventEnvelope<'ColonyStatusChanged'>,
|
||||
tx: Tx,
|
||||
): Promise<void> {
|
||||
const colony = await tx.colony.findUnique({
|
||||
where: { id: event.aggregateId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (colony === null) {
|
||||
if (!(await this.isLivingColonyIn(tx, event.aggregateId))) {
|
||||
return;
|
||||
}
|
||||
await this.automation.evaluateColonyHealthIn(
|
||||
tx,
|
||||
colony.id,
|
||||
event.aggregateId,
|
||||
event.payload.from,
|
||||
event.payload.to,
|
||||
{ id: event.id, occurredAt: event.occurredAt },
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user