feat(backend): Varroamessungen-Modul (Modul 7)
VarroaModule mit Erfassung und Colony-Historie-Endpunkt fuer Varroamessungen.
This commit is contained in:
1 parent
632d382474
commit
f363bffed1
9 files changed
+351
-1
No files matched your search
@@ -11,6 +11,7 @@ import { ColoniesModule } from './colonies/colonies.module';
|
||||
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 { JwtAuthGuard } from './auth/guards/jwt-auth.guard';
|
||||
import { RolesGuard } from './auth/guards/roles.guard';
|
||||
|
||||
@@ -27,6 +28,7 @@ import { RolesGuard } from './auth/guards/roles.guard';
|
||||
QueensModule,
|
||||
InspectionsModule,
|
||||
TasksModule,
|
||||
VarroaModule,
|
||||
],
|
||||
controllers: [],
|
||||
providers: [
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Controller, Get, Param } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { VarroaService } from './varroa.service';
|
||||
|
||||
@ApiTags('varroa-measurements')
|
||||
@ApiBearerAuth()
|
||||
@Controller('colonies')
|
||||
export class ColonyVarroaMeasurementsController {
|
||||
constructor(private readonly varroaService: VarroaService) {}
|
||||
|
||||
@Get(':id/varroa-measurements')
|
||||
findByColony(@Param('id') id: string) {
|
||||
return this.varroaService.findByColony(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import {
|
||||
IsDateString,
|
||||
IsEnum,
|
||||
IsInt,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from 'class-validator';
|
||||
import { VarroaMethod } from '../../../generated/prisma';
|
||||
|
||||
export class CreateVarroaMeasurementDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
colonyId!: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsDateString()
|
||||
date!: string;
|
||||
|
||||
@ApiProperty({ enum: VarroaMethod })
|
||||
@IsEnum(VarroaMethod)
|
||||
method!: VarroaMethod;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
durationMinutes?: number;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
miteCount?: number;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
calculatedValue?: number;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
assessment?: 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 { CreateVarroaMeasurementDto } from './dto/create-varroa-measurement.dto';
|
||||
import { VarroaService } from './varroa.service';
|
||||
|
||||
@ApiTags('varroa-measurements')
|
||||
@ApiBearerAuth()
|
||||
@Controller('varroa-measurements')
|
||||
export class VarroaController {
|
||||
constructor(private readonly varroaService: VarroaService) {}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.varroaService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.varroaService.findById(id);
|
||||
}
|
||||
|
||||
@Roles(Role.ADMIN, Role.BEEKEEPER)
|
||||
@Post()
|
||||
create(
|
||||
@Body() dto: CreateVarroaMeasurementDto,
|
||||
@CurrentUser() actor: AuthenticatedUser,
|
||||
) {
|
||||
return this.varroaService.create(dto, actor.id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { VarroaController } from './varroa.controller';
|
||||
import { ColonyVarroaMeasurementsController } from './colony-varroa-measurements.controller';
|
||||
import { VarroaService } from './varroa.service';
|
||||
|
||||
@Module({
|
||||
controllers: [VarroaController, ColonyVarroaMeasurementsController],
|
||||
providers: [VarroaService],
|
||||
exports: [VarroaService],
|
||||
})
|
||||
export class VarroaModule {}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
import { VarroaService } from './varroa.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { AuditLogService } from '../audit/audit-log.service';
|
||||
|
||||
describe('VarroaService', () => {
|
||||
let service: VarroaService;
|
||||
let prisma: {
|
||||
varroaMeasurement: {
|
||||
findMany: jest.Mock;
|
||||
findUnique: jest.Mock;
|
||||
create: jest.Mock;
|
||||
};
|
||||
};
|
||||
let auditLog: { record: jest.Mock };
|
||||
|
||||
const measurement = {
|
||||
id: 'varroa-1',
|
||||
colonyId: 'colony-1',
|
||||
date: new Date(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
prisma = {
|
||||
varroaMeasurement: {
|
||||
findMany: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
},
|
||||
};
|
||||
auditLog = { record: jest.fn() };
|
||||
service = new VarroaService(
|
||||
prisma as unknown as PrismaService,
|
||||
auditLog as unknown as AuditLogService,
|
||||
);
|
||||
});
|
||||
|
||||
it('findById throws NotFoundException when missing', async () => {
|
||||
prisma.varroaMeasurement.findUnique.mockResolvedValue(null);
|
||||
await expect(service.findById('missing')).rejects.toBeInstanceOf(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it('create stores the acting user as the measurer', async () => {
|
||||
prisma.varroaMeasurement.create.mockResolvedValue(measurement);
|
||||
|
||||
await service.create(
|
||||
{ colonyId: 'colony-1', date: '2026-01-01', method: 'NATURAL_MITE_FALL' },
|
||||
'user-1',
|
||||
);
|
||||
|
||||
const [[createArg]] = prisma.varroaMeasurement.create.mock.calls as [
|
||||
[{ data: { colonyId: string; userId: string } }],
|
||||
];
|
||||
expect(createArg.data.colonyId).toBe('colony-1');
|
||||
expect(createArg.data.userId).toBe('user-1');
|
||||
});
|
||||
|
||||
it('findByColony orders results chronologically', async () => {
|
||||
prisma.varroaMeasurement.findMany.mockResolvedValue([measurement]);
|
||||
|
||||
await service.findByColony('colony-1');
|
||||
|
||||
const [[queryArg]] = prisma.varroaMeasurement.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,53 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { AuditLogService } from '../audit/audit-log.service';
|
||||
import { VarroaMeasurement } from '../../generated/prisma';
|
||||
import { CreateVarroaMeasurementDto } from './dto/create-varroa-measurement.dto';
|
||||
|
||||
@Injectable()
|
||||
export class VarroaService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly auditLog: AuditLogService,
|
||||
) {}
|
||||
|
||||
async findAll(): Promise<VarroaMeasurement[]> {
|
||||
return this.prisma.varroaMeasurement.findMany({
|
||||
orderBy: { date: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<VarroaMeasurement> {
|
||||
const measurement = await this.prisma.varroaMeasurement.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
if (!measurement) {
|
||||
throw new NotFoundException('Varroa measurement not found');
|
||||
}
|
||||
return measurement;
|
||||
}
|
||||
|
||||
async findByColony(colonyId: string): Promise<VarroaMeasurement[]> {
|
||||
return this.prisma.varroaMeasurement.findMany({
|
||||
where: { colonyId },
|
||||
orderBy: { date: 'asc' },
|
||||
});
|
||||
}
|
||||
|
||||
async create(
|
||||
dto: CreateVarroaMeasurementDto,
|
||||
actingUserId: string,
|
||||
): Promise<VarroaMeasurement> {
|
||||
const measurement = await this.prisma.varroaMeasurement.create({
|
||||
data: { ...dto, date: new Date(dto.date), userId: actingUserId },
|
||||
});
|
||||
await this.auditLog.record({
|
||||
entityType: 'VarroaMeasurement',
|
||||
entityId: measurement.id,
|
||||
action: 'CREATE',
|
||||
userId: actingUserId,
|
||||
afterData: measurement,
|
||||
});
|
||||
return measurement;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user