Merge branch 'master' of https://github.com/cheminova2024/cheminova-api
This commit is contained in:
commit
528098d72c
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { PdOrder } from "./pdOrderModal.js";
|
import { PdOrder } from "./pdOrderModal.js";
|
||||||
|
import sendEmail from "../../Utils/sendEmail.js";
|
||||||
|
|
||||||
export const createOrder = async (req, res) => {
|
export const createOrder = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@ -35,6 +36,17 @@ export const createOrder = async (req, res) => {
|
|||||||
billTo,
|
billTo,
|
||||||
orderItem: orderItems.map((item) => ({
|
orderItem: orderItems.map((item) => ({
|
||||||
productId: item._id,
|
productId: item._id,
|
||||||
|
SKU: item.SKU,
|
||||||
|
name: item.name,
|
||||||
|
categoryName: item.category.categoryName, // Store category name
|
||||||
|
|
||||||
|
brandName: item.brand.brandName, // Store brand name
|
||||||
|
|
||||||
|
price: item.price,
|
||||||
|
GST: item.GST,
|
||||||
|
HSN_Code: item.HSN_Code,
|
||||||
|
description: item.description,
|
||||||
|
image: item.image,
|
||||||
quantity: item.count,
|
quantity: item.count,
|
||||||
})),
|
})),
|
||||||
subtotal,
|
subtotal,
|
||||||
@ -88,14 +100,17 @@ export const getPlacedOrder = async (req, res) => {
|
|||||||
export const getPlacedOrderById = async (req, res) => {
|
export const getPlacedOrderById = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const id = req.params.id;
|
const id = req.params.id;
|
||||||
|
|
||||||
if (!mongoose.Types.ObjectId.isValid(id)) {
|
if (!mongoose.Types.ObjectId.isValid(id)) {
|
||||||
return res
|
return res
|
||||||
.status(404)
|
.status(404)
|
||||||
.json({ return_msg: "Not Valid id to search the doc " });
|
.json({ return_msg: "Not Valid id to search the doc " });
|
||||||
}
|
}
|
||||||
const doc = await PdOrder.findById(id).populate({
|
const doc = await PdOrder.findById(id)
|
||||||
|
.populate({
|
||||||
path: "orderItem.productId",
|
path: "orderItem.productId",
|
||||||
});
|
})
|
||||||
|
.populate({ path: "addedBy" });
|
||||||
if (doc) {
|
if (doc) {
|
||||||
return res
|
return res
|
||||||
.status(200)
|
.status(200)
|
||||||
@ -108,24 +123,571 @@ export const getPlacedOrderById = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getPlacedOrderAdmin = async (req, res) => {
|
export const getPlacedNewOrderAdmin = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const plcaedOrders = await PdOrder.find()
|
// Extract page and limit from query parameters
|
||||||
.sort({
|
const page = parseInt(req.query.page, 10) || 1;
|
||||||
createdAt: -1,
|
const limit = parseInt(req.query.limit, 10) || 5;
|
||||||
})
|
|
||||||
.populate({
|
// Calculate the number of documents to skip
|
||||||
path: "orderItem.productId",
|
const skip = (page - 1) * limit;
|
||||||
})
|
|
||||||
.populate({
|
// Get the total count of new orders
|
||||||
path: "addedBy",
|
const totalOrders = await PdOrder.countDocuments({ status: "new" });
|
||||||
});
|
|
||||||
if (plcaedOrders) {
|
// Fetch paginated new orders
|
||||||
return res.status(200).json({ plcaedOrders });
|
const placedOrders = await PdOrder.find({ status: "new" })
|
||||||
|
.sort({ createdAt: -1 })
|
||||||
|
.skip(skip)
|
||||||
|
.limit(limit)
|
||||||
|
.populate({ path: "orderItem.productId" })
|
||||||
|
.populate({ path: "addedBy" });
|
||||||
|
|
||||||
|
if (placedOrders.length > 0) {
|
||||||
|
return res.status(200).json({ placedOrders, totalOrders });
|
||||||
}
|
}
|
||||||
return res.status(404).json({ return_msg: "Not placed yet " });
|
return res.status(404).json({ return_msg: "No new orders placed yet" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error creating order:", error);
|
console.error("Error fetching new orders:", error);
|
||||||
return res.status(500).json({ error: "Internal Server Error" });
|
return res.status(500).json({ error: "Internal Server Error" });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getDispatchedOrdersAdmin = async (req, res) => {
|
||||||
|
try {
|
||||||
|
// Extract page and limit from query parameters
|
||||||
|
const page = parseInt(req.query.page, 10) || 1;
|
||||||
|
const limit = parseInt(req.query.limit, 10) || 5;
|
||||||
|
|
||||||
|
// Calculate the number of documents to skip
|
||||||
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
|
// Get the total count of dispatched orders
|
||||||
|
const totalOrders = await PdOrder.countDocuments({ status: "dispatched" });
|
||||||
|
|
||||||
|
// Fetch paginated dispatched orders
|
||||||
|
const dispatchedOrders = await PdOrder.find({ status: "dispatched" })
|
||||||
|
.sort({ createdAt: -1 })
|
||||||
|
.skip(skip)
|
||||||
|
.limit(limit)
|
||||||
|
.populate({ path: "orderItem.productId" })
|
||||||
|
.populate({ path: "addedBy" });
|
||||||
|
|
||||||
|
if (dispatchedOrders.length > 0) {
|
||||||
|
return res.status(200).json({ dispatchedOrders, totalOrders });
|
||||||
|
}
|
||||||
|
return res.status(404).json({ return_msg: "No dispatched orders yet" });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching dispatched orders:", error);
|
||||||
|
return res.status(500).json({ error: "Internal Server Error" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getCancelledOrdersAdmin = async (req, res) => {
|
||||||
|
try {
|
||||||
|
// Extract page and limit from query parameters
|
||||||
|
const page = parseInt(req.query.page, 10) || 1;
|
||||||
|
const limit = parseInt(req.query.limit, 10) || 5;
|
||||||
|
|
||||||
|
// Calculate the number of documents to skip
|
||||||
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
|
// Get the total count of cancelled orders
|
||||||
|
const totalOrders = await PdOrder.countDocuments({ status: "cancelled" });
|
||||||
|
|
||||||
|
// Fetch paginated cancelled orders
|
||||||
|
const cancelledOrders = await PdOrder.find({ status: "cancelled" })
|
||||||
|
.sort({ createdAt: -1 })
|
||||||
|
.skip(skip)
|
||||||
|
.limit(limit)
|
||||||
|
.populate({ path: "orderItem.productId" })
|
||||||
|
.populate({ path: "addedBy" });
|
||||||
|
|
||||||
|
if (cancelledOrders.length > 0) {
|
||||||
|
return res.status(200).json({ cancelledOrders, totalOrders });
|
||||||
|
}
|
||||||
|
return res.status(404).json({ return_msg: "No cancelled orders yet" });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching cancelled orders:", error);
|
||||||
|
return res.status(500).json({ error: "Internal Server Error" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getProcessingOrdersAdmin = async (req, res) => {
|
||||||
|
try {
|
||||||
|
// Extract page and limit from query parameters
|
||||||
|
const page = parseInt(req.query.page, 10) || 1;
|
||||||
|
const limit = parseInt(req.query.limit, 10) || 5;
|
||||||
|
|
||||||
|
// Calculate the number of documents to skip
|
||||||
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
|
// Get the total count of processing orders
|
||||||
|
const totalOrders = await PdOrder.countDocuments({ status: "processing" });
|
||||||
|
|
||||||
|
// Fetch paginated processing orders
|
||||||
|
const processingOrders = await PdOrder.find({ status: "processing" })
|
||||||
|
.sort({ createdAt: -1 })
|
||||||
|
.skip(skip)
|
||||||
|
.limit(limit)
|
||||||
|
.populate({ path: "orderItem.productId" })
|
||||||
|
.populate({ path: "addedBy" });
|
||||||
|
|
||||||
|
if (processingOrders.length > 0) {
|
||||||
|
return res.status(200).json({ processingOrders, totalOrders });
|
||||||
|
}
|
||||||
|
return res.status(404).json({ return_msg: "No processing orders yet" });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching processing orders:", error);
|
||||||
|
return res.status(500).json({ error: "Internal Server Error" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getDeliveredOrdersAdmin = async (req, res) => {
|
||||||
|
try {
|
||||||
|
// Extract page and limit from query parameters
|
||||||
|
const page = parseInt(req.query.page, 10) || 1;
|
||||||
|
const limit = parseInt(req.query.limit, 10) || 5;
|
||||||
|
|
||||||
|
// Calculate the number of documents to skip
|
||||||
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
|
// Get the total count of delivered orders
|
||||||
|
const totalOrders = await PdOrder.countDocuments({ status: "delivered" });
|
||||||
|
|
||||||
|
// Fetch paginated delivered orders
|
||||||
|
const deliveredOrders = await PdOrder.find({ status: "delivered" })
|
||||||
|
.sort({ createdAt: -1 })
|
||||||
|
.skip(skip)
|
||||||
|
.limit(limit)
|
||||||
|
.populate({ path: "orderItem.productId" })
|
||||||
|
.populate({ path: "addedBy" });
|
||||||
|
|
||||||
|
if (deliveredOrders.length > 0) {
|
||||||
|
return res.status(200).json({ deliveredOrders, totalOrders });
|
||||||
|
}
|
||||||
|
return res.status(404).json({ return_msg: "No delivered orders yet" });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching delivered orders:", error);
|
||||||
|
return res.status(500).json({ error: "Internal Server Error" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const updateOrderStatusById = async (req, res) => {
|
||||||
|
try {
|
||||||
|
let body = { status: req.body.status };
|
||||||
|
|
||||||
|
const currentDate = new Date();
|
||||||
|
body["status_timeline." + req.body.status] = currentDate;
|
||||||
|
|
||||||
|
const order = await PdOrder.findById(req.params.id).populate({
|
||||||
|
path: "addedBy",
|
||||||
|
select: "name email _id",
|
||||||
|
});
|
||||||
|
|
||||||
|
if (req.body.status === "cancelled") {
|
||||||
|
console.log("req came here ");
|
||||||
|
body["order_Cancelled_Reason"] = req.body?.ReasonforCancellation;
|
||||||
|
body["iscancelled"] = true;
|
||||||
|
|
||||||
|
const updatedCan = await PdOrder.findByIdAndUpdate(order._id, body);
|
||||||
|
|
||||||
|
await sendEmail({
|
||||||
|
to: `${order?.addedBy?.email}`, // Change to your recipient
|
||||||
|
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
||||||
|
subject: `Order #${order?.uniqueId} Update: Cancellation and Refund Process`,
|
||||||
|
html: `
|
||||||
|
<strong style="color: #1b03a3; font-size: 16px"> Hi ${
|
||||||
|
order?.addedBy.name
|
||||||
|
},</strong>
|
||||||
|
<h3 style="color: #333; font-family: Arial, sans-serif;">We hope this message finds you well. We're writing to inform you that your order ${
|
||||||
|
order?.uniqueId
|
||||||
|
} has been canceled. We understand that circumstances may change, and we're here to assist you throughout the process.</h3>
|
||||||
|
|
||||||
|
<h4 style="color: #333; font-family: Arial, sans-serif;">Cancellation Reason: ${
|
||||||
|
order?.order_Cancelled_Reason || "No specific reason provided."
|
||||||
|
}</h4>
|
||||||
|
|
||||||
|
<h4 style="color: #333; font-family: Arial, sans-serif;">Items:</h4>
|
||||||
|
<table style="border-collapse: collapse; width: 100%;">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">S No.</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Product Name</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Category</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Brand</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Image</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Quantity</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Price</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">GST Amount</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">SubTotal</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
${order?.orderItem
|
||||||
|
?.map(
|
||||||
|
(product, index) => `
|
||||||
|
<tr>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
index + 1
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.name
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.categoryName
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.brandName
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;"><img src="${
|
||||||
|
product?.image[0]?.url
|
||||||
|
}" alt="${
|
||||||
|
product.name
|
||||||
|
}" style="max-width: 40px; height: auto;"></td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.quantity
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
product.price
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
(product.GST * product.price) / 100
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
(product.price + (product.GST * product.price) / 100) *
|
||||||
|
product.quantity
|
||||||
|
}</td>
|
||||||
|
</tr>
|
||||||
|
`
|
||||||
|
)
|
||||||
|
.join("")}
|
||||||
|
<tr>
|
||||||
|
<th colspan="8" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
order?.grandTotal
|
||||||
|
}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h4 style="color: #333; font-family: Arial, sans-serif;">Refund Information: The amount from your canceled order will be processed for a refund. Please allow up to 7 working days for the amount to be transferred back to your original payment method.</h4>
|
||||||
|
|
||||||
|
<h5 style="color: #333; font-family: Arial, sans-serif;">If you have any concerns or further questions, please feel free to reply to this email. We appreciate your understanding and hope to serve you better in the future.</h5>
|
||||||
|
<br/>
|
||||||
|
<span style="color: #555; font-size: 13px;">Best regards,</span><br/>
|
||||||
|
<span style="color: #555; font-size: 13px;">Team Cheminova</span>
|
||||||
|
`,
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
status: "ok",
|
||||||
|
message: "Order status updated successfully!",
|
||||||
|
updatedCan,
|
||||||
|
});
|
||||||
|
} else if (req.body.status === "processing") {
|
||||||
|
const updatedPro = await PdOrder.findByIdAndUpdate(order._id, body);
|
||||||
|
await sendEmail({
|
||||||
|
to: `${order?.addedBy?.email}`, // Recipient's email address from order model
|
||||||
|
from: `${process.env.SEND_EMAIL_FROM}`, // Sender's email address
|
||||||
|
subject: `Your Order #${order?.uniqueId} is in Processing!`,
|
||||||
|
html: `
|
||||||
|
<div style="font-family: Arial, sans-serif; color: #333;">
|
||||||
|
<h3 style="color: #333;">Exciting news! Your order #${
|
||||||
|
order?.uniqueId
|
||||||
|
} has entered the processing phase. Our team is diligently preparing your items for dispatch. Rest assured, we're working hard to ensure everything is perfect for you.</h3>
|
||||||
|
<strong style="color: #1b03a3; font-size: 16px;">Hi ${
|
||||||
|
order?.addedBy.name
|
||||||
|
},</strong>
|
||||||
|
<h4 style="color: #333;">Order Status: ${
|
||||||
|
order?.status.charAt(0).toUpperCase() + order?.status.slice(1)
|
||||||
|
}</h4>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<h4 style="color: #333;">Order Items:</h4>
|
||||||
|
<table style="border-collapse: collapse; width: 100%;">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">S No.</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Product Name</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Variant</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Image</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Quantity</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Price</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">GST Amount</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">SubTotal</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
${order?.orderItem
|
||||||
|
?.map(
|
||||||
|
(product, index) => `
|
||||||
|
<tr>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
index + 1
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.name
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.variant_Name || "N/A"
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">
|
||||||
|
<img src="${product.image[0]?.url}" alt="${
|
||||||
|
product.name
|
||||||
|
}" style="max-width: 40px; height: auto;">
|
||||||
|
</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.quantity
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
product.price
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
(product.GST * product.price) / 100
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
(product.price + (product.GST * product.price) / 100) *
|
||||||
|
product.quantity
|
||||||
|
}</td>
|
||||||
|
</tr>
|
||||||
|
`
|
||||||
|
)
|
||||||
|
.join("")}
|
||||||
|
<tr>
|
||||||
|
<th colspan="7" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount:</th>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
order?.grandTotal
|
||||||
|
}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h5 style="color: #333;">We'll send you another email with the tracking details as soon as your order is dispatched. If you have any questions or need assistance, feel free to reply to this email.</h5>
|
||||||
|
<h5 style="color: #333;">Thank you for choosing Cheminova!</h5>
|
||||||
|
<br/>
|
||||||
|
<span style="color: #555; font-size: 13px;">Best regards,</span><br/>
|
||||||
|
<span style="color: #555; font-size: 13px;">Team Cheminova</span>
|
||||||
|
</div>
|
||||||
|
`,
|
||||||
|
});
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.json({
|
||||||
|
status: "ok",
|
||||||
|
message: "Order status updated successfully!",
|
||||||
|
updatedPro,
|
||||||
|
});
|
||||||
|
} else if (req.body.status === "dispatched") {
|
||||||
|
body["courier_name"] = req.body.courierName;
|
||||||
|
body["courier_tracking_id"] = req.body.TrackingID;
|
||||||
|
const disRes = await PdOrder.findByIdAndUpdate(order._id, body);
|
||||||
|
await sendEmail({
|
||||||
|
to: `${order?.addedBy?.email}`, // Assuming 'addedBy' references the user who placed the order
|
||||||
|
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
||||||
|
subject: `Your Order #${order?.uniqueId} is On Its Way!`,
|
||||||
|
html: `
|
||||||
|
<strong style="color: #1b03a3; font-size: 16px">Hi,</strong>
|
||||||
|
<h3 style="color: #333; font-family: Arial, sans-serif;">Exciting news! Your order #${
|
||||||
|
order?.uniqueId
|
||||||
|
} has been dispatched and is en route to you. 🚚 Here are the details:</h3>
|
||||||
|
|
||||||
|
<h4 style="color: #333; font-family: Arial, sans-serif;">Courier Name: ${
|
||||||
|
order?.courier_name || "N/A"
|
||||||
|
}</h4>
|
||||||
|
<h4 style="color: #333; font-family: Arial, sans-serif;">Courier Tracking ID: ${
|
||||||
|
order?.courier_tracking_id || "N/A"
|
||||||
|
}</h4>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<h4 style="color: #333; font-family: Arial, sans-serif;">Items:</h4>
|
||||||
|
<table style="border-collapse: collapse; width: 100%;">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">S No.</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Product Name</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">SKU</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Image</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Quantity</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Price</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">GST Amount</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">SubTotal</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
${order?.orderItem
|
||||||
|
?.map(
|
||||||
|
(product, index) => `
|
||||||
|
<tr>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
index + 1
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.name
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.SKU
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">
|
||||||
|
<img src="${product?.image[0]?.url}" alt="${
|
||||||
|
product.name
|
||||||
|
}" style="max-width: 40px; height: auto;">
|
||||||
|
</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.quantity
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
product.price
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
(product.GST * product.price) / 100
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
(product.price + (product.GST * product.price) / 100) *
|
||||||
|
product.quantity
|
||||||
|
}</td>
|
||||||
|
</tr>
|
||||||
|
`
|
||||||
|
)
|
||||||
|
.join("")}
|
||||||
|
<tr>
|
||||||
|
<th colspan="7" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount:</th>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
order?.grandTotal
|
||||||
|
}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h3 style="color: #333; font-family: Arial, sans-serif;">Order Status: Dispatched</h3>
|
||||||
|
<h4 style="color: #333; font-family: Arial, sans-serif;">If you have any questions or need assistance, feel free to reply to this email.</h4>
|
||||||
|
<h5 style="color: #333; font-family: Arial, sans-serif;">Thanks for choosing Cheminova! We hope you enjoy your purchase.</h5>
|
||||||
|
<br/>
|
||||||
|
<span style="color: #555; font-size: 13px;">Best regards,</span><br/>
|
||||||
|
<span style="color: #555; font-size: 13px;">Team Cheminova</span>
|
||||||
|
`,
|
||||||
|
});
|
||||||
|
return res.status(200).json({
|
||||||
|
status: "ok",
|
||||||
|
message: "Order status updated successfully!",
|
||||||
|
disRes,
|
||||||
|
});
|
||||||
|
} else if (req.body.status === "delivered") {
|
||||||
|
body["isDelivered"] = true;
|
||||||
|
body["DeliveredDate"] = req.body.DDate;
|
||||||
|
const updatedDeli = await PdOrder.findByIdAndUpdate(order._id, body);
|
||||||
|
await sendEmail({
|
||||||
|
to: `${order?.addedBy?.email}`, // Using 'addedBy' to reference the user's email
|
||||||
|
from: `${process.env.SEND_EMAIL_FROM}`, // Your verified sender
|
||||||
|
subject: `Your Order #${order?.uniqueId} Has Been Delivered!`,
|
||||||
|
html: `
|
||||||
|
<strong style="color: #1b03a3; font-size: 16px">Hi,</strong>
|
||||||
|
<h3 style="color: #333; font-family: Arial, sans-serif;">
|
||||||
|
Great news! Your order #${
|
||||||
|
order?.uniqueId
|
||||||
|
} has been successfully delivered to your doorstep. We hope everything is just as you expected!
|
||||||
|
</h3>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<h4 style="color: #333; font-family: Arial, sans-serif;">Items:</h4>
|
||||||
|
<table style="border-collapse: collapse; width: 100%;">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">S No.</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Product Name</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">SKU</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Image</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Quantity</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Price</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">GST Amount</th>
|
||||||
|
<th style="border: 1px solid #555; padding: 2px; text-align: center;">SubTotal</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
${order?.orderItem
|
||||||
|
?.map(
|
||||||
|
(product, index) => `
|
||||||
|
<tr>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
index + 1
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.name
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.SKU
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">
|
||||||
|
<img src="${product?.image[0]?.url}" alt="${
|
||||||
|
product.name
|
||||||
|
}" style="max-width: 40px; height: auto;">
|
||||||
|
</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
|
||||||
|
product.quantity
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
product.price
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
(product.GST * product.price) / 100
|
||||||
|
}</td>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
(product.price + (product.GST * product.price) / 100) *
|
||||||
|
product.quantity
|
||||||
|
}</td>
|
||||||
|
</tr>
|
||||||
|
`
|
||||||
|
)
|
||||||
|
.join("")}
|
||||||
|
<tr>
|
||||||
|
<th colspan="7" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount:</th>
|
||||||
|
<td style="border: 1px solid #555; padding: 2px; text-align: center;">₹${
|
||||||
|
order?.grandTotal
|
||||||
|
}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h3 style="color: #333; font-family: Arial, sans-serif;">Delivery Date: ${
|
||||||
|
req.body.DDate
|
||||||
|
}</h3>
|
||||||
|
|
||||||
|
<h4 style="color: #333; font-family: Arial, sans-serif;">
|
||||||
|
Your satisfaction is our priority, and we'd love to hear about your experience. Please take a moment to share your thoughts by leaving a review. Your feedback is invaluable to us!
|
||||||
|
</h4>
|
||||||
|
|
||||||
|
<h5 style="color: #333; font-family: Arial, sans-serif;">
|
||||||
|
If you have any questions or concerns about your order, feel free to reply to this email.
|
||||||
|
</h5>
|
||||||
|
|
||||||
|
<h5 style="color: #333; font-family: Arial, sans-serif;">
|
||||||
|
Thank you for choosing Cheminova! We hope to serve you again soon.
|
||||||
|
</h5>
|
||||||
|
|
||||||
|
<br/>
|
||||||
|
<span style="color: #555; font-size: 13px;">Best regards,</span><br/>
|
||||||
|
<span style="color: #555; font-size: 13px;">Team Cheminova</span>
|
||||||
|
`,
|
||||||
|
});
|
||||||
|
return res.status(200).json({
|
||||||
|
status: "ok",
|
||||||
|
message: "Order status updated successfully!",
|
||||||
|
updatedDeli,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// await Order.findByIdAndUpdate(order._id, body);
|
||||||
|
// console.log(order);
|
||||||
|
res
|
||||||
|
.status(200)
|
||||||
|
.json({ status: "ok", message: "Order status updated successfully!" });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
res
|
||||||
|
.status(500)
|
||||||
|
.json({ message: error?.message || "Something went wrong!" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@ -1,23 +1,60 @@
|
|||||||
import mongoose, { Schema } from "mongoose";
|
import mongoose, { Schema } from "mongoose";
|
||||||
import { nanoid } from "nanoid"; // To generate unique 6-char IDs
|
import { nanoid } from "nanoid"; // To generate unique 6-char IDs
|
||||||
|
|
||||||
const OrderItemSchema = new mongoose.Schema({
|
const orderItemSchema = new Schema({
|
||||||
productId: {
|
productId: {
|
||||||
type: mongoose.Schema.Types.ObjectId,
|
type: Schema.Types.ObjectId,
|
||||||
ref: "Product",
|
ref: "Product",
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
SKU: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
categoryName: {
|
||||||
|
type: String, // Directly store category name
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
brandName: {
|
||||||
|
type: String, // Directly store brand name
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
price: {
|
||||||
|
type: Number,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
GST: {
|
||||||
|
type: Number,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
HSN_Code: {
|
||||||
|
type: Number,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
image: [
|
||||||
|
{
|
||||||
|
public_id: String,
|
||||||
|
url: String,
|
||||||
|
},
|
||||||
|
],
|
||||||
quantity: {
|
quantity: {
|
||||||
type: Number,
|
type: Number,
|
||||||
required: true,
|
required: true,
|
||||||
min: 1,
|
default: 1,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const StatusHistorySchema = new mongoose.Schema({
|
const StatusHistorySchema = new mongoose.Schema({
|
||||||
status: {
|
status: {
|
||||||
type: String,
|
type: String,
|
||||||
enum: ["new", "dispatched", "paid", "process"], // Ensure this matches your status enum
|
enum: ["new", "dispatched", "cancelled", "processing", "delivered"], // Ensure this matches your status enum
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
timestamp: {
|
timestamp: {
|
||||||
@ -41,7 +78,7 @@ const pdOrderSchema = new Schema(
|
|||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
orderItem: [OrderItemSchema],
|
orderItem: [orderItemSchema],
|
||||||
subtotal: {
|
subtotal: {
|
||||||
type: Number,
|
type: Number,
|
||||||
required: true,
|
required: true,
|
||||||
@ -56,7 +93,7 @@ const pdOrderSchema = new Schema(
|
|||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
type: String,
|
type: String,
|
||||||
enum: ["new", "dispatched", "paid", "processing"],
|
enum: ["new", "dispatched", "cancelled", "processing", "delivered"],
|
||||||
default: "new",
|
default: "new",
|
||||||
},
|
},
|
||||||
statusUpdatedAt: {
|
statusUpdatedAt: {
|
||||||
|
@ -2,9 +2,14 @@ import express from "express";
|
|||||||
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
||||||
import {
|
import {
|
||||||
createOrder,
|
createOrder,
|
||||||
|
getCancelledOrdersAdmin,
|
||||||
|
getDeliveredOrdersAdmin,
|
||||||
|
getDispatchedOrdersAdmin,
|
||||||
|
getPlacedNewOrderAdmin,
|
||||||
getPlacedOrder,
|
getPlacedOrder,
|
||||||
getPlacedOrderAdmin,
|
|
||||||
getPlacedOrderById,
|
getPlacedOrderById,
|
||||||
|
getProcessingOrdersAdmin,
|
||||||
|
updateOrderStatusById,
|
||||||
} from "./pdOrderController.js";
|
} from "./pdOrderController.js";
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
@ -23,9 +28,26 @@ router
|
|||||||
authorizeRoles("principal-Distributor"),
|
authorizeRoles("principal-Distributor"),
|
||||||
getPlacedOrder
|
getPlacedOrder
|
||||||
);
|
);
|
||||||
router.route("/get-single-placed-order-pd/:id").get(getPlacedOrderById);
|
|
||||||
router
|
router
|
||||||
.route("/get-placed-order-admin")
|
.route("/get-single-placed-order-pd/:id")
|
||||||
.get(isAuthenticatedUser, authorizeRoles("admin"), getPlacedOrderAdmin);
|
.get(isAuthenticatedUser, getPlacedOrderById);
|
||||||
|
router
|
||||||
|
.route("/get-new-order-admin")
|
||||||
|
.get(isAuthenticatedUser, authorizeRoles("admin"), getPlacedNewOrderAdmin);
|
||||||
|
router
|
||||||
|
.route("/get-dispatched-order-admin")
|
||||||
|
.get(isAuthenticatedUser, authorizeRoles("admin"), getDispatchedOrdersAdmin);
|
||||||
|
router
|
||||||
|
.route("/get-cancelled-order-admin")
|
||||||
|
.get(isAuthenticatedUser, authorizeRoles("admin"), getCancelledOrdersAdmin);
|
||||||
|
router
|
||||||
|
.route("/get-processing-order-admin")
|
||||||
|
.get(isAuthenticatedUser, authorizeRoles("admin"), getProcessingOrdersAdmin);
|
||||||
|
router
|
||||||
|
.route("/get-delivered-order-admin")
|
||||||
|
.get(isAuthenticatedUser, authorizeRoles("admin"), getDeliveredOrdersAdmin);
|
||||||
|
router
|
||||||
|
.route("/change/status/:id")
|
||||||
|
.patch(isAuthenticatedUser, authorizeRoles("admin"), updateOrderStatusById);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
@ -1030,7 +1030,9 @@ export const getAllPrincipalDistributorbytmId = catchAsyncErrors(
|
|||||||
const { page = 1, show = 10, name, mobileNumber } = req.query;
|
const { page = 1, show = 10, name, mobileNumber } = req.query;
|
||||||
const tmId = req.params.id;
|
const tmId = req.params.id;
|
||||||
if (!tmId) {
|
if (!tmId) {
|
||||||
return res.status(400).json({ message: "Please provide Territory Manager ID" });
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ message: "Please provide Territory Manager ID" });
|
||||||
}
|
}
|
||||||
// Create a filter object
|
// Create a filter object
|
||||||
const filter = { role: "principal-Distributor" };
|
const filter = { role: "principal-Distributor" };
|
||||||
|
Loading…
Reference in New Issue
Block a user