ashok 2 months ago
commit 3f963d5e32

@ -6,7 +6,7 @@ const bcrypt = require('bcrypt')
const fastify = require("fastify"); const fastify = require("fastify");
const { Tank, MotorData, IotData } = require('../models/tanks'); 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'; const JWT_SECRET = 'your-secret-key';
async function generateCustomerId(role) { async function generateCustomerId(role) {
@ -426,16 +426,64 @@ exports.createUser = async (request, reply) => {
exports.getAllCompanys = async (req, reply) => { exports.getAllCompanys = async (req, reply) => {
try { try {
const pendingOrders = await City.find(); const companyList = await City.find();
return reply.send({ return reply.send({
status_code: 200, status_code: 200,
message: "Pending orders fetched successfully", message: "Fetched successfully",
data: pendingOrders, data: companyList,
}); });
} catch (err) { } catch (err) {
console.error("Error fetching pending orders:", err); console.error("Error fetching ", err);
return reply.status(500).send({ error: "Internal server error" }); return reply.status(500).send({ error: "Internal server error" });
} }
}; };
// 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"
});
}
};

@ -1271,10 +1271,13 @@ const getDepartmentsByName = async (officeName, city, departmentName) => {
try { try {
const trimmedCity = city.trim(); const trimmedCity = city.trim();
// Allow for extra whitespace before or after the city value in the database
const query = { const query = {
city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } $or: [
{ city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } },
{ officeName: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } }
]
}; };
console.log("MongoDB Query:", JSON.stringify(query, null, 2)); console.log("MongoDB Query:", JSON.stringify(query, null, 2));
const result = await Deparments.find(query) const result = await Deparments.find(query)
@ -1284,11 +1287,49 @@ const getDepartmentsByName = async (officeName, city, departmentName) => {
// Remove duplicate department names // Remove duplicate department names
return [...new Set(result.map(doc => doc.departmentName))]; return [...new Set(result.map(doc => doc.departmentName))];
} catch (error) { } catch (error) {
console.error("Error fetching departments by city:", error); console.error("Error fetching departments by city or officeName:", error);
throw new Error("Error fetching departments by city."); throw new Error("Error fetching departments by city or officeName.");
} }
}; };
exports.getOffices = async (req, reply) => {
try {
const { officeName, city } = req.params;
// Build dynamic filter
const filter = {};
if (officeName && officeName !== 'ALL') {
// support multiple office names as comma-separated
const officeNames = officeName.split(',').map(name => name.trim());
filter.officeName = { $in: officeNames };
}
if (city && city !== 'ALL') {
// support multiple cities as comma-separated
const cities = city.split(',').map(c => c.trim());
filter.city = { $in: cities };
}
const offices = await Deparments.find(filter).lean();
reply.send({
status_code: 200,
message: "Fetched successfully",
data: offices,
});
} catch (error) {
console.error("Error in getOffices:", error);
reply.code(500).send({
status_code: 500,
message: "Internal server error",
error: error.message,
});
}
};
// API route handler // API route handler
exports.getDepartmentsByCity = async (req, reply) => { exports.getDepartmentsByCity = async (req, reply) => {
try { try {

@ -288,6 +288,30 @@ fastify.get("/api/getAllCompanies", {
handler: adminController.getAllCompanys, 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(); next();
}; };

@ -667,7 +667,7 @@ module.exports = function (fastify, opts, next) {
fastify.route({ fastify.route({
method: "GET", method: "GET",
url: "/api/departmentNameList/:city", url: "/api/departmentNameList/:city/:officeName",
schema: { schema: {
tags: ["Department"], tags: ["Department"],
description: "Get a list of department names for a given city", description: "Get a list of department names for a given city",
@ -675,12 +675,14 @@ module.exports = function (fastify, opts, next) {
params: { params: {
type: "object", type: "object",
properties: { properties: {
city: { type: "string" } city: { type: "string" },
officeName: { type: "string" },
}, },
required: ["city"], required: ["city"],
}, },
}, },
handler: departmentController.getDepartmentsByCity, handler: departmentController.getOffices,
}); });
fastify.route({ fastify.route({

Loading…
Cancel
Save