"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.CatalogsService = void 0; const common_1 = require("@nestjs/common"); const client_1 = require("@prisma/client"); const prisma_service_1 = require("../prisma/prisma.service"); let CatalogsService = class CatalogsService { prisma; constructor(prisma) { this.prisma = prisma; } async findAll(companyId, activeOnly = false) { const catalogs = await this.prisma.catalog.findMany({ where: { ...(companyId ? { companyId } : {}), ...(activeOnly ? { isActive: true } : {}), }, orderBy: [{ displayOrder: 'asc' }, { name: 'asc' }], include: { company: { select: { name: true } }, _count: { select: { products: true } }, }, }); return catalogs.map((c) => this.map(c)); } async findOne(id) { const catalog = await this.prisma.catalog.findUnique({ where: { id }, include: { company: { select: { name: true } }, _count: { select: { products: true } }, }, }); if (!catalog) throw new common_1.NotFoundException('کاتالوگ یافت نشد'); return this.map(catalog); } async create(dto) { const company = await this.prisma.company.findUnique({ where: { id: dto.companyId }, }); if (!company) throw new common_1.NotFoundException('شرکت یافت نشد'); try { const catalog = await this.prisma.catalog.create({ data: { companyId: dto.companyId, name: dto.name.trim(), code: dto.code.trim(), description: dto.description?.trim() || null, displayOrder: dto.displayOrder ?? 0, isActive: dto.isActive ?? true, }, include: { company: { select: { name: true } }, _count: { select: { products: true } }, }, }); return this.map(catalog); } catch (e) { if (e instanceof client_1.Prisma.PrismaClientKnownRequestError && e.code === 'P2002') { throw new common_1.ConflictException('کد کاتالوگ در این شرکت تکراری است'); } throw e; } } async update(id, dto) { await this.ensure(id); try { const catalog = await this.prisma.catalog.update({ where: { id }, data: { name: dto.name?.trim(), code: dto.code?.trim(), description: dto.description === undefined ? undefined : dto.description?.trim() || null, displayOrder: dto.displayOrder, isActive: dto.isActive, }, include: { company: { select: { name: true } }, _count: { select: { products: true } }, }, }); return this.map(catalog); } catch (e) { if (e instanceof client_1.Prisma.PrismaClientKnownRequestError && e.code === 'P2002') { throw new common_1.ConflictException('کد کاتالوگ در این شرکت تکراری است'); } throw e; } } async setActive(id, isActive) { await this.ensure(id); const catalog = await this.prisma.catalog.update({ where: { id }, data: { isActive }, include: { company: { select: { name: true } }, _count: { select: { products: true } }, }, }); return this.map(catalog); } async ensure(id) { const catalog = await this.prisma.catalog.findUnique({ where: { id } }); if (!catalog) throw new common_1.NotFoundException('کاتالوگ یافت نشد'); return catalog; } map(catalog) { return { id: catalog.id, companyId: catalog.companyId, companyName: catalog.company.name, name: catalog.name, code: catalog.code, description: catalog.description ?? undefined, displayOrder: catalog.displayOrder, isActive: catalog.isActive, productCount: catalog._count.products, createdAt: catalog.createdAt.toISOString(), updatedAt: catalog.updatedAt.toISOString(), }; } }; exports.CatalogsService = CatalogsService; exports.CatalogsService = CatalogsService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [prisma_service_1.PrismaService]) ], CatalogsService); //# sourceMappingURL=catalogs.service.js.map