diff --git a/public/uploads/Add-PD.xlsx b/public/uploads/Add-PD.xlsx index 21ce549..b17fe24 100644 Binary files a/public/uploads/Add-PD.xlsx and b/public/uploads/Add-PD.xlsx differ diff --git a/resources/Brands/BrandsController.js b/resources/Brands/BrandsController.js index 554b066..f225f01 100644 --- a/resources/Brands/BrandsController.js +++ b/resources/Brands/BrandsController.js @@ -35,13 +35,43 @@ export const addBrand = async (req, res) => { // Get all Brands export const getBrands = async (req, res) => { try { - const brands = await BrandModel.find().sort({ createdAt: -1 }); + const PAGE_SIZE = parseInt(req.query.show) || 10; + const page = parseInt(req.query.page) || 1; + const skip = (page - 1) * PAGE_SIZE; + let filter = {}; + + // Search by brandName if provided + if (req.query.brandName) { + filter.brandName = { + $regex: new RegExp(req.query.brandName, "i"), // Case-insensitive search + }; + } + + // Get total number of brands matching the filter + const total = await BrandModel.countDocuments(filter); + + // Fetch brands with pagination and filtering + const brands = await BrandModel.find(filter) + .limit(PAGE_SIZE) + .skip(skip) + .sort({ createdAt: -1 }) + .exec(); + + // If no brands are found, return 404 error if (!brands.length) { return res.status(404).json({ message: "No brands found" }); } - res.status(200).json({ success: true, brands }); + // Return the paginated and filtered brands list + res.status(200).json({ + success: true, + total_data: total, + total_pages: Math.ceil(total / PAGE_SIZE), + current_page: page, + brands, + }); } catch (error) { + // Handle server error res.status(500).json({ success: false, message: error.message || "Something went wrong", diff --git a/resources/Category/categoryController.js b/resources/Category/categoryController.js index e3eaebd..a9b2d76 100644 --- a/resources/Category/categoryController.js +++ b/resources/Category/categoryController.js @@ -35,14 +35,43 @@ export const addCategory = async (req, res) => { // Get all Categories export const getCategories = async (req, res) => { try { - const categories = await CategoryModel.find().sort({ createdAt: -1 }); + const PAGE_SIZE = parseInt(req.query.show) || 10; + const page = parseInt(req.query.page) || 1; + const skip = (page - 1) * PAGE_SIZE; + let filter = {}; + // Handle filtering by categoryName + if (req.query.categoryName) { + filter.categoryName = { + $regex: new RegExp(req.query.categoryName, "i"), + }; + } + + // Count the total number of documents matching the filter + const total = await CategoryModel.countDocuments(filter); + + // Fetch the categories with pagination + const categories = await CategoryModel.find(filter) + .limit(PAGE_SIZE) + .skip(skip) + .sort({ createdAt: -1 }) + .exec(); + + // If no categories are found, return a 404 error if (!categories.length) { return res.status(404).json({ message: "No categories found" }); } - res.status(200).json({ success: true, categories }); + // Return success response with total data and total pages + res.status(200).json({ + success: true, + total_data: total, + total_pages: Math.ceil(total / PAGE_SIZE), + current_page: page, + categories, + }); } catch (error) { + // Handle server error res.status(500).json({ success: false, message: error.message || "Something went wrong", diff --git a/resources/Leaves/LeaveController.js b/resources/Leaves/LeaveController.js index 2cea40e..5e239c6 100644 --- a/resources/Leaves/LeaveController.js +++ b/resources/Leaves/LeaveController.js @@ -140,6 +140,7 @@ export const AdmingetLeaveByUser = async (req, res) => { success: true, user: leaveDoc.userId, leave: paginatedRecords, + userType: leaveDoc.userType, total_data: totalData, }); } catch (error) { diff --git a/resources/Leaves/LeaveModel.js b/resources/Leaves/LeaveModel.js index bd50258..383d0fd 100644 --- a/resources/Leaves/LeaveModel.js +++ b/resources/Leaves/LeaveModel.js @@ -12,7 +12,7 @@ const leaveRecordSchema = new mongoose.Schema({ }, location: { type: String, - required: true, + // required: true, }, reason: { type: String, @@ -21,7 +21,7 @@ const leaveRecordSchema = new mongoose.Schema({ leaveType: { type: String, required: true, - enum: ["Sick Leave", "Casual Leave"], + enum: ["Sick Leave", "Privilege Leave","Personal Leave"], }, }); diff --git a/resources/RetailDistributor/RetailDistributorController.js b/resources/RetailDistributor/RetailDistributorController.js index 1a22e4a..6ca8102 100644 --- a/resources/RetailDistributor/RetailDistributorController.js +++ b/resources/RetailDistributor/RetailDistributorController.js @@ -430,10 +430,10 @@ export const getAllRetailDistributorApproved = async (req, res) => { export const getRDId = async (req, res) => { try { const { id } = req.params; - console.log(id); + // console.log(id); // Fetch the KYC document from the database by ID const RD = await RetailDistributor.findById(id) - .populate("principal_distributer", "name") + .populate("principal_distributer", "name email phone") .populate("addedBy") .populate("kyc") .populate("mappedTM") @@ -443,7 +443,7 @@ export const getRDId = async (req, res) => { if (!RD) { return res.status(404).json({ message: "No RetailDistributor found" }); } - + // console.log(RD); // Send the fetched KYC document as a response res.status(200).json(RD); } catch (error) { @@ -1150,7 +1150,7 @@ export const updateRDMapped = async (req, res) => { try { const { id } = req.params; const { principal_distributor, mappedTM, mappedSC } = req.body; - console.log(principal_distributor); + // console.log(principal_distributor); // Find the RetailDistributor document by ID const RD = await RetailDistributor.findById(id); diff --git a/resources/user/userController.js b/resources/user/userController.js index e711e09..b6008a8 100644 --- a/resources/user/userController.js +++ b/resources/user/userController.js @@ -932,7 +932,7 @@ export const getUserOrderForAdmin = async (req, res) => { }; // 8.update User password export const updatePassword = catchAsyncErrors(async (req, res, next) => { - const user = await User.findById(req.user.id).select("+password"); + const user = await User.findById(req.user._id).select("+password"); const isPasswordMatched = await user.comparePassword(req.body.oldPassword); diff --git a/resources/user/userModel.js b/resources/user/userModel.js index 148808e..e19c597 100644 --- a/resources/user/userModel.js +++ b/resources/user/userModel.js @@ -11,11 +11,29 @@ const userSchema = new mongoose.Schema( uniqueId: { type: String, unique: true, - required: true, + validate: { + validator: function (value) { + // Only require uniqueId if the role is not "admin" + if (this.role !== "admin" && !value) { + return false; + } + return true; + }, + message: "Unique ID is required for users", + }, }, SBU: { type: String, - required: [true, "Please Enter Your SBU"], + validate: { + validator: function (value) { + // Only require SBU if the role is not "admin" + if (this.role !== "admin" && !value) { + return false; + } + return true; + }, + message: "SBU is required for users", + }, }, name: { type: String,