This commit is contained in:
Sibunnayak 2024-04-19 15:54:49 +05:30
commit 72584b4dd9
7 changed files with 228 additions and 91 deletions

10
.env
View File

@ -12,12 +12,12 @@ CLOUDINARY_API_KEY = "877544192441588"
CLOUDINARY_API_SECRET = "9paejuSC-fY5b0WoaUuSFURSnvM"
WEBHOOK_SECRET_KEY="whsec_m9u7CFBCY1qWarhxq65CkII6egOBf20K"
STRIPE_SECRET_KEY="sk_test_51OhPRdSG6gbAOwcEid1GavJ4FTD0ZuHVTferdvJwKal77RlMtFJGBzL5GjtL0ie8ZJztsGjUWi8DWrnw1pDdDRGS005Hk0ahql"
STRIPE_WEBHOOK_SECRET="whsec_dc9b9084fc764c806c8c5c06dd91de1ee809e9c8deab6d56e8e3ef2fc9c30c67"
<<<<<<< HEAD
# STRIPE_SECRET_KEY="sk_test_51OhPRdSG6gbAOwcEid1GavJ4FTD0ZuHVTferdvJwKal77RlMtFJGBzL5GjtL0ie8ZJztsGjUWi8DWrnw1pDdDRGS005Hk0ahql"
# STRIPE_WEBHOOK_SECRET="whsec_dc9b9084fc764c806c8c5c06dd91de1ee809e9c8deab6d56e8e3ef2fc9c30c67"
FRONTEND_URL="http://127.0.0.1:5173"
RAZERPAY_KEY_ID="rzp_test_smzQmWoS64S2W9"
RAZERPAY_SECRET_KEY="cSn6MgA4xSEaZBpPp4zpDA3C"
RAZERPAY_KEY_ID="rzp_test_2rg1Bq3Ki8xw9e"
RAZERPAY_SECRET_KEY="WFhHbXL7AlLIuull9kKjYiNA"
FRONTEND_URL="https://smellika.com"

View File

