From 0a493c6e95a228799fdf7383745a13b59b375a4b Mon Sep 17 00:00:00 2001 From: pawan-dot <71133473+pawan-dot@users.noreply.github.com> Date: Fri, 3 May 2024 16:38:47 +0530 Subject: [PATCH] change product and razorpay --- .../Orders/RazerPayCheckoutController.js | 85 +++++++++++++++---- resources/Products/ProductController.js | 6 +- resources/Products/ProductModel.js | 37 ++++---- 3 files changed, 87 insertions(+), 41 deletions(-) diff --git a/resources/Orders/RazerPayCheckoutController.js b/resources/Orders/RazerPayCheckoutController.js index 5211a0e..2940c86 100644 --- a/resources/Orders/RazerPayCheckoutController.js +++ b/resources/Orders/RazerPayCheckoutController.js @@ -80,32 +80,85 @@ export const checkout = async (req, res) => { 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), + variant_Name: item?.variant?.variant_Name + ? item?.variant?.variant_Name + : "", + price: Number( + item?.variant?.price + ? item?.variant?.price + : item?.product?.master_price + ), + total_price: + item.quantity * + Number( + item?.variant?.price + ? item?.variant?.price + : item?.product?.master_price + ), - image: item.product.image, - quantity: item.quantity, + 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( + item?.variant?.price + ? item?.variant?.price + : item?.product?.master_price + ) * Number( - (Number(item.variant.price) * item.variant.gst_Id?.tax) / 100 + item?.variant?.gst_Id?.tax + ? item?.variant?.gst_Id?.tax + : item?.product?.master_GST?.tax + )) / + 100 + )?.toFixed(2), + total_gst_amount: Number( + Number(item?.quantity) * + Number( + (Number( + item?.variant?.price + ? item?.variant?.price + : item?.product?.master_price + ) * + Number( + item?.variant?.gst_Id?.tax + ? item?.variant?.gst_Id?.tax + : item?.product?.master_GST?.tax + )) / + 100 ) - )?.toFixed(3), - gst_rate: item.variant.gst_Id?.tax, - tax_Name: item.variant?.gst_Id?.name, + )?.toFixed(2), + gst_rate: item?.variant?.gst_Id?.tax + ? item?.variant?.gst_Id?.tax + : item?.product?.master_GST?.tax, + tax_Name: item?.variant?.gst_Id?.name + ? item?.variant?.gst_Id?.name + : item?.product?.master_GST?.name, product_Subtotal: Number( - Number(item.quantity * Number(item.variant.price)) + + Number( + item.quantity * + Number( + item?.variant?.price + ? item?.variant?.price + : item?.product?.master_price + ) + ) + Number( Number(item.quantity) * Number( - (Number(item.variant.price) * item.variant.gst_Id?.tax) / 100 + (Number( + item?.variant?.price + ? item?.variant?.price + : item?.product?.master_price + ) * + Number( + item?.variant?.gst_Id?.tax + ? item?.variant?.gst_Id?.tax + : item?.product?.master_GST?.tax + )) / + 100 ) ) - ).toFixed(3), + )?.toFixed(2), })); // console.log("line", lineItems[0]); diff --git a/resources/Products/ProductController.js b/resources/Products/ProductController.js index 6af6b46..635d68e 100644 --- a/resources/Products/ProductController.js +++ b/resources/Products/ProductController.js @@ -129,7 +129,7 @@ export const getAllProductAdmin = async (req, res) => { const total = await Product.countDocuments(obj); const product = await Product.find(obj) .populate({ - path: "category addedBy variants.gst_Id", + path: "category addedBy master_GST variants.gst_Id", select: "name categoryName tax", }) .limit(PAGE_SIZE) @@ -175,7 +175,7 @@ export const getAllProductUser = async (req, res) => { const total = await Product.countDocuments(obj); const product = await Product.find(obj) .populate({ - path: "category addedBy variants.gst_Id", + path: "category addedBy master_GST variants.gst_Id", select: "name categoryName tax", }) .limit(PAGE_SIZE) @@ -283,7 +283,7 @@ export const ChangeFeatueProductStatus = async (req, res) => { export const getOneProduct = async (req, res) => { try { const data = await Product.findById(req.params.id).populate({ - path: "category addedBy variants.gst_Id", + path: "category addedBy master_GST variants.gst_Id", select: "name categoryName tax", }); if (data) { diff --git a/resources/Products/ProductModel.js b/resources/Products/ProductModel.js index f68b880..987d436 100644 --- a/resources/Products/ProductModel.js +++ b/resources/Products/ProductModel.js @@ -9,32 +9,26 @@ const productSchema = new Schema( required: [true, "Please Enter product Name"], trim: true, }, - // uniqueId: { - // type: String, - // required: true, - // }, + + category: { + type: Schema.Types.ObjectId, + ref: "CategoryModel", + }, + + master_price: { + type: Number, + required: true, + }, + master_GST: { + type: mongoose.Schema.ObjectId, + ref: "Tax", + }, description: { type: String, maxLength: [400, "description cannot exceed 100 characters"], required: [true, "Please Enter product Description"], }, - category: { - type: Schema.Types.ObjectId, - ref: "CategoryModel", - }, - // gst: { - // type: Schema.Types.ObjectId, - // ref: "Tax", - // }, - // total_amount: { - // type: Number, - // required: true, - // }, - // gst_amount: { - // type: Number, - // required: true, - // }, special_instructions: { type: String, }, @@ -48,8 +42,7 @@ const productSchema = new Schema( weight: { type: Number, default: 0 }, volume: { type: Number, default: 0 }, price: { type: String, default: "" }, - // gst_Name: { type: String, default: "" }, - // gst_Rate: { type: String, default: "" }, + gst_Id: { type: mongoose.Schema.ObjectId, ref: "Tax",