blog,salescoordinator,setting and product managment fixed
This commit is contained in:
parent
efea635c3f
commit
0074536dc3
@ -16,7 +16,6 @@ const sendEmail = async (options) => {
|
|||||||
if (error) {
|
if (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
export default sendEmail;
|
export default sendEmail;
|
||||||
@ -31,31 +30,50 @@ export default sendEmail;
|
|||||||
// export default sendEmail
|
// export default sendEmail
|
||||||
|
|
||||||
// from message bird SMS send------------------------------------
|
// from message bird SMS send------------------------------------
|
||||||
|
// import { initClient } from "messagebird";
|
||||||
|
// const messagebird = initClient("p2YaqxU9uYx2F3d3dV8ywAFtk");
|
||||||
|
// // e7HGr3kMl6su4c79DKjNAwlLQ
|
||||||
|
// //7oOgyzfNuwBnqMc2oK6aGfczs
|
||||||
|
// //11yKY8EbdFJpugJzaKyAH3YaK
|
||||||
import { initClient } from "messagebird";
|
import { initClient } from "messagebird";
|
||||||
|
|
||||||
const messagebird = initClient("p2YaqxU9uYx2F3d3dV8ywAFtk");
|
const messagebird = initClient("p2YaqxU9uYx2F3d3dV8ywAFtk");
|
||||||
// e7HGr3kMl6su4c79DKjNAwlLQ
|
|
||||||
//7oOgyzfNuwBnqMc2oK6aGfczs
|
|
||||||
//11yKY8EbdFJpugJzaKyAH3YaK
|
|
||||||
|
|
||||||
|
|
||||||
export const sendOtp = async (recipient, message) => {
|
export const sendOtp = async (recipient, message) => {
|
||||||
if (!recipient || !message) {
|
if (!recipient || !message) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
originator: "+447418314922",
|
originator: "+447418314922",
|
||||||
recipients: [recipient],
|
recipients: [recipient],
|
||||||
body: message,
|
body: message,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
messagebird.messages.create(params, (err, response) => {
|
messagebird.messages.create(params, (err, response) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error("Error sending message-------:", err);
|
console.error("Error sending message:", err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// console.log("Message sent successfully:", response);
|
// console.log("Message sent successfully:", response);
|
||||||
// console.log("Message details:", response, response?.recipients?.items);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
// export const sendOtp = async (recipient, message) => {
|
||||||
|
// if (!recipient || !message) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// console.log(recipient, message);
|
||||||
|
// const params = {
|
||||||
|
// originator: "+447418314922",
|
||||||
|
// recipients: [recipient],
|
||||||
|
// body: message,
|
||||||
|
// };
|
||||||
|
|
||||||
|
// messagebird.messages.create(params, (err, response) => {
|
||||||
|
// if (err) {
|
||||||
|
// console.error("Error sending message-------:", err);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// // console.log("Message sent successfully:", response);
|
||||||
|
// // console.log("Message details:", response, response?.recipients?.items);
|
||||||
|
// });
|
||||||
|
// };
|
||||||
|
20
app.js
20
app.js
@ -130,19 +130,17 @@ import orderRoute from "./resources/Orders/orderRoute.js";
|
|||||||
import DepartureRouter from "./resources/Departure/DepartureRoute.js";
|
import DepartureRouter from "./resources/Departure/DepartureRoute.js";
|
||||||
import InformationRoute from "./resources/Informations/InformationRoute.js";
|
import InformationRoute from "./resources/Informations/InformationRoute.js";
|
||||||
import Testimonial from "./resources/Testimonials/TestimonialRoute.js";
|
import Testimonial from "./resources/Testimonials/TestimonialRoute.js";
|
||||||
import ContactRequest from "./resources/ContactRequests/ContactRequestRoute.js";
|
|
||||||
|
|
||||||
import StateRouter from "./resources/setting/state/state_routes.js";
|
import StateRouter from "./resources/setting/state/state_routes.js";
|
||||||
//
|
//
|
||||||
import LanguageRoute from "./resources/setting/Language/language_routes.js";
|
import LanguageRoute from "./resources/setting/Language/language_routes.js";
|
||||||
//purpose
|
//purpose
|
||||||
import PurposeRoute from "./resources/setting/Purpose/Purpose_routes.js";
|
import PurposeRoute from "./resources/setting/Purpose/Purpose_routes.js";
|
||||||
//Patient Routes
|
//SalesCoOrdinator Routes
|
||||||
import PatientRoute from './resources/Patients/PatientRoute.js'
|
import SalesCoOrdinatorRoute from "./resources/SalesCoOrdinators/SalesCoOrdinatorRoute.js";
|
||||||
|
|
||||||
// category Route
|
// category Route
|
||||||
import categoryRoute from "./resources/Category/categoryRoutes.js";
|
import categoryRoute from "./resources/Category/categoryRoutes.js";
|
||||||
import bannerRoute from "./resources/Banner/BannerRouter.js";
|
|
||||||
import RegistrationImageRoute from "./resources/RegistrationImage/RegistrationImageRoute.js";
|
import RegistrationImageRoute from "./resources/RegistrationImage/RegistrationImageRoute.js";
|
||||||
import loginImageRoute from "./resources/LoginImage/LoginImageRoute.js";
|
import loginImageRoute from "./resources/LoginImage/LoginImageRoute.js";
|
||||||
import shopImageRoute from "./resources/ShopPageImage/ShopPageImageRoute.js";
|
import shopImageRoute from "./resources/ShopPageImage/ShopPageImageRoute.js";
|
||||||
@ -166,14 +164,11 @@ import SeoRoute from "./resources/SEO&Analytics/SEORouter.js";
|
|||||||
import AffiliateRoute from "./resources/Affiliate&Coupon/Affiliate/AffiliateRoute.js";
|
import AffiliateRoute from "./resources/Affiliate&Coupon/Affiliate/AffiliateRoute.js";
|
||||||
//Blog Routes
|
//Blog Routes
|
||||||
import BlogRoute from "./resources/Blog/BlogRoute.js";
|
import BlogRoute from "./resources/Blog/BlogRoute.js";
|
||||||
// Panel Routes
|
|
||||||
import PanelRoute from "./resources/Panels/PanelRoutes.js";
|
|
||||||
//Coupon Routes
|
//Coupon Routes
|
||||||
import CouponRoute from "./resources/Affiliate&Coupon/Coupon/CouponRoute.js";
|
import CouponRoute from "./resources/Affiliate&Coupon/Coupon/CouponRoute.js";
|
||||||
//short urls
|
//short urls
|
||||||
// import ShortUrlRouter from "./resources/Businesses/Short_Urls/ShortUrlRoute.js";
|
// import ShortUrlRouter from "./resources/Businesses/Short_Urls/ShortUrlRoute.js";
|
||||||
//support Ticket
|
//support Ticket
|
||||||
import SupportRouter from "./resources/Supports/supportRoute.js";
|
|
||||||
// Point of Sale
|
// Point of Sale
|
||||||
app.use("/api/v1/", user);
|
app.use("/api/v1/", user);
|
||||||
|
|
||||||
@ -185,7 +180,6 @@ app.use("/api", ProductRouter);
|
|||||||
|
|
||||||
// Category
|
// Category
|
||||||
app.use("/api/category", categoryRoute);
|
app.use("/api/category", categoryRoute);
|
||||||
app.use("/api/banner", bannerRoute);
|
|
||||||
// registration image
|
// registration image
|
||||||
app.use("/api/registerImage", RegistrationImageRoute);
|
app.use("/api/registerImage", RegistrationImageRoute);
|
||||||
app.use("/api/loginImage", loginImageRoute);
|
app.use("/api/loginImage", loginImageRoute);
|
||||||
@ -195,9 +189,8 @@ app.use("/api/content", ContentRoute);
|
|||||||
// User Address
|
// User Address
|
||||||
app.use("/api/user-address", UserAddressRoute);
|
app.use("/api/user-address", UserAddressRoute);
|
||||||
app.use("/api/shipping/address", ShippingAddressRoute);
|
app.use("/api/shipping/address", ShippingAddressRoute);
|
||||||
//Patient Routes
|
//SalesCoOrdinator Routes
|
||||||
app.use("/api/patient", PatientRoute);
|
app.use("/api/salescoordinator", SalesCoOrdinatorRoute);
|
||||||
|
|
||||||
|
|
||||||
//Order
|
//Order
|
||||||
app.use("/api/order", orderRoute);
|
app.use("/api/order", orderRoute);
|
||||||
@ -205,8 +198,6 @@ app.use("/api/order", orderRoute);
|
|||||||
app.use("/api/departure/", DepartureRouter);
|
app.use("/api/departure/", DepartureRouter);
|
||||||
//Information
|
//Information
|
||||||
app.use("/api/information/", InformationRoute);
|
app.use("/api/information/", InformationRoute);
|
||||||
//Contact Requests
|
|
||||||
app.use("/api/contact/request/", ContactRequest);
|
|
||||||
//Complaints
|
//Complaints
|
||||||
app.use("/api/testimonial/", Testimonial);
|
app.use("/api/testimonial/", Testimonial);
|
||||||
//state
|
//state
|
||||||
@ -234,8 +225,6 @@ app.use("/api/v1/affiliate", AffiliateRoute);
|
|||||||
app.use("/api/v1/coupon", CouponRoute);
|
app.use("/api/v1/coupon", CouponRoute);
|
||||||
//Blog
|
//Blog
|
||||||
app.use("/api/v1/blog", BlogRoute);
|
app.use("/api/v1/blog", BlogRoute);
|
||||||
// panels
|
|
||||||
app.use("/api/panel", PanelRoute);
|
|
||||||
|
|
||||||
//config specialty
|
//config specialty
|
||||||
// app.use("/api/config/specialty", SpecialtiesRouter);
|
// app.use("/api/config/specialty", SpecialtiesRouter);
|
||||||
@ -249,5 +238,4 @@ app.use("/api/panel", PanelRoute);
|
|||||||
|
|
||||||
// Email CMS
|
// Email CMS
|
||||||
// app.use("/api", RegisterEmail);
|
// app.use("/api", RegisterEmail);
|
||||||
app.use("/api", SupportRouter);
|
|
||||||
export default app;
|
export default app;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
//require("dotenv").config();
|
//require("dotenv").config();
|
||||||
|
mongoose.set('strictQuery', true);
|
||||||
const connectDatabase = () => {
|
const connectDatabase = () => {
|
||||||
mongoose
|
mongoose
|
||||||
.connect(process.env.DB_URL, {
|
.connect(process.env.DB_URL, {
|
||||||
|
@ -1,50 +0,0 @@
|
|||||||
import jwt from "jsonwebtoken";
|
|
||||||
import Patient from "../resources/Patients/PatientModel.js";
|
|
||||||
|
|
||||||
export const isAuthenticatedPatient = async (req, res, next) => {
|
|
||||||
try {
|
|
||||||
if (!req.headers.authorization) {
|
|
||||||
return res.status(401).json({
|
|
||||||
success: false,
|
|
||||||
message: "Please Login to access this resource",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const getToken = req.headers;
|
|
||||||
//remove Bearer from token
|
|
||||||
const fronttoken = getToken.authorization.slice(7);
|
|
||||||
|
|
||||||
const frontdecoded = jwt.verify(fronttoken, process.env.JWT_SECRET);
|
|
||||||
if (!frontdecoded) {
|
|
||||||
return res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
message: "incorrect token",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const fpatient = await Patient.findById(frontdecoded.id);
|
|
||||||
if (fpatient) {
|
|
||||||
req.patient = fpatient;
|
|
||||||
return next();
|
|
||||||
} else {
|
|
||||||
return res.status(401).json({
|
|
||||||
success: false,
|
|
||||||
message: "Unauthorized",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
if (error.name === 'TokenExpiredError') {
|
|
||||||
return res.status(401).json({ message: 'Token has expired.' });
|
|
||||||
} else if (error.name === 'JsonWebTokenError') {
|
|
||||||
if (error.message === 'invalid signature') {
|
|
||||||
return res.status(401).json({ message: 'Invalid token!.' });
|
|
||||||
} else {
|
|
||||||
return res.status(401).json({ message: 'Invalid token.' });
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return res.status(500).json({ message: 'An internal error occurred while verifying the token.' });
|
|
||||||
}
|
|
||||||
// return res.status(401).json({
|
|
||||||
// success: false,
|
|
||||||
// message: error.message,
|
|
||||||
// });
|
|
||||||
}
|
|
||||||
};
|
|
52
middlewares/SalesCoOrdinatorAuth.js
Normal file
52
middlewares/SalesCoOrdinatorAuth.js
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
import SalesCoOrdinator from "../resources/SalesCoOrdinators/SalesCoOrdinatorModel.js";
|
||||||
|
|
||||||
|
export const isAuthenticatedSalesCoOrdinator = async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
if (!req.headers.authorization) {
|
||||||
|
return res.status(401).json({
|
||||||
|
success: false,
|
||||||
|
message: "Please Login to access this resource",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const getToken = req.headers;
|
||||||
|
//remove Bearer from token
|
||||||
|
const fronttoken = getToken.authorization.slice(7);
|
||||||
|
|
||||||
|
const frontdecoded = jwt.verify(fronttoken, process.env.JWT_SECRET);
|
||||||
|
if (!frontdecoded) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: "incorrect token",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const salescoordinator = await SalesCoOrdinator.findById(frontdecoded.id);
|
||||||
|
if (salescoordinator) {
|
||||||
|
req.salescoordinator = salescoordinator;
|
||||||
|
return next();
|
||||||
|
} else {
|
||||||
|
return res.status(401).json({
|
||||||
|
success: false,
|
||||||
|
message: "Unauthorized",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error.name === "TokenExpiredError") {
|
||||||
|
return res.status(401).json({ message: "Token has expired." });
|
||||||
|
} else if (error.name === "JsonWebTokenError") {
|
||||||
|
if (error.message === "invalid signature") {
|
||||||
|
return res.status(401).json({ message: "Invalid token!." });
|
||||||
|
} else {
|
||||||
|
return res.status(401).json({ message: "Invalid token." });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return res.status(500).json({
|
||||||
|
message: "An internal error occurred while verifying the token.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// return res.status(401).json({
|
||||||
|
// success: false,
|
||||||
|
// message: error.message,
|
||||||
|
// });
|
||||||
|
}
|
||||||
|
};
|
7400
package-lock.json
generated
7400
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,151 +0,0 @@
|
|||||||
import mongoose from "mongoose";
|
|
||||||
|
|
||||||
import cloudinary from "../../Utils/cloudinary.js";
|
|
||||||
import { BannerModel } from "./BannerModel.js";
|
|
||||||
|
|
||||||
// Add new Category
|
|
||||||
export const addBanner = async (req, res) => {
|
|
||||||
const { bannerName } = req.body;
|
|
||||||
const { bannerImage } = req.files;
|
|
||||||
// console.log(categoryName, categoryImage);
|
|
||||||
|
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
try {
|
|
||||||
if (!mongoose.Types.ObjectId.isValid(req.user._id)) {
|
|
||||||
return res.status(400).json({ message: "please login again " });
|
|
||||||
}
|
|
||||||
const result = await cloudinary.v2.uploader.upload(
|
|
||||||
bannerImage.tempFilePath,
|
|
||||||
{
|
|
||||||
folder: "GetSygnal/banner",
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (result) {
|
|
||||||
const banner = await BannerModel.create({
|
|
||||||
bannerName,
|
|
||||||
bannerImage: result,
|
|
||||||
addedBy: req.user._id,
|
|
||||||
});
|
|
||||||
if (banner) {
|
|
||||||
return res
|
|
||||||
.status(201)
|
|
||||||
.json({ success: true, banner, message: "banner Added" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getBanner = async (req, res) => {
|
|
||||||
try {
|
|
||||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
const banners = await BannerModel.find().sort({
|
|
||||||
createdAt: -1,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!banners) {
|
|
||||||
return res.status(404).json({ message: "No categories found" });
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200).json({ success: true, banners });
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const updateBanner = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
const { _id } = req.params;
|
|
||||||
const { bannerName } = req.body;
|
|
||||||
const olderImage = req.body?.olderImage;
|
|
||||||
const bannerImag = req.files?.bannerImage;
|
|
||||||
|
|
||||||
if (!mongoose.Types.ObjectId.isValid(_id)) {
|
|
||||||
return res.status(404).json({ error: "Can not find the document " });
|
|
||||||
}
|
|
||||||
|
|
||||||
// find the document with the id to delete the image from cloudinary
|
|
||||||
if (JSON.parse(olderImage).length == 0) {
|
|
||||||
const deletefromCloudinary = await BannerModel.findOne({ _id: _id });
|
|
||||||
|
|
||||||
const deleteresponse = await cloudinary.v2.uploader.destroy(
|
|
||||||
deletefromCloudinary.bannerImage.public_id
|
|
||||||
);
|
|
||||||
if (deleteresponse) {
|
|
||||||
const result = await cloudinary.v2.uploader.upload(
|
|
||||||
bannerImag.tempFilePath,
|
|
||||||
{
|
|
||||||
folder: "GetSygnal/banner",
|
|
||||||
}
|
|
||||||
);
|
|
||||||
const update = await BannerModel.findOneAndUpdate(
|
|
||||||
{ _id: _id },
|
|
||||||
{ bannerName: bannerName, bannerImage: result }, // Provide the updated categoryName
|
|
||||||
{ new: true } // To return the updated document
|
|
||||||
);
|
|
||||||
if (!update) {
|
|
||||||
return res
|
|
||||||
.status(404)
|
|
||||||
.json({ message: "Can not update document, something went wrong" });
|
|
||||||
} else {
|
|
||||||
return res.status(200).json({ success: true, update });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const update = await BannerModel.findOneAndUpdate(
|
|
||||||
{ _id: _id },
|
|
||||||
{ bannerName: bannerName, bannerImage: JSON.parse(olderImage) }, // Provide the updated categoryName
|
|
||||||
{ new: true } // To return the updated document
|
|
||||||
);
|
|
||||||
if (update) {
|
|
||||||
return res.status(200).json({ success: true, update });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const deleteBanner = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
const { _id } = req.params;
|
|
||||||
if (!mongoose.Types.ObjectId.isValid(_id)) {
|
|
||||||
return res.status(404).json({ error: "Can not find the document " });
|
|
||||||
}
|
|
||||||
|
|
||||||
const deletefromCloudinary = await BannerModel.findOne({ _id: _id });
|
|
||||||
|
|
||||||
const deleteresponse = await cloudinary.v2.uploader.destroy(
|
|
||||||
deletefromCloudinary.bannerImage.public_id
|
|
||||||
);
|
|
||||||
if (deleteresponse) {
|
|
||||||
const deleteBanner = await BannerModel.findOneAndDelete({ _id: _id });
|
|
||||||
if (!deleteBanner) {
|
|
||||||
return res.status(404).json({
|
|
||||||
error: "Can not find the document with the provided id to delete ",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
res.status(200).json({ success: true, deleteBanner });
|
|
||||||
} else {
|
|
||||||
return res.status(404).json({ error: "can not delete the banner " });
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,19 +0,0 @@
|
|||||||
import mongoose from "mongoose";
|
|
||||||
|
|
||||||
const BannerSchema = new mongoose.Schema(
|
|
||||||
{
|
|
||||||
bannerName: {
|
|
||||||
type: String,
|
|
||||||
required: [true, "Name of Banner required "],
|
|
||||||
},
|
|
||||||
bannerImage: {},
|
|
||||||
addedBy: {
|
|
||||||
type: mongoose.Schema.ObjectId,
|
|
||||||
ref: "User",
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ timestamps: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
export const BannerModel = mongoose.model("BannerModel", BannerSchema);
|
|
@ -1,31 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
|
||||||
|
|
||||||
import {
|
|
||||||
addBanner,
|
|
||||||
deleteBanner,
|
|
||||||
getBanner,
|
|
||||||
updateBanner,
|
|
||||||
} from "./BannerController.js";
|
|
||||||
const router = express.Router();
|
|
||||||
|
|
||||||
router
|
|
||||||
.route("/add")
|
|
||||||
.post(isAuthenticatedUser, authorizeRoles("admin", "Employee"), addBanner);
|
|
||||||
router.route("/getBanners").get(getBanner);
|
|
||||||
router
|
|
||||||
.route("/update/:_id")
|
|
||||||
.patch(
|
|
||||||
isAuthenticatedUser,
|
|
||||||
authorizeRoles("admin", "Employee"),
|
|
||||||
updateBanner
|
|
||||||
);
|
|
||||||
router
|
|
||||||
.route("/delete/:_id")
|
|
||||||
.delete(
|
|
||||||
isAuthenticatedUser,
|
|
||||||
authorizeRoles("admin", "Employee"),
|
|
||||||
deleteBanner
|
|
||||||
);
|
|
||||||
|
|
||||||
export default router;
|
|
@ -12,7 +12,7 @@ export const createBlog = async (req, res) => {
|
|||||||
const result = await cloudinary.v2.uploader.upload(
|
const result = await cloudinary.v2.uploader.upload(
|
||||||
imageFile.tempFilePath,
|
imageFile.tempFilePath,
|
||||||
{
|
{
|
||||||
folder: "smellica/Blog",
|
folder: "chemiNova/Blog",
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ export const getOneBlog = async (req, res) => {
|
|||||||
|
|
||||||
export const deleteImageFromCloudinary = async (req, res) => {
|
export const deleteImageFromCloudinary = async (req, res) => {
|
||||||
const { public_id } = req.params;
|
const { public_id } = req.params;
|
||||||
|
// console.log(public_id);
|
||||||
try {
|
try {
|
||||||
if (!public_id) {
|
if (!public_id) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
@ -99,7 +99,11 @@ export const deleteImageFromCloudinary = async (req, res) => {
|
|||||||
msg: "Please Provide Product ID!",
|
msg: "Please Provide Product ID!",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const response = await cloudinary.v2.uploader.destroy(public_id);
|
// Prepend the folder path to the publicId
|
||||||
|
const folderPath = "chemiNova/Blog/";
|
||||||
|
const fullPublicId = `${folderPath}${public_id}`;
|
||||||
|
// console.log(fullPublicId); // Log the full publicId for debugging
|
||||||
|
const response = await cloudinary.v2.uploader.destroy(fullPublicId);
|
||||||
if (response) {
|
if (response) {
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
@ -113,7 +117,7 @@ export const deleteImageFromCloudinary = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
//delete one Product
|
//delete one Blog
|
||||||
export const deleteBlog = async (req, res) => {
|
export const deleteBlog = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||||
@ -171,7 +175,7 @@ export const updateBlog = async (req, res) => {
|
|||||||
const result = await cloudinary.v2.uploader.upload(
|
const result = await cloudinary.v2.uploader.upload(
|
||||||
uploadedImage.tempFilePath,
|
uploadedImage.tempFilePath,
|
||||||
{
|
{
|
||||||
folder: "smellica/Blog",
|
folder: "chemiNova/Blog",
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
|
||||||
import {
|
|
||||||
AddNewContactRequest,
|
|
||||||
FindAllContactRequest,
|
|
||||||
} from "./ContactRequestsController.js";
|
|
||||||
|
|
||||||
const router = express.Router();
|
|
||||||
|
|
||||||
router.route("/new").post(AddNewContactRequest);
|
|
||||||
router.route("/getAll").get(FindAllContactRequest);
|
|
||||||
|
|
||||||
// router.route("/product/getAll/").get(getAllProduct)
|
|
||||||
|
|
||||||
export default router;
|
|
@ -1,46 +0,0 @@
|
|||||||
import { ContactRequest } from "./ContactRequestsModel.js";
|
|
||||||
export const AddNewContactRequest = async (req, res) => {
|
|
||||||
try {
|
|
||||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
|
|
||||||
const contactRequest = await ContactRequest.create(req.body);
|
|
||||||
|
|
||||||
res.status(201).json({
|
|
||||||
success: true,
|
|
||||||
contactRequest,
|
|
||||||
message: "ContactRequest Added",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const FindAllContactRequest = async (req, res) => {
|
|
||||||
try {
|
|
||||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
// console.log(req?.user)
|
|
||||||
|
|
||||||
const contactRequest = await ContactRequest.find().sort({ createdAt: -1 });
|
|
||||||
if (contactRequest) {
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
contactRequest,
|
|
||||||
message: "Fetched All ContactRequest",
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return res.status(404).json({
|
|
||||||
success: true,
|
|
||||||
|
|
||||||
message: "No ContactRequest till Now",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,29 +0,0 @@
|
|||||||
|
|
||||||
import mongoose from "mongoose";
|
|
||||||
|
|
||||||
const { Schema, model } = mongoose;
|
|
||||||
|
|
||||||
const ContactRequestSchema = new mongoose.Schema(
|
|
||||||
{
|
|
||||||
|
|
||||||
name: {
|
|
||||||
type: String,
|
|
||||||
maxLength: [25, "name cannot exceed 25 characters"],
|
|
||||||
required: [true, "Please Enter name "],
|
|
||||||
},
|
|
||||||
EmailOrMobile: {
|
|
||||||
type: String,
|
|
||||||
|
|
||||||
required: [true, "Please Enter title "],
|
|
||||||
},
|
|
||||||
message: {
|
|
||||||
type: String,
|
|
||||||
maxLength: [500, "message cannot exceed 500 characters"],
|
|
||||||
required: [true, "Please Enter message"],
|
|
||||||
},
|
|
||||||
|
|
||||||
},
|
|
||||||
{ timestamps: true, versionKey: false }
|
|
||||||
);
|
|
||||||
|
|
||||||
export const ContactRequest = mongoose.model("ContactRequest", ContactRequestSchema);
|
|
@ -56,7 +56,7 @@ export const updateTermsAndConditions = async (req, res) => {
|
|||||||
const { content } = req.body;
|
const { content } = req.body;
|
||||||
// id of the terms and conndition document
|
// id of the terms and conndition document
|
||||||
const id = req.query.id;
|
const id = req.query.id;
|
||||||
|
// console.log(id);
|
||||||
// object for updated terms and conndition data
|
// object for updated terms and conndition data
|
||||||
const updatedTermsData = {
|
const updatedTermsData = {
|
||||||
termsAndContionContent: content,
|
termsAndContionContent: content,
|
||||||
|
@ -1,38 +0,0 @@
|
|||||||
import mongoose from "mongoose";
|
|
||||||
const { Schema, model } = mongoose;
|
|
||||||
|
|
||||||
const panel1Schema = new Schema(
|
|
||||||
{
|
|
||||||
title: {
|
|
||||||
type: String,
|
|
||||||
default: ''
|
|
||||||
},
|
|
||||||
content: {
|
|
||||||
type: String,
|
|
||||||
default: ''
|
|
||||||
},
|
|
||||||
image: {
|
|
||||||
public_id: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
url: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
addedBy: {
|
|
||||||
type: mongoose.Schema.ObjectId,
|
|
||||||
ref: "User",
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
displayPanel: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{ timestamps: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
export const Panel1 = model(
|
|
||||||
"Panel1",
|
|
||||||
panel1Schema
|
|
||||||
);
|
|
@ -1,38 +0,0 @@
|
|||||||
import mongoose from "mongoose";
|
|
||||||
const { Schema, model } = mongoose;
|
|
||||||
|
|
||||||
const panel2Schema = new Schema(
|
|
||||||
{
|
|
||||||
title: {
|
|
||||||
type: String,
|
|
||||||
default: ''
|
|
||||||
},
|
|
||||||
content: {
|
|
||||||
type: String,
|
|
||||||
default: ''
|
|
||||||
},
|
|
||||||
image: {
|
|
||||||
public_id: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
url: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
addedBy: {
|
|
||||||
type: mongoose.Schema.ObjectId,
|
|
||||||
ref: "User",
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
displayPanel: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{ timestamps: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
export const Panel2 = model(
|
|
||||||
"Panel2",
|
|
||||||
panel2Schema
|
|
||||||
);
|
|
@ -1,38 +0,0 @@
|
|||||||
import mongoose from "mongoose";
|
|
||||||
const { Schema, model } = mongoose;
|
|
||||||
|
|
||||||
const panel3Schema = new Schema(
|
|
||||||
{
|
|
||||||
title: {
|
|
||||||
type: String,
|
|
||||||
default: ''
|
|
||||||
},
|
|
||||||
content: {
|
|
||||||
type: String,
|
|
||||||
default: ''
|
|
||||||
},
|
|
||||||
image: {
|
|
||||||
public_id: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
url: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
addedBy: {
|
|
||||||
type: mongoose.Schema.ObjectId,
|
|
||||||
ref: "User",
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
displayPanel: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{ timestamps: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
export const Panel3 = model(
|
|
||||||
"Panel3",
|
|
||||||
panel3Schema
|
|
||||||
);
|
|
@ -1,38 +0,0 @@
|
|||||||
import mongoose from "mongoose";
|
|
||||||
const { Schema, model } = mongoose;
|
|
||||||
|
|
||||||
const panel4Schema = new Schema(
|
|
||||||
{
|
|
||||||
title: {
|
|
||||||
type: String,
|
|
||||||
default: ''
|
|
||||||
},
|
|
||||||
content: {
|
|
||||||
type: String,
|
|
||||||
default: ''
|
|
||||||
},
|
|
||||||
image: {
|
|
||||||
public_id: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
url: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
addedBy: {
|
|
||||||
type: mongoose.Schema.ObjectId,
|
|
||||||
ref: "User",
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
displayPanel: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{ timestamps: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
export const Panel4 = model(
|
|
||||||
"Panel4",
|
|
||||||
panel4Schema
|
|
||||||
);
|
|
@ -1,547 +0,0 @@
|
|||||||
import cloudinary from "../../Utils/cloudinary.js";
|
|
||||||
import { Panel1 } from "./Panel1Model.js";
|
|
||||||
import { Panel2 } from "./Panel2Model.js";
|
|
||||||
import { Panel3 } from "./Panel3Model.js";
|
|
||||||
import { Panel4 } from "./Panel4Model.js";
|
|
||||||
|
|
||||||
|
|
||||||
export const AddPanel1 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
// console.log(req?.user)
|
|
||||||
|
|
||||||
req.body.user = req.user._id;
|
|
||||||
const { content,title,displayPanel} = req.body;
|
|
||||||
let image; // To store Cloudinary image details
|
|
||||||
|
|
||||||
if (req.files && req.files.image) {
|
|
||||||
const imageFile = req.files.image;
|
|
||||||
const result = await cloudinary.v2.uploader.upload(
|
|
||||||
imageFile.tempFilePath,
|
|
||||||
{
|
|
||||||
folder: "smellica/blog",
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
image = {
|
|
||||||
public_id: result.public_id,
|
|
||||||
url: result.secure_url,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the blog post
|
|
||||||
const panel1 = await Panel1.create({
|
|
||||||
title,
|
|
||||||
image,
|
|
||||||
content,
|
|
||||||
displayPanel,
|
|
||||||
addedBy: req.user._id,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
res.status(201).json({
|
|
||||||
success: true,
|
|
||||||
panel1,
|
|
||||||
message: "Added successfully",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error)
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getPanel1 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
// console.log(req?.user)
|
|
||||||
|
|
||||||
const panel1 = await Panel1.find();
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
panel1,
|
|
||||||
message: "Found successfully ",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const updatePanel1 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
|
|
||||||
// Check if the user is authenticated
|
|
||||||
if (!req.user) {
|
|
||||||
return res.status(400).json({ message: "Please login!" });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Destructure request body
|
|
||||||
const { title, content,displayPanel } = req.body;
|
|
||||||
|
|
||||||
// Prepare an object for the updated testimonial data
|
|
||||||
const updatedPanel1Data = {
|
|
||||||
title,
|
|
||||||
content,
|
|
||||||
displayPanel
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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
|
|
||||||
updatedPanel1Data.image = image;
|
|
||||||
}
|
|
||||||
const modifiedPanel = await Panel1.findOneAndUpdate(
|
|
||||||
{ _id: req.params.id },
|
|
||||||
{ $set: updatedPanel1Data },
|
|
||||||
{ new: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
ModifyBlog: modifiedPanel,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const AddPanel2 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
// console.log(req?.user)
|
|
||||||
|
|
||||||
req.body.user = req.user._id;
|
|
||||||
const { content,title,displayPanel} = req.body;
|
|
||||||
let image; // To store Cloudinary image details
|
|
||||||
|
|
||||||
if (req.files && req.files.image) {
|
|
||||||
const imageFile = req.files.image;
|
|
||||||
const result = await cloudinary.v2.uploader.upload(
|
|
||||||
imageFile.tempFilePath,
|
|
||||||
{
|
|
||||||
folder: "smellica/blog",
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
image = {
|
|
||||||
public_id: result.public_id,
|
|
||||||
url: result.secure_url,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the blog post
|
|
||||||
const panel2 = await Panel2.create({
|
|
||||||
title,
|
|
||||||
image,
|
|
||||||
content,
|
|
||||||
displayPanel,
|
|
||||||
addedBy: req.user._id,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
res.status(201).json({
|
|
||||||
success: true,
|
|
||||||
panel2,
|
|
||||||
message: "Added successfully",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error)
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getPanel2 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
// console.log(req?.user)
|
|
||||||
|
|
||||||
const panel2 = await Panel2.find();
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
panel2,
|
|
||||||
message: "Found successfully ",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const updatePanel2 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
|
|
||||||
// Check if the user is authenticated
|
|
||||||
if (!req.user) {
|
|
||||||
return res.status(400).json({ message: "Please login!" });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Destructure request body
|
|
||||||
const { title, content,displayPanel } = req.body;
|
|
||||||
|
|
||||||
// Prepare an object for the updated testimonial data
|
|
||||||
const updatedPanel2Data = {
|
|
||||||
title,
|
|
||||||
content,
|
|
||||||
displayPanel
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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
|
|
||||||
updatedPanel2Data.image = image;
|
|
||||||
}
|
|
||||||
const modifiedPanel = await Panel2.findOneAndUpdate(
|
|
||||||
{ _id: req.params.id },
|
|
||||||
{ $set: updatedPanel2Data },
|
|
||||||
{ new: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
ModifyBlog: modifiedPanel,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const AddPanel3 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
// console.log(req?.user)
|
|
||||||
|
|
||||||
req.body.user = req.user._id;
|
|
||||||
const { content,title,displayPanel} = req.body;
|
|
||||||
let image; // To store Cloudinary image details
|
|
||||||
|
|
||||||
if (req.files && req.files.image) {
|
|
||||||
const imageFile = req.files.image;
|
|
||||||
const result = await cloudinary.v2.uploader.upload(
|
|
||||||
imageFile.tempFilePath,
|
|
||||||
{
|
|
||||||
folder: "smellica/blog",
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
image = {
|
|
||||||
public_id: result.public_id,
|
|
||||||
url: result.secure_url,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the blog post
|
|
||||||
const panel3 = await Panel3.create({
|
|
||||||
title,
|
|
||||||
image,
|
|
||||||
content,
|
|
||||||
displayPanel,
|
|
||||||
addedBy: req.user._id,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
res.status(201).json({
|
|
||||||
success: true,
|
|
||||||
panel3,
|
|
||||||
message: "Added successfully",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error)
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getPanel3 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
// console.log(req?.user)
|
|
||||||
|
|
||||||
const panel3 = await Panel3.find();
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
panel3,
|
|
||||||
message: "Found successfully ",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const updatePanel3 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
|
|
||||||
// Check if the user is authenticated
|
|
||||||
if (!req.user) {
|
|
||||||
return res.status(400).json({ message: "Please login!" });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Destructure request body
|
|
||||||
const { title, content,displayPanel } = req.body;
|
|
||||||
|
|
||||||
// Prepare an object for the updated testimonial data
|
|
||||||
const updatedPanel3Data = {
|
|
||||||
title,
|
|
||||||
content,
|
|
||||||
displayPanel
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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
|
|
||||||
updatedPanel3Data.image = image;
|
|
||||||
}
|
|
||||||
const modifiedPanel = await Panel3.findOneAndUpdate(
|
|
||||||
{ _id: req.params.id },
|
|
||||||
{ $set: updatedPanel3Data },
|
|
||||||
{ new: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
ModifyBlog: modifiedPanel,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
export const AddPanel4 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
// console.log(req?.user)
|
|
||||||
|
|
||||||
req.body.user = req.user._id;
|
|
||||||
const { content,title,displayPanel} = req.body;
|
|
||||||
let image; // To store Cloudinary image details
|
|
||||||
|
|
||||||
if (req.files && req.files.image) {
|
|
||||||
const imageFile = req.files.image;
|
|
||||||
const result = await cloudinary.v2.uploader.upload(
|
|
||||||
imageFile.tempFilePath,
|
|
||||||
{
|
|
||||||
folder: "smellica/blog",
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
image = {
|
|
||||||
public_id: result.public_id,
|
|
||||||
url: result.secure_url,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the blog post
|
|
||||||
const panel4 = await Panel4.create({
|
|
||||||
title,
|
|
||||||
image,
|
|
||||||
content,
|
|
||||||
displayPanel,
|
|
||||||
addedBy: req.user._id,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
res.status(201).json({
|
|
||||||
success: true,
|
|
||||||
panel4,
|
|
||||||
message: "Added successfully",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error)
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getPanel4 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
// console.log(req?.user)
|
|
||||||
|
|
||||||
const panel4 = await Panel4.find();
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
panel4,
|
|
||||||
message: "Found successfully ",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const updatePanel4 = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
||||||
|
|
||||||
// Check if the user is authenticated
|
|
||||||
if (!req.user) {
|
|
||||||
return res.status(400).json({ message: "Please login!" });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Destructure request body
|
|
||||||
const { title, content,displayPanel } = req.body;
|
|
||||||
|
|
||||||
// Prepare an object for the updated testimonial data
|
|
||||||
const updatePanel4Data = {
|
|
||||||
title,
|
|
||||||
content,
|
|
||||||
displayPanel
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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
|
|
||||||
updatePanel4Data.image = image;
|
|
||||||
}
|
|
||||||
const modifiedPanel = await Panel4.findOneAndUpdate(
|
|
||||||
{ _id: req.params.id },
|
|
||||||
{ $set: updatePanel4Data },
|
|
||||||
{ new: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
ModifyBlog: modifiedPanel,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: 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: "Deleted Successfully!!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
msg: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,81 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
|
|
||||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
|
||||||
import {
|
|
||||||
AddPanel1,
|
|
||||||
AddPanel2,
|
|
||||||
AddPanel3,
|
|
||||||
AddPanel4,
|
|
||||||
deleteImageFromCloudinary,
|
|
||||||
getPanel1,
|
|
||||||
getPanel2,
|
|
||||||
getPanel3,
|
|
||||||
getPanel4,
|
|
||||||
updatePanel1,
|
|
||||||
updatePanel2,
|
|
||||||
updatePanel3,
|
|
||||||
updatePanel4,
|
|
||||||
} from "./PanelController.js";
|
|
||||||
|
|
||||||
const router = express.Router();
|
|
||||||
|
|
||||||
router
|
|
||||||
.route("/panel1/add")
|
|
||||||
.post(isAuthenticatedUser, authorizeRoles("admin", "Employee"), AddPanel1);
|
|
||||||
router.route("/panel1/get").get(getPanel1);
|
|
||||||
|
|
||||||
router
|
|
||||||
.route("/panel1/update/:id")
|
|
||||||
.patch(
|
|
||||||
isAuthenticatedUser,
|
|
||||||
authorizeRoles("admin", "Employee"),
|
|
||||||
updatePanel1
|
|
||||||
);
|
|
||||||
|
|
||||||
router
|
|
||||||
.route("/panel2/add")
|
|
||||||
.post(isAuthenticatedUser, authorizeRoles("admin", "Employee"), AddPanel2);
|
|
||||||
router.route("/panel2/get").get(getPanel2);
|
|
||||||
|
|
||||||
router
|
|
||||||
.route("/panel2/update/:id")
|
|
||||||
.patch(
|
|
||||||
isAuthenticatedUser,
|
|
||||||
authorizeRoles("admin", "Employee"),
|
|
||||||
updatePanel2
|
|
||||||
);
|
|
||||||
|
|
||||||
router
|
|
||||||
.route("/panel3/add")
|
|
||||||
.post(isAuthenticatedUser, authorizeRoles("admin", "Employee"), AddPanel3);
|
|
||||||
router.route("/panel3/get").get(getPanel3);
|
|
||||||
|
|
||||||
router
|
|
||||||
.route("/panel3/update/:id")
|
|
||||||
.patch(
|
|
||||||
isAuthenticatedUser,
|
|
||||||
authorizeRoles("admin", "Employee"),
|
|
||||||
updatePanel3
|
|
||||||
);
|
|
||||||
|
|
||||||
router
|
|
||||||
.route("/panel4/add")
|
|
||||||
.post(isAuthenticatedUser, authorizeRoles("admin", "Employee"), AddPanel4);
|
|
||||||
router.route("/panel4/get").get(getPanel4);
|
|
||||||
|
|
||||||
router
|
|
||||||
.route("/panel4/update/:id")
|
|
||||||
.patch(
|
|
||||||
isAuthenticatedUser,
|
|
||||||
authorizeRoles("admin", "Employee"),
|
|
||||||
updatePanel4
|
|
||||||
);
|
|
||||||
|
|
||||||
router
|
|
||||||
.route("/deleteImage/jatinMor/panel/:public_id")
|
|
||||||
.delete(
|
|
||||||
isAuthenticatedUser,
|
|
||||||
authorizeRoles("admin", "Employee"),
|
|
||||||
deleteImageFromCloudinary
|
|
||||||
);
|
|
||||||
export default router;
|
|
@ -1,750 +0,0 @@
|
|||||||
// import hashPassword from '../utils/hashPassword';
|
|
||||||
|
|
||||||
import crypto from 'crypto';
|
|
||||||
import Patient from './PatientModel.js'
|
|
||||||
import sendEmail, { sendOtp } from '../../Utils/sendEmail.js';
|
|
||||||
import validator from "validator";
|
|
||||||
import password from "secure-random-password";
|
|
||||||
import cloudinary from '../../Utils/cloudinary.js';
|
|
||||||
|
|
||||||
export const register = async (req, res) => {
|
|
||||||
let { name, countryCode, mobileNumber } = req.body;
|
|
||||||
// Trim the country code and mobile number
|
|
||||||
countryCode = countryCode?.trim();
|
|
||||||
mobileNumber = mobileNumber?.trim();
|
|
||||||
const fullMobileNumber = `${countryCode}${mobileNumber}`;
|
|
||||||
try {
|
|
||||||
let patient = await Patient.findOne({ mobileNumber: fullMobileNumber });
|
|
||||||
if (patient && patient.isVerified) {
|
|
||||||
return res.status(400).json({ message: 'Patient already registered and verified for This Mobile No.' });
|
|
||||||
}
|
|
||||||
const otp = crypto.randomInt(100000, 1000000).toString();
|
|
||||||
const otpExpires = Date.now() + 3 * 60 * 1000; // 3 minutes
|
|
||||||
|
|
||||||
if (patient) {
|
|
||||||
patient.otp = otp;
|
|
||||||
patient.otpExpires = otpExpires;
|
|
||||||
} else {
|
|
||||||
patient = new Patient({ name, mobileNumber: fullMobileNumber, otp, otpExpires });
|
|
||||||
}
|
|
||||||
await patient.save();
|
|
||||||
await sendOtp(fullMobileNumber, `Your Cheminova verification OTP is: ${otp}`);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return res.status(200).json({ message: `OTP sent to your mobile number ${fullMobileNumber} successfully` });
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
message: error.message ? error.message : "Server error!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const verifyOtp = async (req, res) => {
|
|
||||||
const { mobileNumber, otp } = req.body;
|
|
||||||
try {
|
|
||||||
let mobileNmr = mobileNumber?.trim();
|
|
||||||
const patient = await Patient.findOne({ mobileNumber: mobileNmr });
|
|
||||||
|
|
||||||
if (!patient) {
|
|
||||||
return res.status(400).json({ message: 'Invalid mobile number or OTP' });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (patient.otp !== otp || patient.otpExpires < Date.now()) {
|
|
||||||
return res.status(400).json({ message: 'Invalid or expired OTP' });
|
|
||||||
}
|
|
||||||
patient.isVerified = true;
|
|
||||||
patient.otp = undefined;
|
|
||||||
patient.otpExpires = undefined;
|
|
||||||
await patient.save();
|
|
||||||
const token = patient.getJWTToken();
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
token, message: 'Mobile number verified successfully'
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
message: error.message ? error.message : "Server error!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// Login Patient
|
|
||||||
export const loginPatient = async (req, res) => {
|
|
||||||
const { email, password } = req.body;
|
|
||||||
// checking if patient has given password and email both
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!email || !password) {
|
|
||||||
return res.status(400).json({ message: "Please Enter Email & Password" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const patient = await Patient.findOne({ email }).select("+password");
|
|
||||||
|
|
||||||
if (!patient) {
|
|
||||||
return res.status(400).json({ message: "Invalid Email or Password" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const isPasswordMatched = await patient.comparePassword(password);
|
|
||||||
|
|
||||||
if (!isPasswordMatched) {
|
|
||||||
return res.status(400).json({ message: "Invalid Email or Password" });
|
|
||||||
}
|
|
||||||
const token = patient.getJWTToken();
|
|
||||||
|
|
||||||
return res.status(200).json({ success: true, token, message: "Login Successfully" });
|
|
||||||
} catch (error) {
|
|
||||||
return res
|
|
||||||
.status(500)
|
|
||||||
.json({
|
|
||||||
message: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//get All patient
|
|
||||||
export const getAllPatient = 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?.mobileNumber)
|
|
||||||
obj.mobileNumber = {
|
|
||||||
$regex: new RegExp(req.query.mobileNumber),
|
|
||||||
$options: "i",
|
|
||||||
};
|
|
||||||
|
|
||||||
if (req.query?.category) obj.category = req.query.category;
|
|
||||||
if (req.query?.isVerified)
|
|
||||||
obj.isVerified = req.query.isVerified;
|
|
||||||
const total = await Patient.countDocuments(obj);
|
|
||||||
const patient = await Patient.find(obj)
|
|
||||||
// .populate({
|
|
||||||
// path: "category addedBy master_GST variants.gst_Id",
|
|
||||||
// select: "name categoryName tax",
|
|
||||||
// })
|
|
||||||
.limit(PAGE_SIZE)
|
|
||||||
.skip(PAGE_SIZE * page)
|
|
||||||
// .sort("name")
|
|
||||||
.sort({
|
|
||||||
createdAt: -1,
|
|
||||||
})
|
|
||||||
.exec();
|
|
||||||
|
|
||||||
if (patient) {
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
total_data: total,
|
|
||||||
total_pages: Math.ceil(total / PAGE_SIZE),
|
|
||||||
patient,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
msg: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
export const getOnePatient = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req.paras.id) {
|
|
||||||
return res.status(400).json({ message: "Please provide patient ID" });
|
|
||||||
}
|
|
||||||
const data = await Patient.findById(req.paras.id);
|
|
||||||
if (data) {
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: "feched!",
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export const create1RegistrationDetails = async (req, res) => {
|
|
||||||
const {
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
confirmPassword,
|
|
||||||
} = req.body;
|
|
||||||
if (!email) return res.status(400).json({ message: 'Email is required' });
|
|
||||||
if (!password) return res.status(400).json({ message: 'Password is required' });
|
|
||||||
if (!confirmPassword) return res.status(400).json({ message: 'Confirm password is required' });
|
|
||||||
// Validate email format
|
|
||||||
if (!validator.isEmail(email)) {
|
|
||||||
return res.status(400).json({ message: 'Invalid email format' });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (password !== confirmPassword) {
|
|
||||||
return res.status(400).json({ message: 'Password and confirm password do not match' });
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const patient = await Patient.findById(req.patient._id);
|
|
||||||
if (!patient) {
|
|
||||||
return res.status(404).json({ message: 'Patient not found ' });
|
|
||||||
}
|
|
||||||
if (!patient.isVerified) {
|
|
||||||
return res.status(400).json({ message: 'Patient not verified' });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if another patient with the same email exists
|
|
||||||
const emailExists = await Patient.findOne({ email });
|
|
||||||
if (emailExists && emailExists._id.toString() !== req.patient._id.toString()) {
|
|
||||||
return res.status(400).json({ message: 'This Email ID is already in use By Another patient' });
|
|
||||||
}
|
|
||||||
patient.email = email;
|
|
||||||
patient.password = password;
|
|
||||||
|
|
||||||
await patient.save();
|
|
||||||
const patientResponse = patient.toObject();
|
|
||||||
delete patientResponse.password;
|
|
||||||
|
|
||||||
res.status(200).json({ patient: patientResponse, message: 'Registration details updated successfully' });
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
res.status(500).json({
|
|
||||||
message: error.message ? error.message : "Server error!",
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
export const EnterPatientDetails = async (req, res) => {
|
|
||||||
const {
|
|
||||||
gender,
|
|
||||||
weightValue,
|
|
||||||
weightUnit,
|
|
||||||
heightValue,
|
|
||||||
heightUnit,
|
|
||||||
age,
|
|
||||||
} = req.body;
|
|
||||||
|
|
||||||
switch (true) {
|
|
||||||
case !gender:
|
|
||||||
return res.status(400).json({ message: 'Gender is required' });
|
|
||||||
case !weightValue:
|
|
||||||
return res.status(400).json({ message: 'weight Value is required' });
|
|
||||||
case !weightUnit:
|
|
||||||
return res.status(400).json({ message: 'weight Unit is required' });
|
|
||||||
case !heightValue:
|
|
||||||
return res.status(400).json({ message: 'height Value is required' });
|
|
||||||
case !heightUnit:
|
|
||||||
return res.status(400).json({ message: 'height Unit is required' });
|
|
||||||
case !age:
|
|
||||||
return res.status(400).json({ message: 'Age is required and Must Be a Number' });
|
|
||||||
default:
|
|
||||||
|
|
||||||
//gender Validate
|
|
||||||
if (!['Male', 'Female'].includes(gender)) {
|
|
||||||
return res.status(400).json({ message: 'Invalid gender:gender Must be "Male" or "Female"' });
|
|
||||||
}
|
|
||||||
// Validate weightUnit
|
|
||||||
if (!['Kgs', 'Lbs'].includes(weightUnit)) {
|
|
||||||
return res.status(400).json({ message: 'Invalid weight unit. Must be "Kgs" or "Lbs"' });
|
|
||||||
}
|
|
||||||
// Ensure weightValue is a number
|
|
||||||
if (isNaN(weightValue)) {
|
|
||||||
return res.status(400).json({ message: 'Weight value must be a number' });
|
|
||||||
}
|
|
||||||
// Validate heightUnit
|
|
||||||
if (!['Ft', 'Cm'].includes(heightUnit)) {
|
|
||||||
return res.status(400).json({ message: 'Invalid height unit. Must be "Ft" or "Cm"' });
|
|
||||||
}
|
|
||||||
// Ensure heightValue is a number
|
|
||||||
if (isNaN(heightValue)) {
|
|
||||||
return res.status(400).json({ message: 'Height value must be a number' });
|
|
||||||
}
|
|
||||||
if (isNaN(age)) {
|
|
||||||
return res.status(400).json({ message: 'age must be a number' });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const patient = await Patient.findById(req.patient._id);
|
|
||||||
if (!patient) {
|
|
||||||
return res.status(404).json({ message: 'Patient not found ' });
|
|
||||||
}
|
|
||||||
if (!patient.isVerified) {
|
|
||||||
return res.status(400).json({ message: 'Patient not verified' });
|
|
||||||
}
|
|
||||||
|
|
||||||
patient.gender = gender;
|
|
||||||
patient.weight = {
|
|
||||||
value: weightValue,
|
|
||||||
unit: weightUnit,
|
|
||||||
};
|
|
||||||
patient.height = {
|
|
||||||
value: heightValue,
|
|
||||||
unit: heightUnit,
|
|
||||||
};
|
|
||||||
patient.age = age;
|
|
||||||
await patient.save();
|
|
||||||
const patientResponse = patient.toObject();
|
|
||||||
delete patientResponse.password;
|
|
||||||
res.status(200).json({ patient: patientResponse, message: 'Patient details updated successfully' });
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
res.status(500).json({
|
|
||||||
message: error.message ? error.message : "Server error!",
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const EnterPersonalDetails = async (req, res) => {
|
|
||||||
const {
|
|
||||||
commonHealthComplaints,
|
|
||||||
familyHistory,
|
|
||||||
personalHistory,
|
|
||||||
dailyRoutine,
|
|
||||||
} = req.body;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const patient = await Patient.findById(req.patient._id);
|
|
||||||
if (!patient) {
|
|
||||||
return res.status(404).json({ message: 'Patient not found ' });
|
|
||||||
}
|
|
||||||
if (!patient.isVerified) {
|
|
||||||
return res.status(400).json({ message: 'Patient not verified' });
|
|
||||||
}
|
|
||||||
// Check if another patient with the same email exists
|
|
||||||
|
|
||||||
patient.commonHealthComplaints = commonHealthComplaints;
|
|
||||||
patient.familyHistory = familyHistory;
|
|
||||||
patient.personalHistory = personalHistory;
|
|
||||||
patient.dailyRoutine = dailyRoutine;
|
|
||||||
await patient.save();
|
|
||||||
const patientResponse = patient.toObject();
|
|
||||||
delete patientResponse.password;
|
|
||||||
res.status(200).json({ patient: patientResponse, message: 'Patient Pesonal details updated successfully' });
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
res.status(500).json({
|
|
||||||
message: error.message ? error.message : "Server error!",
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// 4.Forgot Password
|
|
||||||
|
|
||||||
export const forgotPassword = async (req, res) => {
|
|
||||||
const patient = await Patient.findOne({ email: req.body.email });
|
|
||||||
if (!req.body.email) {
|
|
||||||
return res.status(400).json({ message: 'please Enter Email!' });
|
|
||||||
}
|
|
||||||
if (!patient) {
|
|
||||||
return res.status(404).json({ message: "Patient not found" });
|
|
||||||
}
|
|
||||||
// Get ResetPassword Token
|
|
||||||
// const resetToken = patient.getResetPasswordToken(); //call function
|
|
||||||
|
|
||||||
//save database reset token
|
|
||||||
// await patient.save({ validateBeforeSave: false });
|
|
||||||
|
|
||||||
const passwords = password.randomPassword({
|
|
||||||
length: 12,
|
|
||||||
characters: [
|
|
||||||
{ characters: password.upper, exactly: 1 },
|
|
||||||
{ characters: password.symbols, exactly: 1 },
|
|
||||||
password.lower,
|
|
||||||
password.digits,
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
patient.password = passwords;
|
|
||||||
await patient.save();
|
|
||||||
// const message = `Your password reset token are :- \n\n ${resetPasswordUrl} \n\nyour new password is:${password}\n\nIf you have not requested this email then, please ignore it.`;
|
|
||||||
try {
|
|
||||||
await sendEmail({
|
|
||||||
to: `${patient?.email}`, // Change to your recipient
|
|
||||||
|
|
||||||
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
|
||||||
|
|
||||||
subject: `Cheminova Password Recovery`,
|
|
||||||
html: `your new password is: <br/> <strong> ${passwords}</strong><br/><br/>If you have not requested this email then, please ignore it.`,
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: `Email sent to ${patient?.email} successfully`,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
patient.resetPasswordToken = undefined;
|
|
||||||
patient.resetPasswordExpire = undefined;
|
|
||||||
|
|
||||||
await patient.save({ validateBeforeSave: false });
|
|
||||||
|
|
||||||
return res
|
|
||||||
.status(500)
|
|
||||||
.json({
|
|
||||||
message: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//update Patient Profile Image
|
|
||||||
export const UploadProfileImage = async (req, res) => {
|
|
||||||
if (!req.files) {
|
|
||||||
return res.status(404).json({ message: "Please Select Image" });
|
|
||||||
}
|
|
||||||
const patient = await Patient.findById(req.patient._id);
|
|
||||||
if (!patient) {
|
|
||||||
return res.status(404).json({ message: 'Patient not found ' });
|
|
||||||
}
|
|
||||||
if (!patient.isVerified) {
|
|
||||||
return res.status(400).json({ message: 'Patient not verified First verify Mobile No.' });
|
|
||||||
}
|
|
||||||
let newPatientData = {};
|
|
||||||
try {
|
|
||||||
if (req.files) {
|
|
||||||
const patientImage = req.files?.avatar;
|
|
||||||
const patient = await Patient.findById(req.patient._id);
|
|
||||||
|
|
||||||
if (patient?.avatar?.public_id) {
|
|
||||||
const imageId = patient?.avatar?.public_id;
|
|
||||||
await cloudinary.uploader.destroy(imageId)
|
|
||||||
}
|
|
||||||
|
|
||||||
const myCloud = await cloudinary.v2.uploader.upload(patientImage.tempFilePath,
|
|
||||||
{
|
|
||||||
folder: "Cheminova/patient-image",
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
newPatientData.avatar = {
|
|
||||||
public_id: myCloud.public_id,
|
|
||||||
url: myCloud.secure_url,
|
|
||||||
};
|
|
||||||
let patientDetail = await Patient.findByIdAndUpdate(
|
|
||||||
req.patient._id, newPatientData,
|
|
||||||
{ new: true } // Return the updated document
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: "Image Uploaded Successfully!",
|
|
||||||
patientDetail,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
//Update mobile Number
|
|
||||||
export const updateMobileNumber = async (req, res) => {
|
|
||||||
let { newCountryCode, newMobileNumber } = req.body;
|
|
||||||
newCountryCode = newCountryCode?.trim();
|
|
||||||
newMobileNumber = newMobileNumber?.trim();
|
|
||||||
const newFullMobileNumber = `${newCountryCode}${newMobileNumber}`;
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (req.patient?.mobileNumber === newFullMobileNumber) {
|
|
||||||
return res.status(400).json({ message: 'New mobile number cannot be the same as the old mobile number' });
|
|
||||||
}
|
|
||||||
let patient = await Patient.findOne({ mobileNumber: req.patient?.mobileNumber });
|
|
||||||
if (!patient) {
|
|
||||||
return res.status(400).json({ message: 'Patient not found' });
|
|
||||||
}
|
|
||||||
|
|
||||||
const otp = crypto.randomInt(100000, 1000000).toString();
|
|
||||||
const otpExpires = Date.now() + 3 * 60 * 1000; // 3 minutes
|
|
||||||
|
|
||||||
patient.newMobileNumber = newFullMobileNumber;
|
|
||||||
patient.otp = otp;
|
|
||||||
patient.otpExpires = otpExpires;
|
|
||||||
|
|
||||||
await patient.save();
|
|
||||||
await sendOtp(newFullMobileNumber, `Your Cheminova verification OTP is: ${otp}`);
|
|
||||||
|
|
||||||
return res.status(200).json({ message: `OTP sent to your new mobile number ${newFullMobileNumber} successfully` });
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
message: error.message ? error.message : "Server error!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
//verify Updated Number OTP
|
|
||||||
export const verifyUpdatedMobileOtp = async (req, res) => {
|
|
||||||
const { newMobileNumber, otp } = req.body;
|
|
||||||
try {
|
|
||||||
let mobileNmr = newMobileNumber?.trim();
|
|
||||||
const patient = await Patient.findOne({ newMobileNumber: mobileNmr });
|
|
||||||
|
|
||||||
if (!patient) {
|
|
||||||
return res.status(400).json({ message: 'Invalid mobile number or OTP' });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (patient.otp !== otp || patient.otpExpires < Date.now()) {
|
|
||||||
return res.status(400).json({ message: 'Invalid or expired OTP' });
|
|
||||||
}
|
|
||||||
|
|
||||||
patient.mobileNumber = patient.newMobileNumber;
|
|
||||||
patient.newMobileNumber = undefined;
|
|
||||||
patient.isVerified = true;
|
|
||||||
patient.otp = undefined;
|
|
||||||
patient.otpExpires = undefined;
|
|
||||||
await patient.save();
|
|
||||||
const token = patient.getJWTToken();
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
token, message: 'Mobile number updated and verified successfully'
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
message: error.message ? error.message : "Server error!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
//getmyProfile
|
|
||||||
export const getmyProfile = async (req, res) => {
|
|
||||||
try {
|
|
||||||
const myData = await Patient.findById(req.patient?._id);
|
|
||||||
if (myData) {
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: "feched!",
|
|
||||||
myData,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
export const deletePatient = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req.params.id) {
|
|
||||||
return res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
message: "Please Provide Patient ID!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const getPatient = await Patient.findById(req.params.id);
|
|
||||||
if (!getPatient) {
|
|
||||||
return res.status(404).json({
|
|
||||||
success: false,
|
|
||||||
message: "patient not Found!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deleting Images From Cloudinary
|
|
||||||
if (getPatient?.avatar?.public_id) {
|
|
||||||
await cloudinary.v2.uploader.destroy(getPatient.avatar?.public_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------//
|
|
||||||
const patient = await Patient.findByIdAndDelete(req.params.id);
|
|
||||||
if (!patient) {
|
|
||||||
return res.status(404).json({ message: "patient Not Found" });
|
|
||||||
}
|
|
||||||
await patient.remove();
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: "patient Deleted Successfully!!",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
export const UpdateProile = async (req, res) => {
|
|
||||||
const {
|
|
||||||
name,
|
|
||||||
email,
|
|
||||||
gender,
|
|
||||||
weightValue,
|
|
||||||
weightUnit,
|
|
||||||
heightValue,
|
|
||||||
heightUnit,
|
|
||||||
age,
|
|
||||||
commonHealthComplaints,
|
|
||||||
familyHistory,
|
|
||||||
personalHistory,
|
|
||||||
dailyRoutine,
|
|
||||||
} = req.body;
|
|
||||||
// Validate email
|
|
||||||
if (!validator.isEmail(email)) {
|
|
||||||
return res.status(400).json({ message: 'Invalid email address' });
|
|
||||||
}
|
|
||||||
|
|
||||||
//gender Validate
|
|
||||||
if (gender && !['Male', 'Female'].includes(gender)) {
|
|
||||||
return res.status(400).json({ message: 'Invalid gender:gender Must be "Male" or "Female"' });
|
|
||||||
}
|
|
||||||
// Validate weightUnit
|
|
||||||
if (weightUnit && !['Kgs', 'Lbs'].includes(weightUnit)) {
|
|
||||||
return res.status(400).json({ message: 'Invalid weight unit. Must be "Kgs" or "Lbs"' });
|
|
||||||
}
|
|
||||||
// Ensure weightValue is a number
|
|
||||||
if (weightValue && isNaN(weightValue)) {
|
|
||||||
return res.status(400).json({ message: 'Weight value must be a number' });
|
|
||||||
}
|
|
||||||
// Validate heightUnit
|
|
||||||
if (heightUnit && !['Ft', 'Cm'].includes(heightUnit)) {
|
|
||||||
return res.status(400).json({ message: 'Invalid height unit. Must be "Ft" or "Cm"' });
|
|
||||||
}
|
|
||||||
// Ensure heightValue is a number
|
|
||||||
if (heightValue && isNaN(heightValue)) {
|
|
||||||
return res.status(400).json({ message: 'Height value must be a number' });
|
|
||||||
}
|
|
||||||
if (age && isNaN(age)) {
|
|
||||||
return res.status(400).json({ message: 'age must be a number' });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const patient = await Patient.findById(req.patient._id);
|
|
||||||
if (!patient) {
|
|
||||||
return res.status(404).json({ message: 'Patient not found ' });
|
|
||||||
}
|
|
||||||
if (!patient.isVerified) {
|
|
||||||
return res.status(400).json({ message: 'Patient not verified' });
|
|
||||||
}
|
|
||||||
if (email) {
|
|
||||||
const emailExists = await Patient.findOne({ email });
|
|
||||||
if (emailExists && emailExists._id.toString() !== req.patient._id.toString()) {
|
|
||||||
return res.status(400).json({ message: 'This Email ID is already in use By Another patient' });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateData = {
|
|
||||||
weight: {
|
|
||||||
value: weightValue,
|
|
||||||
unit: weightUnit,
|
|
||||||
},
|
|
||||||
height: {
|
|
||||||
value: heightValue,
|
|
||||||
unit: heightUnit,
|
|
||||||
},
|
|
||||||
...req.body
|
|
||||||
};
|
|
||||||
let NewPatientDetail = await Patient.findByIdAndUpdate(
|
|
||||||
req.patient._id,
|
|
||||||
updateData
|
|
||||||
,
|
|
||||||
{ new: true } // Return the updated document
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.status(200).json({ patient: NewPatientDetail, message: 'Profile updated successfully' });
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
message: error.message ? error.message : "Server error!",
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//change Patient password
|
|
||||||
export const ChangePassword = async (req, res) => {
|
|
||||||
const { oldPassword, newPassword, confirmPassword } = req.body
|
|
||||||
|
|
||||||
if (!oldPassword) {
|
|
||||||
return res.status(400).json({ message: 'Please Enter Old password' });
|
|
||||||
|
|
||||||
}
|
|
||||||
if (!newPassword) {
|
|
||||||
return res.status(400).json({ message: 'Please Enter New Password ' });
|
|
||||||
|
|
||||||
}
|
|
||||||
if (!confirmPassword) {
|
|
||||||
return res.status(400).json({ message: 'Please Enter Confirm Password' });
|
|
||||||
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const patient = await Patient.findById(req.patient._id).select("+password");
|
|
||||||
|
|
||||||
const isPasswordMatched = await patient.comparePassword(req.body.oldPassword);
|
|
||||||
|
|
||||||
if (!isPasswordMatched) {
|
|
||||||
return res.status(400).json({ message: 'Old password is incorrect' });
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (req.body.newPassword !== req.body.confirmPassword) {
|
|
||||||
return res.status(400).json({ message: 'New password and confirm Password does not match' });
|
|
||||||
|
|
||||||
}
|
|
||||||
patient.password = req.body.newPassword;
|
|
||||||
await patient.save();
|
|
||||||
// const token = patient.getJWTToken();
|
|
||||||
|
|
||||||
return res.status(200).json({ success: true, message: 'Password updated successfully' });
|
|
||||||
|
|
||||||
// sendToken(patient, 200, res);
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
message: error.message ? error.message : "Server error!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const Otp = async (req, res) => {
|
|
||||||
// const { name, mobileNumber } = req.body;
|
|
||||||
try {
|
|
||||||
// let patient = await Patient.findOne({ mobileNumber });
|
|
||||||
|
|
||||||
// if (patient && patient.isVerified) {
|
|
||||||
// return res.status(400).json({ message: 'Patient already registered and verified for This Mobile No.' });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const otp = crypto.randomInt(100000, 1000000).toString();
|
|
||||||
|
|
||||||
// // const otp ="123456";
|
|
||||||
|
|
||||||
// const otpExpires = Date.now() + 10 * 60 * 1000; // 10 minutes
|
|
||||||
|
|
||||||
// if (patient) {
|
|
||||||
// patient.otp = otp;
|
|
||||||
// patient.otpExpires = otpExpires;
|
|
||||||
// } else {
|
|
||||||
// patient = new Patient({ name, mobileNumber, otp, otpExpires });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// await patient.save();
|
|
||||||
await sendOtp();
|
|
||||||
|
|
||||||
// res.status(200).json({patient, message: `OTP ${otp} sent to your mobile number successfully` });
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
message: error.message ? error.message : "Server error!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,44 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
|
|
||||||
const router = express.Router();
|
|
||||||
import { EnterPatientDetails, EnterPersonalDetails, Otp, UploadProfileImage, create1RegistrationDetails, deletePatient, forgotPassword, getAllPatient, loginPatient, register, updateMobileNumber, verifyUpdatedMobileOtp, verifyOtp, UpdateProile, getmyProfile, ChangePassword, getOnePatient } from "./PatientController.js";
|
|
||||||
import { isAuthenticatedPatient } from "../../middlewares/PatientAuth.js";
|
|
||||||
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
|
||||||
|
|
||||||
|
|
||||||
router.post('/register', register);
|
|
||||||
router.post('/verify-otp', verifyOtp);
|
|
||||||
router.post('/login', loginPatient);
|
|
||||||
|
|
||||||
|
|
||||||
router.post('/rgstr_details-p1', isAuthenticatedPatient, create1RegistrationDetails);
|
|
||||||
router.post('/rgstr_details-p2', isAuthenticatedPatient, EnterPatientDetails);
|
|
||||||
router.post('/rgstr_psrnl_details-p3', isAuthenticatedPatient, EnterPersonalDetails);
|
|
||||||
//admin
|
|
||||||
router.get('/getAll', isAuthenticatedUser, authorizeRoles("admin"), getAllPatient);
|
|
||||||
router.get('/getOne/:id', isAuthenticatedUser, authorizeRoles("admin"), getOnePatient);
|
|
||||||
router.get('/my-profile', isAuthenticatedPatient, getmyProfile);
|
|
||||||
|
|
||||||
//Update Mobile Number
|
|
||||||
router.post('/update-mobile-number', isAuthenticatedPatient, updateMobileNumber);
|
|
||||||
router.post('/verify-updated-mobile-otp', isAuthenticatedPatient, verifyUpdatedMobileOtp);
|
|
||||||
router.post('/forgot-password', forgotPassword);
|
|
||||||
router.post('/profile-image/upload', isAuthenticatedPatient, UploadProfileImage);
|
|
||||||
router.patch('/profile/update', isAuthenticatedPatient, UpdateProile);
|
|
||||||
|
|
||||||
//change password
|
|
||||||
router.put('/password/update', isAuthenticatedPatient, ChangePassword);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//delete Patient
|
|
||||||
router.delete('/delete/:id', isAuthenticatedUser, authorizeRoles("admin"), deletePatient);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
router.get('/otp', Otp);
|
|
||||||
|
|
||||||
|
|
||||||
export default router;
|
|
@ -85,7 +85,7 @@ export const updateProduct = async (req, res) => {
|
|||||||
const imagesLinks = [];
|
const imagesLinks = [];
|
||||||
for (let i = 0; i < images.length; i++) {
|
for (let i = 0; i < images.length; i++) {
|
||||||
const result = await cloudinary.v2.uploader.upload(images[i], {
|
const result = await cloudinary.v2.uploader.upload(images[i], {
|
||||||
folder: "smellica/product",
|
folder: "chemiNova/product",
|
||||||
});
|
});
|
||||||
|
|
||||||
imagesLinks.push({
|
imagesLinks.push({
|
||||||
@ -379,7 +379,7 @@ export const getAllProductsDevicesFirst = async (req, res) => {
|
|||||||
// const result = await cloudinary.v2.uploader.upload(
|
// const result = await cloudinary.v2.uploader.upload(
|
||||||
// newuploadImages[i].tempFilePath,
|
// newuploadImages[i].tempFilePath,
|
||||||
// {
|
// {
|
||||||
// folder: "smellica/product",
|
// folder: "chemiNova/product",
|
||||||
// }
|
// }
|
||||||
// );
|
// );
|
||||||
|
|
||||||
@ -472,7 +472,7 @@ export const getAllProductsDevicesFirst = async (req, res) => {
|
|||||||
// const result = await cloudinary.v2.uploader.upload(
|
// const result = await cloudinary.v2.uploader.upload(
|
||||||
// newUploadImages[i].tempFilePath,
|
// newUploadImages[i].tempFilePath,
|
||||||
// {
|
// {
|
||||||
// folder: "smellica/product",
|
// folder: "chemiNova/product",
|
||||||
// }
|
// }
|
||||||
// );
|
// );
|
||||||
|
|
||||||
|
570
resources/SalesCoOrdinators/SalesCoOrdinatorController.js
Normal file
570
resources/SalesCoOrdinators/SalesCoOrdinatorController.js
Normal file
@ -0,0 +1,570 @@
|
|||||||
|
// import hashPassword from '../utils/hashPassword';
|
||||||
|
|
||||||
|
import crypto from "crypto";
|
||||||
|
import SalesCoOrdinator from "./SalesCoOrdinatorModel.js";
|
||||||
|
import sendEmail, { sendOtp } from "../../Utils/sendEmail.js";
|
||||||
|
import validator from "validator";
|
||||||
|
import password from "secure-random-password";
|
||||||
|
|
||||||
|
export const register = async (req, res) => {
|
||||||
|
let { name, email, countryCode, mobileNumber } = req.body;
|
||||||
|
// console.log(req.body);
|
||||||
|
countryCode = countryCode?.trim();
|
||||||
|
mobileNumber = mobileNumber?.trim();
|
||||||
|
const fullMobileNumber = `${countryCode}${mobileNumber}`;
|
||||||
|
// console.log(fullMobileNumber);
|
||||||
|
|
||||||
|
try {
|
||||||
|
let salesCoordinator = await SalesCoOrdinator.findOne({
|
||||||
|
mobileNumber: fullMobileNumber,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (salesCoordinator && salesCoordinator.isVerified) {
|
||||||
|
return res.status(400).json({
|
||||||
|
message:
|
||||||
|
"SalesCoordinator already registered and verified for this mobile number.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const otp = crypto.randomInt(100000, 1000000).toString();
|
||||||
|
const otpExpires = Date.now() + 3 * 60 * 1000; // 3 minutes
|
||||||
|
|
||||||
|
if (salesCoordinator) {
|
||||||
|
salesCoordinator.otp = otp;
|
||||||
|
salesCoordinator.otpExpires = otpExpires;
|
||||||
|
} else {
|
||||||
|
salesCoordinator = new SalesCoOrdinator({
|
||||||
|
name,
|
||||||
|
email,
|
||||||
|
mobileNumber: fullMobileNumber,
|
||||||
|
otp,
|
||||||
|
otpExpires,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await salesCoordinator.save();
|
||||||
|
await sendOtp(
|
||||||
|
fullMobileNumber,
|
||||||
|
`Your Cheminova verification OTP is: ${otp}`
|
||||||
|
);
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
message: `OTP sent to your mobile number ${fullMobileNumber} successfully`,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
message: error.message ? error.message : "Server error!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const verifyOtp = async (req, res) => {
|
||||||
|
const { fullMobileNumber, otp } = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
let mobileNmr = fullMobileNumber?.trim();
|
||||||
|
const salesCoordinator = await SalesCoOrdinator.findOne({
|
||||||
|
mobileNumber: mobileNmr,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!salesCoordinator) {
|
||||||
|
return res.status(400).json({ message: "Invalid mobile number or OTP" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
salesCoordinator.otp !== otp ||
|
||||||
|
salesCoordinator.otpExpires < Date.now()
|
||||||
|
) {
|
||||||
|
return res.status(400).json({ message: "Invalid or expired OTP" });
|
||||||
|
}
|
||||||
|
const name = salesCoordinator.name;
|
||||||
|
const mobile = salesCoordinator.mobileNumber;
|
||||||
|
salesCoordinator.isVerified = true;
|
||||||
|
salesCoordinator.otp = undefined;
|
||||||
|
salesCoordinator.otpExpires = undefined;
|
||||||
|
|
||||||
|
// Generate a random password
|
||||||
|
const newPassword = password.randomPassword({
|
||||||
|
length: 12,
|
||||||
|
characters: [
|
||||||
|
{ characters: password.upper, exactly: 1 },
|
||||||
|
{ characters: password.symbols, exactly: 1 },
|
||||||
|
password.lower,
|
||||||
|
password.digits,
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
// Save the new password (hashed)
|
||||||
|
salesCoordinator.password = newPassword;
|
||||||
|
await salesCoordinator.save();
|
||||||
|
|
||||||
|
// Send email with the new password
|
||||||
|
await sendEmail({
|
||||||
|
to: `${salesCoordinator?.email}`, // Change to your recipient
|
||||||
|
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
||||||
|
subject: `Cheminova Account Created`,
|
||||||
|
html: `Your Account is created successfully.
|
||||||
|
<br/>name is: <strong>${name}</strong><br/>
|
||||||
|
<br/>MobileNumber is: <strong>${mobile}</strong><br/>
|
||||||
|
<br/>password is: <strong>${newPassword}</strong><br/><br/>If you have not requested this email, please ignore it.`,
|
||||||
|
});
|
||||||
|
|
||||||
|
const token = salesCoordinator.getJWTToken();
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
token,
|
||||||
|
message: "Mobile number verified and new password sent successfully",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
message: error.message ? error.message : "Server error!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Login SalesCoOrdinator
|
||||||
|
export const loginSalesCoOrdinator = async (req, res) => {
|
||||||
|
const { email, password } = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!email || !password) {
|
||||||
|
return res.status(400).json({ message: "Please Enter Email & Password" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const salesCoordinator = await SalesCoOrdinator.findOne({ email }).select(
|
||||||
|
"+password"
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!salesCoordinator) {
|
||||||
|
return res.status(400).json({ message: "Invalid Email or Password" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const isPasswordMatched = await salesCoordinator.comparePassword(password);
|
||||||
|
|
||||||
|
if (!isPasswordMatched) {
|
||||||
|
return res.status(400).json({ message: "Invalid Email or Password" });
|
||||||
|
}
|
||||||
|
const token = salesCoordinator.getJWTToken();
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
token,
|
||||||
|
message: "Login Successfully",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
return res.status(500).json({
|
||||||
|
message: error.message ? error.message : "Something went wrong!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//get All salescoordinator
|
||||||
|
export const getAllSalesCoOrdinator = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const PAGE_SIZE = parseInt(req.query?.show || "10");
|
||||||
|
const page = parseInt(req.query?.page || "1") - 1;
|
||||||
|
let filter = {};
|
||||||
|
if (req.query?.name) {
|
||||||
|
filter.name = {
|
||||||
|
$regex: new RegExp(req.query.name, "i"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (req.query?.mobileNumber) {
|
||||||
|
filter.mobileNumber = {
|
||||||
|
$regex: new RegExp(req.query.mobileNumber, "i"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (req.query?.isVerified) {
|
||||||
|
filter.isVerified = req.query.isVerified === "true";
|
||||||
|
}
|
||||||
|
|
||||||
|
const total = await SalesCoOrdinator.countDocuments(filter);
|
||||||
|
const salesCoOrinators = await SalesCoOrdinator.find(filter)
|
||||||
|
.limit(PAGE_SIZE)
|
||||||
|
.skip(PAGE_SIZE * page)
|
||||||
|
.sort({ createdAt: -1 });
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
total_data: total,
|
||||||
|
total_pages: Math.ceil(total / PAGE_SIZE),
|
||||||
|
salesCoOrinators,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: error.message ? error.message : "Something went wrong!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getOneSalesCoOrdinator = async (req, res) => {
|
||||||
|
try {
|
||||||
|
if (!req.params.id) {
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ message: "Please provide sales coordinator ID" });
|
||||||
|
}
|
||||||
|
const salesCoordinator = await SalesCoOrdinator.findById(req.params.id);
|
||||||
|
if (salesCoordinator) {
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "Fetched successfully",
|
||||||
|
data: salesCoordinator,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: "Sales coordinator not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: error.message ? error.message : "Something went wrong!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 4.Forgot Password
|
||||||
|
|
||||||
|
export const forgotPassword = async (req, res) => {
|
||||||
|
const salescoordinator = await SalesCoOrdinator.findOne({
|
||||||
|
email: req.body.email,
|
||||||
|
});
|
||||||
|
if (!req.body.email) {
|
||||||
|
return res.status(400).json({ message: "please Enter Email!" });
|
||||||
|
}
|
||||||
|
if (!salescoordinator) {
|
||||||
|
return res.status(404).json({ message: "SalesCoOrdinator not found" });
|
||||||
|
}
|
||||||
|
// Get ResetPassword Token
|
||||||
|
// const resetToken = salescoordinator.getResetPasswordToken(); //call function
|
||||||
|
|
||||||
|
//save database reset token
|
||||||
|
// await salescoordinator.save({ validateBeforeSave: false });
|
||||||
|
|
||||||
|
const passwords = password.randomPassword({
|
||||||
|
length: 12,
|
||||||
|
characters: [
|
||||||
|
{ characters: password.upper, exactly: 1 },
|
||||||
|
{ characters: password.symbols, exactly: 1 },
|
||||||
|
password.lower,
|
||||||
|
password.digits,
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
salescoordinator.password = passwords;
|
||||||
|
await salescoordinator.save();
|
||||||
|
// const message = `Your password reset token are :- \n\n ${resetPasswordUrl} \n\nyour new password is:${password}\n\nIf you have not requested this email then, please ignore it.`;
|
||||||
|
try {
|
||||||
|
await sendEmail({
|
||||||
|
to: `${salescoordinator?.email}`, // Change to your recipient
|
||||||
|
|
||||||
|
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
||||||
|
|
||||||
|
subject: `Cheminova Password Recovery`,
|
||||||
|
html: `your new password is: <br/> <strong> ${passwords}</strong><br/><br/>If you have not requested this email then, please ignore it.`,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: `Email sent to ${salescoordinator?.email} successfully`,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
salescoordinator.resetPasswordToken = undefined;
|
||||||
|
salescoordinator.resetPasswordExpire = undefined;
|
||||||
|
|
||||||
|
await salescoordinator.save({ validateBeforeSave: false });
|
||||||
|
|
||||||
|
return res.status(500).json({
|
||||||
|
message: error.message ? error.message : "Something went wrong!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//Update mobile Number
|
||||||
|
export const updateMobileNumber = async (req, res) => {
|
||||||
|
const { id } = req.params;
|
||||||
|
let { newCountryCode, newMobileNumber } = req.body;
|
||||||
|
newCountryCode = newCountryCode?.trim();
|
||||||
|
newMobileNumber = newMobileNumber?.trim();
|
||||||
|
const newFullMobileNumber = `${newCountryCode}${newMobileNumber}`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const salescoordinator = await SalesCoOrdinator.findById(id);
|
||||||
|
if (!salescoordinator) {
|
||||||
|
return res.status(400).json({ message: "SalesCoOrdinator not found" });
|
||||||
|
}
|
||||||
|
if (salescoordinator.mobileNumber === newFullMobileNumber) {
|
||||||
|
return res.status(400).json({
|
||||||
|
message:
|
||||||
|
"New mobile number cannot be the same as the old mobile number",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Check if the new mobile number already exists in another document
|
||||||
|
const existingSalesCoordinator = await SalesCoOrdinator.findOne({
|
||||||
|
mobileNumber: newFullMobileNumber,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingSalesCoordinator) {
|
||||||
|
return res.status(400).json({ message: "Mobile number already registered and verified by someone" });
|
||||||
|
}
|
||||||
|
const otp = crypto.randomInt(100000, 1000000).toString();
|
||||||
|
const otpExpires = Date.now() + 3 * 60 * 1000; // 3 minutes
|
||||||
|
|
||||||
|
salescoordinator.newMobileNumber = newFullMobileNumber;
|
||||||
|
salescoordinator.otp = otp;
|
||||||
|
salescoordinator.otpExpires = otpExpires;
|
||||||
|
|
||||||
|
await salescoordinator.save();
|
||||||
|
await sendOtp(
|
||||||
|
newFullMobileNumber,
|
||||||
|
`Your Cheminova verification OTP is: ${otp}`
|
||||||
|
);
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
message: `OTP sent to your new mobile number ${newFullMobileNumber} successfully`,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating mobile number:", error);
|
||||||
|
return res.status(500).json({
|
||||||
|
message: error.message ? error.message : "Server error!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//verify Updated Number OTP
|
||||||
|
export const verifyUpdatedMobileOtp = async (req, res) => {
|
||||||
|
const { newMobileNumber, otp } = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
let mobileNmr = newMobileNumber?.trim();
|
||||||
|
const salesCoordinator = await SalesCoOrdinator.findOne({
|
||||||
|
newMobileNumber: mobileNmr,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!salesCoordinator) {
|
||||||
|
return res.status(400).json({ message: "Invalid mobile number or OTP" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
salesCoordinator.otp !== otp ||
|
||||||
|
salesCoordinator.otpExpires < Date.now()
|
||||||
|
) {
|
||||||
|
return res.status(400).json({ message: "Invalid or expired OTP" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update sales coordinator's mobile number and verification status
|
||||||
|
salesCoordinator.mobileNumber = salesCoordinator.newMobileNumber;
|
||||||
|
salesCoordinator.newMobileNumber = undefined;
|
||||||
|
salesCoordinator.isVerified = true;
|
||||||
|
salesCoordinator.otp = undefined;
|
||||||
|
salesCoordinator.otpExpires = undefined;
|
||||||
|
|
||||||
|
// Save changes to the database
|
||||||
|
await salesCoordinator.save();
|
||||||
|
|
||||||
|
const token = salesCoordinator.getJWTToken();
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
token,
|
||||||
|
message: "Mobile number updated and verified successfully",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error verifying mobile OTP:", error);
|
||||||
|
return res.status(500).json({
|
||||||
|
message: error.message ? error.message : "Server error!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//getmyProfile
|
||||||
|
export const getmyProfile = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const myData = await SalesCoOrdinator.findById(req.salescoordinator?._id);
|
||||||
|
if (myData) {
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "feched!",
|
||||||
|
myData,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: error.message ? error.message : "Something went wrong!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
export const deleteSalesCoOrdinator = async (req, res) => {
|
||||||
|
try {
|
||||||
|
if (!req.params.id) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: "Please Provide SalesCoOrdinator ID!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const getSalesCoOrdinator = await SalesCoOrdinator.findById(req.params.id);
|
||||||
|
if (!getSalesCoOrdinator) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: "salescoordinator not Found!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------//
|
||||||
|
const salescoordinator = await SalesCoOrdinator.findByIdAndDelete(
|
||||||
|
req.params.id
|
||||||
|
);
|
||||||
|
if (!salescoordinator) {
|
||||||
|
return res.status(404).json({ message: "salescoordinator Not Found" });
|
||||||
|
}
|
||||||
|
await salescoordinator.remove();
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "salescoordinator Deleted Successfully!!",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: error.message ? error.message : "Something went wrong!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const UpdateProfile = async (req, res) => {
|
||||||
|
const { name, email } = req.body;
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
// Validate email
|
||||||
|
if (!validator.isEmail(email)) {
|
||||||
|
return res.status(400).json({ message: "Invalid email address" });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const salesCoordinator = await SalesCoOrdinator.findById(id); // Use the ID from params
|
||||||
|
|
||||||
|
if (!salesCoordinator) {
|
||||||
|
return res.status(404).json({ message: "SalesCoOrdinator not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!salesCoordinator.isVerified) {
|
||||||
|
return res.status(400).json({ message: "SalesCoOrdinator not verified" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if email is being changed and already exists
|
||||||
|
if (email && email !== salesCoordinator.email) {
|
||||||
|
const emailExists = await SalesCoOrdinator.findOne({ email });
|
||||||
|
if (emailExists && emailExists._id.toString() !== id) {
|
||||||
|
return res.status(400).json({
|
||||||
|
message:
|
||||||
|
"This Email ID is already in use by another SalesCoOrdinator",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
salesCoordinator.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update name if provided
|
||||||
|
if (name) {
|
||||||
|
salesCoordinator.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the updated salesCoordinator
|
||||||
|
await salesCoordinator.save();
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
salesCoordinator,
|
||||||
|
message: "Profile updated successfully",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
message: error.message ? error.message : "Server error!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//change SalesCoOrdinator password
|
||||||
|
export const ChangePassword = async (req, res) => {
|
||||||
|
const { id } = req.params; // Retrieve id from req.params
|
||||||
|
const { oldPassword, newPassword, confirmPassword } = req.body;
|
||||||
|
|
||||||
|
if (!oldPassword) {
|
||||||
|
return res.status(400).json({ message: "Please Enter Old password" });
|
||||||
|
}
|
||||||
|
if (!newPassword) {
|
||||||
|
return res.status(400).json({ message: "Please Enter New Password " });
|
||||||
|
}
|
||||||
|
if (!confirmPassword) {
|
||||||
|
return res.status(400).json({ message: "Please Enter Confirm Password" });
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const salescoordinator = await SalesCoOrdinator.findById(id).select(
|
||||||
|
"+password"
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!salescoordinator) {
|
||||||
|
return res.status(404).json({ message: "SalesCoOrdinator not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const isPasswordMatched = await salescoordinator.comparePassword(
|
||||||
|
oldPassword
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!isPasswordMatched) {
|
||||||
|
return res.status(400).json({ message: "Old password is incorrect" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newPassword !== confirmPassword) {
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ message: "New password and confirm Password does not match" });
|
||||||
|
}
|
||||||
|
|
||||||
|
salescoordinator.password = newPassword;
|
||||||
|
await salescoordinator.save();
|
||||||
|
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, message: "Password updated successfully" });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating password:", error);
|
||||||
|
return res.status(500).json({
|
||||||
|
message: error.message ? error.message : "Server error!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Otp = async (req, res) => {
|
||||||
|
// const { name, mobileNumber } = req.body;
|
||||||
|
try {
|
||||||
|
// let salescoordinator = await SalesCoOrdinator.findOne({ mobileNumber });
|
||||||
|
|
||||||
|
// if (salescoordinator && salescoordinator.isVerified) {
|
||||||
|
// return res.status(400).json({ message: 'SalesCoOrdinator already registered and verified for This Mobile No.' });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const otp = crypto.randomInt(100000, 1000000).toString();
|
||||||
|
|
||||||
|
// // const otp ="123456";
|
||||||
|
|
||||||
|
// const otpExpires = Date.now() + 10 * 60 * 1000; // 10 minutes
|
||||||
|
|
||||||
|
// if (salescoordinator) {
|
||||||
|
// salescoordinator.otp = otp;
|
||||||
|
// salescoordinator.otpExpires = otpExpires;
|
||||||
|
// } else {
|
||||||
|
// salescoordinator = new SalesCoOrdinator({ name, mobileNumber, otp, otpExpires });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// await salescoordinator.save();
|
||||||
|
await sendOtp();
|
||||||
|
|
||||||
|
// res.status(200).json({salescoordinator, message: `OTP ${otp} sent to your mobile number successfully` });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
message: error.message ? error.message : "Server error!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
@ -5,7 +5,7 @@ import validator from "validator";
|
|||||||
import bcrypt from "bcryptjs";
|
import bcrypt from "bcryptjs";
|
||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
const patientSchema = new mongoose.Schema(
|
const salescoordinatorSchema = new mongoose.Schema(
|
||||||
{
|
{
|
||||||
name: {
|
name: {
|
||||||
type: String,
|
type: String,
|
||||||
@ -27,61 +27,17 @@ const patientSchema = new mongoose.Schema(
|
|||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false,
|
default: false,
|
||||||
},
|
},
|
||||||
deviceAdded: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false,
|
|
||||||
},
|
|
||||||
email: {
|
email: {
|
||||||
type: String,
|
type: String,
|
||||||
required: false,
|
required: false,
|
||||||
unique: true,
|
unique: true,
|
||||||
validate: [validator.isEmail, "Please Enter a valid Email"],
|
validate: [validator.isEmail, "Please Enter a valid Email"],
|
||||||
},
|
},
|
||||||
|
|
||||||
password: {
|
password: {
|
||||||
type: String,
|
type: String,
|
||||||
minLength: [6, "Password should be greater than 6 characters"],
|
minLength: [6, "Password should be greater than 6 characters"],
|
||||||
select: false, //find not got passpord
|
select: false, //find not got passpord
|
||||||
},
|
},
|
||||||
avatar: {
|
|
||||||
public_id: {
|
|
||||||
type: String,
|
|
||||||
// required: true,
|
|
||||||
},
|
|
||||||
url: {
|
|
||||||
type: String,
|
|
||||||
// required: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
gender: {
|
|
||||||
type: String,
|
|
||||||
enum: ['Male', 'Female'],
|
|
||||||
},
|
|
||||||
|
|
||||||
weight: {
|
|
||||||
value: {
|
|
||||||
type: Number,
|
|
||||||
},
|
|
||||||
unit: {
|
|
||||||
type: String,
|
|
||||||
enum: ['Kgs', 'Lbs'],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
height: {
|
|
||||||
value: {
|
|
||||||
type: Number,
|
|
||||||
},
|
|
||||||
unit: {
|
|
||||||
type: String,
|
|
||||||
enum: ['Ft', 'Cm'],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
age: Number,
|
|
||||||
commonHealthComplaints: String,
|
|
||||||
familyHistory: String,
|
|
||||||
personalHistory: String,
|
|
||||||
dailyRoutine: String,
|
|
||||||
newMobileNumber: { type: String },
|
newMobileNumber: { type: String },
|
||||||
resetPasswordToken: String,
|
resetPasswordToken: String,
|
||||||
resetPasswordExpire: Date,
|
resetPasswordExpire: Date,
|
||||||
@ -89,7 +45,7 @@ const patientSchema = new mongoose.Schema(
|
|||||||
{ timestamps: true }
|
{ timestamps: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
patientSchema.pre("save", async function (next) {
|
salescoordinatorSchema.pre("save", async function (next) {
|
||||||
if (!this.isModified("password")) {
|
if (!this.isModified("password")) {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
@ -98,21 +54,21 @@ patientSchema.pre("save", async function (next) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// JWT TOKEN
|
// JWT TOKEN
|
||||||
patientSchema.methods.getJWTToken = function () {
|
salescoordinatorSchema.methods.getJWTToken = function () {
|
||||||
return jwt.sign({ id: this._id }, process.env.JWT_SECRET);
|
return jwt.sign({ id: this._id }, process.env.JWT_SECRET);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Compare Password
|
// Compare Password
|
||||||
patientSchema.methods.comparePassword = async function (password) {
|
salescoordinatorSchema.methods.comparePassword = async function (password) {
|
||||||
return await bcrypt.compare(password, this.password);
|
return await bcrypt.compare(password, this.password);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Generating Reset Token
|
// Generating Reset Token
|
||||||
patientSchema.methods.getResetPasswordToken = function () {
|
salescoordinatorSchema.methods.getResetPasswordToken = function () {
|
||||||
// Generating Token
|
// Generating Token
|
||||||
const resetToken = crypto.randomBytes(20).toString("hex");
|
const resetToken = crypto.randomBytes(20).toString("hex");
|
||||||
|
|
||||||
// Hashing and adding reset PasswordToken to patientSchema
|
// Hashing and adding reset PasswordToken to salescoordinatorSchema
|
||||||
this.resetPasswordToken = crypto
|
this.resetPasswordToken = crypto
|
||||||
.createHash("sha256")
|
.createHash("sha256")
|
||||||
.update(resetToken)
|
.update(resetToken)
|
||||||
@ -124,9 +80,9 @@ patientSchema.methods.getResetPasswordToken = function () {
|
|||||||
return resetToken;
|
return resetToken;
|
||||||
};
|
};
|
||||||
|
|
||||||
const Patient = mongoose.model("Patient", patientSchema);
|
const SalesCoOrdinator = mongoose.model(
|
||||||
|
"SalesCoOrdinator",
|
||||||
export default Patient;
|
salescoordinatorSchema
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
export default SalesCoOrdinator;
|
94
resources/SalesCoOrdinators/SalesCoOrdinatorRoute.js
Normal file
94
resources/SalesCoOrdinators/SalesCoOrdinatorRoute.js
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
import express from "express";
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
import {
|
||||||
|
Otp,
|
||||||
|
deleteSalesCoOrdinator,
|
||||||
|
forgotPassword,
|
||||||
|
getAllSalesCoOrdinator,
|
||||||
|
loginSalesCoOrdinator,
|
||||||
|
register,
|
||||||
|
updateMobileNumber,
|
||||||
|
verifyUpdatedMobileOtp,
|
||||||
|
verifyOtp,
|
||||||
|
UpdateProfile,
|
||||||
|
getmyProfile,
|
||||||
|
ChangePassword,
|
||||||
|
getOneSalesCoOrdinator,
|
||||||
|
} from "./SalesCoOrdinatorController.js";
|
||||||
|
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
|
||||||
|
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
||||||
|
|
||||||
|
router.post("/register", register);
|
||||||
|
router.post("/verify-otp", verifyOtp);
|
||||||
|
router.post("/login", loginSalesCoOrdinator);
|
||||||
|
|
||||||
|
//admin
|
||||||
|
router.get(
|
||||||
|
"/getAll",
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
getAllSalesCoOrdinator
|
||||||
|
);
|
||||||
|
router.get(
|
||||||
|
"/getOne/:id",
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
getOneSalesCoOrdinator
|
||||||
|
);
|
||||||
|
router.get("/my-profile", isAuthenticatedSalesCoOrdinator, getmyProfile);
|
||||||
|
|
||||||
|
//Update Mobile Number
|
||||||
|
router.post(
|
||||||
|
"/update-mobile-number/:id",
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
updateMobileNumber
|
||||||
|
);
|
||||||
|
router.post(
|
||||||
|
"/update-mobile-number/:id",
|
||||||
|
isAuthenticatedSalesCoOrdinator,
|
||||||
|
updateMobileNumber
|
||||||
|
);
|
||||||
|
router.post(
|
||||||
|
"/verify-updated-mobile-otp",
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
verifyUpdatedMobileOtp
|
||||||
|
);
|
||||||
|
router.post(
|
||||||
|
"/verify-updated-mobile-otp",
|
||||||
|
isAuthenticatedSalesCoOrdinator,
|
||||||
|
verifyUpdatedMobileOtp
|
||||||
|
);
|
||||||
|
router.post("/forgot-password", forgotPassword);
|
||||||
|
router.patch(
|
||||||
|
"/profile/update/:id",
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
UpdateProfile
|
||||||
|
);
|
||||||
|
router.patch(
|
||||||
|
"/profile/update/:id",
|
||||||
|
isAuthenticatedSalesCoOrdinator,
|
||||||
|
UpdateProfile
|
||||||
|
);
|
||||||
|
//change password
|
||||||
|
router.put(
|
||||||
|
"/password/update/:id",
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
ChangePassword
|
||||||
|
);
|
||||||
|
router.put("/password/update/:id", isAuthenticatedSalesCoOrdinator, ChangePassword);
|
||||||
|
//delete SalesCoOrdinator
|
||||||
|
router.delete(
|
||||||
|
"/delete/:id",
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
deleteSalesCoOrdinator
|
||||||
|
);
|
||||||
|
|
||||||
|
router.get("/otp", Otp);
|
||||||
|
|
||||||
|
export default router;
|
@ -1,292 +0,0 @@
|
|||||||
import { Support } from "./supportModel.js";
|
|
||||||
import cloudinary from "../../Utils/cloudinary.js";
|
|
||||||
|
|
||||||
export const createSupport = async (req, res) => {
|
|
||||||
// console.log(req.body);
|
|
||||||
// console.log(req.files.image);
|
|
||||||
try {
|
|
||||||
// const { ticketId, createdOn, subject, description } = req.body;
|
|
||||||
if(req.files && req.files.image){
|
|
||||||
let images = [];
|
|
||||||
let Allfiles = req.files.image;
|
|
||||||
if (typeof Allfiles.tempFilePath === "string") {
|
|
||||||
let filepath = Allfiles.tempFilePath;
|
|
||||||
|
|
||||||
images.push(filepath);
|
|
||||||
} else {
|
|
||||||
Allfiles.map((item) => {
|
|
||||||
images.push(item.tempFilePath);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const imagesLinks = [];
|
|
||||||
for (let i = 0; i < images.length; i++) {
|
|
||||||
const result = await cloudinary.v2.uploader.upload(images[i], {
|
|
||||||
folder: "smellica/CustomerSupport",
|
|
||||||
});
|
|
||||||
|
|
||||||
imagesLinks.push({
|
|
||||||
public_id: result.public_id,
|
|
||||||
url: result.secure_url,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
req.body.image = imagesLinks;
|
|
||||||
}
|
|
||||||
req.body.addedBy = req.user._id;
|
|
||||||
// Check if any required field is missing
|
|
||||||
// if (!ticketId || !createdOn || !subject || !description) {
|
|
||||||
// return res.status(400).json({
|
|
||||||
// success: false,
|
|
||||||
// msg: "All fields are required.",
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Create the support ticket
|
|
||||||
// const support = await Support.create({
|
|
||||||
// ticketId,
|
|
||||||
// createdOn,
|
|
||||||
// subject,
|
|
||||||
// description,
|
|
||||||
// addedBy: req.user._id,
|
|
||||||
// image: imagesLinks,
|
|
||||||
// });
|
|
||||||
const support = await Support.create({ ...req.body });
|
|
||||||
// Return the created support ticket
|
|
||||||
res.status(201).json({
|
|
||||||
success: true,
|
|
||||||
data: support,
|
|
||||||
msg: "Support ticket created successfully.",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
// Handle errors
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
msg: error.message,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// ****************************
|
|
||||||
|
|
||||||
export const getAllSupportTicket = async (req, res) => {
|
|
||||||
try {
|
|
||||||
// Use the find method to retrieve all support tickets
|
|
||||||
const support = await Support.find().sort({ createdAt: -1 });
|
|
||||||
|
|
||||||
// Check if support tickets were found
|
|
||||||
if (support) {
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
support,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// Handle errors
|
|
||||||
// console.error(error);
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
msg: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
export const getOneSupportTicket = async (req, res) => {
|
|
||||||
try {
|
|
||||||
// console.log(req.params.id);
|
|
||||||
const support = await Support.findOne({ ticketId: req.params.id });
|
|
||||||
if (support) {
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
support,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return res.status(404).json({
|
|
||||||
success: false,
|
|
||||||
msg: "Support ticket not found",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
msg: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// ************************8
|
|
||||||
|
|
||||||
export const getAllSupportTicketofuser = async (req, res) => {
|
|
||||||
try {
|
|
||||||
// Retrieve the user ID from the request
|
|
||||||
const userId = req.user._id;
|
|
||||||
|
|
||||||
// Use the find method to retrieve all support tickets created by the user
|
|
||||||
const support = await Support.find({ addedBy: userId }).sort({
|
|
||||||
createdAt: -1,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Check if support tickets were found
|
|
||||||
if (support) {
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
support,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return res.status(404).json({
|
|
||||||
success: false,
|
|
||||||
msg: "No support tickets found for the user.",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// Handle errors
|
|
||||||
// console.error(error);
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
msg: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// ************************8
|
|
||||||
|
|
||||||
export const deleteSupport = async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req.params.id) {
|
|
||||||
return res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
msg: "Please Provide Support ID!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// console.log(req.params.id);
|
|
||||||
const getSupport = await Support.findById(req.params.id);
|
|
||||||
if (!getSupport) {
|
|
||||||
return res.status(404).json({
|
|
||||||
success: false,
|
|
||||||
msg: "Support not Found!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Deleting Images From Cloudinary
|
|
||||||
for (let i = 0; i < getSupport.image.length; i++) {
|
|
||||||
await cloudinary.v2.uploader.destroy(getSupport.image[i].public_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------//
|
|
||||||
const supportticket = await Support.findByIdAndDelete(req.params.id);
|
|
||||||
if (!supportticket) {
|
|
||||||
return res.status(404).json({ message: "Support Not Found" });
|
|
||||||
}
|
|
||||||
await supportticket.remove();
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Support Deleted Successfully!!",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
msg: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const updateSupport = async (req, res) => {
|
|
||||||
try {
|
|
||||||
const { status, message } = req.body;
|
|
||||||
// console.log(req.params.id);
|
|
||||||
// Prepare an array for the images
|
|
||||||
// const jsonArray = JSON.parse(image);
|
|
||||||
// const AllImages = jsonArray.map(({ public_id, url }) => ({
|
|
||||||
// public_id,
|
|
||||||
// url,
|
|
||||||
// }));
|
|
||||||
|
|
||||||
// if (req.files && req.files.newImages) {
|
|
||||||
// const newuploadImages = Array.isArray(req.files.newImages)
|
|
||||||
// ? req.files.newImages
|
|
||||||
// : [req.files.newImages];
|
|
||||||
|
|
||||||
// const imagesLinks = [];
|
|
||||||
|
|
||||||
// for (let i = 0; i < newuploadImages.length; i++) {
|
|
||||||
// const result = await cloudinary.v2.uploader.upload(
|
|
||||||
// newuploadImages[i].tempFilePath,
|
|
||||||
// {
|
|
||||||
// folder: "smellica/product",
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
|
|
||||||
// imagesLinks.push({
|
|
||||||
// public_id: result.public_id,
|
|
||||||
// url: result.secure_url,
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Combine the existing images and the newly uploaded images
|
|
||||||
// const updatedImages = [...AllImages, ...imagesLinks];
|
|
||||||
|
|
||||||
// Perform the product update
|
|
||||||
// Find the support ticket by ID
|
|
||||||
const supportTicket = await Support.findOne({ ticketId: req.params.id });
|
|
||||||
// Check if the support ticket exists
|
|
||||||
if (!supportTicket) {
|
|
||||||
return res.status(404).json({
|
|
||||||
success: false,
|
|
||||||
msg: "Support ticket not found",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the support ticket fields
|
|
||||||
if (status) {
|
|
||||||
supportTicket.status = status;
|
|
||||||
}
|
|
||||||
if (message) {
|
|
||||||
const newMessage = {
|
|
||||||
message: message.message,
|
|
||||||
user: message.user,
|
|
||||||
replyDate: message.replyDate, // Add a timestamp to the message object
|
|
||||||
};
|
|
||||||
supportTicket.message.push(newMessage);
|
|
||||||
// Update the last reply to the timestamp of the new message if the user is admin
|
|
||||||
if (message.user === "admin") {
|
|
||||||
supportTicket.lastreply = newMessage.replyDate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the updated support ticket
|
|
||||||
const updatedSupportTicket = await supportTicket.save();
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
updatedSupportTicket,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
// Handle errors
|
|
||||||
// console.error(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: "CustomerSupport Deleted Successfully!!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
msg: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,67 +0,0 @@
|
|||||||
import mongoose from "mongoose";
|
|
||||||
const { Schema, model } = mongoose;
|
|
||||||
const supportSchema = new Schema(
|
|
||||||
{
|
|
||||||
ticketId: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
unique: true,
|
|
||||||
},
|
|
||||||
addedBy: {
|
|
||||||
type: Schema.Types.ObjectId,
|
|
||||||
ref: "User",
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
subject: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
description: {
|
|
||||||
type: String,
|
|
||||||
maxLength: [100, "description cannot exceed 100 characters"],
|
|
||||||
required: [true, "Please Enter product Description"],
|
|
||||||
},
|
|
||||||
createdOn: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
lastreply: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
status: {
|
|
||||||
type: String,
|
|
||||||
enum: ["Open", "Close"],
|
|
||||||
default: "Open",
|
|
||||||
},
|
|
||||||
image: [
|
|
||||||
{
|
|
||||||
public_id: {
|
|
||||||
type: String,
|
|
||||||
// required: true,
|
|
||||||
},
|
|
||||||
url: {
|
|
||||||
type: String,
|
|
||||||
// required: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
message: [
|
|
||||||
{
|
|
||||||
message: {
|
|
||||||
type: String,
|
|
||||||
default: "",
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
type: String,
|
|
||||||
enum: ["admin", "user"],
|
|
||||||
default: "user",
|
|
||||||
},
|
|
||||||
replyDate: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{ timestamps: true, versionKey: false }
|
|
||||||
);
|
|
||||||
|
|
||||||
export const Support = model("Support", supportSchema);
|
|
@ -1,46 +0,0 @@
|
|||||||
import bodyParser from "body-parser";
|
|
||||||
import {
|
|
||||||
createSupport,
|
|
||||||
deleteImageFromCloudinary,
|
|
||||||
deleteSupport,
|
|
||||||
getAllSupportTicket,
|
|
||||||
getAllSupportTicketofuser,
|
|
||||||
getOneSupportTicket,
|
|
||||||
updateSupport,
|
|
||||||
} from "./supportController.js";
|
|
||||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
|
||||||
import express from "express";
|
|
||||||
|
|
||||||
const app = express();
|
|
||||||
|
|
||||||
// Configure bodyParser to parse the raw request body as a buffer
|
|
||||||
app.use(bodyParser.raw({ type: "application/json" }));
|
|
||||||
|
|
||||||
const router = express.Router();
|
|
||||||
//checkout Routes-------------------------//
|
|
||||||
router.route("/support/create/").post(isAuthenticatedUser, createSupport);
|
|
||||||
router
|
|
||||||
.route("/support/getAll/")
|
|
||||||
.get(
|
|
||||||
isAuthenticatedUser,
|
|
||||||
authorizeRoles("admin", "Employee"),
|
|
||||||
getAllSupportTicket
|
|
||||||
);
|
|
||||||
router
|
|
||||||
.route("/support/userticket/")
|
|
||||||
.get(isAuthenticatedUser, getAllSupportTicketofuser);
|
|
||||||
router.route("/support/delete/:id").delete(deleteSupport);
|
|
||||||
router
|
|
||||||
.route("/support/getOne/:id")
|
|
||||||
.get(isAuthenticatedUser, getOneSupportTicket);
|
|
||||||
router.route("/support/update/:id").patch(isAuthenticatedUser, updateSupport);
|
|
||||||
router
|
|
||||||
.route("/support/deleteImage/jatinMor/CustomerSupport/:public_id")
|
|
||||||
.delete(
|
|
||||||
isAuthenticatedUser,
|
|
||||||
authorizeRoles("admin", "Employee"),
|
|
||||||
deleteImageFromCloudinary
|
|
||||||
);
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
|
|
||||||
export default router;
|
|
@ -256,88 +256,63 @@ const getConfig = async (req, res) => {
|
|||||||
// add logo
|
// add logo
|
||||||
const addLogo = async (req, res) => {
|
const addLogo = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const configuration = await Config.find();
|
const configuration = await Config.findOne();
|
||||||
|
let result1, result2, result3;
|
||||||
|
const folderPath = "Logo";
|
||||||
// console.log(req.files);
|
// console.log(req.files);
|
||||||
// console.log(configuration[0].logo);
|
// Upload new logos if they are present in the request
|
||||||
// console.log(configuration[0]);
|
|
||||||
// console.log(configuration[0].logo[0].Headerlogo);
|
|
||||||
// console.log(configuration[0].logo[0].Footerlogo);
|
|
||||||
// console.log(configuration[0].logo[0].Adminlogo);
|
|
||||||
|
|
||||||
let result1;
|
|
||||||
let result2;
|
|
||||||
let result3;
|
|
||||||
// console.log(req.files.Headerlogo)
|
|
||||||
if (req.files.Headerlogo) {
|
if (req.files.Headerlogo) {
|
||||||
const result = await cloudinary.v2.uploader.upload(
|
const result = await cloudinary.v2.uploader.upload(
|
||||||
req.files.Headerlogo.tempFilePath,
|
req.files.Headerlogo.tempFilePath,
|
||||||
{ folder: "bolo/Logo" }
|
{ folder: folderPath }
|
||||||
);
|
);
|
||||||
result1 = result.secure_url;
|
result1 = result.secure_url;
|
||||||
|
// Delete old logo if a new one is uploaded
|
||||||
|
if (configuration && configuration.logo[0].Headerlogo) {
|
||||||
|
const fullPublicId = configuration.logo[0].Headerlogo.match(
|
||||||
|
/\/(Logo\/.*)\.[a-z]+$/i
|
||||||
|
)[1];
|
||||||
|
await cloudinary.v2.uploader.destroy(fullPublicId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (req.files.Footerlogo) {
|
if (req.files.Footerlogo) {
|
||||||
const result = await cloudinary.v2.uploader.upload(
|
const result = await cloudinary.v2.uploader.upload(
|
||||||
req.files.Footerlogo.tempFilePath,
|
req.files.Footerlogo.tempFilePath,
|
||||||
{ folder: "bolo/Logo" }
|
{ folder: folderPath }
|
||||||
);
|
);
|
||||||
result2 = result.secure_url;
|
result2 = result.secure_url;
|
||||||
|
// Delete old logo if a new one is uploaded
|
||||||
|
if (configuration && configuration.logo[0].Footerlogo) {
|
||||||
|
const fullPublicId = configuration.logo[0].Footerlogo.match(
|
||||||
|
/\/(Logo\/.*)\.[a-z]+$/i
|
||||||
|
)[1];
|
||||||
|
await cloudinary.v2.uploader.destroy(fullPublicId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (req.files.Adminlogo) {
|
if (req.files.Adminlogo) {
|
||||||
// console.log(req.files.Adminlogo.path)
|
|
||||||
const result = await cloudinary.v2.uploader.upload(
|
const result = await cloudinary.v2.uploader.upload(
|
||||||
req.files.Adminlogo.tempFilePath,
|
req.files.Adminlogo.tempFilePath,
|
||||||
{ folder: "bolo/Logo" }
|
{ folder: folderPath }
|
||||||
);
|
);
|
||||||
|
// console.log(result);
|
||||||
result3 = result.secure_url;
|
result3 = result.secure_url;
|
||||||
|
// Delete old logo if a new one is uploaded
|
||||||
|
if (configuration && configuration.logo[0].Adminlogo) {
|
||||||
|
const fullPublicId = configuration.logo[0].Adminlogo.match(
|
||||||
|
/\/(Logo\/.*)\.[a-z]+$/i
|
||||||
|
)[1];
|
||||||
|
// console.log(fullPublicId);
|
||||||
|
await cloudinary.v2.uploader.destroy(fullPublicId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log(result1);
|
if (!configuration) {
|
||||||
// console.log(result2);
|
|
||||||
// console.log(result3);
|
|
||||||
|
|
||||||
// if (configuration.length === 0) {
|
|
||||||
// const createLogo = await Config.create({
|
|
||||||
// logo: {
|
|
||||||
// Headerlogo: result1,
|
|
||||||
// Footerlogo: result2,
|
|
||||||
// Adminlogo: result3,
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
|
|
||||||
// if (createLogo) {
|
|
||||||
// return res.status(200).json({
|
|
||||||
// status: "success",
|
|
||||||
// message: "Created Logos Successfully",
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// const updateLogo = await Config.updateOne(
|
|
||||||
// {},
|
|
||||||
// {
|
|
||||||
// $set: {
|
|
||||||
// logo: {
|
|
||||||
// Headerlogo: result1,
|
|
||||||
// Footerlogo: result2,
|
|
||||||
// Adminlogo: result3,
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
// if (updateLogo) {
|
|
||||||
// return res.status(200).json({
|
|
||||||
// status: "success",
|
|
||||||
// message: "Updated Logos Successfully",
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
if (configuration.length === 0) {
|
|
||||||
// If no configuration exists, create a new one with uploaded logos
|
// If no configuration exists, create a new one with uploaded logos
|
||||||
const createLogo = await Config.create({
|
const createLogo = await Config.create({
|
||||||
logo: {
|
logo: {
|
||||||
Headerlogo: result1 || configuration[0].logo[0].Headerlogo,
|
Headerlogo: result1 || null,
|
||||||
Footerlogo: result2 || configuration[0].logo[0].Footerlogo,
|
Footerlogo: result2 || null,
|
||||||
Adminlogo: result3 || configuration[0].logo[0].Adminlogo,
|
Adminlogo: result3 || null,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -351,20 +326,16 @@ const addLogo = async (req, res) => {
|
|||||||
// If configuration exists, update only the logos that are present in the current request
|
// If configuration exists, update only the logos that are present in the current request
|
||||||
const updatedLogos = {
|
const updatedLogos = {
|
||||||
Headerlogo:
|
Headerlogo:
|
||||||
result1 !== undefined ? result1 : configuration[0].logo[0].Headerlogo,
|
result1 !== undefined ? result1 : configuration.logo[0].Headerlogo,
|
||||||
Footerlogo:
|
Footerlogo:
|
||||||
result2 !== undefined ? result2 : configuration[0].logo[0].Footerlogo,
|
result2 !== undefined ? result2 : configuration.logo[0].Footerlogo,
|
||||||
Adminlogo:
|
Adminlogo:
|
||||||
result3 !== undefined ? result3 : configuration[0].logo[0].Adminlogo,
|
result3 !== undefined ? result3 : configuration.logo[0].Adminlogo,
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateLogo = await Config.updateOne(
|
const updateLogo = await Config.updateOne(
|
||||||
{},
|
{},
|
||||||
{
|
{ $set: { logo: updatedLogos } }
|
||||||
$set: {
|
|
||||||
logo: updatedLogos,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (updateLogo) {
|
if (updateLogo) {
|
||||||
@ -376,6 +347,10 @@ const addLogo = async (req, res) => {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
return res.status(500).json({
|
||||||
|
status: "error",
|
||||||
|
message: "An error occurred while updating logos.",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user