|
|
|
@ -10,7 +10,7 @@ const fastify = require("fastify")({
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const { Counter} = require('../models/User')
|
|
|
|
const { Counter} = require('../models/User')
|
|
|
|
|
|
|
|
|
|
|
|
const {Department, Desgination, City, Deparments} = require('../models/Department')
|
|
|
|
const {Department, Desgination, City, Deparments, Branch} = require('../models/Department')
|
|
|
|
// const generateDepartmentId = async (prefix) => {
|
|
|
|
// const generateDepartmentId = async (prefix) => {
|
|
|
|
// const result = await Counter.findOneAndUpdate(
|
|
|
|
// const result = await Counter.findOneAndUpdate(
|
|
|
|
// { _id: 'department_id' },
|
|
|
|
// { _id: 'department_id' },
|
|
|
|
@ -27,6 +27,15 @@ const generateCityId = async () => {
|
|
|
|
{ upsert: true, new: true }
|
|
|
|
{ upsert: true, new: true }
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result.seq;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const generateBranchId = async () => {
|
|
|
|
|
|
|
|
var result = await Counter.findOneAndUpdate(
|
|
|
|
|
|
|
|
{ _id: 'customer_id' },
|
|
|
|
|
|
|
|
{ $inc: { seq: 1 } },
|
|
|
|
|
|
|
|
{ upsert: true, new: true }
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return result.seq;
|
|
|
|
return result.seq;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// const generateDesginationId = async (prefix) => {
|
|
|
|
// const generateDesginationId = async (prefix) => {
|
|
|
|
@ -67,6 +76,7 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
pincode,
|
|
|
|
pincode,
|
|
|
|
createdBy,
|
|
|
|
createdBy,
|
|
|
|
updatedBy,
|
|
|
|
updatedBy,
|
|
|
|
|
|
|
|
email
|
|
|
|
} = request.body;
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
// Generate departmentId based on departmentName
|
|
|
|
// Generate departmentId based on departmentName
|
|
|
|
@ -92,6 +102,7 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
zone,
|
|
|
|
zone,
|
|
|
|
country,
|
|
|
|
country,
|
|
|
|
pincode,
|
|
|
|
pincode,
|
|
|
|
|
|
|
|
email,
|
|
|
|
// departmentName,
|
|
|
|
// departmentName,
|
|
|
|
createdBy,
|
|
|
|
createdBy,
|
|
|
|
updatedBy,
|
|
|
|
updatedBy,
|
|
|
|
@ -104,6 +115,64 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.addBranch = async (request, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
|
|
|
phone,
|
|
|
|
|
|
|
|
land_line_number,
|
|
|
|
|
|
|
|
officeName,
|
|
|
|
|
|
|
|
location,
|
|
|
|
|
|
|
|
city,
|
|
|
|
|
|
|
|
state,
|
|
|
|
|
|
|
|
country,
|
|
|
|
|
|
|
|
zone,
|
|
|
|
|
|
|
|
office_address1,
|
|
|
|
|
|
|
|
address2,
|
|
|
|
|
|
|
|
pincode,
|
|
|
|
|
|
|
|
createdBy,
|
|
|
|
|
|
|
|
updatedBy,
|
|
|
|
|
|
|
|
email
|
|
|
|
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate departmentId based on departmentName
|
|
|
|
|
|
|
|
// const prefix = departmentName.substring(0, 2).toUpperCase(); // Extract first two letters and convert to uppercase
|
|
|
|
|
|
|
|
const b_id = await generateBranchId();
|
|
|
|
|
|
|
|
const branchId = `AWBR${b_id}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check for existing department
|
|
|
|
|
|
|
|
const existingStore = await Branch.findOne({ branchId });
|
|
|
|
|
|
|
|
if (existingStore) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ message: 'Branch is already registered' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create new department
|
|
|
|
|
|
|
|
const branch = new Branch({
|
|
|
|
|
|
|
|
branchId,
|
|
|
|
|
|
|
|
phone,
|
|
|
|
|
|
|
|
land_line_number,
|
|
|
|
|
|
|
|
officeName,
|
|
|
|
|
|
|
|
location,
|
|
|
|
|
|
|
|
city,
|
|
|
|
|
|
|
|
office_address1,
|
|
|
|
|
|
|
|
address2,
|
|
|
|
|
|
|
|
state,
|
|
|
|
|
|
|
|
zone,
|
|
|
|
|
|
|
|
country,
|
|
|
|
|
|
|
|
pincode,
|
|
|
|
|
|
|
|
email,
|
|
|
|
|
|
|
|
// departmentName,
|
|
|
|
|
|
|
|
createdBy,
|
|
|
|
|
|
|
|
updatedBy,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await branch.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ branch, message: 'Account Created Successfully' });
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
// exports.getSinledepartmentData = async (req, reply) => {
|
|
|
|
// exports.getSinledepartmentData = async (req, reply) => {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// const { departmentId } = req.params;
|
|
|
|
// const { departmentId } = req.params;
|
|
|
|
@ -132,21 +201,24 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getallcities = async (req, reply) => {
|
|
|
|
exports.getallCompanyNames = async (req, reply) => {
|
|
|
|
// try {
|
|
|
|
try {
|
|
|
|
// await City.find()
|
|
|
|
await City.find()
|
|
|
|
// .exec()
|
|
|
|
.select("officeName -_id") // Select only officeName and exclude _id
|
|
|
|
// .then((docs) => {
|
|
|
|
.exec()
|
|
|
|
// reply.send({ status_code: 200, data: docs, count: docs.length });
|
|
|
|
.then((docs) => {
|
|
|
|
// })
|
|
|
|
const officeNames = docs.map((doc) => doc.officeName); // Extract only officeName values
|
|
|
|
// .catch((err) => {
|
|
|
|
reply.send({ status_code: 200, data: officeNames, count: officeNames.length });
|
|
|
|
// console.log(err);
|
|
|
|
})
|
|
|
|
// reply.send({ error: err });
|
|
|
|
.catch((err) => {
|
|
|
|
// });
|
|
|
|
console.log(err);
|
|
|
|
// } catch (err) {
|
|
|
|
reply.send({ error: err });
|
|
|
|
// throw boom.boomify(err);
|
|
|
|
});
|
|
|
|
// }
|
|
|
|
} catch (err) {
|
|
|
|
// };
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getAllDepartmentsParticularFields = async (req, reply) => {
|
|
|
|
// exports.getAllDepartmentsParticularFields = async (req, reply) => {
|
|
|
|
@ -188,13 +260,23 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.deleteBranchInfo = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const branchId = req.params.branchId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const branch = await Branch.findOneAndDelete({ branchId:branchId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, message: 'Delete Sucessfully', branch});
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
exports.editcity = async (request, reply) => {
|
|
|
|
exports.editcity = async (request, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { cityId } = request.params;
|
|
|
|
const { cityId } = request.params;
|
|
|
|
const {
|
|
|
|
const {
|
|
|
|
|
|
|
|
|
|
|
|
// phone,
|
|
|
|
phone,
|
|
|
|
city,
|
|
|
|
city,
|
|
|
|
state,
|
|
|
|
state,
|
|
|
|
country,
|
|
|
|
country,
|
|
|
|
@ -202,7 +284,8 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
address1,
|
|
|
|
address1,
|
|
|
|
address2,
|
|
|
|
address2,
|
|
|
|
pincode,
|
|
|
|
pincode,
|
|
|
|
// departmentName
|
|
|
|
email,
|
|
|
|
|
|
|
|
officeName
|
|
|
|
|
|
|
|
|
|
|
|
} = request.body;
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
@ -218,16 +301,17 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// existing.phone = phone || existing.phone;
|
|
|
|
existing.phone = phone || existing.phone;
|
|
|
|
existing.city = city || existing.city;
|
|
|
|
existing.city = city || existing.city;
|
|
|
|
existing.state = state || existing.state;
|
|
|
|
existing.state = state || existing.state;
|
|
|
|
existing.country = country || existing.country;
|
|
|
|
existing.country = country || existing.country;
|
|
|
|
existing.zone = zone || existing.zone;
|
|
|
|
existing.zone = zone || existing.zone;
|
|
|
|
// existing.departmentName = departmentName || existing.departmentName;
|
|
|
|
existing.officeName = officeName || existing.officeName;
|
|
|
|
existing.pincode = pincode || existing.pincode;
|
|
|
|
existing.pincode = pincode || existing.pincode;
|
|
|
|
|
|
|
|
|
|
|
|
existing.address1 = address1 || existing.address1;
|
|
|
|
existing.address1 = address1 || existing.address1;
|
|
|
|
existing.address2 = address2 || existing.address2;
|
|
|
|
existing.address2 = address2 || existing.address2;
|
|
|
|
|
|
|
|
existing.email = email || existing.email;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -239,6 +323,61 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.editBranch = async (request, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { branchId } = request.params;
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
phone,
|
|
|
|
|
|
|
|
land_line_number,
|
|
|
|
|
|
|
|
officeName,
|
|
|
|
|
|
|
|
city,
|
|
|
|
|
|
|
|
state,
|
|
|
|
|
|
|
|
country,
|
|
|
|
|
|
|
|
zone,
|
|
|
|
|
|
|
|
address1,
|
|
|
|
|
|
|
|
address2,
|
|
|
|
|
|
|
|
pincode,
|
|
|
|
|
|
|
|
email
|
|
|
|
|
|
|
|
// departmentName
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const existing = await Branch.findOne({ branchId });
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
|
|
|
return reply.status(404).send({ message: 'Branch not found' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const phoneExists = await Department.findOne({ phone, departmentId: { $ne: departmentId } });
|
|
|
|
|
|
|
|
// if (phoneExists) {
|
|
|
|
|
|
|
|
// return reply.status(400).send({ message: 'Phone is already registered to another user' });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
existing.phone = phone || existing.phone;
|
|
|
|
|
|
|
|
existing.land_line_number = land_line_number || existing.land_line_number;
|
|
|
|
|
|
|
|
existing.city = city || existing.city;
|
|
|
|
|
|
|
|
existing.state = state || existing.state;
|
|
|
|
|
|
|
|
existing.country = country || existing.country;
|
|
|
|
|
|
|
|
existing.zone = zone || existing.zone;
|
|
|
|
|
|
|
|
existing.officeName = officeName || existing.officeName;
|
|
|
|
|
|
|
|
existing.pincode = pincode || existing.pincode;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
existing.address1 = address1 || existing.address1;
|
|
|
|
|
|
|
|
existing.address2 = address2 || existing.address2;
|
|
|
|
|
|
|
|
existing.email = email || existing.email;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await existing.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ message: 'Branch user updated successfully' });
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.addDesgination = async (request, reply) => {
|
|
|
|
// exports.addDesgination = async (request, reply) => {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
@ -312,10 +451,14 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const {
|
|
|
|
const {
|
|
|
|
phone,
|
|
|
|
phone,
|
|
|
|
|
|
|
|
officeName,
|
|
|
|
alternativeContactNumber,
|
|
|
|
alternativeContactNumber,
|
|
|
|
gender,
|
|
|
|
gender,
|
|
|
|
personalEmail,
|
|
|
|
personalEmail,
|
|
|
|
city,
|
|
|
|
city,
|
|
|
|
|
|
|
|
personal_city,
|
|
|
|
|
|
|
|
reportingManager_mobile_number,
|
|
|
|
|
|
|
|
reportingManager_email,
|
|
|
|
firstName,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
lastName,
|
|
|
|
departmentName,
|
|
|
|
departmentName,
|
|
|
|
@ -350,6 +493,10 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
const department = new Deparments({
|
|
|
|
const department = new Deparments({
|
|
|
|
departmentId,
|
|
|
|
departmentId,
|
|
|
|
alternativeContactNumber,
|
|
|
|
alternativeContactNumber,
|
|
|
|
|
|
|
|
officeName,
|
|
|
|
|
|
|
|
reportingManager_mobile_number,
|
|
|
|
|
|
|
|
reportingManager_email,
|
|
|
|
|
|
|
|
personal_city,
|
|
|
|
gender,
|
|
|
|
gender,
|
|
|
|
city,
|
|
|
|
city,
|
|
|
|
firstName,
|
|
|
|
firstName,
|
|
|
|
@ -508,7 +655,12 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
address1,
|
|
|
|
address1,
|
|
|
|
address2,
|
|
|
|
address2,
|
|
|
|
pincode,
|
|
|
|
pincode,
|
|
|
|
desginationName
|
|
|
|
desginationName,
|
|
|
|
|
|
|
|
personal_city,
|
|
|
|
|
|
|
|
reportingManager_mobile_number,
|
|
|
|
|
|
|
|
reportingManager_email,
|
|
|
|
|
|
|
|
officeName,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} = request.body;
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
@ -543,7 +695,10 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
existing.lastName = lastName || existing.lastName;
|
|
|
|
existing.lastName = lastName || existing.lastName;
|
|
|
|
existing.departmentName = departmentName || existing.departmentName;
|
|
|
|
existing.departmentName = departmentName || existing.departmentName;
|
|
|
|
existing.reportingManager = reportingManager || existing.reportingManager
|
|
|
|
existing.reportingManager = reportingManager || existing.reportingManager
|
|
|
|
|
|
|
|
existing.personal_city = personal_city || existing.personal_city;
|
|
|
|
|
|
|
|
existing.reportingManager_mobile_number = reportingManager_mobile_number || existing.reportingManager_mobile_number;
|
|
|
|
|
|
|
|
existing.reportingManager_email = reportingManager_email || existing.reportingManager_email;
|
|
|
|
|
|
|
|
existing.officeName = officeName || existing.officeName
|
|
|
|
|
|
|
|
|
|
|
|
await existing.save();
|
|
|
|
await existing.save();
|
|
|
|
|
|
|
|
|
|
|
|
@ -584,7 +739,7 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
const getLocationsByCityAndZone = async (city, zone) => {
|
|
|
|
const getLocationsByCityAndZone = async (city, zone) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const result = await City.aggregate([
|
|
|
|
const result = await Branch.aggregate([
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$project: {
|
|
|
|
$project: {
|
|
|
|
city: { $toLower: { $trim: { input: "$city" } } }, // Normalize city name (lowercase & trim)
|
|
|
|
city: { $toLower: { $trim: { input: "$city" } } }, // Normalize city name (lowercase & trim)
|
|
|
|
@ -721,7 +876,7 @@ exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
const getZonesByCitys = async (city) => {
|
|
|
|
const getZonesByCitys = async (city) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const result = await City.aggregate([
|
|
|
|
const result = await Branch.aggregate([
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$project: {
|
|
|
|
$project: {
|
|
|
|
city: { $trim: { input: "$city" } }, // Trim city field in DB
|
|
|
|
city: { $trim: { input: "$city" } }, // Trim city field in DB
|
|
|
|
@ -784,36 +939,73 @@ exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getDepartmentsByName = async (departmentName) => {
|
|
|
|
// const getDepartmentsByName = async (departmentName, city) => {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// const result = await Deparments.find({
|
|
|
|
|
|
|
|
// departmentName: { $regex: `^${departmentName.trim()}$`, $options: "i" }, // Case-insensitive search
|
|
|
|
|
|
|
|
// city: { $regex: `^${city.trim()}$`, $options: "i" }, // Case-insensitive search
|
|
|
|
|
|
|
|
// }).lean(); // Convert to plain JSON
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return result;
|
|
|
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
|
|
|
// console.error("Error fetching department data:", err);
|
|
|
|
|
|
|
|
// throw new Error("Error fetching department data.");
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getDepartmentsByName = async (departmentName, city) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const result = await Deparments.find({
|
|
|
|
const trimmedDepartment = departmentName.trim();
|
|
|
|
departmentName: { $regex: `^${departmentName}$`, $options: "i" }, // Case-insensitive search
|
|
|
|
const trimmedCity = city.trim();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
|
|
|
|
departmentName: { $regex: trimmedDepartment, $options: "i" },
|
|
|
|
|
|
|
|
city: { $regex: trimmedCity, $options: "i" }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log("MongoDB Query:", JSON.stringify(query, null, 2));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const result = await Deparments.find(query).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log("Query Result:", result);
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
console.error("Error fetching department data:", err);
|
|
|
|
throw new Error("Error fetching department data.");
|
|
|
|
throw new Error("Error fetching department data.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// API Route
|
|
|
|
// API Route
|
|
|
|
exports.getDepartments = async (req, reply) => {
|
|
|
|
exports.getDepartments = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { departmentName } = req.params; // Get departmentName from request params
|
|
|
|
console.log("Request Params:", req.params); // Debugging log
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let { departmentName, city } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!departmentName || !city) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ message: "Department Name and City are required." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
departmentName = departmentName.trim();
|
|
|
|
|
|
|
|
city = city.trim();
|
|
|
|
|
|
|
|
|
|
|
|
if (!departmentName) {
|
|
|
|
const departments = await getDepartmentsByName(departmentName, city);
|
|
|
|
return reply.status(400).send({ message: "Department Name is required." });
|
|
|
|
|
|
|
|
|
|
|
|
if (departments.length === 0) {
|
|
|
|
|
|
|
|
return reply.status(404).send({ message: "No departments found for the specified name and city." });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const departments = await getDepartmentsByName(departmentName);
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: departments });
|
|
|
|
reply.send({ status_code: 200, data: departments });
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error("API Error:", err);
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getDepartmentNames = async () => {
|
|
|
|
const getDepartmentNames = async () => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const result = await Deparments.aggregate([
|
|
|
|
const result = await Deparments.aggregate([
|
|
|
|
@ -858,3 +1050,32 @@ exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getCitiesByOfficeName = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
let { officeName } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Trim and normalize spaces
|
|
|
|
|
|
|
|
officeName = officeName.trim().replace(/\s+/g, ' '); // Replace multiple spaces with one
|
|
|
|
|
|
|
|
const regexOfficeName = new RegExp(officeName.replace(/\s+/g, '\\s*'), "i");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Debugging: Check all available office names in DB
|
|
|
|
|
|
|
|
console.log("All Cities with Office Name:", await City.find().select("officeName city").lean());
|
|
|
|
|
|
|
|
console.log("All Branches with Office Name:", await Branch.find().select("officeName city").lean());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Query both collections with case-insensitive regex
|
|
|
|
|
|
|
|
const cityResults = await City.find({ officeName: { $regex: officeName, $options: "i" } }).select("city -_id").lean();
|
|
|
|
|
|
|
|
const branchResults = await Branch.find({ officeName: { $regex: officeName, $options: "i" } }).select("city -_id").lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Extract and merge unique city names
|
|
|
|
|
|
|
|
const cityNames = [...new Set([...cityResults.map(c => c.city), ...branchResults.map(b => b.city)])];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log("Final City Results:", cityNames);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: cityNames });
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error("Error fetching cities:", err);
|
|
|
|
|
|
|
|
reply.send({ error: err.message });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|