From 6c8de9867a66e3431cb20d80364b11afd497a83f Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sat, 29 Nov 2025 09:30:12 +0330 Subject: [PATCH] product variant --- src/modules/category/category.service.ts | 13 ++++++++----- src/modules/product/providers/product.service.ts | 14 +++++++------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/modules/category/category.service.ts b/src/modules/category/category.service.ts index 4f3b391..9d57fd4 100644 --- a/src/modules/category/category.service.ts +++ b/src/modules/category/category.service.ts @@ -127,17 +127,20 @@ class CategoryService { createDto.title_en = slugify(createDto.title_en); const existTitle = await this.categoryRepository.findByTitle(createDto.title_en); - - const theme = await this.themeRepo.findById(createDto.themeId); - if (!theme) throw new BadRequestError(CommonMessage.NotFoundById); - if (existTitle) throw new BadRequestError(CategoryMessage.DuplicateTitle); + + let themeIdOrNull: any = null; + if (createDto.themeId) { + const themeDoc = await this.themeRepo.findById(createDto.themeId); + if (!themeDoc) throw new BadRequestError(CommonMessage.NotFoundById); + themeIdOrNull = themeDoc._id; + } //if parent exist first set parent leaf false if (createDto.parent) { await this.categoryRepository.model.findByIdAndUpdate(createDto.parent, { leaf: false }); } //we set the leaf to true - const category = await this.categoryRepository.model.create({ ...createDto, theme: theme._id }); + const category = await this.categoryRepository.model.create({ ...createDto, theme: themeIdOrNull }); return { message: CategoryMessage.Created, diff --git a/src/modules/product/providers/product.service.ts b/src/modules/product/providers/product.service.ts index 41b6433..912d777 100644 --- a/src/modules/product/providers/product.service.ts +++ b/src/modules/product/providers/product.service.ts @@ -564,14 +564,14 @@ class ProductService { // Check if a variant with the same themeValue already exists // const query: FilterQuery = { product: product._id }; - // if (addVariantDto.themeValueId) { - // const existingVariant = await this.productVariantRepo.model.findOne({ - // product: product._id, - // themeValue: addVariantDto.themeValueId, - // }); + if (addVariantDto.themeValueId) { + const existingVariant = await this.productVariantRepo.model.findOne({ + product: product._id, + themeValue: addVariantDto.themeValueId, + }); - // if (existingVariant) throw new BadRequestError(ProductMessage.VariantAlreadyExists); - // } + if (existingVariant) throw new BadRequestError(ProductMessage.VariantAlreadyExists); + } const productVariant = await this.createProductVariant(addVariantDto, product._id, shop._id.toString(), session);