|
|
|
@ -60,7 +60,6 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
return `AW${cityPrefix}${departmentPrefix}${result.seq}`; // Generate ID
|
|
|
|
return `AW${cityPrefix}${departmentPrefix}${result.seq}`; // Generate ID
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.addCity = async (request, reply) => {
|
|
|
|
exports.addCity = async (request, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const {
|
|
|
|
const {
|
|
|
|
@ -79,19 +78,27 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
email
|
|
|
|
email
|
|
|
|
} = request.body;
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
// Generate departmentId based on departmentName
|
|
|
|
// Generate unique cityId
|
|
|
|
// const prefix = departmentName.substring(0, 2).toUpperCase(); // Extract first two letters and convert to uppercase
|
|
|
|
const c_id = await generateCityId();
|
|
|
|
const c_id = await generateCityId();
|
|
|
|
const cityId = `AWCI${c_id}`;
|
|
|
|
const cityId = `AWCI${c_id}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check for existing department
|
|
|
|
// Check for existing records with specific fields
|
|
|
|
const existingStore = await City.findOne({ cityId });
|
|
|
|
const existingPhone = await City.findOne({ phone });
|
|
|
|
if (existingStore) {
|
|
|
|
if (existingPhone) {
|
|
|
|
return reply.status(400).send({ message: 'City is already registered' });
|
|
|
|
return reply.status(400).send({ message: 'Phone number already exists' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create new department
|
|
|
|
const existingOfficeName = await City.findOne({ officeName });
|
|
|
|
|
|
|
|
if (existingOfficeName) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ message: 'Office name already exists' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const existingCityId = await City.findOne({ cityId });
|
|
|
|
|
|
|
|
if (existingCityId) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ message: 'City ID already exists' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create new city record
|
|
|
|
const citys = new City({
|
|
|
|
const citys = new City({
|
|
|
|
cityId,
|
|
|
|
cityId,
|
|
|
|
phone,
|
|
|
|
phone,
|
|
|
|
@ -105,18 +112,18 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
country,
|
|
|
|
country,
|
|
|
|
pincode,
|
|
|
|
pincode,
|
|
|
|
email,
|
|
|
|
email,
|
|
|
|
// departmentName,
|
|
|
|
|
|
|
|
createdBy,
|
|
|
|
createdBy,
|
|
|
|
updatedBy,
|
|
|
|
updatedBy,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await citys.save();
|
|
|
|
await citys.save();
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ citys, message: 'Account Created Successfully' });
|
|
|
|
reply.send({ citys, message: 'City Created Successfully' });
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.addBranch = async (request, reply) => {
|
|
|
|
exports.addBranch = async (request, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
@ -985,6 +992,69 @@ exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getZonesByCityAndOffice = async (city, officeName) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const result = await Zone.aggregate([
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$project: {
|
|
|
|
|
|
|
|
city: { $trim: { input: "$city" } }, // Trim city field
|
|
|
|
|
|
|
|
officeName: { $trim: { input: "$officeName" } }, // Trim officeName field
|
|
|
|
|
|
|
|
zone: 1 // Keep zone field
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$match: {
|
|
|
|
|
|
|
|
city: { $regex: `^${city.trim()}$`, $options: "i" }, // Case-insensitive match for city
|
|
|
|
|
|
|
|
officeName: { $regex: `^${officeName.trim()}$`, $options: "i" } // Case-insensitive match for officeName
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$group: {
|
|
|
|
|
|
|
|
_id: { city: { $toUpper: "$city" }, officeName: { $toUpper: "$officeName" } }, // Normalize city and officeName
|
|
|
|
|
|
|
|
zones: { $addToSet: "$zone" } // Collect unique zones
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$project: {
|
|
|
|
|
|
|
|
_id: 0, // Exclude _id
|
|
|
|
|
|
|
|
city: "$_id.city", // Return city name
|
|
|
|
|
|
|
|
officeName: "$_id.officeName", // Return officeName
|
|
|
|
|
|
|
|
zones: 1 // Return collected zones
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add "ALL" to the zones array and sort it
|
|
|
|
|
|
|
|
result.forEach(item => {
|
|
|
|
|
|
|
|
item.zones = ["ALL", ...new Set(item.zones)].sort((a, b) => (a === "ALL" ? -1 : a - b));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error("Error fetching zones:", err);
|
|
|
|
|
|
|
|
throw new Error("Error fetching zones.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getZonesByCityAndOffice = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { city, officeName } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!city || city.trim() === "" || !officeName || officeName.trim() === "") {
|
|
|
|
|
|
|
|
return reply.status(400).send({ message: "City and officeName are required." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const zones = await getZonesByCityAndOffice(city.trim(), officeName.trim()); // Trim inputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (zones.length === 0) {
|
|
|
|
|
|
|
|
return reply.status(404).send({ message: "No zones found for the specified city and officeName." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: zones });
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|