This commit is contained in:
roshangarg28 2024-04-02 14:32:01 +05:30
commit 2fc5111503
13 changed files with 5957 additions and 76 deletions

4
.env
View File

@ -14,6 +14,10 @@ WEBHOOK_SECRET_KEY="whsec_m9u7CFBCY1qWarhxq65CkII6egOBf20K"
STRIPE_SECRET_KEY="sk_test_51OhPRdSG6gbAOwcEid1GavJ4FTD0ZuHVTferdvJwKal77RlMtFJGBzL5GjtL0ie8ZJztsGjUWi8DWrnw1pDdDRGS005Hk0ahql"
STRIPE_WEBHOOK_SECRET="whsec_dc9b9084fc764c806c8c5c06dd91de1ee809e9c8deab6d56e8e3ef2fc9c30c67"
<<<<<<< HEAD
FRONTEND_URL="http://127.0.0.1:5173"
RAZERPAY_KEY_ID="rzp_test_smzQmWoS64S2W9"
RAZERPAY_SECRET_KEY="cSn6MgA4xSEaZBpPp4zpDA3C"
FRONTEND_URL="https://smellika.com"

4
app.js
View File

@ -9,8 +9,8 @@ import cors from "cors";
import cookieParser from "cookie-parser";
// Design Router
import designRoute from "./resources/Design/designRouter.js";
// app.use(express.json({ limit: "50mb" }));
// app.use(express.urlencoded({ extended: true, limit: "50mb" }));
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ extended: true, limit: "50mb" }));
//webhook call route according strip (webhook want raw data not json data)
app.use((req, res, next) => {

5353
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -29,6 +29,8 @@
"multer-storage-cloudinary": "^4.0.0",
"nodemailer": "^6.9.4",
"nodemon": "^3.0.1",
"pm2": "^5.3.1",
"razorpay": "^2.9.2",
"secure-random-password": "^0.2.3",
"stripe": "^14.16.0",
"uuid": "^9.0.1",

View File

@ -1,59 +1,64 @@
import Blog from "./BlogModel.js";
import cloudinary from "../../Utils/cloudinary.js";
export const createBlog = async (req, res) => {
const { title, tags, image, blog_content } = req.body;
console.log(req.body);
// Checking Fields
if (!title || !tags || !image || !blog_content) {
return res.status(400).json({
success: false,
message: "All fields are mandatory",
});
}
const { title, tags, blog_content } = req.body;
try {
let images = [];
let Allfiles = req.files.image;
console.log(Allfiles);
// if (!Array.isArray(Allfiles)) {
// Allfiles = [Allfiles]; // Convert to array if it's not already
// }
let image; // To store Cloudinary image details
// Allfiles.forEach((file) => {
// if (typeof file.tempFilePath === "string") {
// let filepath = file.tempFilePath;
// images.push(filepath);
// }
// });
if (req.files && req.files.image) {
const imageFile = req.files.image;
const result = await cloudinary.v2.uploader.upload(
imageFile.tempFilePath,
{
folder: "smellica/Blog",
}
);
// const newBlog = await Blog.create({
// title,
// tags,
// image: images, // Assign the array of image file paths
// blog_content,
// });
image = {
public_id: result.public_id,
url: result.secure_url,
};
}
// Create the blog post
const blog = await Blog.create({
title,
tags: tags.split(/\s*,\s*|\s+/)
.filter(tag => tag.trim() !== ''), // Splitting tags string into array
image,
blog_content,
});
res.status(201).json({
success: true,
blog,
message: "Blog created successfully",
data: images,
});
} catch (error) {
console.error("Error creating blog:", error);
res.status(500).json({
success: false,
message: error.message ? error.message : "Internal server error",
message: error.message || "Internal server error",
});
}
};
export const getAllBlog = async (req, res) => {
try {
const saveData = await Blog.find();
res.status(200).json({
success: true,
message: saveData,
});
const BlogData = await Blog.find().sort({ createdAt: -1 });
if (BlogData) {
return res.status(200).json({
success: true,
BlogData,
message: "Fetched All Blog",
});
} else {
return res.status(404).json({
success: true,
message: "No Blog till Now",
});
}
} catch {
res.status(500).json({
success: false,
@ -61,3 +66,140 @@ export const getAllBlog = async (req, res) => {
});
}
};
//get single Blog
export const getOneBlog = async (req, res) => {
try {
// console.log(req.params.id);
const blog = await Blog.findById(req.params.id);
if (blog) {
return res.status(200).json({
success: true,
blog,
});
} else {
return res.status(404).json({
success: false,
msg: "Blog not found",
});
}
} catch (error) {
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!",
});
}
};
export const deleteImageFromCloudinary = async (req, res) => {
const { public_id } = req.params;
try {
if (!public_id) {
return res.status(400).json({
success: false,
msg: "Please Provide Product ID!",
});
}
const response = await cloudinary.v2.uploader.destroy(public_id);
if (response) {
res.status(200).json({
success: true,
msg: "Blog Deleted Successfully!!",
});
}
} catch (error) {
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!",
});
}
};
//delete one Product
export const deleteBlog = async (req, res) => {
try {
if (!req?.user) return res.status(400).json({ message: "please login !" });
// console.log(req?.user)
if (!req.params.id)
return res.status(400).json({ message: "please give Blog ID !" });
// console.log(req.params.id)
const getblog = await Blog.findById(req.params.id);
// console.log(getblog)
if (!getblog) {
return res
.status(404)
.json({ success: false, msg: "Testimonial not Found!" });
}
// Deleting Images From Cloudinary
await cloudinary.v2.uploader.destroy(getblog.image.public_id);
//-------------------------//
const blog = await Blog.findByIdAndDelete(req.params.id);
if (!blog) {
return res.status(404).json({ message: "blog Not Found" });
}
await blog.remove();
res.status(200).json({ success: true, msg: "blog Deleted Successfully!!" });
} catch (error) {
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!",
});
}
};
//update blog
export const updateBlog = async (req, res) => {
try {
// Check if the user is authenticated
if (!req.user) {
return res.status(400).json({ message: "Please login!" });
}
// Destructure request body
const { title, tags, blog_content } = req.body;
// Prepare an object for the updated testimonial data
const updatedBlogData = {
title,
tags: tags.split(/\s*,\s*|\s+/)
.filter(tag => tag.trim() !== ''), // Splitting tags string into array
blog_content,
};
// Check if files are uploaded
if (req.files && req.files.image) {
// If image file is uploaded, upload it to cloudinary
const uploadedImage = req.files.image;
const result = await cloudinary.v2.uploader.upload(
uploadedImage.tempFilePath,
{
folder: "smellica/Blog",
}
);
// Prepare the image object with public_id and url
const image = {
public_id: result.public_id,
url: result.secure_url,
};
// Assign the uploaded image to the Blog's image field
updatedBlogData.image = image;
}
const modifiedBlog = await Blog.findOneAndUpdate(
{ _id: req.params.id },
{ $set: updatedBlogData },
{ new: true }
);
return res.status(200).json({
success: true,
ModifyBlog: modifiedBlog,
});
} catch (error) {
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!",
});
}
};

