point of sale order contoller with razorpay completed and cash with mail completed

This commit is contained in:
Sibunnayak 2024-04-29 15:10:57 +05:30
parent 6dc55eeb28
commit 105da2bccb
3 changed files with 146 additions and 107 deletions

3
app.js
View File

@ -172,7 +172,6 @@ import CouponRoute from "./resources/Affiliate&Coupon/Coupon/CouponRoute.js";
//support Ticket
import SupportRouter from "./resources/Supports/supportRoute.js";
// Point of Sale
import PosorderRoute from "./resources/PosOrders/PosorderRoute.js";
app.use("/api/v1/", user);
//Product
@ -240,6 +239,4 @@ app.use("/api/panel", PanelRoute);
// app.use("/api/shorturl", ShortUrlRouter);
//Support
app.use("/api", SupportRouter);
// Point of Sale
app.use("/api/posOrder", PosorderRoute);
export default app;

9
package-lock.json generated
View File

@ -5417,7 +5417,8 @@
"cloudinary-core": {
"version": "2.12.3",
"resolved": "https://registry.npmjs.org/cloudinary-core/-/cloudinary-core-2.12.3.tgz",
"integrity": "sha512-Ll4eDzcrIVn4zCttMh3Mdi+KNz07p5EEjBT2PQSRx8Eok1lKPt3uBBenOk/w88RKK3B8SFIWcEe/mN4BHQ0p8A=="
"integrity": "sha512-Ll4eDzcrIVn4zCttMh3Mdi+KNz07p5EEjBT2PQSRx8Eok1lKPt3uBBenOk/w88RKK3B8SFIWcEe/mN4BHQ0p8A==",
"requires": {}
},
"color-convert": {
"version": "2.0.1",
@ -6660,7 +6661,8 @@
"multer-storage-cloudinary": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/multer-storage-cloudinary/-/multer-storage-cloudinary-4.0.0.tgz",
"integrity": "sha512-25lm9R6o5dWrHLqLvygNX+kBOxprzpmZdnVKH4+r68WcfCt8XV6xfQaMuAg+kUE5Xmr8mJNA4gE0AcBj9FJyWA=="
"integrity": "sha512-25lm9R6o5dWrHLqLvygNX+kBOxprzpmZdnVKH4+r68WcfCt8XV6xfQaMuAg+kUE5Xmr8mJNA4gE0AcBj9FJyWA==",
"requires": {}
},
"mute-stream": {
"version": "0.0.8",
@ -7893,7 +7895,8 @@
"ws": {
"version": "7.4.6",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.4.6.tgz",
"integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A=="
"integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==",
"requires": {}
},
"xregexp": {
"version": "2.0.0",

View File

@ -1,6 +1,7 @@
import { shippingAddress } from "../ShippingAddresses/ShippingAddressModel.js";
import { Order } from "./orderModel.js";
import sendEmail from "../../Utils/sendEmail.js";
//generate unique order id
const generateUniqueOrderId = async () => {
const currentYear = new Date().getFullYear();
@ -21,99 +22,6 @@ const generateUniqueOrderId = async () => {
return orderId;
};
// export const poscreateOrderCheckout = async (req, res) => {
// try {
// const { userr,address, cart, subtotal,orderType } = req.body;
// // console.log(req.body)
// // Perform validation
// if (cart.length < 1)
// return res.status(400).json({ message: "cart is empty!" });
// if (!address)
// return res
// .status(404)
// .json({ message: "please select shipping address!" });
// if (!subtotal)
// return res
// .status(404)
// .json({ message: "please provide product subtotal!" });
// if (!userr)
// return res.status(400).json({ message: "user is not defined" });
// // Retrieve shipping address from database
// let addss = await shippingAddress.findById(address);
// let shipping = {
// first_Name: addss.first_Name,
// last_Name: addss?.last_Name,
// phone_Number: addss?.phone_Number,
// street: addss?.street,
// city: addss?.city,
// state: addss?.state,
// postalCode: addss?.postalCode,
// country: addss?.country,
// company_name: addss?.company_name,
// gst_number: addss?.gst_number,
// addressId: address,
// };
// // Construct order items array
// const orderItems = await cart.map((item) => ({
// product: item.product._id,
// name: item.product.name,
// variant_Name: item.variant.variant_Name,
// price: Number(item.variant.price),
// total_price: item.quantity * Number(item.variant.price),
// image: item.product.image,
// quantity: item.quantity,
// gst_amount: Number(
// (Number(item.variant.price) * item.variant.gst_Id?.tax) / 100
// )?.toFixed(3),
// total_gst_amount: Number(
// Number(item.quantity) *
// Number(
// (Number(item.variant.price) * item.variant.gst_Id?.tax) / 100
// )
// )?.toFixed(3),
// gst_rate: item.variant.gst_Id?.tax,
// tax_Name: item.variant?.gst_Id?.name,
// product_Subtotal: Number(
// Number(item.quantity * Number(item.variant.price)) +
// Number(
// Number(item.quantity) *
// Number(
// (Number(item.variant.price) * item.variant.gst_Id?.tax) / 100
// )
// )
// ).toFixed(3),
// }));
// // Generate a unique order ID
// const Id = await generateUniqueOrderId();
// // Create the order document
// const order = await Order.create({
// orderID: Id,
// total_amount: subtotal,
// orderItems,
// shippingInfo: shipping,
// user: userr,
// orderType,
// paymentMode:"cod",
// payment_status:"success",
// isPaid:true,
// paidAt:new Date().toISOString(),
// });
// return res.status(201).json({ success: true, order });
// } catch (error) {
// console.error("Error creating order:", error);
// return res
// .status(500)
// .json({ success: false, message: "Internal server error" });
// }
// };
export const poscreateOrderCheckout = async (req, res) => {
try {
const { userr, address, cart, subtotal, orderType } = req.body;
@ -122,11 +30,14 @@ export const poscreateOrderCheckout = async (req, res) => {
if (cart.length < 1)
return res.status(400).json({ message: "Cart is empty!" });
if (!address)
return res.status(404).json({ message: "Please select a shipping address!" });
return res
.status(404)
.json({ message: "Please select a shipping address!" });
if (!subtotal)
return res.status(404).json({ message: "Please provide the product subtotal!" });
if (!userr)
return res.status(400).json({ message: "User is not defined" });
return res
.status(404)
.json({ message: "Please provide the product subtotal!" });
if (!userr) return res.status(400).json({ message: "User is not defined" });
// Retrieve shipping address from database
let addss = await shippingAddress.findById(address);
@ -154,11 +65,24 @@ export const poscreateOrderCheckout = async (req, res) => {
total_price: item.quantity * Number(item.variant.price),
image: item.product.image,
quantity: item.quantity,
gst_amount: Number((Number(item.variant.price) * item.variant.gst_Id?.tax) / 100)?.toFixed(3),
total_gst_amount: Number(Number(item.quantity) * Number((Number(item.variant.price) * item.variant.gst_Id?.tax) / 100))?.toFixed(3),
gst_amount: Number(
(Number(item.variant.price) * item.variant.gst_Id?.tax) / 100
)?.toFixed(3),
total_gst_amount: Number(
Number(item.quantity) *
Number((Number(item.variant.price) * item.variant.gst_Id?.tax) / 100)
)?.toFixed(3),
gst_rate: item.variant.gst_Id?.tax,
tax_Name: item.variant?.gst_Id?.name,
product_Subtotal: Number(Number(item.quantity * Number(item.variant.price)) + Number(Number(item.quantity) * Number((Number(item.variant.price) * item.variant.gst_Id?.tax) / 100))).toFixed(3),
product_Subtotal: Number(
Number(item.quantity * Number(item.variant.price)) +
Number(
Number(item.quantity) *
Number(
(Number(item.variant.price) * item.variant.gst_Id?.tax) / 100
)
)
).toFixed(3),
}));
// Generate a unique order ID
@ -177,10 +101,125 @@ export const poscreateOrderCheckout = async (req, res) => {
isPaid: true,
paidAt: new Date().toISOString(),
});
// console.log(order);
// Find the user associated with the order
const orderWithUser = await Order.findById(order._id).populate("user");
if (!orderWithUser) {
return res
.status(404)
.json({ success: false, message: "Order not found" });
}
const user = orderWithUser.user;
const userEmail = user.email;
// Send email after order creation
await sendEmail({
to: userEmail,
from: `${process.env.SEND_EMAIL_FROM}`,
subject: `Your Order #${order?.orderID} Confirmation`,
html: ` <h1 style="color: #333; text-align: center; font-family: Arial, sans-serif;">Welcome to Smellika - Let the Shopping Begin!</h1>
<strong style="color: #1b03a3; font-size: 16px"> Hi ${
order?.shippingInfo?.first_Name
},</strong>
<p style="color: #555; font-size: 15px;">Great news! Your order #${
order?.orderID
} has been confirmed. Here are the details</p>
<h4 style="color: #333; font-family: Arial, sans-serif;">Shipping Address : ${
order?.shippingInfo?.first_Name
} ${order?.shippingInfo?.last_Name} , ${order?.shippingInfo?.street} ${
order?.shippingInfo?.city
} ${order?.shippingInfo?.state} ${order?.shippingInfo?.country}, PIN-${
order?.shippingInfo?.postalCode
}, Phone Number: ${order?.shippingInfo?.phone_Number}
${
order?.shippingInfo?.company_name
? ",Company Name :" + order?.shippingInfo?.company_name + ""
: ""
} ${
order?.shippingInfo?.gst_number
? ", GST_NO:" + order?.shippingInfo?.gst_number
: ""
}</h4>
<h4 style="color: #333; font-family: Arial, sans-serif;">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?.orderItems
?.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
}</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_amount
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product?.product_Subtotal
}</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?.total_amount
}</td>
</tr>
</tbody>
</table>
<br/>
<span style="color: #555; font-size: 13px;">Best regards,</span><br/>
<span style="color: #555; font-size: 13px;">Team Smellika</span>`,
});
return res.status(201).json({ success: true, order });
} catch (error) {
console.error("Error creating order:", error);
return res.status(500).json({ success: false, message: "Internal server error" });
return res
.status(500)
.json({ success: false, message: "Internal server error" });
}
};