franchisee edit

This commit is contained in:
pawan-dot 2023-02-16 11:56:02 +05:30
parent 55fd1c72f7
commit b2ec37a50b
6 changed files with 352 additions and 8 deletions

6
app.js
View File

@ -31,8 +31,8 @@ import orderRoute from './resources/Orders/orderRoute.js'
app.use("/api", orderRoute); app.use("/api", orderRoute);
//Franchisee //Franchisee
import FranchiseeRouter from "./resources/Franchisee/FranchiseeRoute.js"; import FranchiseeRouter from "./resources/Temple/FranchiseeRoute.js";
app.use("/api/franchisee", FranchiseeRouter); app.use("/api/franchisee/", FranchiseeRouter);
//state //state
import StateRouter from "./resources/setting/state/state_routes.js"; import StateRouter from "./resources/setting/state/state_routes.js";
app.use("/api/state", StateRouter); app.use("/api/state", StateRouter);
@ -46,6 +46,6 @@ app.use("/api/tax", TaxRouter);
//config //config
import ConfigRouter from "./resources/setting/Configration/Config_routes.js"; import ConfigRouter from "./resources/setting/Configration/Config_routes.js";
app.use("/api/config", ConfigRouter); app.use("/api/config", ConfigRouter)
export default app; export default app;

View File