View File

@ -13,7 +13,12 @@ const blogSchema = new Schema(
required: [true, "Tags are required"],
},
image: {
type: String,
public_id: {
type: String,
},
url: {
type: String,
},
},
blog_content: {
type: Object,

View File

@ -1,11 +1,30 @@
import express from "express";
import { createBlog, getAllBlog } from "./BlogController.js";
import { createBlog, getAllBlog, getOneBlog, deleteBlog, deleteImageFromCloudinary, updateBlog } from "./BlogController.js";
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
const router = express.Router();
router.post("/create", createBlog);
router.get("/getallblog", getAllBlog);
router
.route("/create")
.post(isAuthenticatedUser, authorizeRoles("admin"), createBlog);
router
.route("/getallblog")
.get(getAllBlog);
router
.route("/getoneblog/:id")
.get(getOneBlog);
router
.route("/deleteblog/:id")
.delete(isAuthenticatedUser, authorizeRoles("admin"), deleteBlog);
router
.route("/deleteImage/jatinMor/Blog/:public_id")
.delete(
isAuthenticatedUser,
authorizeRoles("admin"),
deleteImageFromCloudinary
);
router
.route("/updateblog/:id")
.patch(isAuthenticatedUser, authorizeRoles("admin"), updateBlog);
export default router;

View File

@ -1,5 +0,0 @@
[
{
"title": "String0"
}
]

View File

@ -0,0 +1,360 @@
import bodyParser from "body-parser";
import crypto from "crypto";
import Razorpay from "razorpay";
import { Order } from "./orderModel.js";
import { shippingAddress } from "../ShippingAddresses/ShippingAddressModel.js";
import sendEmail from "../../Utils/sendEmail.js";
const instance = new Razorpay({
key_id: process.env.RAZERPAY_KEY_ID,
key_secret: process.env.RAZERPAY_SECRET_KEY,
});
const generateUniqueOrderId = async () => {
const currentYear = new Date().getFullYear();
// Find the latest order to get the last serial number
const latestOrder = await Order.findOne({}, {}, { sort: { orderID: -1 } });
let serialNumber = 1;
if (latestOrder) {
const lastYear = parseInt(latestOrder.orderID.substring(0, 4), 10);
if (lastYear === currentYear) {
// If the last order was in the current year, increment the serial number
serialNumber = parseInt(latestOrder.orderID.substring(4), 10) + 1;
}
}
// Pad the serial number with zeros and concatenate with the current year
const paddedSerialNumber = serialNumber.toString().padStart(7, "0");
const orderId = `${currentYear}${paddedSerialNumber}`;
return orderId;
};
export const getRzpkey = async (req, res) => {
const { name, email } = req.user;
res.status(200).json({
success: true,
key: process.env.RAZERPAY_KEY_ID,
name,
email,
});
};
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" });
const { address, cart, subtotal } = req.body;
if (cart.length < 1)
return res.status(400).json({ message: "cart is empty!" });
switch (true) {
//validation
case !address: {
return res.status(404).json({ msg: "please provide shipping address" });
}
case !subtotal: {
return res.status(404).json({ msg: "please provide product subtotal" });
}
}
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,
addressId: address,
};
const orderItems = await cart.map((item) => ({
product: item.product._id,
name: item.product.name,
price: item.product.total_amount,
image: item.product.image,
quantity: item.quantity,
product_Subtotal: item.subtotal,
}));
// console.log("line", lineItems[0]);
const Id = await generateUniqueOrderId();
const orders = await Order.create({
orderID: Id,
total_amount: subtotal,
orderItems,
shippingInfo: shipping,
user: req.user._id,
razorpay_order_id: order?.id,
});
} else {
res.status(400).json({
success: false,
message: "Failled to order Create",
});
}
res.status(200).json({
success: true,
order,
});
};
export const paymentVerification = async (req, res) => {
const { razorpay_order_id, razorpay_payment_id, razorpay_signature } =
req.body;
const body = razorpay_order_id + "|" + razorpay_payment_id;
const expectedSignature = crypto
.createHmac("sha256", process.env.RAZERPAY_SECRET_KEY)
.update(body.toString())
.digest("hex");
const isAuthentic = expectedSignature === razorpay_signature;
if (isAuthentic) {
// Database comes here
let findSameOrder = await Order.findOne({
razorpay_order_id: razorpay_order_id,
}).populate({
path: "user",
select: "name email -_id",
});
console.log("findSameOrder", findSameOrder);
if (findSameOrder) {
(findSameOrder.razorpay_payment_id = razorpay_payment_id), // await Payment.create({
(findSameOrder.isPaid = true),
(findSameOrder.paidAt = Date.now()),
(findSameOrder.razorpay_signature = razorpay_signature);
// await Payment.create({
findSameOrder.payment_status = "success";
findSameOrder.orderStatus = "new";
await findSameOrder.save();
}
//send email to customer
await sendEmail({
to: `${findSameOrder?.user?.email}`, // Change to your recipient
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
subject: `Your Order #${findSameOrder?.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 ${findSameOrder?.shippingInfo?.first_Name},</strong>
<p style="color: #555; font-size: 15px;">Great news! Your order #${findSameOrder?.orderID} has been confirmed. Here are the details</p>
<br/>
<span style="color: #555; font-size: 13px;">Best regards,</span><br/>
<span style="color: #555; font-size: 13px;">Team Smellika</span>`,
});
// console.log("findSameOrder", findSameOrder);
// // findSameOrder.razorpay_payment_id=razorpay_payment_id,// await Payment.create({
// findOrder.paidAt = new Date(event.data.object.created * 1000);
// findOrder.isPaid = true;
// razorpay_signature: { type: String },
// razorpay_order_id,
// razorpay_payment_id,
// razorpay_signature,
// });
res.redirect(`http://localhost:5173/account`);
// res.redirect(
// `http://localhost:5173/cart/paymentsuccess?reference=${razorpay_payment_id}`
// );
} else {
res.status(400).json({
success: false,
});
}
};
export const handlePayment = async (req, res) => {
try {
const { email } = req.user;
if (!email)
return res.status(400).send({ message: "Please enter the email" });
const { address, cart, subtotal } = req.body;
if (cart.length < 1)
return res.status(400).json({ message: "cart is empty!" });
switch (true) {
//validation
case !address: {
return res.status(404).json({ msg: "please provide shipping address" });
}
case !subtotal: {
return res.status(404).json({ msg: "please provide product subtotal" });
}
}
let addss = await shippingAddress.findById(address);
// console.log(addss?.postalCode);
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,
addressId: address,
};
const orderItems = await cart.map((item) => ({
product: item.product._id,
name: item.product.name,
price: item.product.total_amount,
image: item.product.image,
quantity: item.quantity,
product_Subtotal: item.subtotal,
}));
// console.log("line", lineItems[0]);
const Id = await generateUniqueOrderId();
const order = await Order.create({
orderID: Id,
total_amount: subtotal,
orderItems,
shippingInfo: shipping,
user: req.user._id,
});
console.log("fffffffff", order, "llllllllll");
const lineItems = await cart.map((item) => ({
price_data: {
currency: "inr",
product_data: {
name: item.product.name,
images: [item.product.image[0]?.url],
},
unit_amount: Number(item.product.total_amount) * 100,
},
quantity: Number(item.quantity),
}));
if (order) {
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items: lineItems,
mode: "payment",
customer_email: `${email}`,
metadata: {
orderId: order._id.toString(),
// Add any other key-value pairs as needed
},
success_url: `${process.env.FRONTEND_URL}/cart`,
cancel_url: `${process.env.FRONTEND_URL}/error`,
});
// res.json({ sessionId: session.id });
res.status(200).send({ message: "order created", url: session.url });
}
} catch (err) {
console.log(err);
res.status(500).send({ message: "Something went wrong", err });
}
};
export const webhook = async (req, res) => {
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
const signature = req.headers["stripe-signature"];
let event;
if (webhookSecret) {
try {
event = stripe.webhooks.constructEvent(
req.body,
signature,
webhookSecret
);
} catch (err) {
console.log(`❌ Error message: ${err.message}`);
res.status(400).send(`Webhook Error: ${err.message}`);
return;
}
}
if (event.type === "checkout.session.completed") {
// console.log("dddddddddddd", event.data);
const findOrder = await Order.findById(event.data.object.metadata?.orderId);
findOrder.paypal_payer_id = event.data.object.id;
findOrder.paidAt = new Date(event.data.object.created * 1000);
findOrder.isPaid = true;
if (event.data.object?.payment_status === "paid") {
findOrder.payment_status = "success";
} else {
findOrder.payment_status = "failed";
}
findOrder.orderStatus = "new";
await findOrder.save();
await sendEmail({
to: `${event.data.object.customer_email}`, // Change to your recipient
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
subject: `Your Order #${findOrder?.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 ${findOrder?.shippingInfo?.first_Name},</strong>
<p style="color: #555; font-size: 15px;">Great news! Your order #${findOrder?.orderID} has been confirmed. Here are the details</p>
<br/>
<span style="color: #555; font-size: 13px;">Best regards,</span><br/>
<span style="color: #555; font-size: 13px;">Team Smellika</span>`,
});
// Items: [List of Purchased Items]
// Total Amount: [Total Amount]
// Shipping Address: [Shipping Address]
// We'll keep you updated on the shipping progress. Thanks for choosing Smellika!
// Best regards
// Team Smellika
console.log(
"event.data.object",
event.data.object,
"---------------------"
);
console.log(`💰 Payment status: ${event.data.object?.payment_status}`);
// Saving the payment details in the database
// const payment = await Payment.create({
// customer_email: event.data.object.customer_email,
// amount: event.data.object.amount_total / 100,
// paymentId: event.data.object.id,
// paymentStatus: event.data.object.payment_status,
// createdAt: event.data.object.created,
// });
}
// if (event.type === "checkout.session.completed") {
// console.log("dddddddddddd", event.data);
// console.log("event.data.object", event.data.object);
// console.log(`💰 Payment status: ${event.data.object?.payment_status}`);
// payment_intent.payment_failed;
// // Saving the payment details in the database
// // const payment = await Payment.create({
// // customer_email: event.data.object.customer_email,
// // amount: event.data.object.amount_total / 100,
// // paymentId: event.data.object.id,
// // paymentStatus: event.data.object.payment_status,
// // createdAt: event.data.object.created,
// // });
// }
// Return a 200 res to acknowledge receipt of the event
res.status(200).end();
// res.send().end();
};