@ -39,20 +39,7 @@ export const getRzpkey = async (req, res) => {
});
};
export const checkout = async (req, res) => {
// console.log(req.body.subtotal);
const options = {
amount: Number(req.body.subtotal * 100),
currency: "INR",
};
const order = await instance.orders.create(options);
// id: "order_Ns423uPG0r36Dk";
//save order in database
if (order?.id) {
const { email } = req.user;
if (!email)
return res.status(400).send({ message: "Please enter the email" });
try {
const { address, cart, subtotal } = req.body;
if (cart.length < 1)
return res.status(400).json({ message: "cart is empty!" });
@ -64,15 +51,16 @@ export const checkout = async (req, res) => {
return res
.status(404)
.json({ message: "please provide product subtotal!" });
// switch (true) {
// //validation
// case !address: {
// return res.status(404).json({ msg: "please select shipping address" });
// }
// case !subtotal: {
// return res.status(404).json({ msg: "please provide product subtotal" });
// }
// }
const options = {
amount: Number(req.body.subtotal * 100),
currency: "INR",
};
const order = await instance.orders.create(options);
//save order in database
if (order?.id) {
const { email } = req.user;
if (!email)
return res.status(400).send({ message: "Please enter the email" });
let addss = await shippingAddress.findById(address);
let shipping = {
first_Name: addss.first_Name,
@ -89,15 +77,32 @@ export const checkout = async (req, res) => {
const orderItems = await cart.map((item) => ({
product: item.product._id,
name: item.product.name,
price: item.product.price,
total_Amount: item.product.total_amount,
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: item.product.gst_amount,
gst_rate: item.product.gst?.tax,
tax_Name: item.product.gst?.name,
product_Subtotal: item.subtotal,
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),
}));
// console.log("line", lineItems[0]);
@ -111,16 +116,25 @@ export const checkout = async (req, res) => {
razorpay_order_id: order?.id,
});
} else {
res.status(400).json({
return res.status(400).json({
success: false,
message: "Failled to order Create",
});
}
res.status(200).json({
return res.status(200).json({
success: true,
order,
});
} catch (error) {
console.log("error", error);
return res.status(400).json({
success: false,
message: error?.description
? "Razorpay" + error?.description
: "Something went wrong!",
});
}
};
export const paymentVerification = async (req, res) => {
@ -188,6 +202,8 @@ export const paymentVerification = async (req, res) => {
<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>
@ -209,6 +225,9 @@ export const paymentVerification = async (req, res) => {
<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
@ -226,7 +245,7 @@ export const paymentVerification = async (req, res) => {
product?.gst_amount
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product.product_Subtotal
product?.product_Subtotal
}</td>
</tr>
@ -234,7 +253,7 @@ export const paymentVerification = async (req, res) => {
)
.join("")}
<tr>
<th colspan="6" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<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;">${
findSameOrder?.total_amount
}</td>

View File

@ -176,14 +176,12 @@ export const updateOrderStatusById = async (req, res) => {
<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>
@ -199,6 +197,9 @@ export const updateOrderStatusById = async (req, res) => {
<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
@ -224,7 +225,7 @@ export const updateOrderStatusById = async (req, res) => {
)
.join("")}
<tr>
<th colspan="6" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<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>
@ -267,6 +268,7 @@ export const updateOrderStatusById = async (req, res) => {
<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>
@ -288,6 +290,9 @@ export const updateOrderStatusById = async (req, res) => {
<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
@ -313,7 +318,7 @@ export const updateOrderStatusById = async (req, res) => {
)
.join("")}
<tr>
<th colspan="6" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<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>
@ -383,6 +388,7 @@ export const updateOrderStatusById = async (req, res) => {
<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>
@ -404,6 +410,9 @@ export const updateOrderStatusById = async (req, res) => {
<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
@ -429,7 +438,7 @@ export const updateOrderStatusById = async (req, res) => {
)
.join("")}
<tr>
<th colspan="6" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<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>
@ -470,8 +479,8 @@ export const updateOrderStatusById = async (req, res) => {
<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>
@ -491,6 +500,9 @@ export const updateOrderStatusById = async (req, res) => {
<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
@ -516,7 +528,7 @@ export const updateOrderStatusById = async (req, res) => {
)
.join("")}
<tr>
<th colspan="6" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<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>

View File

@ -62,11 +62,15 @@ const orderSchema = new mongoose.Schema(
type: String,
default: "",
},
variant_Name: {
type: String,
default: "",
},
price: {
type: Number,
default: "",
},
total_Amount: {
total_price: {
type: Number,
default: "",
},
@ -85,6 +89,10 @@ const orderSchema = new mongoose.Schema(
type: Number,
default: "",
},
total_gst_amount: {
type: Number,
default: "",
},
gst_rate: {
type: Number,
default: "",

View File

@ -62,9 +62,6 @@ export const updateProduct = async (req, res) => {
}
if (req.files) {
// req.body.addedBy = req.user.id;
// const image_file = req.files.image;
// console.log("req.files", req.files);
const getProduct = await Product.findById(req.params.id);
if (getProduct.image?.length > 0) {
@ -116,19 +113,36 @@ export const updateProduct = async (req, res) => {
////////////////////////////////////////////////////////////////////////////
//get All Product
export const getAllProduct = async (req, res) => {
export const getAllProductAdmin = async (req, res) => {
try {
const product = await Product.find()
const PAGE_SIZE = parseInt(req.query?.show || "10");
const page = parseInt(req.query?.page - 1 || "0");
let obj = {};
if (req.query?.name)
obj.name = {
$regex: new RegExp(req.query.name),
$options: "i",
};
if (req.query?.category) obj.category = req.query.category;
const total = await Product.countDocuments(obj);
const product = await Product.find(obj)
.populate({
path: "category addedBy variants.gst_Id",
select: "name categoryName tax",
})
.limit(PAGE_SIZE)
.skip(PAGE_SIZE * page)
// .sort("name")
.sort({
createdAt: -1,
});
})
.exec();
if (product) {
return res.status(200).json({
success: true,
total_data: total,
total_pages: Math.ceil(total / PAGE_SIZE),
product,
});
}
@ -139,6 +153,83 @@ export const getAllProduct = async (req, res) => {
});
}
};
//get All Product User(website)
export const getAllProductUser = async (req, res) => {
try {
const PAGE_SIZE = parseInt(req.query?.show || "10");
const page = parseInt(req.query?.page - 1 || "0");
let obj = {};
if (req.query?.name)
obj.name = {
$regex: new RegExp(req.query.name),
$options: "i",
};
if (req.query?.category) obj.category = req.query.category;
obj.product_Status = "Active";
const total = await Product.countDocuments(obj);
const product = await Product.find(obj)
.populate({
path: "category addedBy variants.gst_Id",
select: "name categoryName tax",
})
.limit(PAGE_SIZE)
.skip(PAGE_SIZE * page)
// .sort("name")
.sort({
createdAt: -1,
})
.exec();
if (product) {
return res.status(200).json({
success: true,
total_data: total,
total_pages: Math.ceil(total / PAGE_SIZE),
product,
});
}
} catch (error) {
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!",
});
}
};
//Change Product status
export const ChangeProductStatus = async (req, res) => {
try {
const data = await Product.findById(req.params.id);
if (data) {
if (data?.product_Status === "Active") {
let product = await Product.findByIdAndUpdate(
req.params.id,
{ product_Status: "inActive" },
{ new: true } // Return the updated document
);
return res.status(200).json({
success: true,
msg: "Changed status inActive",
});
} else {
let product = await Product.findByIdAndUpdate(
req.params.id,
{ product_Status: "Active" },
{ new: true } // Return the updated document
);
return res.status(200).json({
success: true,
msg: "Changed status Active",
});
}
}
} catch (error) {
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!",
});
}
};
//get One Product
export const getOneProduct = async (req, res) => {
try {
@ -165,11 +256,9 @@ export const getOneProduct = async (req, res) => {
export const getAllProductsDevicesFirst = async (req, res) => {
try {
// we want products with category name Device to be displayed first, so i have first found the products with category name Devices then made another request to find all products and filtered products with category devices , then merged both arrays so we get devices first then all other categories
const categoryName = "Devices";
// Find the category object by name first
const category = await CategoryModel.findOne({ categoryName });
if (!category) {
throw new Error("Category not found");
}

View File

@ -66,7 +66,7 @@ const productSchema = new Schema(
],
product_Status: {
type: String,
enum: ["Active", "Inactive"],
enum: ["Active", "inActive"],
default: "Active",
},
addedBy: {

View File

@ -1,20 +1,29 @@
import express from "express";
import {
createProduct,
getAllProduct,
getAllProductAdmin,
updateProduct,
deleteProduct,
getOneProduct,
deleteImageFromCloudinary,
getProductsByCategory,
getAllProductUser,
getAllProductsDevicesFirst,
ChangeProductStatus,
} from "./ProductController.js";
const router = express.Router();
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
router
.route("/product/create/")
.post(isAuthenticatedUser, authorizeRoles("admin"), createProduct);
router.route("/product/getAll/").get(getAllProduct);
router
.route("/product/getAll/admin/")
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllProductAdmin);
//change Product status
router.route("/product/admin/status/:id").patch(ChangeProductStatus);
//get all product user
router.route("/product/getAll/user/").get(getAllProductUser);
router
.route("/product/getAllProductsDevicesFrist/")
.get(getAllProductsDevicesFirst);