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
Test Suite / test (24.15.0) (push) Has been cancelled
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { ForbiddenException, Injectable } from '@nestjs/common';
|
||||
import { ForbiddenException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { CreateReceiptAliasDto } from './dto/create-receipt-alias.dto';
|
||||
|
||||
@@ -25,59 +25,48 @@ export class ReceiptAliasService {
|
||||
|
||||
async upsert(dto: CreateReceiptAliasDto, userId: number, role: string) {
|
||||
const normalized = dto.receiptName.toLowerCase().trim();
|
||||
|
||||
const wantsGlobal = dto.isGlobal === true;
|
||||
|
||||
if (wantsGlobal && role !== 'admin') {
|
||||
throw new ForbiddenException('Endast admin kan skapa globala alias');
|
||||
}
|
||||
|
||||
if (wantsGlobal) {
|
||||
const existing = await this.prisma.receiptAlias.findFirst({
|
||||
where: { receiptName: normalized, isGlobal: true },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return this.prisma.receiptAlias.update({
|
||||
where: { id: existing.id },
|
||||
data: { productId: dto.productId },
|
||||
});
|
||||
}
|
||||
|
||||
return this.prisma.receiptAlias.create({
|
||||
data: {
|
||||
receiptName: normalized,
|
||||
productId: dto.productId,
|
||||
isGlobal: true,
|
||||
ownerId: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
return this.upsertAliasRecord(
|
||||
normalized,
|
||||
dto.productId,
|
||||
wantsGlobal ? null : userId,
|
||||
wantsGlobal,
|
||||
);
|
||||
}
|
||||
|
||||
private async upsertAliasRecord(
|
||||
receiptName: string,
|
||||
productId: number,
|
||||
ownerId: number | null,
|
||||
isGlobal: boolean,
|
||||
) {
|
||||
const existing = await this.prisma.receiptAlias.findFirst({
|
||||
where: { receiptName: normalized, ownerId: userId, isGlobal: false },
|
||||
where: isGlobal
|
||||
? { receiptName, isGlobal: true }
|
||||
: { receiptName, ownerId, isGlobal: false },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return this.prisma.receiptAlias.update({
|
||||
where: { id: existing.id },
|
||||
data: { productId: dto.productId },
|
||||
data: { productId },
|
||||
});
|
||||
}
|
||||
|
||||
return this.prisma.receiptAlias.create({
|
||||
data: {
|
||||
receiptName: normalized,
|
||||
productId: dto.productId,
|
||||
ownerId: userId,
|
||||
isGlobal: false,
|
||||
},
|
||||
data: { receiptName, productId, ownerId, isGlobal },
|
||||
});
|
||||
}
|
||||
|
||||
async remove(id: number, userId: number, role: string) {
|
||||
const alias = await this.prisma.receiptAlias.findUnique({ where: { id } });
|
||||
if (!alias) {
|
||||
return this.prisma.receiptAlias.delete({ where: { id } });
|
||||
throw new NotFoundException(`Aliaspost med id ${id} hittades inte`);
|
||||
}
|
||||
|
||||
const canDelete =
|
||||
|
||||
Reference in New Issue
Block a user