View File

@ -121,7 +121,10 @@ const orderSchema = new mongoose.Schema(
},
// paypal_payer_id: { type: String },
stripe_payment_id: { type: String },
// stripe_payment_id: { type: String },
razorpay_payment_id: { type: String },
razorpay_order_id: { type: String },
razorpay_signature: { type: String },
// paypal_signature: { type: String },
// order_used: { type: Boolean, default: false },
isDelivered: { type: Boolean, required: true, default: false },

View File

@ -19,6 +19,11 @@ const app = express();
// Configure bodyParser to parse the raw request body as a buffer
app.use(bodyParser.raw({ type: "application/json" }));
import { handlePayment, webhook } from "./StripeCheckOutController.js";
import {
checkout,
getRzpkey,
paymentVerification,
} from "./RazerPayCheckoutController.js";
const router = express.Router();
//checkout Routes-------------------------//
router.route("/checkout/").post(isAuthenticatedUser, createOrderCheckout);
@ -48,6 +53,11 @@ router
.route("/delete/:id")
.delete(isAuthenticatedUser, authorizeRoles("admin"), deleteOneOrder);
//RAZERPAY checkout
router.route("/getRzpKey/").get(isAuthenticatedUser, getRzpkey);
router.route("/Rzpcheckout/").post(isAuthenticatedUser, checkout);
router.route("/paymentverification").post(paymentVerification);
// router.route("/product/getAll/").get(getAllProduct)
export default router;

