Refactor code structure for improved readability and maintainability
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:
@@ -0,0 +1,76 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ReceiptAliasService = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const prisma_service_1 = require("../prisma/prisma.service");
|
||||
let ReceiptAliasService = class ReceiptAliasService {
|
||||
constructor(prisma) {
|
||||
this.prisma = prisma;
|
||||
}
|
||||
findAllForUser(userId, role) {
|
||||
const where = role === 'admin'
|
||||
? undefined
|
||||
: {
|
||||
OR: [
|
||||
{ ownerId: userId, isGlobal: false },
|
||||
{ isGlobal: true },
|
||||
],
|
||||
};
|
||||
return this.prisma.receiptAlias.findMany({
|
||||
where,
|
||||
include: { product: { select: { id: true, name: true, canonicalName: true } } },
|
||||
orderBy: { receiptName: 'asc' },
|
||||
});
|
||||
}
|
||||
async upsert(dto, userId, role) {
|
||||
const normalized = dto.receiptName.toLowerCase().trim();
|
||||
const wantsGlobal = dto.isGlobal === true;
|
||||
if (wantsGlobal && role !== 'admin') {
|
||||
throw new common_1.ForbiddenException('Endast admin kan skapa globala alias');
|
||||
}
|
||||
return this.upsertAliasRecord(normalized, dto.productId, wantsGlobal ? null : userId, wantsGlobal);
|
||||
}
|
||||
async upsertAliasRecord(receiptName, productId, ownerId, isGlobal) {
|
||||
const existing = await this.prisma.receiptAlias.findFirst({
|
||||
where: isGlobal
|
||||
? { receiptName, isGlobal: true }
|
||||
: { receiptName, ownerId, isGlobal: false },
|
||||
});
|
||||
if (existing) {
|
||||
return this.prisma.receiptAlias.update({
|
||||
where: { id: existing.id },
|
||||
data: { productId },
|
||||
});
|
||||
}
|
||||
return this.prisma.receiptAlias.create({
|
||||
data: { receiptName, productId, ownerId, isGlobal },
|
||||
});
|
||||
}
|
||||
async remove(id, userId, role) {
|
||||
const alias = await this.prisma.receiptAlias.findUnique({ where: { id } });
|
||||
if (!alias) {
|
||||
throw new common_1.NotFoundException(`Aliaspost med id ${id} hittades inte`);
|
||||
}
|
||||
const canDelete = role === 'admin' ||
|
||||
(alias.ownerId === userId && alias.isGlobal === false);
|
||||
if (!canDelete) {
|
||||
throw new common_1.ForbiddenException('Du har inte behörighet att ta bort aliaset');
|
||||
}
|
||||
return this.prisma.receiptAlias.delete({ where: { id } });
|
||||
}
|
||||
};
|
||||
exports.ReceiptAliasService = ReceiptAliasService;
|
||||
exports.ReceiptAliasService = ReceiptAliasService = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
|
||||
], ReceiptAliasService);
|
||||
//# sourceMappingURL=receipt-alias.service.js.map
|
||||
Reference in New Issue
Block a user