"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.CompaniesService = void 0; const common_1 = require("@nestjs/common"); const client_1 = require("@prisma/client"); const prisma_service_1 = require("../prisma/prisma.service"); let CompaniesService = class CompaniesService { prisma; constructor(prisma) { this.prisma = prisma; } async findAll(activeOnly = false) { const companies = await this.prisma.company.findMany({ where: activeOnly ? { isActive: true } : undefined, orderBy: [{ displayOrder: 'asc' }, { name: 'asc' }], include: { _count: { select: { catalogs: true, products: true, bankAccounts: true }, }, }, }); return companies.map((c) => this.mapCompany(c)); } async findOne(id) { const company = await this.prisma.company.findUnique({ where: { id }, include: { bankAccounts: { orderBy: { displayOrder: 'asc' } }, _count: { select: { catalogs: true, products: true, bankAccounts: true }, }, }, }); if (!company) throw new common_1.NotFoundException('شرکت یافت نشد'); return { ...this.mapCompany(company), bankAccounts: company.bankAccounts.map((b) => this.mapBankAccount(b)), }; } async create(dto) { try { const company = await this.prisma.company.create({ data: { name: dto.name.trim(), slug: dto.slug.trim(), description: dto.description?.trim() || null, logoUrl: dto.logoUrl?.trim() || null, displayOrder: dto.displayOrder ?? 0, maxMeteragePerInvoice: dto.maxMeteragePerInvoice ?? 5000, cashInvoiceValidityHours: dto.cashInvoiceValidityHours ?? 8, cashInvoiceValidUntilHour: dto.cashInvoiceValidUntilHour ?? null, isActive: dto.isActive ?? true, }, include: { _count: { select: { catalogs: true, products: true, bankAccounts: true }, }, }, }); return this.mapCompany(company); } 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.ensureCompany(id); try { const company = await this.prisma.company.update({ where: { id }, data: { name: dto.name?.trim(), slug: dto.slug?.trim(), description: dto.description === undefined ? undefined : dto.description?.trim() || null, logoUrl: dto.logoUrl === undefined ? undefined : dto.logoUrl?.trim() || null, displayOrder: dto.displayOrder, maxMeteragePerInvoice: dto.maxMeteragePerInvoice, cashInvoiceValidityHours: dto.cashInvoiceValidityHours, cashInvoiceValidUntilHour: dto.cashInvoiceValidUntilHour, isActive: dto.isActive, }, include: { _count: { select: { catalogs: true, products: true, bankAccounts: true }, }, }, }); return this.mapCompany(company); } 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.ensureCompany(id); const company = await this.prisma.company.update({ where: { id }, data: { isActive }, include: { _count: { select: { catalogs: true, products: true, bankAccounts: true }, }, }, }); return this.mapCompany(company); } async listBankAccounts(companyId) { await this.ensureCompany(companyId); const accounts = await this.prisma.bankAccount.findMany({ where: { companyId }, orderBy: { displayOrder: 'asc' }, }); return accounts.map((a) => this.mapBankAccount(a)); } async createBankAccount(companyId, dto) { await this.ensureCompany(companyId); const account = await this.prisma.bankAccount.create({ data: { companyId, bankName: dto.bankName.trim(), accountHolder: dto.accountHolder.trim(), accountNumber: dto.accountNumber.trim(), cardNumber: dto.cardNumber.trim(), iban: dto.iban.trim(), notes: dto.notes?.trim() || null, displayOrder: dto.displayOrder ?? 0, isActive: dto.isActive ?? true, }, }); return this.mapBankAccount(account); } async updateBankAccount(id, dto) { await this.ensureBankAccount(id); const account = await this.prisma.bankAccount.update({ where: { id }, data: { bankName: dto.bankName?.trim(), accountHolder: dto.accountHolder?.trim(), accountNumber: dto.accountNumber?.trim(), cardNumber: dto.cardNumber?.trim(), iban: dto.iban?.trim(), notes: dto.notes === undefined ? undefined : dto.notes?.trim() || null, displayOrder: dto.displayOrder, isActive: dto.isActive, }, }); return this.mapBankAccount(account); } async setBankAccountActive(id, isActive) { await this.ensureBankAccount(id); const account = await this.prisma.bankAccount.update({ where: { id }, data: { isActive }, }); return this.mapBankAccount(account); } async ensureCompany(id) { const company = await this.prisma.company.findUnique({ where: { id } }); if (!company) throw new common_1.NotFoundException('شرکت یافت نشد'); return company; } async ensureBankAccount(id) { const account = await this.prisma.bankAccount.findUnique({ where: { id } }); if (!account) throw new common_1.NotFoundException('حساب بانکی یافت نشد'); return account; } mapCompany(company) { return { id: company.id, name: company.name, slug: company.slug, description: company.description ?? '', logoUrl: company.logoUrl, logo: company.logoUrl ?? undefined, isActive: company.isActive, displayOrder: company.displayOrder, maxMeteragePerInvoice: company.maxMeteragePerInvoice, cashInvoiceValidityHours: company.cashInvoiceValidityHours, cashInvoiceValidUntilHour: company.cashInvoiceValidUntilHour ?? undefined, catalogCount: company._count?.catalogs ?? 0, productCount: company._count?.products ?? 0, bankAccountCount: company._count?.bankAccounts ?? 0, createdAt: company.createdAt.toISOString(), updatedAt: company.updatedAt.toISOString(), }; } mapBankAccount(account) { return { id: account.id, companyId: account.companyId, bankName: account.bankName, accountHolder: account.accountHolder, accountNumber: account.accountNumber, cardNumber: account.cardNumber, iban: account.iban, notes: account.notes ?? undefined, isActive: account.isActive, displayOrder: account.displayOrder, createdAt: account.createdAt.toISOString(), updatedAt: account.updatedAt.toISOString(), }; } }; exports.CompaniesService = CompaniesService; exports.CompaniesService = CompaniesService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [prisma_service_1.PrismaService]) ], CompaniesService); //# sourceMappingURL=companies.service.js.map