"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.CartsService = void 0; const common_1 = require("@nestjs/common"); const prisma_service_1 = require("../prisma/prisma.service"); let CartsService = class CartsService { prisma; constructor(prisma) { this.prisma = prisma; } async getCart(userId) { const cart = await this.ensureCart(userId); return this.mapCart(cart); } async addItem(userId, dto) { const product = await this.prisma.product.findUnique({ where: { id: dto.productId }, include: { catalog: true, company: true, }, }); if (!product || !product.isActive) { throw new common_1.NotFoundException('محصول یافت نشد'); } this.validateMeterage(product, dto.meterage); const cart = await this.ensureCart(userId); const existing = await this.prisma.cartItem.findUnique({ where: { cartId_productId: { cartId: cart.id, productId: product.id }, }, }); if (existing) { const newMeterage = existing.meterage + dto.meterage; this.validateMeterage(product, newMeterage); await this.prisma.cartItem.update({ where: { id: existing.id }, data: { meterage: newMeterage, notes: dto.notes?.trim() || existing.notes, }, }); } else { await this.prisma.cartItem.create({ data: { cartId: cart.id, productId: product.id, catalogCode: product.catalog.code, productName: product.name, productCode: product.code, color: product.color, meterage: dto.meterage, notes: dto.notes?.trim() || null, companyId: product.companyId, companyName: product.company.name, }, }); } await this.prisma.cart.update({ where: { id: cart.id }, data: { companyId: product.companyId, updatedAt: new Date(), }, }); return this.getCart(userId); } async updateItem(userId, itemId, dto) { const cart = await this.ensureCart(userId); const item = await this.prisma.cartItem.findFirst({ where: { id: itemId, cartId: cart.id }, include: { product: true }, }); if (!item) throw new common_1.NotFoundException('آیتم سبد یافت نشد'); this.validateMeterage(item.product, dto.meterage); await this.prisma.cartItem.update({ where: { id: itemId }, data: { meterage: dto.meterage, notes: dto.notes === undefined ? undefined : dto.notes?.trim() || null, }, }); await this.prisma.cart.update({ where: { id: cart.id }, data: { updatedAt: new Date() }, }); return this.getCart(userId); } async removeItem(userId, itemId) { const cart = await this.ensureCart(userId); const item = await this.prisma.cartItem.findFirst({ where: { id: itemId, cartId: cart.id }, }); if (!item) throw new common_1.NotFoundException('آیتم سبد یافت نشد'); await this.prisma.cartItem.delete({ where: { id: itemId } }); const remaining = await this.prisma.cartItem.findMany({ where: { cartId: cart.id }, take: 1, }); await this.prisma.cart.update({ where: { id: cart.id }, data: { companyId: remaining[0]?.companyId ?? null, updatedAt: new Date(), }, }); return this.getCart(userId); } async updateNotes(userId, dto) { const cart = await this.ensureCart(userId); await this.prisma.cart.update({ where: { id: cart.id }, data: { notes: dto.notes?.trim() || null }, }); return this.getCart(userId); } async clearCart(userId) { const cart = await this.ensureCart(userId); await this.prisma.cartItem.deleteMany({ where: { cartId: cart.id } }); await this.prisma.cart.update({ where: { id: cart.id }, data: { notes: null, companyId: null }, }); return this.getCart(userId); } validateMeterage(product, meterage) { if (meterage < product.minMeterage) { throw new common_1.BadRequestException(`حداقل متراژ برای ${product.name} برابر ${product.minMeterage} است`); } const rem = Math.round((meterage / product.orderMultiplier) * 1000) / 1000; if (Math.abs(rem - Math.round(rem)) > 1e-6) { throw new common_1.BadRequestException(`متراژ باید مضرب ${product.orderMultiplier} باشد`); } } async ensureCart(userId) { let cart = await this.prisma.cart.findFirst({ where: { userId }, include: { items: true }, }); if (!cart) { cart = await this.prisma.cart.create({ data: { userId }, include: { items: true }, }); } return cart; } mapCart(cart) { const companyNames = [ ...new Set(cart.items.map((item) => item.companyName).filter(Boolean)), ]; return { id: cart.id, userId: cart.userId, companyId: cart.companyId ?? cart.items[0]?.companyId, companyName: companyNames.join('، '), notes: cart.notes ?? undefined, items: cart.items.map((item) => ({ id: item.id, productId: item.productId, catalogCode: item.catalogCode, productName: item.productName, productCode: item.productCode, color: item.color, meterage: item.meterage, notes: item.notes ?? undefined, companyId: item.companyId, companyName: item.companyName, })), totalMeterage: cart.items.reduce((s, i) => s + i.meterage, 0), itemCount: cart.items.length, createdAt: cart.createdAt.toISOString(), updatedAt: cart.updatedAt.toISOString(), }; } }; exports.CartsService = CartsService; exports.CartsService = CartsService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [prisma_service_1.PrismaService]) ], CartsService); //# sourceMappingURL=carts.service.js.map