feat: refactor inventory and recipe services for improved error handling and code reuse; add systematic backend review plan
Test Suite / test (24.15.0) (push) Has been cancelled

This commit is contained in:
Nils-Johan Gynther
2026-05-04 20:44:43 +02:00
parent 6dfd4c372d
commit a645d6a364
6 changed files with 212 additions and 194 deletions
+26 -42
View File
@@ -14,6 +14,26 @@ type InventoryQuery = {
export class InventoryService {
constructor(private prisma: PrismaService) {}
private throwInventoryItemNotFound(id: number): never {
throw new NotFoundException(`Inventory item with id ${id} not found`);
}
private async findInventoryItemByIdOrThrow(id: number) {
const existing = await this.prisma.inventoryItem.findUnique({ where: { id } });
if (!existing) {
this.throwInventoryItemNotFound(id);
}
return existing;
}
private async ensureProductExists(productId: number) {
const product = await this.prisma.product.findUnique({ where: { id: productId } });
if (!product) {
throw new NotFoundException('Product not found');
}
return product;
}
async findAll(query?: InventoryQuery) {
const where: Prisma.InventoryItemWhereInput = {};
const orderBy: Prisma.InventoryItemOrderByWithRelationInput[] = [];
@@ -46,16 +66,7 @@ export class InventoryService {
}
async consume(id: number, data: ConsumeInventoryDto) {
const existing = await this.prisma.inventoryItem.findUnique({
where: { id },
include: {
product: true,
},
});
if (!existing) {
throw new NotFoundException(`Inventory item with id ${id} not found`);
}
const existing = await this.findInventoryItemByIdOrThrow(id);
const currentQuantity = Number(existing.quantity);
const newQuantity = Math.max(0, currentQuantity - data.amountUsed);
@@ -84,13 +95,7 @@ export class InventoryService {
}
async findConsumptionHistory(id: number) {
const existing = await this.prisma.inventoryItem.findUnique({
where: { id },
});
if (!existing) {
throw new NotFoundException(`Inventory item with id ${id} not found`);
}
await this.findInventoryItemByIdOrThrow(id);
return this.prisma.inventoryConsumption.findMany({
where: {
@@ -129,13 +134,7 @@ export class InventoryService {
}
async create(data: CreateInventoryDto) {
const product = await this.prisma.product.findUnique({
where: { id: data.productId },
});
if (!product) {
throw new NotFoundException('Product not found');
}
await this.ensureProductExists(data.productId);
return this.prisma.inventoryItem.create({
data: {
@@ -161,22 +160,10 @@ export class InventoryService {
}
async update(id: number, data: UpdateInventoryDto) {
const existing = await this.prisma.inventoryItem.findUnique({
where: { id },
});
if (!existing) {
throw new NotFoundException(`Inventory item with id ${id} not found`);
}
await this.findInventoryItemByIdOrThrow(id);
if (typeof data.productId === 'number') {
const product = await this.prisma.product.findUnique({
where: { id: data.productId },
});
if (!product) {
throw new NotFoundException('Product not found');
}
await this.ensureProductExists(data.productId);
}
const updateData: Prisma.InventoryItemUpdateInput = {};
@@ -241,10 +228,7 @@ export class InventoryService {
}
async remove(id: number) {
const existing = await this.prisma.inventoryItem.findUnique({ where: { id } });
if (!existing) {
throw new NotFoundException(`Inventory item with id ${id} not found`);
}
await this.findInventoryItemByIdOrThrow(id);
return this.prisma.inventoryItem.delete({ where: { id } });
}
}