ashok 2 months ago
commit 9757fd4954

@ -353,33 +353,30 @@ exports.getAllDepartments = async (request, reply) => {
}); });
} }
// Prepare case-insensitive regex for matching // Case-insensitive regex without start/end anchors to avoid trailing space issues
const nameRegex = new RegExp(`^${officeName.trim()}$`, "i"); const nameRegex = new RegExp(officeName.trim().replace(/\s+/g, "\\s*"), "i");
const cityRegex = new RegExp(`^${city.trim()}$`, "i"); const cityRegex = new RegExp(city.trim().replace(/\s+/g, "\\s*"), "i");
// 1Check Branch schema for match // 1Branch match
const branchMatch = await Branch.findOne({ const branchMatch = await Branch.findOne({
officeName: nameRegex, officeName: nameRegex,
city: cityRegex, city: cityRegex,
}).lean(); }).lean();
// 2⃣ Check City schema for match // 2⃣ City match
const cityMatch = await City.findOne({ const cityMatch = await City.findOne({
officeName: nameRegex, officeName: nameRegex,
city: cityRegex, city: cityRegex,
}).lean(); }).lean();
// 3Get all departments for this officeName & city // 3Departments
const departments = await Deparments.find({ const departments = await Deparments.find({
officeName: nameRegex, officeName: nameRegex,
city: cityRegex, city: cityRegex,
}).lean(); }).lean();
// Start response data with "Self"
const responseData = [{ firstName: "Self" }]; const responseData = [{ firstName: "Self" }];
// If a Branch or City record is found, add it right after "Self"
if (branchMatch) { if (branchMatch) {
responseData.push({ responseData.push({
officeType: "branch", officeType: "branch",
@ -393,7 +390,6 @@ exports.getAllDepartments = async (request, reply) => {
}); });
} }
// Then add department docs
responseData.push(...departments); responseData.push(...departments);
return reply.send({ return reply.send({
@ -419,6 +415,7 @@ exports.getAllDepartments = async (request, reply) => {
}; };
exports.assignTeamMemberToQuotation = async (request, reply) => { exports.assignTeamMemberToQuotation = async (request, reply) => {
try { try {
const { installationId } = request.params; const { installationId } = request.params;

Loading…
Cancel
Save