zone based ALL filed added

master^2
Bhaskar 8 months ago
parent 9ecf77e259
commit 6049db5923

@ -743,6 +743,15 @@ exports.addDepartment = async (request, reply) => {
const getLocationsByCityAndZone = async (city, zone) => { const getLocationsByCityAndZone = async (city, zone) => {
try { try {
const matchCondition = {
city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" }, // Case-insensitive city match
};
// If zone is not "ALL", filter by the specific zone
if (zone.trim().toUpperCase() !== "ALL") {
matchCondition.zone = zone.trim();
}
const result = await Branch.aggregate([ const result = await Branch.aggregate([
{ {
$project: { $project: {
@ -752,10 +761,7 @@ const getLocationsByCityAndZone = async (city, zone) => {
}, },
}, },
{ {
$match: { $match: matchCondition, // Dynamic match condition for city & zone
city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" }, // Case-insensitive & trimmed
zone: zone.trim(),
},
}, },
{ {
$group: { $group: {
@ -779,15 +785,14 @@ const getLocationsByCityAndZone = async (city, zone) => {
}, },
{ {
$group: { $group: {
_id: { city: "$city", zone: "$zone" }, // Merge all data into a single object _id: "$city", // Group all locations under the city
locations: { $push: "$locations" }, // Collect all locations locations: { $push: "$locations" },
}, },
}, },
{ {
$project: { $project: {
_id: 0, _id: 0,
city: "$_id.city", city: "$_id",
zone: "$_id.zone",
locations: { locations: {
$reduce: { $reduce: {
input: "$locations", input: "$locations",
@ -811,6 +816,11 @@ exports.getZonebasedLocations = async (req, reply) => {
try { try {
const { city, zone } = req.query; const { city, zone } = req.query;
console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`); // Debugging input console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`); // Debugging input
if (!city || !zone) {
return reply.status(400).send({ message: "City and zone are required." });
}
const locations = await getLocationsByCityAndZone(city.trim(), zone.trim()); const locations = await getLocationsByCityAndZone(city.trim(), zone.trim());
if (!locations) { if (!locations) {
@ -902,19 +912,19 @@ exports.getZonebasedLocations = async (req, reply) => {
$project: { $project: {
_id: 0, // Exclude _id _id: 0, // Exclude _id
city: "$_id", // Return city name city: "$_id", // Return city name
zones: 1 // Return collected zones (no sorting in aggregation) zones: 1 // Return collected zones
} }
} }
]); ]);
// Sort the zones array in ascending order // Add "ALL" to the zones array and sort it
result.forEach(item => { result.forEach(item => {
item.zones.sort((a, b) => a - b); // Ensure zones are sorted numerically item.zones = ["ALL", ...new Set(item.zones)].sort((a, b) => (a === "ALL" ? -1 : a - b));
}); });
return result; return result;
} catch (err) { } catch (err) {
console.error("Error fetching zones:", err); // Detailed error logging console.error("Error fetching zones:", err);
throw new Error("Error fetching zones."); throw new Error("Error fetching zones.");
} }
}; };

@ -797,49 +797,34 @@ fastify.post('/api/uploads_company_profile/:customerId', async (request, reply)
const customerId = request.params.customerId; const customerId = request.params.customerId;
const data = await request.file(); const data = await request.file();
// Generate a unique file name
const fileName = `${data.filename}`;
// Define the destination bucket and file path
const bucketName = 'arminta_profile_pictures'; const bucketName = 'arminta_profile_pictures';
const filePath = `arminta_company_profiles/${fileName}`; const filePath = `arminta_company_profiles/${data.filename}`;
// Create a write stream to the destination file in the bucket const file = storage.bucket(bucketName).file(filePath);
const writeStream = storage.bucket(bucketName).file(filePath).createWriteStream(); const writeStream = file.createWriteStream();
// Pipe the file data to the write stream
data.file.pipe(writeStream); data.file.pipe(writeStream);
writeStream.on('finish', async () => { await new Promise((resolve, reject) => {
try { writeStream.on('finish', resolve);
// Make the uploaded file publicly accessible writeStream.on('error', reject);
await storage.bucket(bucketName).file(filePath).makePublic(); });
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; // Make file public
await file.makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
CompanyProfilePicture.findOneAndUpdate( // Update DB with async/await
{ customerId }, const picture = await CompanyProfilePicture.findOneAndUpdate(
{ picture: publicUrl }, { customerId },
{ new: true, upsert: true }, { picture: publicUrl },
(error, picture) => { { new: true, upsert: true }
if (error) { );
reply.code(500).send({ error: 'Failed to update database' });
} else {
// Return the public URL
reply.send({ picture: publicUrl });
}
}
);
} catch (error) {
reply.code(500).send({ error: 'Failed to make file public' });
}
});
writeStream.on('error', (err) => { reply.send({ picture: publicUrl });
reply.code(500).send({ error: 'Failed to move file' });
});
} catch (err) { } catch (err) {
reply.code(500).send({ error: 'An error occurred' }); console.error(err);
reply.code(500).send({ error: 'An error occurred', details: err.message });
} }
}); });

@ -29,6 +29,9 @@ const adminSchema = new mongoose.Schema({
required: true, // Customer ID is now required required: true, // Customer ID is now required
unique: true, unique: true,
}, },
picture:{
type: String,
},
date: { date: {
type: Date, type: Date,
default: Date.now, default: Date.now,

Loading…
Cancel
Save