fix get all brand , get all categories and add user type for single user leave
This commit is contained in:
parent
d602fdc7be
commit
e03de96466
@ -35,13 +35,43 @@ export const addBrand = async (req, res) => {
|
|||||||
// Get all Brands
|
// Get all Brands
|
||||||
export const getBrands = async (req, res) => {
|
export const getBrands = async (req, res) => {
|
||||||
try {
|
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) {
|
if (!brands.length) {
|
||||||
return res.status(404).json({ message: "No brands found" });
|
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) {
|
} catch (error) {
|
||||||
|
// Handle server error
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: error.message || "Something went wrong",
|
message: error.message || "Something went wrong",
|
||||||
|
@ -35,14 +35,43 @@ export const addCategory = async (req, res) => {
|
|||||||
// Get all Categories
|
// Get all Categories
|
||||||
export const getCategories = async (req, res) => {
|
export const getCategories = async (req, res) => {
|
||||||
try {
|
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) {
|
if (!categories.length) {
|
||||||
return res.status(404).json({ message: "No categories found" });
|
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) {
|
} catch (error) {
|
||||||
|
// Handle server error
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: error.message || "Something went wrong",
|
message: error.message || "Something went wrong",
|
||||||
|
@ -140,6 +140,7 @@ export const AdmingetLeaveByUser = async (req, res) => {
|
|||||||
success: true,
|
success: true,
|
||||||
user: leaveDoc.userId,
|
user: leaveDoc.userId,
|
||||||
leave: paginatedRecords,
|
leave: paginatedRecords,
|
||||||
|
userType: leaveDoc.userType,
|
||||||
total_data: totalData,
|
total_data: totalData,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user