feat(backend): Ernten- und Schleuderungs-Module (Modul 10)

HarvestsModule und ExtractionsModule als Fundament der
Rückverfolgbarkeitskette: eine Ernte kann mehrere Völker umfassen,
eine Schleuderung mehrere Ernten zusammenführen.
This commit is contained in:
Nick Szaunig committed 2026-08-22 00:25:36 +02:00
1 parent d71785d531
commit 340397b34d
14 files changed
+705 -1

No files matched your search

@@ -0,0 +1,69 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { AuditLogService } from '../audit/audit-log.service';
import { CreateExtractionDto } from './dto/create-extraction.dto';
const extractionInclude = {
harvests: {
include: {
harvest: { include: { colonies: { include: { colony: true } } } },
},
},
} as const;
@Injectable()
export class ExtractionsService {
constructor(
private readonly prisma: PrismaService,
private readonly auditLog: AuditLogService,
) {}
async findAll() {
return this.prisma.extraction.findMany({
include: extractionInclude,
orderBy: { date: 'desc' },
});
}
async findById(id: string) {
const extraction = await this.prisma.extraction.findUnique({
where: { id },
include: extractionInclude,
});
if (!extraction) {
throw new NotFoundException('Extraction not found');
}
return extraction;
}
async create(dto: CreateExtractionDto, actingUserId: string) {
const extraction = await this.prisma.$transaction(async (tx) => {
const created = await tx.extraction.create({
data: {
date: new Date(dto.date),
amount: dto.amount,
room: dto.room,
waterContent: dto.waterContent,
temperature: dto.temperature,
notes: dto.notes,
userId: actingUserId,
},
});
await tx.extractionHarvest.createMany({
data: dto.harvestIds.map((harvestId) => ({
extractionId: created.id,
harvestId,
})),
});
return created;
});
await this.auditLog.record({
entityType: 'Extraction',
entityId: extraction.id,
action: 'CREATE',
userId: actingUserId,
afterData: extraction,
});
return this.findById(extraction.id);
}
}