feat(backend): Behandlungen-Modul (Modul 8)
TreatmentsModule mit Erfassung und Colony-Historie-Endpunkt fuer Tierarzneimittel-Behandlungen.
This commit is contained in:
1 parent
66c14cf306
commit
61e0bbe794
9 files changed
+360
-1
No files matched your search
@@ -12,6 +12,7 @@ import { QueensModule } from './queens/queens.module';
|
||||
import { InspectionsModule } from './inspections/inspections.module';
|
||||
import { TasksModule } from './tasks/tasks.module';
|
||||
import { VarroaModule } from './varroa/varroa.module';
|
||||
import { TreatmentsModule } from './treatments/treatments.module';
|
||||
import { JwtAuthGuard } from './auth/guards/jwt-auth.guard';
|
||||
import { RolesGuard } from './auth/guards/roles.guard';
|
||||
|
||||
@@ -29,6 +30,7 @@ import { RolesGuard } from './auth/guards/roles.guard';
|
||||
InspectionsModule,
|
||||
TasksModule,
|
||||
VarroaModule,
|
||||
TreatmentsModule,
|
||||
],
|
||||
controllers: [],
|
||||
providers: [
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Controller, Get, Param } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { TreatmentsService } from './treatments.service';
|
||||
|
||||
@ApiTags('treatments')
|
||||
@ApiBearerAuth()
|
||||
@Controller('colonies')
|
||||
export class ColonyTreatmentsController {
|
||||
constructor(private readonly treatmentsService: TreatmentsService) {}
|
||||
|
||||
@Get(':id/treatments')
|
||||
findByColony(@Param('id') id: string) {
|
||||
return this.treatmentsService.findByColony(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsDateString, IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class CreateTreatmentDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
colonyId!: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsDateString()
|
||||
date!: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
startDate?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
endDate?: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
product!: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
activeIngredient?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
amount?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
method?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
reason?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
batchNumber?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
notes?: string;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { Role } from '../../generated/prisma';
|
||||
import { Roles } from '../auth/decorators/roles.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 { TreatmentsService } from './treatments.service';
|
||||
|
||||
@ApiTags('treatments')
|
||||
@ApiBearerAuth()
|
||||
@Controller('treatments')
|
||||
export class TreatmentsController {
|
||||
constructor(private readonly treatmentsService: TreatmentsService) {}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.treatmentsService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.treatmentsService.findById(id);
|
||||
}
|
||||
|
||||
@Roles(Role.ADMIN, Role.BEEKEEPER)
|
||||
@Post()
|
||||
create(
|
||||
@Body() dto: CreateTreatmentDto,
|
||||
@CurrentUser() actor: AuthenticatedUser,
|
||||
) {
|
||||
return this.treatmentsService.create(dto, actor.id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TreatmentsController } from './treatments.controller';
|
||||
import { ColonyTreatmentsController } from './colony-treatments.controller';
|
||||
import { TreatmentsService } from './treatments.service';
|
||||
|
||||
@Module({
|
||||
controllers: [TreatmentsController, ColonyTreatmentsController],
|
||||
providers: [TreatmentsService],
|
||||
exports: [TreatmentsService],
|
||||
})
|
||||
export class TreatmentsModule {}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
import { TreatmentsService } from './treatments.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { AuditLogService } from '../audit/audit-log.service';
|
||||
|
||||
describe('TreatmentsService', () => {
|
||||
let service: TreatmentsService;
|
||||
let prisma: {
|
||||
treatment: {
|
||||
findMany: jest.Mock;
|
||||
findUnique: jest.Mock;
|
||||
create: jest.Mock;
|
||||
};
|
||||
};
|
||||
let auditLog: { record: jest.Mock };
|
||||
|
||||
const treatment = {
|
||||
id: 'treatment-1',
|
||||
colonyId: 'colony-1',
|
||||
date: new Date(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
prisma = {
|
||||
treatment: {
|
||||
findMany: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
},
|
||||
};
|
||||
auditLog = { record: jest.fn() };
|
||||
service = new TreatmentsService(
|
||||
prisma as unknown as PrismaService,
|
||||
auditLog as unknown as AuditLogService,
|
||||
);
|
||||
});
|
||||
|
||||
it('findById throws NotFoundException when missing', async () => {
|
||||
prisma.treatment.findUnique.mockResolvedValue(null);
|
||||
await expect(service.findById('missing')).rejects.toBeInstanceOf(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it('create stores the acting user and product', async () => {
|
||||
prisma.treatment.create.mockResolvedValue(treatment);
|
||||
|
||||
await service.create(
|
||||
{ colonyId: 'colony-1', date: '2026-01-01', product: 'Ameisensäure' },
|
||||
'user-1',
|
||||
);
|
||||
|
||||
const [[createArg]] = prisma.treatment.create.mock.calls as [
|
||||
[{ data: { colonyId: string; userId: string; product: string } }],
|
||||
];
|
||||
expect(createArg.data.colonyId).toBe('colony-1');
|
||||
expect(createArg.data.userId).toBe('user-1');
|
||||
expect(createArg.data.product).toBe('Ameisensäure');
|
||||
});
|
||||
|
||||
it('findByColony orders results chronologically', async () => {
|
||||
prisma.treatment.findMany.mockResolvedValue([treatment]);
|
||||
|
||||
await service.findByColony('colony-1');
|
||||
|
||||
const [[queryArg]] = prisma.treatment.findMany.mock.calls as [
|
||||
[{ where: { colonyId: string }; orderBy: { date: string } }],
|
||||
];
|
||||
expect(queryArg.where.colonyId).toBe('colony-1');
|
||||
expect(queryArg.orderBy.date).toBe('asc');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { AuditLogService } from '../audit/audit-log.service';
|
||||
import { Treatment } from '../../generated/prisma';
|
||||
import { CreateTreatmentDto } from './dto/create-treatment.dto';
|
||||
|
||||
@Injectable()
|
||||
export class TreatmentsService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly auditLog: AuditLogService,
|
||||
) {}
|
||||
|
||||
async findAll(): Promise<Treatment[]> {
|
||||
return this.prisma.treatment.findMany({ orderBy: { date: 'desc' } });
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Treatment> {
|
||||
const treatment = await this.prisma.treatment.findUnique({ where: { id } });
|
||||
if (!treatment) {
|
||||
throw new NotFoundException('Treatment not found');
|
||||
}
|
||||
return treatment;
|
||||
}
|
||||
|
||||
async findByColony(colonyId: string): Promise<Treatment[]> {
|
||||
return this.prisma.treatment.findMany({
|
||||
where: { colonyId },
|
||||
orderBy: { date: 'asc' },
|
||||
});
|
||||
}
|
||||
|
||||
async create(
|
||||
dto: CreateTreatmentDto,
|
||||
actingUserId: string,
|
||||
): Promise<Treatment> {
|
||||
const treatment = await this.prisma.treatment.create({
|
||||
data: {
|
||||
...dto,
|
||||
date: new Date(dto.date),
|
||||
startDate: dto.startDate ? new Date(dto.startDate) : undefined,
|
||||
endDate: dto.endDate ? new Date(dto.endDate) : undefined,
|
||||
userId: actingUserId,
|
||||
},
|
||||
});
|
||||
await this.auditLog.record({
|
||||
entityType: 'Treatment',
|
||||
entityId: treatment.id,
|
||||
action: 'CREATE',
|
||||
userId: actingUserId,
|
||||
afterData: treatment,
|
||||
});
|
||||
return treatment;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user