diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js index 5a3fb5d9..1e03f5e2 100644 --- a/src/controllers/admincontroller.js +++ b/src/controllers/admincontroller.js @@ -486,4 +486,53 @@ exports.getAllOffices = async (req, reply) => { } }; +exports.getOfficeDetails = async (req, reply) => { + try { + let { officeName, city } = req.params; + + if (!officeName || !city) { + return reply.code(400).send({ message: "officeName and city are required." }); + } + + // Normalize whitespace and case + officeName = officeName.trim().replace(/\s+/g, ' '); + city = city.trim().replace(/\s+/g, ' '); + + const filters = {}; + + if (officeName.toUpperCase() !== 'ALL') { + filters.officeName = { $regex: new RegExp(officeName.replace(/\s+/g, '\\s*'), 'i') }; + } + + if (city.toUpperCase() !== 'ALL') { + filters.city = { $regex: new RegExp(city.replace(/\s+/g, '\\s*'), 'i') }; + } + + // Query City collection + const cityResults = await City.find(filters).lean(); + + // Query Branch collection + const branchResults = await Branch.find(filters).lean(); + + const combinedResults = [...cityResults, ...branchResults]; + + if (combinedResults.length === 0) { + return reply.status(404).send({ message: "No office details found for the given filters." }); + } + + reply.send({ + status_code: 200, + message: "Office details fetched successfully.", + data: combinedResults, + }); + + } catch (error) { + console.error("Error in getOfficeDetails:", error); + reply.status(500).send({ + status_code: 500, + message: "Internal server error", + error: error.message, + }); + } +}; diff --git a/src/routes/adminRoute.js b/src/routes/adminRoute.js index 20c97d4c..ce6d59b6 100644 --- a/src/routes/adminRoute.js +++ b/src/routes/adminRoute.js @@ -311,6 +311,22 @@ fastify.get("/api/getBranchDetails", { handler: adminController.getAllOffices, }); +fastify.get("/api/getOfficeDetails/:officeName/:city", { + schema: { + tags: ["Admin"], + description: "Get office details from both City and Branch collections", + summary: "Get merged office data", + params: { + type: "object", + properties: { + officeName: { type: "string" }, + city: { type: "string" } + }, + required: ["officeName", "city"] + }, + }, + handler: adminController.getOfficeDetails +}); next();