|
@@ -1,7 +1,8 @@
|
|
|
import mongoose from "mongoose";
|
|
import mongoose from "mongoose";
|
|
|
import {
|
|
import {
|
|
|
RecommendedProduct,
|
|
RecommendedProduct,
|
|
|
- Product
|
|
|
|
|
|
|
+ Product,
|
|
|
|
|
+ Menu
|
|
|
} from "../../../models";
|
|
} from "../../../models";
|
|
|
import {
|
|
import {
|
|
|
AddRecommendedProductResult,
|
|
AddRecommendedProductResult,
|
|
@@ -15,20 +16,37 @@ const addRecommendedProduct = async (userID: string, recommendedProductLimit: nu
|
|
|
...rest
|
|
...rest
|
|
|
} = input;
|
|
} = input;
|
|
|
|
|
|
|
|
|
|
+ const objectUserID = new mongoose.Types.ObjectId(userID);
|
|
|
|
|
+ const objectProductID = new mongoose.Types.ObjectId(productID);
|
|
|
|
|
+
|
|
|
|
|
+ const activeMenu = await Menu.findOne({
|
|
|
|
|
+ userID: objectUserID,
|
|
|
|
|
+ isActive: true
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (!activeMenu) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ message: "active-menu-not-found",
|
|
|
|
|
+ code: 404
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const activeMenuID = activeMenu._id;
|
|
|
|
|
+
|
|
|
const currentCount = await RecommendedProduct.countDocuments({
|
|
const currentCount = await RecommendedProduct.countDocuments({
|
|
|
- userID: userID
|
|
|
|
|
|
|
+ menuID: activeMenuID
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
if (currentCount >= recommendedProductLimit) {
|
|
if (currentCount >= recommendedProductLimit) {
|
|
|
return {
|
|
return {
|
|
|
- message: "recommended-product-limit-reached",
|
|
|
|
|
|
|
+ message: "recommended-product-limit-reached-for-this-menu",
|
|
|
code: 403
|
|
code: 403
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const existingProduct = await Product.findOne({
|
|
const existingProduct = await Product.findOne({
|
|
|
- _id: new mongoose.Types.ObjectId(productID),
|
|
|
|
|
- userID: userID
|
|
|
|
|
|
|
+ _id: objectProductID,
|
|
|
|
|
+ userID: objectUserID
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
if (!existingProduct) {
|
|
if (!existingProduct) {
|
|
@@ -38,21 +56,31 @@ const addRecommendedProduct = async (userID: string, recommendedProductLimit: nu
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ const isProductInActiveMenu = activeMenu.products.some((p) => p.productID.toString() === productID);
|
|
|
|
|
+
|
|
|
|
|
+ if (!isProductInActiveMenu) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ message: "product-is-not-in-active-menu",
|
|
|
|
|
+ code: 400
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
const alreadyRecommended = await RecommendedProduct.findOne({
|
|
const alreadyRecommended = await RecommendedProduct.findOne({
|
|
|
- productID: new mongoose.Types.ObjectId(productID),
|
|
|
|
|
- userID: userID
|
|
|
|
|
|
|
+ productID: objectProductID,
|
|
|
|
|
+ menuID: activeMenuID
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
if (alreadyRecommended) {
|
|
if (alreadyRecommended) {
|
|
|
return {
|
|
return {
|
|
|
- message: "product-is-already-recommended",
|
|
|
|
|
|
|
+ message: "product-is-already-recommended-in-this-menu",
|
|
|
code: 409
|
|
code: 409
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const newRecommendedProduct = await RecommendedProduct.create({
|
|
const newRecommendedProduct = await RecommendedProduct.create({
|
|
|
- productID: new mongoose.Types.ObjectId(productID),
|
|
|
|
|
- userID: userID,
|
|
|
|
|
|
|
+ productID: objectProductID,
|
|
|
|
|
+ userID: objectUserID,
|
|
|
|
|
+ menuID: activeMenuID,
|
|
|
...rest
|
|
...rest
|
|
|
});
|
|
});
|
|
|
|
|
|