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:
Nick Szaunig committed 2026-08-22 11:08:09 +02:00
1 parent 8b7fd61c1c
commit e82cae257b
17 files changed
+916 -2

No files matched your search

+4
View File
@@ -22,6 +22,8 @@ import { DashboardModule } from './dashboard/dashboard.module';
import { SearchModule } from './search/search.module';
import { ExportModule } from './export/export.module';
import { QrModule } from './qr/qr.module';
import { ProductsModule } from './products/products.module';
import { BottlingModule } from './bottling/bottling.module';
import { JwtAuthGuard } from './auth/guards/jwt-auth.guard';
import { RolesGuard } from './auth/guards/roles.guard';
@@ -49,6 +51,8 @@ import { RolesGuard } from './auth/guards/roles.guard';
SearchModule,
ExportModule,
QrModule,
ProductsModule,
BottlingModule,
],
controllers: [],
providers: [
@@ -0,0 +1,34 @@
import { Body, Controller, Get, Param, Post, Query } 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 { CreateBottlingBatchDto } from './dto/create-bottling-batch.dto';
import { BottlingService } from './bottling.service';
@ApiTags('bottling')
@ApiBearerAuth()
@Controller('bottling-batches')
export class BottlingController {
constructor(private readonly bottlingService: BottlingService) {}
@Get()
findAll(@Query('honeyBatchId') honeyBatchId?: string) {
return this.bottlingService.findAll({ honeyBatchId });
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.bottlingService.findById(id);
}
@Roles(Role.ADMIN, Role.BEEKEEPER)
@Post()
create(
@Body() dto: CreateBottlingBatchDto,
@CurrentUser() actor: AuthenticatedUser,
) {
return this.bottlingService.create(dto, actor.id);
}
}
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { BottlingController } from './bottling.controller';
import { BottlingService } from './bottling.service';
@Module({
controllers: [BottlingController],
providers: [BottlingService],
})
export class BottlingModule {}
@@ -0,0 +1,124 @@
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { BottlingService } from './bottling.service';
import { PrismaService } from '../prisma/prisma.service';
import { AuditLogService } from '../audit/audit-log.service';
describe('BottlingService', () => {
let service: BottlingService;
let prisma: {
honeyBatch: { findUnique: jest.Mock; update: jest.Mock };
product: { findUnique: jest.Mock };
bottlingBatch: {
findUnique: jest.Mock;
findMany: jest.Mock;
create: jest.Mock;
};
$transaction: jest.Mock;
};
let auditLog: { record: jest.Mock };
let tx: {
bottlingBatch: { create: jest.Mock };
honeyBatch: { update: jest.Mock };
};
beforeEach(() => {
tx = {
bottlingBatch: { create: jest.fn() },
honeyBatch: { update: jest.fn() },
};
prisma = {
honeyBatch: { findUnique: jest.fn(), update: jest.fn() },
product: { findUnique: jest.fn() },
bottlingBatch: {
findUnique: jest.fn(),
findMany: jest.fn(),
create: jest.fn(),
},
$transaction: jest.fn((callback: (txArg: typeof tx) => unknown) =>
callback(tx),
),
};
auditLog = { record: jest.fn() };
service = new BottlingService(
prisma as unknown as PrismaService,
auditLog as unknown as AuditLogService,
);
});
it('throws NotFoundException when the honey batch does not exist', async () => {
prisma.honeyBatch.findUnique.mockResolvedValueOnce(null);
await expect(
service.create(
{
honeyBatchId: 'missing',
date: '2026-01-01',
jarSize: 500,
jarCount: 10,
bestBeforeDate: '2027-01-01',
lotNumber: 'L-1',
},
'user-1',
),
).rejects.toBeInstanceOf(NotFoundException);
});
it('rejects when the bottled amount exceeds the available honey batch amount', async () => {
prisma.honeyBatch.findUnique.mockResolvedValueOnce({
id: 'h1',
amount: 5,
});
await expect(
service.create(
{
honeyBatchId: 'h1',
date: '2026-01-01',
jarSize: 500,
jarCount: 20,
bestBeforeDate: '2027-01-01',
lotNumber: 'L-1',
},
'user-1',
),
).rejects.toBeInstanceOf(BadRequestException);
});
it('creates a bottling batch and reduces the honey batch amount transactionally', async () => {
prisma.honeyBatch.findUnique.mockResolvedValueOnce({
id: 'h1',
amount: 25,
});
tx.bottlingBatch.create.mockResolvedValueOnce({
id: 'b1',
totalAmount: 25,
});
prisma.bottlingBatch.findUnique.mockResolvedValueOnce({
id: 'b1',
totalAmount: 25,
});
const result = await service.create(
{
honeyBatchId: 'h1',
date: '2026-01-01',
jarSize: 500,
jarCount: 50,
bestBeforeDate: '2027-01-01',
lotNumber: 'L-1',
},
'user-1',
);
expect(result).toEqual({ id: 'b1', totalAmount: 25 });
expect(tx.honeyBatch.update).toHaveBeenCalledWith({
where: { id: 'h1' },
data: { amount: 0, status: 'BOTTLED' },
});
expect(auditLog.record).toHaveBeenCalledWith(
expect.objectContaining({
entityType: 'BottlingBatch',
action: 'CREATE',
}),
);
});
});
@@ -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);
}
}
@@ -0,0 +1,42 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
IsDateString,
IsInt,
IsNumber,
IsOptional,
IsPositive,
IsString,
} from 'class-validator';
export class CreateBottlingBatchDto {
@ApiProperty()
@IsString()
honeyBatchId!: string;
@ApiPropertyOptional()
@IsOptional()
@IsString()
productId?: string;
@ApiProperty()
@IsDateString()
date!: string;
@ApiProperty({ description: 'Glasgröße in Gramm' })
@IsNumber()
@IsPositive()
jarSize!: number;
@ApiProperty()
@IsInt()
@IsPositive()
jarCount!: number;
@ApiProperty()
@IsDateString()
bestBeforeDate!: string;
@ApiProperty()
@IsString()
lotNumber!: string;
}
@@ -0,0 +1,33 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsNumber, IsOptional, IsString } from 'class-validator';
export class CreateProductDto {
@ApiProperty()
@IsString()
name!: string;
@ApiPropertyOptional()
@IsOptional()
@IsString()
description?: string;
@ApiPropertyOptional()
@IsOptional()
@IsString()
sku?: string;
@ApiPropertyOptional()
@IsOptional()
@IsNumber()
packagingSize?: number;
@ApiPropertyOptional()
@IsOptional()
@IsNumber()
weight?: number;
@ApiPropertyOptional()
@IsOptional()
@IsString()
salesUnit?: string;
}
@@ -0,0 +1,11 @@
import { ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { IsEnum, IsOptional } from 'class-validator';
import { ProductStatus } from '../../../generated/prisma';
import { CreateProductDto } from './create-product.dto';
export class UpdateProductDto extends PartialType(CreateProductDto) {
@ApiPropertyOptional({ enum: ProductStatus })
@IsOptional()
@IsEnum(ProductStatus)
status?: ProductStatus;
}
@@ -0,0 +1,53 @@
import {
Body,
Controller,
Get,
Param,
Patch,
Post,
Query,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { ProductStatus, 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 { CreateProductDto } from './dto/create-product.dto';
import { UpdateProductDto } from './dto/update-product.dto';
import { ProductsService } from './products.service';
@ApiTags('products')
@ApiBearerAuth()
@Controller('products')
export class ProductsController {
constructor(private readonly productsService: ProductsService) {}
@Get()
findAll(@Query('status') status?: ProductStatus) {
return this.productsService.findAll({ status });
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.productsService.findById(id);
}
@Roles(Role.ADMIN, Role.BEEKEEPER)
@Post()
create(
@Body() dto: CreateProductDto,
@CurrentUser() actor: AuthenticatedUser,
) {
return this.productsService.create(dto, actor.id);
}
@Roles(Role.ADMIN, Role.BEEKEEPER)
@Patch(':id')
update(
@Param('id') id: string,
@Body() dto: UpdateProductDto,
@CurrentUser() actor: AuthenticatedUser,
) {
return this.productsService.update(id, dto, actor.id);
}
}
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { ProductsController } from './products.controller';
import { ProductsService } from './products.service';
@Module({
controllers: [ProductsController],
providers: [ProductsService],
exports: [ProductsService],
})
export class ProductsModule {}
@@ -0,0 +1,74 @@
import { NotFoundException } from '@nestjs/common';
import { ProductsService } from './products.service';
import { PrismaService } from '../prisma/prisma.service';
import { AuditLogService } from '../audit/audit-log.service';
describe('ProductsService', () => {
let service: ProductsService;
let prisma: {
product: {
findMany: jest.Mock;
findUnique: jest.Mock;
create: jest.Mock;
update: jest.Mock;
};
};
let auditLog: { record: jest.Mock };
beforeEach(() => {
prisma = {
product: {
findMany: jest.fn(),
findUnique: jest.fn(),
create: jest.fn(),
update: jest.fn(),
},
};
auditLog = { record: jest.fn() };
service = new ProductsService(
prisma as unknown as PrismaService,
auditLog as unknown as AuditLogService,
);
});
it('creates a product and records an audit log entry', async () => {
const created = { id: 'p1', name: 'Sommerhonig' };
prisma.product.create.mockResolvedValueOnce(created);
const result = await service.create({ name: 'Sommerhonig' }, 'user-1');
expect(result).toEqual(created);
expect(auditLog.record).toHaveBeenCalledWith(
expect.objectContaining({ entityType: 'Product', action: 'CREATE' }),
);
});
it('throws NotFoundException when the product does not exist', async () => {
prisma.product.findUnique.mockResolvedValueOnce(null);
await expect(service.findById('missing')).rejects.toBeInstanceOf(
NotFoundException,
);
});
it('updates a product and records before/after audit data', async () => {
const before = { id: 'p1', name: 'Sommerhonig' };
const after = { id: 'p1', name: 'Sommerhonig 500g' };
prisma.product.findUnique.mockResolvedValueOnce(before);
prisma.product.update.mockResolvedValueOnce(after);
const result = await service.update(
'p1',
{ name: 'Sommerhonig 500g' },
'user-1',
);
expect(result).toEqual(after);
expect(auditLog.record).toHaveBeenCalledWith(
expect.objectContaining({
action: 'UPDATE',
beforeData: before,
afterData: after,
}),
);
});
});
@@ -0,0 +1,66 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { AuditLogService } from '../audit/audit-log.service';
import { Product, ProductStatus } from '../../generated/prisma';
import { CreateProductDto } from './dto/create-product.dto';
import { UpdateProductDto } from './dto/update-product.dto';
export interface ProductFilter {
status?: ProductStatus;
}
@Injectable()
export class ProductsService {
constructor(
private readonly prisma: PrismaService,
private readonly auditLog: AuditLogService,
) {}
async findAll(filter: ProductFilter = {}): Promise<Product[]> {
return this.prisma.product.findMany({
where: { status: filter.status },
orderBy: { name: 'asc' },
});
}
async findById(id: string): Promise<Product> {
const product = await this.prisma.product.findUnique({ where: { id } });
if (!product) {
throw new NotFoundException('Product not found');
}
return product;
}
async create(dto: CreateProductDto, actingUserId: string): Promise<Product> {
const product = await this.prisma.product.create({ data: { ...dto } });
await this.auditLog.record({
entityType: 'Product',
entityId: product.id,
action: 'CREATE',
userId: actingUserId,
afterData: product,
});
return product;
}
async update(
id: string,
dto: UpdateProductDto,
actingUserId: string,
): Promise<Product> {
const before = await this.findById(id);
const product = await this.prisma.product.update({
where: { id },
data: { ...dto },
});
await this.auditLog.record({
entityType: 'Product',
entityId: id,
action: 'UPDATE',
userId: actingUserId,
beforeData: before,
afterData: product,
});
return product;
}
}