From 0c589165c27016698925d46eee4a8e87f9797a48 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sat, 25 Oct 2025 16:35:48 +0330 Subject: [PATCH] fix : put attibute value directl in product --- src/modules/product/DTO/CreateProduct.dto.ts | 5 ++- .../product/providers/product.service.ts | 43 ++++++++++--------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/modules/product/DTO/CreateProduct.dto.ts b/src/modules/product/DTO/CreateProduct.dto.ts index 109c6c2..0e99cb6 100644 --- a/src/modules/product/DTO/CreateProduct.dto.ts +++ b/src/modules/product/DTO/CreateProduct.dto.ts @@ -115,8 +115,9 @@ export class Attributes { @Expose() @IsNotEmpty() @IsArray() - values: number[]; + values: (string | number)[]; } + export class ProductStepTwoDTO { @Expose() @IsNotEmpty() @@ -127,7 +128,7 @@ export class ProductStepTwoDTO { @Expose() @IsArray() - @ArrayMinSize(1) + @ArrayMinSize(0) @ValidateNested({ each: true }) @Type(() => Attributes) @ApiProperty({ diff --git a/src/modules/product/providers/product.service.ts b/src/modules/product/providers/product.service.ts index c0d5d7f..7743b36 100644 --- a/src/modules/product/providers/product.service.ts +++ b/src/modules/product/providers/product.service.ts @@ -161,17 +161,18 @@ class ProductService { } // Fetch the attribute values from the database - const attributeValues = await this.attributeValueRepo.model.find({ - _id: { $in: attribute.values }, - }); + // const attributeValues = await this.attributeValueRepo.model.find({ + // _id: { $in: attribute.values }, + // }); // Check if any attribute values were found - if (attributeValues.length === 0) { - throw new BadRequestError(`No valid attribute values found for attribute id ${attribute.id}`); - } + // if (attributeValues.length === 0 ) { + // throw new BadRequestError(`No valid attribute values found for attribute id ${attribute.id}`); + // } // Map the values to their text value - const selectedValues = attributeValues.map((val: IAttributeValue) => val.text); + // const selectedValues = attributeValues.map((val: IAttributeValue) => val.text); + const selectedValues = attribute.values.map((val) => String(val)); specifications.push({ title: attributeDetails.title, @@ -279,17 +280,17 @@ class ProductService { } // Fetch the attribute values from the database - const attributeValues = await this.attributeValueRepo.model.find({ - _id: { $in: attribute.values }, - }); + // const attributeValues = await this.attributeValueRepo.model.find({ + // _id: { $in: attribute.values }, + // }); // Check if any attribute values were found - if (attributeValues.length === 0) { - throw new BadRequestError(`No valid attribute values found for attribute id ${attribute.id}`); - } + // if (attributeValues.length === 0) { + // throw new BadRequestError(`No valid attribute values found for attribute id ${attribute.id}`); + // } // Map the values to their text value - const selectedValues = attributeValues.map((val: IAttributeValue) => val.text); + const selectedValues = attribute.values.map((val) => String(val)); specifications.push({ title: attributeDetails.title, @@ -423,15 +424,15 @@ class ProductService { throw new BadRequestError(AttributeMessage.AttributeIdIsIncorrect); } - const attributeValues = await this.attributeValueRepo.model.find({ - _id: { $in: attribute.values }, - }); + // const attributeValues = await this.attributeValueRepo.model.find({ + // _id: { $in: attribute.values }, + // }); - if (attributeValues.length === 0) { - throw new BadRequestError(`No valid attribute values found for attribute id ${attribute.id}`); - } + // if (attributeValues.length === 0) { + // throw new BadRequestError(`No valid attribute values found for attribute id ${attribute.id}`); + // } - const selectedValues = attributeValues.map((val: IAttributeValue) => val.text); + const selectedValues = attribute.values.map((val) => String(val)); specifications.push({ title: attributeDetails.title,