feat: warn against harvesting honey during a treatment's Wartezeit

Treatments (Ameisensäure, Oxalsäure, etc.) can require a legally
mandated withdrawal period before honey from that colony may be sold.
Nothing in the app tracked this before, so a beekeeper had no
safeguard against harvesting into an active withdrawal window.

Treatments now optionally record withdrawalPeriodDays (from the
product's Packungsbeilage). The new harvest form fetches each
candidate colony's active withdrawal window and shows an inline
warning naming the product and the date it clears, computed as
max(endDate ?? date) + withdrawalPeriodDays across a colony's
treatments, restricted to windows still in the future.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
Nick SzaunigandClaude Sonnet 5 committed 2026-09-06 02:50:34 +02:00
1 parent 0881a38800
commit d42f8331f9
13 files changed
+270 -56

No files matched your search

@@ -1,5 +1,11 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsDateString, IsOptional, IsString } from 'class-validator';
import {
IsDateString,
IsInt,
IsOptional,
IsString,
Min,
} from 'class-validator';
export class CreateTreatmentDto {
@ApiProperty()
@@ -53,4 +59,13 @@ export class CreateTreatmentDto {
@IsOptional()
@IsString()
notes?: string;
@ApiPropertyOptional({
description:
'Wartezeit in Tagen ab Behandlungsende, bevor Honig aus diesem Volk geerntet werden darf.',
})
@IsOptional()
@IsInt()
@Min(0)
withdrawalPeriodDays?: number;
}
@@ -13,5 +13,6 @@ export class UpdateTreatmentDto extends PartialType(
'reason',
'batchNumber',
'notes',
'withdrawalPeriodDays',
] as const),
) {}
@@ -1,4 +1,12 @@
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common';
import {
Body,
Controller,
Get,
Param,
Patch,
Post,
Query,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { RequirePermission } from '../auth/decorators/require-permission.decorator';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
@@ -19,6 +27,13 @@ export class TreatmentsController {
return this.treatmentsService.findAll();
}
@RequirePermission('treatments:read')
@Get('withdrawal-status')
getWithdrawalStatus(@Query('colonyIds') colonyIds?: string) {
const ids = colonyIds ? colonyIds.split(',').filter(Boolean) : [];
return this.treatmentsService.getWithdrawalStatus(ids);
}
@RequirePermission('treatments:read')
@Get(':id')
findOne(@Param('id') id: string) {
@@ -97,4 +97,58 @@ describe('TreatmentsService', () => {
expect(queryArg.where.colonyId).toBe('colony-1');
expect(queryArg.orderBy.date).toBe('asc');
});
describe('getWithdrawalStatus', () => {
it('returns an empty array without querying when no colony ids are given', async () => {
const result = await service.getWithdrawalStatus([]);
expect(result).toEqual([]);
expect(prisma.treatment.findMany).not.toHaveBeenCalled();
});
it('reports the treatment whose withdrawal window reaches furthest into the future', async () => {
const now = Date.now();
const inTenDays = new Date(now + 10 * 24 * 60 * 60 * 1000);
const inFiveDays = new Date(now + 5 * 24 * 60 * 60 * 1000);
prisma.treatment.findMany.mockResolvedValue([
{
colonyId: 'colony-1',
date: inTenDays,
endDate: null,
withdrawalPeriodDays: 0,
product: 'Ameisensäure',
},
{
colonyId: 'colony-1',
date: inFiveDays,
endDate: null,
withdrawalPeriodDays: 21,
product: 'Api Bioxal',
},
]);
const result = await service.getWithdrawalStatus(['colony-1']);
expect(result).toHaveLength(1);
expect(result[0].colonyId).toBe('colony-1');
expect(result[0].product).toBe('Api Bioxal');
});
it('omits a colony whose withdrawal window has already passed', async () => {
const longAgo = new Date('2020-01-01');
prisma.treatment.findMany.mockResolvedValue([
{
colonyId: 'colony-1',
date: longAgo,
endDate: null,
withdrawalPeriodDays: 14,
product: 'Ameisensäure',
},
]);
const result = await service.getWithdrawalStatus(['colony-1']);
expect(result).toEqual([]);
});
});
});
@@ -6,6 +6,12 @@ import { Treatment } from '../../generated/prisma';
import { CreateTreatmentDto } from './dto/create-treatment.dto';
import { UpdateTreatmentDto } from './dto/update-treatment.dto';
export interface WithdrawalStatus {
colonyId: string;
activeUntil: string;
product: string;
}
@Injectable()
export class TreatmentsService {
constructor(
@@ -33,6 +39,44 @@ export class TreatmentsService {
});
}
// For each colony, returns the treatment whose withdrawal window (its
// end date, or start date if the treatment is still ongoing, plus its
// withdrawalPeriodDays) reaches furthest into the future - only when
// that window is still active. Colonies with no active window are
// omitted rather than returned as null, since most colonies have none.
async getWithdrawalStatus(colonyIds: string[]): Promise<WithdrawalStatus[]> {
if (colonyIds.length === 0) {
return [];
}
const treatments = await this.prisma.treatment.findMany({
where: {
colonyId: { in: colonyIds },
withdrawalPeriodDays: { not: null },
},
});
const now = new Date();
const activeByColony = new Map<string, WithdrawalStatus>();
for (const treatment of treatments) {
const referenceDate = treatment.endDate ?? treatment.date;
const activeUntil = new Date(referenceDate);
activeUntil.setUTCDate(
activeUntil.getUTCDate() + (treatment.withdrawalPeriodDays ?? 0),
);
if (activeUntil <= now) {
continue;
}
const current = activeByColony.get(treatment.colonyId);
if (!current || activeUntil > new Date(current.activeUntil)) {
activeByColony.set(treatment.colonyId, {
colonyId: treatment.colonyId,
activeUntil: activeUntil.toISOString(),
product: treatment.product,
});
}
}
return [...activeByColony.values()];
}
async create(
dto: CreateTreatmentDto,
actingUserId: string,