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'; import type { AuthenticatedUser } from '../auth/types/authenticated-user'; import { CreateTreatmentDto } from './dto/create-treatment.dto'; import { UpdateTreatmentDto } from './dto/update-treatment.dto'; import { WithdrawalStatusQueryDto } from './dto/withdrawal-status-query.dto'; import { LastByProductQueryDto } from './dto/last-by-product-query.dto'; import { TreatmentPrefillService } from './treatment-prefill.service'; import { TreatmentsService } from './treatments.service'; import { Idempotent } from '../common/idempotency/idempotent.decorator'; @ApiTags('treatments') @ApiBearerAuth() @Controller('treatments') export class TreatmentsController { constructor( private readonly treatmentsService: TreatmentsService, private readonly treatmentPrefillService: TreatmentPrefillService, ) {} @RequirePermission('treatments:read') @Get() findAll() { return this.treatmentsService.findAll(); } /** Active withdrawal windows per colony at `at` (the harvest date), default now. */ @RequirePermission('treatments:read') @Get('withdrawal-status') getWithdrawalStatus(@Query() query: WithdrawalStatusQueryDto) { return this.treatmentsService.getWithdrawalStatus( query.colonyIds ?? [], query.at ? new Date(query.at) : new Date(), ); } /** The caller's newest treatment with this product, to prefill a new one. */ @RequirePermission('treatments:create') @Get('last-by-product') async getLastByProduct( @Query() query: LastByProductQueryDto, @CurrentUser() actor: AuthenticatedUser, ) { return { treatment: await this.treatmentPrefillService.lastByProduct( actor.id, query.product, ), }; } @RequirePermission('treatments:read') @Get(':id') findOne(@Param('id') id: string) { return this.treatmentsService.findById(id); } @RequirePermission('treatments:create') @Idempotent() @Post() create( @Body() dto: CreateTreatmentDto, @CurrentUser() actor: AuthenticatedUser, ) { return this.treatmentsService.create(dto, actor.id); } @RequirePermission('treatments:update') @Patch(':id') update( @Param('id') id: string, @Body() dto: UpdateTreatmentDto, @CurrentUser() actor: AuthenticatedUser, ) { return this.treatmentsService.update(id, dto, actor.id); } }