|
@@ -1,3 +1,6 @@
|
|
|
|
|
+import {
|
|
|
|
|
+ Menu
|
|
|
|
|
+} from "../../../models/Menu";
|
|
|
import {
|
|
import {
|
|
|
Product
|
|
Product
|
|
|
} from "../../../models/Product";
|
|
} from "../../../models/Product";
|
|
@@ -10,7 +13,7 @@ const addProduct = async (body: any, context: { userID: string }): Promise<AddPr
|
|
|
const {
|
|
const {
|
|
|
description,
|
|
description,
|
|
|
isAvailable,
|
|
isAvailable,
|
|
|
- categoryID,
|
|
|
|
|
|
|
+ categoryIDs,
|
|
|
isActive,
|
|
isActive,
|
|
|
photos,
|
|
photos,
|
|
|
price,
|
|
price,
|
|
@@ -21,12 +24,12 @@ const addProduct = async (body: any, context: { userID: string }): Promise<AddPr
|
|
|
userID
|
|
userID
|
|
|
} = context;
|
|
} = context;
|
|
|
|
|
|
|
|
- const normalizedCategoryID = Array.isArray(categoryID)
|
|
|
|
|
- ? categoryID
|
|
|
|
|
- : categoryID ? [categoryID] : [];
|
|
|
|
|
|
|
+ const normalizedCategoryIDs = Array.isArray(categoryIDs)
|
|
|
|
|
+ ? categoryIDs
|
|
|
|
|
+ : categoryIDs ? [categoryIDs] : [];
|
|
|
|
|
|
|
|
const newProduct = await Product.create({
|
|
const newProduct = await Product.create({
|
|
|
- categoryID: normalizedCategoryID,
|
|
|
|
|
|
|
+ categoryIDs: normalizedCategoryIDs,
|
|
|
userID: userID,
|
|
userID: userID,
|
|
|
description,
|
|
description,
|
|
|
isAvailable,
|
|
isAvailable,
|
|
@@ -36,11 +39,46 @@ const addProduct = async (body: any, context: { userID: string }): Promise<AddPr
|
|
|
price
|
|
price
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ const activeMenu = await Menu.findOne({
|
|
|
|
|
+ userID: userID,
|
|
|
|
|
+ isActive: true
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (activeMenu) {
|
|
|
|
|
+ const existingCategoryIDs = new Set(
|
|
|
|
|
+ activeMenu.products.flatMap((p: any) => p.categoryIDs.map((id: any) => id.toString()))
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ const newCategoryIDs = normalizedCategoryIDs.filter(
|
|
|
|
|
+ (id: string) => !existingCategoryIDs.has(id.toString())
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ await Menu.findByIdAndUpdate(activeMenu._id, {
|
|
|
|
|
+ $push: {
|
|
|
|
|
+ products: {
|
|
|
|
|
+ productID: newProduct._id,
|
|
|
|
|
+ categoryIDs: newCategoryIDs,
|
|
|
|
|
+ isViewPrice: true
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return {
|
|
return {
|
|
|
message: "product-created-successfully",
|
|
message: "product-created-successfully",
|
|
|
code: 201,
|
|
code: 201,
|
|
|
payload: {
|
|
payload: {
|
|
|
- product: newProduct
|
|
|
|
|
|
|
+ _id: newProduct._id,
|
|
|
|
|
+ categoryIDs: newProduct.categoryIDs,
|
|
|
|
|
+ userID: newProduct.userID,
|
|
|
|
|
+ title: newProduct.title,
|
|
|
|
|
+ description: newProduct.description,
|
|
|
|
|
+ price: newProduct.price,
|
|
|
|
|
+ photos: newProduct.photos,
|
|
|
|
|
+ isActive: newProduct.isActive,
|
|
|
|
|
+ isAvailable: newProduct.isAvailable,
|
|
|
|
|
+ createdAt: newProduct.createdAt,
|
|
|
|
|
+ updatedAt: newProduct.updatedAt
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
} catch (error) {
|
|
} catch (error) {
|