fix: Paket 0 – data integrity, security hardening and field usability
Backend Image / build-and-push (push) Successful in 3m15s
Frontend Image / build-and-push (push) Successful in 6m2s

Backend
- Backup covers every Prisma model (bundle v3); a guard test fails for any
  new model that is neither backed up nor explicitly excluded. Unsealed
  bundles may only add rows to 27 legacy master/field tables and never
  overwrite; counters, ledgers, settings, roles etc. need a sealed file.
  Shared import limit BACKUP_MAX_IMPORT_BYTES (default 50 MB, max 256 MB)
  with warnings in log and UI; replaced document files are removed after
  commit.
- Post-commit side effects (automation, calendar sync, webhooks) run via
  runAfterCommit and never turn a committed write into a 500, so retries
  with the same Idempotency-Key no longer create duplicates.
- Calendar outbox is multi-replica safe: per-entry claims with stale
  takeover, revision-based settle, new Graph event only on 404/410,
  transactionId on create, 30 s Graph timeout, URL-encoded ids, GUID
  validation for groupId, "run now" in background and resets FAILED.
- Automation tasks are deduplicated under a row lock on the rule.
- Update DTOs follow one null rule: omitted = unchanged, null clears a
  nullable field, null on a required field is a 400.
- Varroa mites/day is recomputed on correction; withdrawal status is
  checked against the harvest date (?at=), bounded colonyIds; inspection
  create is audited; updates re-enqueue calendar entries.
- Dashboard, statistics and apiary/colony guards treat WEAK, CONSPICUOUS
  and QUEENLESS colonies as living.
- Prisma errors and body-parser errors are logged without query values or
  request bodies.
- Migration 20261012100000_calendar_sync_claims; schema index name aligned
  with the existing database object.

Frontend
- Sticky save bar sticks again (no scroll container ancestors, card uses
  overflow-clip), safe-area insets, scroll-padding, FAB hidden on forms,
  sticky desktop sidebar.
- Inspections record yes / no / not checked instead of silent defaults.
- All living colonies are selectable; shared RecordSelect, EnumSelect,
  ChoiceGroup and TriStateField replace per-page copies; selects show
  names instead of ids; labels point at real controls.
- Emptied optional fields are sent as null (emptyToNull) so every field
  can be cleared; dates default to the local day and are sent via
  toApiDate.
- Harvest withdrawal warning uses the harvest date; backup page warns when
  a backup outgrows the import limit and lists skipped tables.

Build and docs
- Docker builds fail unless lint, typecheck and unit tests pass.
- ARCHITECTURE.md updated (assumptions 9-12, 19-21, section 8) and spec
  docs/superpowers/specs/2026-10-12-plattform-rueckgrat-und-fahrplan-design.md
  for the backbone (api/worker/job roles, domain event outbox) and the
  feature roadmap T3-T7.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Nick SzaunigandClaude Opus 5.5 committed 2026-10-12 16:38:55 +02:00
1 parent f5b1964223
commit 10cbe9bf2d
310 files changed
+12177 -3746

No files matched your search

