fix: keep quantities, colony/queen states and tasks consistent under retries
Data-integrity fixes found in a systematic review of ApiCore: - Bottling booked the honey batch amount from a value read outside the transaction and wrote an absolute result back, so two concurrent bottlings (double tap, two devices) could both pass the check and one deduction was lost. Bookings now use a conditional UPDATE inside the transaction; corrections and deletions give amounts back atomically. - Outgoing stock movements could drive stock below zero; the removal is now a conditional decrement that refuses when stock is insufficient. - Weak, conspicuous and queenless colonies were treated as "not active" and could not be moved, split or merged - uniting a queenless colony is the textbook merge. Only terminal statuses lock a colony now; merging a colony into itself and dissolving twice are rejected. - Moving a queen to another colony failed on the unique currentQueenId because the old colony kept pointing at her. The old colony now releases her (and becomes QUEENLESS); a queenless colony receiving a queen becomes ACTIVE; dissolved colonies cannot receive a queen. - Completing a task twice moved completedAt and re-sent the webhook; reopening a task kept completedAt. Recurring templates created tasks without a due date and caught up missed weeks one stale task at a time. - Automation rules created a new task and warning on every trigger (every stock removal below minimum). Tasks now record their source trigger and at most one open task per rule and object exists. The varroa threshold (mites per day) is only compared with natural-mite-fall rates, not with sample-based counts in another unit. - New opt-in Idempotency-Key support for record-creating endpoints, so the offline queue can retry after a lost response without duplicates. - Permission catalog sync crashed replicas booting concurrently on a fresh database (unique violation); it now uses createMany/skipDuplicates. - Server-side validation for inspection rating scales (1-5), unique colony/harvest lists and net weight <= gross weight. - Stale e2e expectations updated (permission count, trigger list, real PNG fixture for the file-signature check). Migration is additive only (nullable Task column, new table). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
786d929df9
commit
5d11756a87
37 files changed
+1420
-164
No files matched your search
@@ -3,6 +3,7 @@ import * as bcrypt from 'bcryptjs';
|
||||
import request from 'supertest';
|
||||
import { App } from 'supertest/types';
|
||||
import { PrismaService } from '../src/prisma/prisma.service';
|
||||
import { AutomationTriggerType } from '../generated/prisma';
|
||||
import { createTestApp } from './utils/create-test-app';
|
||||
import { login } from './utils/api';
|
||||
|
||||
@@ -96,18 +97,16 @@ describe('Automation rules (e2e)', () => {
|
||||
.expect(403);
|
||||
});
|
||||
|
||||
it('seeds the three fixed triggers for an admin', async () => {
|
||||
it('seeds every fixed trigger for an admin', async () => {
|
||||
const response = await request(app.getHttpServer())
|
||||
.get('/api/v1/automation-rules')
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.expect(200);
|
||||
const rules = response.body as AutomationRuleBody[];
|
||||
const triggerTypes = rules.map((rule) => rule.triggerType).sort();
|
||||
expect(triggerTypes).toEqual([
|
||||
'FEED_STATUS_CRITICAL',
|
||||
'QUEENLESS_NO_EGGS',
|
||||
'VARROA_THRESHOLD',
|
||||
]);
|
||||
expect(triggerTypes).toEqual(
|
||||
[...Object.values(AutomationTriggerType)].sort(),
|
||||
);
|
||||
const varroaRule = rules.find(
|
||||
(rule) => rule.triggerType === 'VARROA_THRESHOLD',
|
||||
);
|
||||
@@ -184,6 +183,8 @@ describe('Automation rules (e2e)', () => {
|
||||
date: new Date().toISOString(),
|
||||
method: 'NATURAL_MITE_FALL',
|
||||
miteCount: 20,
|
||||
// One day on the board: 20 mites per day, above the default 10.
|
||||
durationMinutes: 24 * 60,
|
||||
})
|
||||
.expect(201);
|
||||
|
||||
@@ -194,12 +195,35 @@ describe('Automation rules (e2e)', () => {
|
||||
expect(tasks.some((task) => task.title.includes('Behandlung'))).toBe(true);
|
||||
});
|
||||
|
||||
it('does not compare a sample-based count against the per-day threshold', async () => {
|
||||
const before = await prisma.task.count({ where: { relatedId: colonyId } });
|
||||
await request(app.getHttpServer())
|
||||
.post('/api/v1/varroa-measurements')
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.send({
|
||||
colonyId,
|
||||
date: new Date().toISOString(),
|
||||
method: 'POWDERED_SUGAR',
|
||||
miteCount: 50,
|
||||
})
|
||||
.expect(201);
|
||||
expect(await prisma.task.count({ where: { relatedId: colonyId } })).toBe(
|
||||
before,
|
||||
);
|
||||
});
|
||||
|
||||
it('lets an admin deactivate a rule, preventing further triggers', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.patch(`/api/v1/automation-rules/${varroaRuleId}`)
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.send({ active: false })
|
||||
.expect(200);
|
||||
// Close the earlier tasks so the one-open-task-per-rule guard cannot be
|
||||
// what keeps a new task from appearing - only the deactivation may.
|
||||
await prisma.task.updateMany({
|
||||
where: { relatedId: colonyId },
|
||||
data: { status: 'DONE' },
|
||||
});
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/api/v1/varroa-measurements')
|
||||
@@ -209,6 +233,7 @@ describe('Automation rules (e2e)', () => {
|
||||
date: new Date().toISOString(),
|
||||
method: 'NATURAL_MITE_FALL',
|
||||
miteCount: 30,
|
||||
durationMinutes: 24 * 60,
|
||||
})
|
||||
.expect(201);
|
||||
|
||||
|
||||
Reference in new issue
Block a user