feat(backend): Abfuellungen und Produkte (Modul 23)
ProductsModule (GET/POST /products, GET/PATCH /products/:id) und BottlingModule (GET/POST /bottling-batches) mit transaktionaler Reduzierung der HoneyBatch-Restmenge beim Abfuellen; Ablehnung bei Abfuellmenge > verfuegbarer Menge. Neue Prisma-Modelle Product und BottlingBatch (Migration bottling_products).
This commit is contained in:
1 parent
8b7fd61c1c
commit
e82cae257b
17 files changed
+916
-2
No files matched your search
@@ -0,0 +1,108 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { AuditLogService } from '../audit/audit-log.service';
|
||||
import { BottlingBatch } from '../../generated/prisma';
|
||||
import { CreateBottlingBatchDto } from './dto/create-bottling-batch.dto';
|
||||
|
||||
const bottlingBatchInclude = {
|
||||
honeyBatch: true,
|
||||
product: true,
|
||||
} as const;
|
||||
|
||||
export interface BottlingBatchFilter {
|
||||
honeyBatchId?: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class BottlingService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly auditLog: AuditLogService,
|
||||
) {}
|
||||
|
||||
async findAll(filter: BottlingBatchFilter = {}) {
|
||||
return this.prisma.bottlingBatch.findMany({
|
||||
where: { honeyBatchId: filter.honeyBatchId },
|
||||
include: bottlingBatchInclude,
|
||||
orderBy: { date: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
async findById(id: string) {
|
||||
const bottlingBatch = await this.prisma.bottlingBatch.findUnique({
|
||||
where: { id },
|
||||
include: bottlingBatchInclude,
|
||||
});
|
||||
if (!bottlingBatch) {
|
||||
throw new NotFoundException('Bottling batch not found');
|
||||
}
|
||||
return bottlingBatch;
|
||||
}
|
||||
|
||||
async create(dto: CreateBottlingBatchDto, actingUserId: string) {
|
||||
const honeyBatch = await this.prisma.honeyBatch.findUnique({
|
||||
where: { id: dto.honeyBatchId },
|
||||
});
|
||||
if (!honeyBatch) {
|
||||
throw new NotFoundException('Honey batch not found');
|
||||
}
|
||||
if (dto.productId) {
|
||||
const product = await this.prisma.product.findUnique({
|
||||
where: { id: dto.productId },
|
||||
});
|
||||
if (!product) {
|
||||
throw new NotFoundException('Product not found');
|
||||
}
|
||||
}
|
||||
|
||||
const totalAmount = (dto.jarSize * dto.jarCount) / 1000;
|
||||
if (honeyBatch.amount !== null && totalAmount > honeyBatch.amount) {
|
||||
throw new BadRequestException(
|
||||
'Abfüllmenge übersteigt die verfügbare Menge der Honigcharge',
|
||||
);
|
||||
}
|
||||
|
||||
const bottlingBatch: BottlingBatch = await this.prisma.$transaction(
|
||||
async (tx) => {
|
||||
const created = await tx.bottlingBatch.create({
|
||||
data: {
|
||||
honeyBatchId: dto.honeyBatchId,
|
||||
productId: dto.productId,
|
||||
date: new Date(dto.date),
|
||||
jarSize: dto.jarSize,
|
||||
jarCount: dto.jarCount,
|
||||
totalAmount,
|
||||
bestBeforeDate: new Date(dto.bestBeforeDate),
|
||||
lotNumber: dto.lotNumber,
|
||||
userId: actingUserId,
|
||||
},
|
||||
});
|
||||
await tx.honeyBatch.update({
|
||||
where: { id: dto.honeyBatchId },
|
||||
data: {
|
||||
amount:
|
||||
honeyBatch.amount !== null
|
||||
? honeyBatch.amount - totalAmount
|
||||
: null,
|
||||
status: 'BOTTLED',
|
||||
},
|
||||
});
|
||||
return created;
|
||||
},
|
||||
);
|
||||
|
||||
await this.auditLog.record({
|
||||
entityType: 'BottlingBatch',
|
||||
entityId: bottlingBatch.id,
|
||||
action: 'CREATE',
|
||||
userId: actingUserId,
|
||||
afterData: bottlingBatch,
|
||||
});
|
||||
|
||||
return this.findById(bottlingBatch.id);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user