product manual completed
This commit is contained in:
parent
c6cee73fdc
commit
4775d20253
56
middlewares/generalAuth.js
Normal file
56
middlewares/generalAuth.js
Normal file
@ -0,0 +1,56 @@
|
||||
import jwt from "jsonwebtoken";
|
||||
import SalesCoOrdinator from "../resources/SalesCoOrdinators/SalesCoOrdinatorModel.js";
|
||||
import TerritoryManager from "../resources/TerritoryManagers/TerritoryManagerModel.js";
|
||||
|
||||
export const isAuthenticated_SC_TM = 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.authorization;
|
||||
const token = getToken.slice(7);
|
||||
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
||||
if (!decoded) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Incorrect token",
|
||||
});
|
||||
}
|
||||
|
||||
let user = await SalesCoOrdinator.findById(decoded.id);
|
||||
if (user) {
|
||||
req.user = user;
|
||||
req.userType = "SalesCoOrdinator";
|
||||
} else {
|
||||
user = await TerritoryManager.findById(decoded.id);
|
||||
if (user) {
|
||||
req.user = user;
|
||||
req.userType = "TerritoryManager";
|
||||
}
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: "Unauthorized",
|
||||
});
|
||||
}
|
||||
|
||||
return next();
|
||||
} catch (error) {
|
||||
if (error.name === "TokenExpiredError") {
|
||||
return res.status(401).json({ message: "Token has expired." });
|
||||
} else if (error.name === "JsonWebTokenError") {
|
||||
return res.status(401).json({ message: "Invalid token." });
|
||||
} else {
|
||||
return res.status(500).json({
|
||||
message: "An internal error occurred while verifying the token.",
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
BIN
public/temp/tmp-1-1724746788677
Normal file
BIN
public/temp/tmp-1-1724746788677
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724746833987
Normal file
BIN
public/temp/tmp-1-1724746833987
Normal file
Binary file not shown.
6006
public/temp/tmp-1-1724747670176
Normal file
6006
public/temp/tmp-1-1724747670176
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-1-1724748558089
Normal file
6006
public/temp/tmp-1-1724748558089
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-1-1724749456516
Normal file
6006
public/temp/tmp-1-1724749456516
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-1-1724749570807
Normal file
6006
public/temp/tmp-1-1724749570807
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-1-1724750263481
Normal file
6006
public/temp/tmp-1-1724750263481
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-1-1724750281823
Normal file
6006
public/temp/tmp-1-1724750281823
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-1-1724750304440
Normal file
6006
public/temp/tmp-1-1724750304440
Normal file
File diff suppressed because it is too large
Load Diff
BIN
public/temp/tmp-1-1724750599576
Normal file
BIN
public/temp/tmp-1-1724750599576
Normal file
Binary file not shown.
6006
public/temp/tmp-1-1724757542930
Normal file
6006
public/temp/tmp-1-1724757542930
Normal file
File diff suppressed because it is too large
Load Diff
BIN
public/temp/tmp-1-1724760618075
Normal file
BIN
public/temp/tmp-1-1724760618075
Normal file
Binary file not shown.
6006
public/temp/tmp-1-1724760822218
Normal file
6006
public/temp/tmp-1-1724760822218
Normal file
File diff suppressed because it is too large
Load Diff
BIN
public/temp/tmp-1-1724760880967
Normal file
BIN
public/temp/tmp-1-1724760880967
Normal file
Binary file not shown.
6006
public/temp/tmp-1-1724761063195
Normal file
6006
public/temp/tmp-1-1724761063195
Normal file
File diff suppressed because it is too large
Load Diff
BIN
public/temp/tmp-1-1724761125087
Normal file
BIN
public/temp/tmp-1-1724761125087
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724761165388
Normal file
BIN
public/temp/tmp-1-1724761165388
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724761216665
Normal file
BIN
public/temp/tmp-1-1724761216665
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724761272232
Normal file
BIN
public/temp/tmp-1-1724761272232
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724761386239
Normal file
BIN
public/temp/tmp-1-1724761386239
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724762193202
Normal file
BIN
public/temp/tmp-1-1724762193202
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724764554211
Normal file
BIN
public/temp/tmp-1-1724764554211
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724764884872
Normal file
BIN
public/temp/tmp-1-1724764884872
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724765240957
Normal file
BIN
public/temp/tmp-1-1724765240957
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724765320856
Normal file
BIN
public/temp/tmp-1-1724765320856
Normal file
Binary file not shown.
6006
public/temp/tmp-1-1724765615231
Normal file
6006
public/temp/tmp-1-1724765615231
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-1-1724766268456
Normal file
6006
public/temp/tmp-1-1724766268456
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-1-1724766379146
Normal file
6006
public/temp/tmp-1-1724766379146
Normal file
File diff suppressed because it is too large
Load Diff
BIN
public/temp/tmp-1-1724766492550
Normal file
BIN
public/temp/tmp-1-1724766492550
Normal file
Binary file not shown.
6006
public/temp/tmp-1-1724767466175
Normal file
6006
public/temp/tmp-1-1724767466175
Normal file
File diff suppressed because it is too large
Load Diff
BIN
public/temp/tmp-1-1724767958384
Normal file
BIN
public/temp/tmp-1-1724767958384
Normal file
Binary file not shown.
6006
public/temp/tmp-1-1724768128492
Normal file
6006
public/temp/tmp-1-1724768128492
Normal file
File diff suppressed because it is too large
Load Diff
12933
public/temp/tmp-1-1724768256080
Normal file
12933
public/temp/tmp-1-1724768256080
Normal file
File diff suppressed because one or more lines are too long
BIN
public/temp/tmp-1-1724768571993
Normal file
BIN
public/temp/tmp-1-1724768571993
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724769239158
Normal file
BIN
public/temp/tmp-1-1724769239158
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724770285115
Normal file
BIN
public/temp/tmp-1-1724770285115
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724773346278
Normal file
BIN
public/temp/tmp-1-1724773346278
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724773410425
Normal file
BIN
public/temp/tmp-1-1724773410425
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724774155797
Normal file
BIN
public/temp/tmp-1-1724774155797
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724774178540
Normal file
BIN
public/temp/tmp-1-1724774178540
Normal file
Binary file not shown.
BIN
public/temp/tmp-1-1724774207152
Normal file
BIN
public/temp/tmp-1-1724774207152
Normal file
Binary file not shown.
6006
public/temp/tmp-1-1724774414898
Normal file
6006
public/temp/tmp-1-1724774414898
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-1-1724774467807
Normal file
6006
public/temp/tmp-1-1724774467807
Normal file
File diff suppressed because it is too large
Load Diff
BIN
public/temp/tmp-2-1724746910741
Normal file
BIN
public/temp/tmp-2-1724746910741
Normal file
Binary file not shown.
6006
public/temp/tmp-2-1724748970338
Normal file
6006
public/temp/tmp-2-1724748970338
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-2-1724760775832
Normal file
6006
public/temp/tmp-2-1724760775832
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-2-1724760927098
Normal file
6006
public/temp/tmp-2-1724760927098
Normal file
File diff suppressed because it is too large
Load Diff
BIN
public/temp/tmp-2-1724766506639
Normal file
BIN
public/temp/tmp-2-1724766506639
Normal file
Binary file not shown.
BIN
public/temp/tmp-2-1724774608644
Normal file
BIN
public/temp/tmp-2-1724774608644
Normal file
Binary file not shown.
6006
public/temp/tmp-3-1724746924114
Normal file
6006
public/temp/tmp-3-1724746924114
Normal file
File diff suppressed because it is too large
Load Diff
6006
public/temp/tmp-3-1724760961910
Normal file
6006
public/temp/tmp-3-1724760961910
Normal file
File diff suppressed because it is too large
Load Diff
BIN
public/temp/tmp-3-1724766538042
Normal file
BIN
public/temp/tmp-3-1724766538042
Normal file
Binary file not shown.
@ -1,31 +1,53 @@
|
||||
import ProductManual from "./ProductManualModel.js";
|
||||
import cloudinary from "../../Utils/cloudinary.js";
|
||||
|
||||
import path from "path";
|
||||
// Create a new product manual
|
||||
export const createProductManual = async (req, res) => {
|
||||
const { title } = req.body;
|
||||
const pdfFile = req.files ? req.files.pdfFile : null;
|
||||
|
||||
// Check if title is provided
|
||||
if (!title) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Title is required",
|
||||
});
|
||||
}
|
||||
|
||||
// Check if a file is provided
|
||||
if (!pdfFile) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "File is required",
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
let productManualDetails;
|
||||
let productManualDetails = null;
|
||||
let filename = "";
|
||||
|
||||
// Upload the file to Cloudinary
|
||||
if (pdfFile) {
|
||||
filename = pdfFile.name;
|
||||
// console.log(pdfFile);
|
||||
const originalFilename = path.basename(pdfFile.name, path.extname(pdfFile.name));
|
||||
|
||||
// Check if a file is provided
|
||||
if (req.files && req.files.product_manual) {
|
||||
const pdfFile = req.files.product_manual;
|
||||
const result = await cloudinary.v2.uploader.upload(pdfFile.tempFilePath, {
|
||||
folder: "chemiNova/ProductManuals",
|
||||
resource_type: "raw", // For PDF or other non-image files
|
||||
public_id: originalFilename,
|
||||
});
|
||||
|
||||
// console.log(result);
|
||||
productManualDetails = {
|
||||
public_id: result.public_id,
|
||||
url: result.secure_url,
|
||||
filename: filename,
|
||||
};
|
||||
}
|
||||
|
||||
// Create the product manual
|
||||
const productManual = await ProductManual.create({
|
||||
title,
|
||||
product_manual: productManualDetails,
|
||||
product_manual: productManualDetails || {}, // Ensure product_manual is an empty object if no file is provided
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
@ -41,14 +63,34 @@ export const createProductManual = async (req, res) => {
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Get all product manuals
|
||||
export const getAllProductManuals = async (req, res) => {
|
||||
try {
|
||||
const productManuals = await ProductManual.find();
|
||||
const PAGE_SIZE = parseInt(req.query.show) || 10;
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const skip = (page - 1) * PAGE_SIZE;
|
||||
let filter = {};
|
||||
if (req.query.title) {
|
||||
filter.title = {
|
||||
$regex: new RegExp(req.query.title, "i"),
|
||||
};
|
||||
}
|
||||
|
||||
// Fetch total count of documents
|
||||
const total = await ProductManual.countDocuments(filter);
|
||||
|
||||
// Fetch paginated data
|
||||
const productManuals = await ProductManual.find(filter)
|
||||
.limit(PAGE_SIZE)
|
||||
.skip(skip)
|
||||
.sort({ createdAt: -1 })
|
||||
.exec();
|
||||
|
||||
// Send response
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
productManuals,
|
||||
total,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching product manuals:", error);
|
||||
@ -90,7 +132,6 @@ export const getSingleProductManual = async (req, res) => {
|
||||
export const updateProductManual = async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { title } = req.body;
|
||||
|
||||
try {
|
||||
const productManual = await ProductManual.findById(id);
|
||||
|
||||
@ -100,33 +141,41 @@ export const updateProductManual = async (req, res) => {
|
||||
message: "Product manual not found",
|
||||
});
|
||||
}
|
||||
|
||||
let filename = "";
|
||||
// Check if a new file is provided
|
||||
if (req.files && req.files.product_manual) {
|
||||
if (req.files && req.files.pdfFile) {
|
||||
// Delete the old file from Cloudinary
|
||||
if (productManual.product_manual.public_id) {
|
||||
await cloudinary.v2.uploader.destroy(productManual.product_manual.public_id, {
|
||||
resource_type: "raw",
|
||||
});
|
||||
await cloudinary.v2.uploader.destroy(
|
||||
productManual.product_manual.public_id,
|
||||
{
|
||||
folder: "chemiNova/ProductManuals",
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Upload the new file to Cloudinary
|
||||
const pdfFile = req.files.product_manual;
|
||||
const pdfFile = req.files.pdfFile;
|
||||
// console.log(pdfFile);
|
||||
filename = pdfFile.name;
|
||||
const originalFilename = path.basename(pdfFile.name, path.extname(pdfFile.name));
|
||||
|
||||
const result = await cloudinary.v2.uploader.upload(pdfFile.tempFilePath, {
|
||||
folder: "chemiNova/ProductManuals",
|
||||
resource_type: "raw",
|
||||
public_id: originalFilename,
|
||||
});
|
||||
|
||||
// console.log(result);
|
||||
// Update the product manual details
|
||||
productManual.product_manual = {
|
||||
public_id: result.public_id,
|
||||
url: result.secure_url,
|
||||
filename: filename,
|
||||
};
|
||||
}
|
||||
|
||||
// Update the title
|
||||
productManual.title = title || productManual.title;
|
||||
|
||||
// console.log(productManual);
|
||||
await productManual.save();
|
||||
|
||||
res.status(200).json({
|
||||
@ -159,9 +208,11 @@ export const deleteProductManual = async (req, res) => {
|
||||
|
||||
// Delete the file from Cloudinary
|
||||
if (productManual.product_manual.public_id) {
|
||||
await cloudinary.v2.uploader.destroy(productManual.product_manual.public_id, {
|
||||
resource_type: "raw",
|
||||
});
|
||||
await cloudinary.v2.uploader.destroy(
|
||||
productManual.product_manual.public_id,
|
||||
{
|
||||
folder: "chemiNova/ProductManuals",
|
||||
});
|
||||
}
|
||||
|
||||
// Delete the product manual from the database
|
||||
@ -178,4 +229,4 @@ export const deleteProductManual = async (req, res) => {
|
||||
message: error.message || "Internal server error",
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
@ -1,4 +1,4 @@
|
||||
import mongoose from "mongoose";
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
const ProductManualSchema = new mongoose.Schema(
|
||||
{
|
||||
@ -9,17 +9,18 @@ const ProductManualSchema = new mongoose.Schema(
|
||||
product_manual: {
|
||||
public_id: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
url: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
filename: {
|
||||
type: String,
|
||||
},
|
||||
},
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
const ProductManual = mongoose.model("ProductManual", ProductManualSchema);
|
||||
const ProductManual = mongoose.model('ProductManual', ProductManualSchema);
|
||||
|
||||
export default ProductManual;
|
||||
|
@ -6,12 +6,8 @@ import {
|
||||
updateProductManual,
|
||||
deleteProductManual,
|
||||
} from "./ProductManualController.js";
|
||||
import {
|
||||
isAuthenticatedUser,
|
||||
authorizeRoles,
|
||||
} from "../../middlewares/auth.js";
|
||||
import { isAuthenticatedSalesCoOrdinator } from "../../middlewares/SalesCoOrdinatorAuth.js";
|
||||
import { isAuthenticatedTerritoryManager } from "../../middlewares/TerritoryManagerAuth.js";
|
||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
||||
import { isAuthenticated_SC_TM } from "../../middlewares/generalAuth.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@ -19,64 +15,17 @@ const router = express.Router();
|
||||
router
|
||||
.route("/create")
|
||||
.post(isAuthenticatedUser, authorizeRoles("admin"), createProductManual);
|
||||
router
|
||||
.route("/")
|
||||
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllProductManuals);
|
||||
|
||||
// Route for getting all product manuals (accessible to Sales Coordinator, Territory Manager, and Admin)
|
||||
router.route("/").get(
|
||||
(req, res, next) => {
|
||||
// Allow access if the user is a sales coordinator, territory manager, or admin
|
||||
isAuthenticatedSalesCoOrdinator(req, res, (err) => {
|
||||
if (err) {
|
||||
isAuthenticatedTerritoryManager(req, res, (err) => {
|
||||
if (err) {
|
||||
isAuthenticatedUser(req, res, (err) => {
|
||||
if (err || !["admin"].includes(req.user.role)) {
|
||||
return res.status(403).json({
|
||||
success: false,
|
||||
message: "Access denied. Unauthorized role.",
|
||||
});
|
||||
}
|
||||
next();
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
},
|
||||
getAllProductManuals
|
||||
);
|
||||
router.route("/getall").get(isAuthenticatedUser, getAllProductManuals);
|
||||
|
||||
// Route for getting a single product manual by ID (accessible to Sales Coordinator, Territory Manager, and Admin)
|
||||
router.route("/:id").get(
|
||||
(req, res, next) => {
|
||||
// Allow access if the user is a sales coordinator, territory manager, or admin
|
||||
isAuthenticatedSalesCoOrdinator(req, res, (err) => {
|
||||
if (err) {
|
||||
isAuthenticatedTerritoryManager(req, res, (err) => {
|
||||
if (err) {
|
||||
isAuthenticatedUser(req, res, (err) => {
|
||||
if (err || !["admin"].includes(req.user.role)) {
|
||||
return res.status(403).json({
|
||||
success: false,
|
||||
message: "Access denied. Unauthorized role.",
|
||||
});
|
||||
}
|
||||
next();
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
},
|
||||
getSingleProductManual
|
||||
);
|
||||
router
|
||||
.route("/:id")
|
||||
.get(isAuthenticatedUser, authorizeRoles("admin"), getSingleProductManual);
|
||||
|
||||
router.route("/getone/:id").get(isAuthenticatedUser, getSingleProductManual);
|
||||
// Route to update a product manual by ID
|
||||
router
|
||||
.route("/update/:id")
|
||||
@ -87,4 +36,4 @@ router
|
||||
.route("/delete/:id")
|
||||
.delete(isAuthenticatedUser, authorizeRoles("admin"), deleteProductManual);
|
||||
export default router;
|
||||
// /api/productmanual
|
||||
// /api/productmanual/update
|
||||
|
Loading…
Reference in New Issue
Block a user