This commit is contained in:
Mr Swift
2024-04-27 20:18:51 +03:30
parent e324c39dea
commit d0b51c7019
2 changed files with 2 additions and 160 deletions
+2 -3
View File
@@ -1,6 +1,6 @@
{
"app":"api-asanmarket",
"port":"5050",
"app":"api-delearn",
"port":"6060",
"disks":[
{
"name": "uploads",
@@ -10,5 +10,4 @@
"node": {
"version": "18"
}
}
-157
View File
@@ -1,157 +0,0 @@
const createOrder = [
asyncHandler(async (req, res) => {
const {
discountCode,
} = req.body;
const userID = req.user.id;
const userData = await User.findById(req.user.id)
if (userData.cart.length <= 0) {
res.status(400)
throw new Error("سبد خرید خالی است")
}
const shippingAddress = userData.state + " - " + userData.city + " - " + userData.address + " - " + userData.postCode + " - " + userData.phoneNumber
const orderDate = new Date()
const data = {
userID,
totalAmount: 0,
shippingAddress,
orderDate,
status: "Pending",
};
let myPromise = new Promise(async function (myResolve, myReject) {
try {
let products = []
// Use Promise.all to wait for all asynchronous operations to complete
await Promise.all(
userData.cart.map(async (product) => {
const pd = await Product.findById(product.product);
if (pd.specialPrice) {
data.totalAmount = pd.specialPrice * product.quantity + data.totalAmount;
} else {
data.totalAmount = pd.price * product.quantity + data.totalAmount;
}
products.push({ product: pd.id, quantity: product.quantity, price: pd.price, specialPrice: pd.specialPrice })
})
);
data.products = products
// Resolve the main promise after all asynchronous operations are finished
myResolve(data);
} catch (error) {
// Reject the promise if an error occurs
myReject(error);
}
});
if (discountCode) {
const discount = await Discount.findOne({ discountCode });
if (!discount) {
res.status(404);
throw new Error(_sr.fa.not_found.item_id);
}
if (discount.expirationDate <= Date.now()) {
res.status(404);
throw new Error("این کد منقضی شده");
}
if (discount.use >= usageLimit) {
res.status(404);
throw new Error("این کد منقضی شده");
}
if (discount.usedBy.length > 0) {
let thisUser = true;
discount.usedBy.forEach((user) => {
if (user === userID) {
thisUser = false;
}
});
if (thisUser) {
res.status(404);
throw new Error("این کد منقضی شده");
}
}
if (discount.applicableProducts.length > 0) {
let thisProducts = true;
discount.applicableProducts.forEach((product) => {
userData.cart.forEach((productX) => {
if (product === productX) {
thisProducts = false;
}
});
});
if (thisProducts) {
res.status(404);
throw new Error("این کد منقضی شده");
}
}
switch (discount.type) {
case "f":
data.priceWithDiscount = data.totalAmount - discount.amount;
break;
case "p":
const darsad = discount.amount / 100;
const manfi = data.totalAmount * darsad;
data.priceWithDiscount = data.totalAmount - manfi;
break;
}
} else {
data.priceWithDiscount = data.totalAmount
}
var order
myPromise.then(async () => {
order = await Order.create(data);
let findOrder = await Order.findById(order._id).populate('products.product', ' title uid').populate('userID', sp.select.userCustom)
userData.cart.map(async (productData) => {
var pusher = productData
pusher.status = "Pending"
pusher.date = Date.now()
pusher.orderDate = Date.now()
const product = await Product.findById(productData.product)
pusher.price = product?.specialPrice || product.price
await Product.findByIdAndUpdate(
productData.product, {
order: product?.order + 1 || 1
}
)
await Shop.findByIdAndUpdate(
product.shopID,
{
$push: { pendingProductss: pusher },
},
{ new: true }
);
});
const emailInfo = await Email.findOne({ status: data.status });
const smsInfo = await Sms.findOne({ status: data.status });
if (emailInfo.send) {
// email(userData.email, "Asan Market Order", emailInfo.body, emailInfo.html);
}
if (smsInfo.send) {
await sms(userData.phoneNumber, `سفارش شما با ایدیه ${findOrder?._id} در آسان مارکت ثبت شد و در انتظار پرداخت میباشد`);
}
userData.cart = []
userData.orders.push(order)
userData.save()
res.status(201).json(findOrder);
})
})
]