chore: fix problem of transcation beeing commited throw update the "

category variant
This commit is contained in:
mahyargdz
2025-09-21 11:45:24 +03:30
parent 3d1aa01f2e
commit 79f597cc90
23 changed files with 52 additions and 97 deletions
@@ -1,8 +1,14 @@
import { Types } from "mongoose";
import { CategoryThemeEnum } from "../../../../common/enums/category.enum";
import { ProductDiscountType, ProductMarketStatus } from "../../../../common/enums/product.enum";
export interface IProductVariant {
// Dynamic type for category theme fields
type CategoryThemeFields = {
[K in Lowercase<Exclude<CategoryThemeEnum, CategoryThemeEnum.No_color_No_sized>>]?: number;
};
export interface IProductVariant extends CategoryThemeFields {
_id: Types.ObjectId;
product: number;
statistics: IStatistics;
@@ -19,9 +25,6 @@ export interface IProductVariant {
saleFormat: ISaleFormat;
dimensions: IDimensions;
sellerSpecialCode: string;
color?: number;
size?: number;
meterage?: number;
}
export interface IPrice {
@@ -1,6 +1,7 @@
import { Schema, model } from "mongoose";
import { IDimensions, IPrice, IProductVariant, ISaleFormat, IStatistics } from "./Abstraction/IProductVariant";
import { CategoryThemeEnum } from "../../../common/enums/category.enum";
import { ProductMarketStatus } from "../../../common/enums/product.enum";
const productStatisticsSchema = new Schema<IStatistics>(
@@ -44,6 +45,24 @@ const productPriceSchema = new Schema<IPrice>(
{ _id: false },
);
// Utility function to generate dynamic category theme fields
const generateCategoryThemeFields = () => {
const fields: Record<string, { type: NumberConstructor; ref: string; required: boolean }> = {};
Object.values(CategoryThemeEnum).forEach((theme) => {
if (theme !== CategoryThemeEnum.No_color_No_sized) {
const fieldName = theme.toLowerCase();
fields[fieldName] = {
type: Number,
ref: theme,
required: false,
};
}
});
return fields;
};
const productVariantSchema = new Schema<IProductVariant>(
{
product: { type: Number, ref: "Product", required: true },
@@ -59,9 +78,7 @@ const productVariantSchema = new Schema<IProductVariant>(
saleFormat: { type: productSaleFormat },
dimensions: { type: productDimensionsSchema, required: true },
sellerSpecialCode: { type: String },
color: { type: Number, ref: "Color", required: false },
size: { type: Number, ref: "Size", required: false },
meterage: { type: Number, ref: "Meterage", required: false },
...generateCategoryThemeFields(),
},
{ timestamps: true, toObject: { virtuals: true, versionKey: false }, toJSON: { virtuals: true, versionKey: false }, id: false },
);
@@ -239,7 +239,6 @@ class ProductService {
await this.checkForDuplicateProduct(createStepOneDto.model);
const shop = await this.shopeRepo.model.findById(shopId);
console.log({ shop, shopId });
if (!shop) throw new BadRequestError(ShopMessage.ShopNotFound);
const createdProduct = await this.productRepo.model.create({
@@ -1627,10 +1626,9 @@ class ProductService {
async deletePopularProduct(productId: number) {
await this.validateProduct(productId);
const deletedPopular = await this.popularProductRepo.model.findOneAndDelete({
await this.popularProductRepo.model.findOneAndDelete({
product: productId,
});
console.log(deletedPopular);
return {
message: CommonMessage.Deleted,
};
@@ -1674,7 +1672,6 @@ class ProductService {
async updateProduct(productId: number, updateDto: UpdateProductDTO) {
const product = await this.validateProduct(productId);
const updatedProduct = await this.productRepo.model.findByIdAndUpdate({ _id: product._id }, { ...updateDto }, { new: true });
console.log({ updatedProduct });
return { updatedProduct };
}