diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js index bc487095..5a3fb5d9 100644 --- a/src/controllers/admincontroller.js +++ b/src/controllers/admincontroller.js @@ -6,7 +6,7 @@ const bcrypt = require('bcrypt') const fastify = require("fastify"); const { Tank, MotorData, IotData } = require('../models/tanks'); -const { Deparments, City } = require('../models/Department'); +const { Deparments, City, Branch } = require('../models/Department'); const JWT_SECRET = 'your-secret-key'; async function generateCustomerId(role) { @@ -426,16 +426,64 @@ exports.createUser = async (request, reply) => { exports.getAllCompanys = async (req, reply) => { try { - const pendingOrders = await City.find(); + const companyList = await City.find(); return reply.send({ status_code: 200, - message: "Pending orders fetched successfully", - data: pendingOrders, + message: "Fetched successfully", + data: companyList, }); } catch (err) { - console.error("Error fetching pending orders:", err); + console.error("Error fetching ", err); return reply.status(500).send({ error: "Internal server error" }); } -}; \ No newline at end of file +}; + +// exports.getBranchDetails = async (req, reply) => { +// try { +// const { officeName } = req.params; + +// const branchDetails = await Branch.find({ +// officeName: { $regex: new RegExp(`^${officeName}$`, 'i') } +// }); + +// return reply.send({ +// status_code: 200, +// message: "Fetched successfully", +// data: branchDetails, +// }); +// } catch (err) { +// console.error("Error fetching branch details:", err); +// return reply.status(500).send({ error: "Internal server error" }); +// } +// }; + + +exports.getAllOffices = async (req, reply) => { + try { + const { officeName } = req.query; + + let filter = {}; + if (officeName) { + // Case-insensitive exact match or partial match + filter.officeName = { $regex: new RegExp(officeName, "i") }; + } + + const offices = await Branch.find(filter).lean(); + + return reply.code(200).send({ + status_code: 200, + message: "Fetched successfully", + data: offices + }); + } catch (error) { + console.error("Error fetching offices:", error); + return reply.code(500).send({ + status_code: 500, + message: "Internal server error" + }); + } +}; + + diff --git a/src/routes/adminRoute.js b/src/routes/adminRoute.js index 288c2d3a..20c97d4c 100644 --- a/src/routes/adminRoute.js +++ b/src/routes/adminRoute.js @@ -288,6 +288,30 @@ fastify.get("/api/getAllCompanies", { handler: adminController.getAllCompanys, }); +fastify.get("/api/getBranchDetails", { + schema: { + tags: ["Admin"], + description: "Get Branch Details", + summary: "Get Branch Details", + // params: { + // type: "object", + // properties: { + // officeName: { type: "string", description: "Office name" } + // }, + // required: ["officeName"] + // }, + querystring: { // ✅ allow customerId in query string + type: 'object', + required: ['officeName'], + properties: { + officeName: { type: 'string' } + } + } + }, + handler: adminController.getAllOffices, +}); + + next(); };