View File

@ -75,6 +75,48 @@ export const getAllProduct = async (req, res) => {
});
}
};
// get all product with device products first
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");
}
// products with device category
const deviceProducts = await Product.find({ category: category._id }).populate('category');
// all products
const allProducts = await Product.find()
.populate({
path: "category gst addedBy",
select: "name categoryName tax",
})
.sort({
createdAt: -1,
});
// filtering out products with device category
const filteredProducts = allProducts.filter((ele) => { return ele.category?.categoryName !== categoryName })
// merging both deviceProcuts and filtered products
const product = deviceProducts.concat(filteredProducts)
if (product) {
return res.status(200).json({
success: true,
product,
});
}
} 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 {

View File

@ -7,6 +7,7 @@ import {
getOneProduct,
deleteImageFromCloudinary,
getProductsByCategory,
getAllProductsDevicesFirst,
} from "./ProductController.js";
const router = express.Router();
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
@ -14,6 +15,7 @@ router
.route("/product/create/")
.post(isAuthenticatedUser, authorizeRoles("admin"), createProduct);
router.route("/product/getAll/").get(getAllProduct);
router.route("/product/getAllProductsDevicesFrist/").get(getAllProductsDevicesFirst);
router.route("/product/getOne/:id").get(getOneProduct);
router
.route("/product/update/:id")