chore: fix problem of transcation beeing commited throw update the "
category variant
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user