Get merged office data

master^2
Bhaskar 2 months ago
parent 18b677eefb
commit 9be73eca16

@ -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,
});
}
};

@ -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();

Loading…
Cancel
Save