@ -1,10 +1,10 @@
import User from "../resources/user/userModel.js"; import User from "../resources/user/userModel.js";
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import ErrorHander from "../Utils/errorhander.js" import ErrorHander from "../Utils/errorhander.js"
import { Franchisee } from "../resources/Temple/FranchiseeModel.js";
export const isAuthenticatedUser = async (req, res, next) => { export const isAuthenticatedUser = async (req, res, next) => {
try { try {
if (!req.headers.authorization) { if (!req.headers.authorization) {
return res.status(400).json({ return res.status(400).json({
success: false, success: false,
@ -36,6 +36,49 @@ export const isAuthenticatedUser = async (req, res, next) => {
}); });
} }
}; };
export const isFranchiAuthenticated = async (req, res, next) => {
try {
if (!req.headers.authorization) {
return res.status(400).json({
success: false,
message: "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",
});
}
// console.log(frontdecoded)
const fuser = await Franchisee.findById(frontdecoded.id);
req.franchi = fuser;
next();
} catch (error) {
return res.status(400).json({
success: false,
message: error.message,
});
}
};
export const authorizeRoles = (...roles) => {//pass admin export const authorizeRoles = (...roles) => {//pass admin
return (req, res, next) => { return (req, res, next) => {
if (!roles.includes(req.user.role)) { if (!roles.includes(req.user.role)) {

View File

@ -15,7 +15,10 @@ const FranchiseeSchema = new Schema(
}, email: { }, email: {
type: String, type: String,
required: [true, "Please Enter Your Email"], required: [true, "Please Enter Your Email"],
unique: true,
unique: [true, "Email already exist ! please try with diffent email"],
validate: [validator.isEmail, "Please Enter a valid Email"], validate: [validator.isEmail, "Please Enter a valid Email"],
}, },
password: { password: {
@ -42,6 +45,11 @@ const FranchiseeSchema = new Schema(
pin_Code: { type: Number, required: true }, pin_Code: { type: Number, required: true },
short_url: { type: String, default: "" }, short_url: { type: String, default: "" },
banner: { type: Object, default: { url: "", public_id: "" } }, banner: { type: Object, default: { url: "", public_id: "" } },
verify: {
type: Boolean,
required: true,
default: false
}
}, },
{ timestamps: true } { timestamps: true }
); );

View File

@ -12,13 +12,24 @@ import {
addProductToFranchisee, addProductToFranchisee,
// addGradeToFranchisee, // addGradeToFranchisee,
getFranchiseeByIdPopulated, getFranchiseeByIdPopulated,
FranchiseeLogin,
franchiseeForgotPassword,
franchiseeUpdatePassword,
getFransiDetails,
EditFranchiseeProfile,
} from "./Franchisee_controller.js"; } from "./Franchisee_controller.js";
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js"; import { authorizeRoles, isAuthenticatedUser, isFranchiAuthenticated } from "../../middlewares/auth.js";
import { FranchiseeVarificationFromAdmin } from "./Franchisee_controller.js";
import { FranchiseePriceLevelProduct } from "./Franchisee_controller.js";
router.get("/", getAllFranchisees); router.get("/", getAllFranchisees);
router.get("/withpopulate", isAuthenticatedUser, getAllFranchiseesPopulated); router.get("/withpopulate", isAuthenticatedUser, getAllFranchiseesPopulated);
// router.get("/withpopulate/:option", getAllFranchiseesPopulatedWithOption); // router.get("/withpopulate/:option", getAllFranchiseesPopulatedWithOption);
router.get("/withoutpopulate/:id", isAuthenticatedUser, getFranchiseeByIdWithoutPopulate); router.get("/withoutpopulate/:id", isAuthenticatedUser, getFranchiseeByIdWithoutPopulate);
router.get("/:id", isAuthenticatedUser, getFranchiseeById); router.get("/:id", isAuthenticatedUser, getFranchiseeById);
router.get("/arrayspopulate/:id", getFranchiseeByIdPopulated); router.get("/arrayspopulate/:id", getFranchiseeByIdPopulated);
router.post("/", isAuthenticatedUser, authorizeRoles("admin"), addFranchisee); router.post("/", isAuthenticatedUser, authorizeRoles("admin"), addFranchisee);
@ -26,5 +37,26 @@ router.patch("/product/:id", isAuthenticatedUser, addProductToFranchisee);
// router.patch("/grade/:id", addGradeToFranchisee); // router.patch("/grade/:id", addGradeToFranchisee);
router.patch("/:id", isAuthenticatedUser, authorizeRoles("admin"), updateFranchisee); router.patch("/:id", isAuthenticatedUser, authorizeRoles("admin"), updateFranchisee);
router.delete("/:id", isAuthenticatedUser, authorizeRoles("admin"), deleteFranchiseeById); router.delete("/:id", isAuthenticatedUser, authorizeRoles("admin"), deleteFranchiseeById);
//varify
router.get("/admin/verify/:id", isAuthenticatedUser, authorizeRoles("admin"), FranchiseeVarificationFromAdmin);
// ---------franchisee Auth ----------------------////////
franchiseeForgotPassword
router.post("/login", FranchiseeLogin);
router.post("/password/forgot", franchiseeForgotPassword)
router.get("/getDetails/me", isFranchiAuthenticated, getFransiDetails);
router.patch("/edit/self", isFranchiAuthenticated, EditFranchiseeProfile);
router.route("/password/update").put(isFranchiAuthenticated, franchiseeUpdatePassword);
//fetch product franchisee Wise
router.route("/product/price_level").get(isFranchiAuthenticated, FranchiseePriceLevelProduct);
export default router; export default router;

View File

@ -6,6 +6,7 @@ import bcrypt from "bcryptjs"
import password from 'secure-random-password' import password from 'secure-random-password'
import mongoose from "mongoose"; import mongoose from "mongoose";
import { Product } from "../Products/ProductModel.js";
const addFranchisee = async (req, res) => { const addFranchisee = async (req, res) => {
const image_file = req?.files?.image; const image_file = req?.files?.image;
@ -47,6 +48,7 @@ const addFranchisee = async (req, res) => {
}) })
req.body.password = passwords; req.body.password = passwords;
req.user.role === 'admin' ? req.body.verify = true : req.body.verify = false
const entity = await Franchisee.create(req.body); const entity = await Franchisee.create(req.body);
await sendEmail({ await sendEmail({
@ -96,7 +98,7 @@ const getFranchiseeById = async (req, res) => {
.populate("products"); .populate("products");
return res.status(200).json({ status: "OK", data: entity }); return res.status(200).json({ status: "OK", data: entity });
} catch (err) { } catch (err) {
console.log(err); // console.log(err);
return res.status(500).json({ message: "Unable to get menu items." }); return res.status(500).json({ message: "Unable to get menu items." });
} }
}; };
@ -191,6 +193,8 @@ const updateFranchisee = async (req, res) => {
}) })
req.body.password = await bcrypt.hash(passwords, 12) req.body.password = await bcrypt.hash(passwords, 12)
req.user.role === 'admin' ? req.body.verify = true : req.body.verify = false
const entity = await Franchisee.findByIdAndUpdate(req.params.id, req.body); const entity = await Franchisee.findByIdAndUpdate(req.params.id, req.body);
await sendEmail({ await sendEmail({
@ -211,7 +215,7 @@ const updateFranchisee = async (req, res) => {
console.log(err); console.log(err);
if (req?.files?.image?.tempFilePath) if (req?.files?.image?.tempFilePath)
fs.unlinkSync(image_file?.tempFilePath); fs.unlinkSync(image_file?.tempFilePath);
return res.status(500).json({ message: "Unable to create." }); return res.status(500).json({ message: err.message ? err.message : "Unable to Update." });
} }
}; };
@ -259,8 +263,264 @@ const findFranchiseeByURL = async (req, res) => {
return res.status(500).json({ message: "Franchisee not found" }); return res.status(500).json({ message: "Franchisee not found" });
} }
}; };
//------------------------------Franchisee Auth----------------------//
export const FranchiseeLogin = async (req, res) => {
try {
const { email, password } = req.body;
const franchisee = await Franchisee.findOne({ email })
.select("+password")
if (!franchisee) {
return res.status(400).json({
success: false,
message: "Franchisee does not exist",
});
}
const isMatch = await franchisee.comparePassword(password);
if (!isMatch) {
return res.status(400).json({
success: false,
message: "Incorrect password",
});
}
const token = await franchisee.getJWTToken();
res.status(200).json({
success: true,
franchisee,
token,
});
} catch (error) {
res.status(500).json({
success: false,
message: error.message,
});
}
};
//6.Get User Detail
export const getFransiDetails = async (req, res) => {
try {
const franchisee = await Franchisee.findById(req.franchi._id);
res.status(200).json({
success: true,
franchisee,
});
}
catch (error) {
res.status(500).json({
success: false,
message: error.message,
})
}
};
export const franchiseeUpdatePassword = async (req, res) => {
try {
const franchisee = await Franchisee.findById(req.franchi._id).select("+password");
const { oldPassword, newPassword } = req.body;
if (!oldPassword || !newPassword) {
return res.status(400).json({
success: false,
message: "Please provide old and new password",
});
}
const isMatch = await franchisee.comparePassword(oldPassword);
if (!isMatch) {
return res.status(400).json({
success: false,
message: "Incorrect Old password",
});
}
franchisee.password = newPassword;
await franchisee.save();
res.status(200).json({
success: true,
message: "Password Updated",
});
} catch (error) {
res.status(500).json({
success: false,
message: error.message,
});
}
};
export const franchiseeForgotPassword = async (req, res, next) => {
const franchisee = await Franchisee.findOne({ email: req.body.email });
if (!franchisee) {
return res.status(404).json({ message: "Franchisee not found" });
}
const passwords = password.randomPassword({
length: 12,
characters: [
{ characters: password.upper, exactly: 1 },
{ characters: password.symbols, exactly: 1 },
password.lower,
password.digits]
})
franchisee.password = passwords;
// req.body.password = await bcrypt.hash(passwords, 12)
await franchisee.save()
try {
await sendEmail({
to: `${franchisee.email}`, // Change to your recipient
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
subject: `ATP Franchisee 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: `Password sent to ${franchisee.email} successfully`,
});
} catch (error) {
return res.status(500).json({ message: error?.message || "Something went wrong!" });
}
}
//edit franchi profile self
export const EditFranchiseeProfile = async (req, res, next) => {
const image_file = req?.files?.image;
try {
const FranchiseeWithURL = await Franchisee.findOne({
short_url: req.body?.short_url,
});
if (
FranchiseeWithURL?._id &&
FranchiseeWithURL?._id?.toString() !== req.franchi._id.toString()
) {
if (req?.files?.image?.tempFilePath)
fs.unlinkSync(image_file?.tempFilePath);
return res.status(400).json({ message: "Franchisee URL is not available!" });
}
const getFranchisee = await Franchisee.findById(req.franchi._id);
if (image_file?.tempFilePath) {
if (getFranchisee?.banner) {
const imageId = getFranchisee?.banner?.public_id;
await cloudinary.uploader.destroy(imageId)
}
const result = await cloudinary.v2.uploader.upload(
image_file?.tempFilePath,
{
folder: "ATP/Franchisee_banners",
}
);
const image = { url: result?.secure_url, public_id: result?.public_id };
req.body.banner = image;
fs.unlinkSync(image_file?.tempFilePath);
await cloudinary.v2.uploader.destroy(getFranchisee.banner.public_id);
}
req.body.verify = false
const entity = await Franchisee.findByIdAndUpdate(req.franchi._id, req.body);
return res.status(200).json({
success: true,
data: entity,
message: `Franchisee Edited successfully and varified by admin soon !`,
});
} catch (err) {
console.log(err);
if (req?.files?.image?.tempFilePath)
fs.unlinkSync(image_file?.tempFilePath);
return res.status(500).json({ message: err.message });
}
}
//franchisee varification
export const FranchiseeVarificationFromAdmin = async (req, res) => {
try {
const getFranchisee = await Franchisee.findById(req.params.id);
if (getFranchisee.verify === false) {
if (req.user.role === 'admin') {
getFranchisee.verify = true
getFranchisee.save()
await sendEmail({
to: `${getFranchisee.email}`, // Change to your recipient
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
subject: `Cakestick Franchisee Varification`,
html: `your franchisee ${getFranchisee.name} is Varified !!<br/><br/><h3>Thank You</h3>`
});
return res.status(200).json({
success: true,
message: `Franchisee varify successfully`,
});
}
else {
return res.status(400).json({
success: false,
message: `Franchisee varification failled! only admin can varify, please try with admin account `,
});
}
} else {
return res.status(400).json({
success: false,
message: `Franchisee Allready Varified`,
});
}
} catch (err) {
// console.log(err)
return res.status(500).json({ message: err.message ? err.message : "Something went wrong." });
}
};
//filter franchisee wise product
export const FranchiseePriceLevelProduct = async (req, res) => {
try {
const getFranchisee = await Franchisee.findById(req.franchi._id);
// console.log(getFranchisee.price_Lable)
if (getFranchisee?.price_Lable) {
const getFranchiseeProduct = await Product.find().select(`${getFranchisee.price_Lable}`);
return res.status(200).json({
success: true,
getFranchiseeProduct,
message: `Franchisee product fetched`,
});
}
} catch (err) {
return res.status(500).json({ message: err.message ? err.message : "Something went wrong." });
}
};
export { export {
addFranchisee, addFranchisee,

View File

@ -29,6 +29,7 @@ router.route("/address").post(isAuthenticatedUser, authorizeRoles("admin"), addA
// router.route("/scrollText").post(isAuthenticatedUser, authorizeRoles("admin"), addScrollText); // router.route("/scrollText").post(isAuthenticatedUser, authorizeRoles("admin"), addScrollText);
router.route("/logo").post(isAuthenticatedUser, authorizeRoles("admin"), addLogo); router.route("/logo").post(isAuthenticatedUser, authorizeRoles("admin"), addLogo);
router.route("/").get(getConfig).delete(isAuthenticatedUser, authorizeRoles("admin"), deleteConfig); router.route("/").get(getConfig).delete(isAuthenticatedUser, authorizeRoles("admin"), deleteConfig);
router router
.route("/termsofuse") .route("/termsofuse")
.get(isAuthenticatedUser, authorizeRoles("admin"), getTermsOfUse) .get(isAuthenticatedUser, authorizeRoles("admin"), getTermsOfUse)