@@ -8,4 +8,5 @@ import { CreateVarroaMeasurementDto } from './create-varroa-measurement.dto';
*/
export class UpdateVarroaMeasurementDto extends PartialType(
CreateVarroaMeasurementDto,
{ skipNullProperties: false },
) {}
@@ -201,5 +201,143 @@ describe('VarroaService', () => {
];
expect(updateArg.data.calculatedValue).toBeUndefined();
});
describe('when an input of the derivation is corrected', () => {
const naturalMiteFall = {
...measurement,
method: 'NATURAL_MITE_FALL',
miteCount: 45,
durationMinutes: 3 * 24 * 60,
calculatedValue: 15,
};
async function storedCalculatedValueAfter(
before: object,
dto: Parameters<VarroaService['update']>[1],
): Promise<number | null | undefined> {
prisma.varroaMeasurement.findUnique.mockResolvedValue(before);
prisma.varroaMeasurement.update.mockResolvedValue(measurement);
await service.update('varroa-1', dto, 'user-1');
const [[updateArg]] = prisma.varroaMeasurement.update.mock.calls as [
[{ data: { calculatedValue?: number | null } }],
];
return updateArg.data.calculatedValue;
}
it('recomputes mites/day when the count changes', async () => {
await expect(
storedCalculatedValueAfter(naturalMiteFall, { miteCount: 60 }),
).resolves.toBe(20);
});
it('recomputes mites/day when the duration changes', async () => {
await expect(
storedCalculatedValueAfter(naturalMiteFall, {
durationMinutes: 5 * 24 * 60,
}),
).resolves.toBe(9);
});
it('recomputes when the edit form echoes the stored rate back unchanged', async () => {
await expect(
storedCalculatedValueAfter(naturalMiteFall, {
method: 'NATURAL_MITE_FALL',
miteCount: 60,
durationMinutes: 3 * 24 * 60,
calculatedValue: 15,
}),
).resolves.toBe(20);
});
it('keeps a changed rate as a manual override', async () => {
await expect(
storedCalculatedValueAfter(naturalMiteFall, {
miteCount: 60,
calculatedValue: 99,
}),
).resolves.toBe(99);
});
it('keeps a manually entered rate on an edit that echoes all fields back', async () => {
await expect(
storedCalculatedValueAfter(
{ ...naturalMiteFall, calculatedValue: 99 },
{
method: 'NATURAL_MITE_FALL',
miteCount: 45,
durationMinutes: 3 * 24 * 60,
calculatedValue: 99,
notes: 'Windel sauber',
},
),
).resolves.toBeUndefined();
});
it('clears a rate that can no longer be derived', async () => {
await expect(
storedCalculatedValueAfter(naturalMiteFall, { durationMinutes: 0 }),
).resolves.toBeNull();
});
it('clears the mites/day rate when the method changes to a non-time-based one', async () => {
await expect(
storedCalculatedValueAfter(naturalMiteFall, {
method: 'POWDERED_SUGAR',
}),
).resolves.toBeNull();
});
it('derives the rate when the method changes to natural mite fall', async () => {
await expect(
storedCalculatedValueAfter(
{
...naturalMiteFall,
method: 'POWDERED_SUGAR',
calculatedValue: 3,
},
{ method: 'NATURAL_MITE_FALL' },
),
).resolves.toBe(15);
});
// @IsOptional lets an explicit null through the validation pipe.
const clearedValue = { calculatedValue: null as unknown as number };
it('re-derives the rate when a natural mite fall value is cleared', async () => {
await expect(
storedCalculatedValueAfter(
{ ...naturalMiteFall, calculatedValue: 99 },
clearedValue,
),
).resolves.toBe(15);
});
it('clears the manual value of a non-time-based method on request', async () => {
await expect(
storedCalculatedValueAfter(
{
...naturalMiteFall,
method: 'POWDERED_SUGAR',
calculatedValue: 3,
},
clearedValue,
),
).resolves.toBeNull();
});
it('leaves a manual value of a non-time-based method alone when its count changes', async () => {
await expect(
storedCalculatedValueAfter(
{
...naturalMiteFall,
method: 'POWDERED_SUGAR',
durationMinutes: null,
calculatedValue: 3,
},
{ miteCount: 12 },
),
).resolves.toBeUndefined();
});
});
});
});
+53 -18
View File
@@ -2,7 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { AuditLogService } from '../audit/audit-log.service';
import { AutomationService } from '../automation/automation.service';
import { VarroaMeasurement } from '../../generated/prisma';
import { VarroaMeasurement, VarroaMethod } from '../../generated/prisma';
import { CreateVarroaMeasurementDto } from './dto/create-varroa-measurement.dto';
import { UpdateVarroaMeasurementDto } from './dto/update-varroa-measurement.dto';
@@ -30,6 +30,56 @@ function computeNaturalMiteFallPerDay(
return (miteCount / durationMinutes) * MINUTES_PER_DAY;
}
function deriveCalculatedValue(
method: VarroaMethod,
miteCount: number | null | undefined,
durationMinutes: number | null | undefined,
): number | undefined {
return method === 'NATURAL_MITE_FALL'
? computeNaturalMiteFallPerDay(miteCount, durationMinutes)
: undefined;
}
// calculatedValue for an edit, undefined meaning "leave the stored value".
// A value that differs from the stored one is a manual override and wins,
// as on create. Otherwise - no value sent, or the edit form echoing the
// stored one back - the value is derived again from the merged record
// whenever an input of the derivation changed (the method, and count or
// duration for a natural mite fall) or nothing is stored yet, exactly as
// create would derive it: a corrected count must never leave a stale
// mites/day rate (and threshold evaluation) behind. Unrelated edits keep
// the stored value, which may have been entered by hand.
function calculatedValueForUpdate(
before: VarroaMeasurement,
dto: UpdateVarroaMeasurementDto,
): number | null | undefined {
const sent = dto.calculatedValue;
if (sent !== undefined && sent !== null && sent !== before.calculatedValue) {
return sent;
}
const method = dto.method ?? before.method;
const miteCount =
dto.miteCount === undefined ? before.miteCount : dto.miteCount;
const durationMinutes =
dto.durationMinutes === undefined
? before.durationMinutes
: dto.durationMinutes;
const derivationInputChanged =
method !== before.method ||
(method === 'NATURAL_MITE_FALL' &&
(miteCount !== before.miteCount ||
durationMinutes !== before.durationMinutes));
// An explicit null asks for the value to be cleared - like a create
// without a value, that means "derive it where a rule exists".
const mustDerive =
derivationInputChanged || sent === null || before.calculatedValue === null;
if (!mustDerive) {
return undefined;
}
// null clears a value the merged record no longer supports.
return deriveCalculatedValue(method, miteCount, durationMinutes) ?? null;
}
@Injectable()
export class VarroaService {
constructor(
@@ -67,9 +117,7 @@ export class VarroaService {
): Promise<VarroaMeasurement> {
const calculatedValue =
dto.calculatedValue ??
(dto.method === 'NATURAL_MITE_FALL'
? computeNaturalMiteFallPerDay(dto.miteCount, dto.durationMinutes)
: undefined);
deriveCalculatedValue(dto.method, dto.miteCount, dto.durationMinutes);
const measurement = await this.prisma.varroaMeasurement.create({
data: {
...dto,
@@ -95,20 +143,7 @@ export class VarroaService {
actingUserId: string,
): Promise<VarroaMeasurement> {
const before = await this.findById(id);
// Only auto-fill calculatedValue when nothing has set it yet - never
// overwrite a value the user (or a prior auto-fill) already stored,
// even if this edit only touches an unrelated field like notes.
const shouldAutoCompute =
dto.calculatedValue === undefined && before.calculatedValue === null;
const method = dto.method ?? before.method;
const calculatedValue =
dto.calculatedValue ??
(shouldAutoCompute && method === 'NATURAL_MITE_FALL'
? computeNaturalMiteFallPerDay(
dto.miteCount ?? before.miteCount,
dto.durationMinutes ?? before.durationMinutes,
)
: undefined);
const calculatedValue = calculatedValueForUpdate(before, dto);
const measurement = await this.prisma.varroaMeasurement.update({
where: { id },
data: {