From 6049db5923a8847733edb9a65176085cae37398a Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 5 Feb 2025 16:55:11 +0530 Subject: [PATCH] zone based ALL filed added --- src/controllers/departmentController.js | 34 ++++++++++------ src/index.js | 53 +++++++++---------------- src/models/admin.js | 3 ++ 3 files changed, 44 insertions(+), 46 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 91832060..88375e9e 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -743,6 +743,15 @@ exports.addDepartment = async (request, reply) => { const getLocationsByCityAndZone = async (city, zone) => { 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([ { $project: { @@ -752,10 +761,7 @@ const getLocationsByCityAndZone = async (city, zone) => { }, }, { - $match: { - city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" }, // Case-insensitive & trimmed - zone: zone.trim(), - }, + $match: matchCondition, // Dynamic match condition for city & zone }, { $group: { @@ -779,15 +785,14 @@ const getLocationsByCityAndZone = async (city, zone) => { }, { $group: { - _id: { city: "$city", zone: "$zone" }, // Merge all data into a single object - locations: { $push: "$locations" }, // Collect all locations + _id: "$city", // Group all locations under the city + locations: { $push: "$locations" }, }, }, { $project: { _id: 0, - city: "$_id.city", - zone: "$_id.zone", + city: "$_id", locations: { $reduce: { input: "$locations", @@ -811,6 +816,11 @@ exports.getZonebasedLocations = async (req, reply) => { try { const { city, zone } = req.query; 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()); if (!locations) { @@ -902,19 +912,19 @@ exports.getZonebasedLocations = async (req, reply) => { $project: { _id: 0, // Exclude _id 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 => { - 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; } catch (err) { - console.error("Error fetching zones:", err); // Detailed error logging + console.error("Error fetching zones:", err); throw new Error("Error fetching zones."); } }; diff --git a/src/index.js b/src/index.js index f37ddccc..d13a58df 100644 --- a/src/index.js +++ b/src/index.js @@ -797,49 +797,34 @@ fastify.post('/api/uploads_company_profile/:customerId', async (request, reply) const customerId = request.params.customerId; 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 filePath = `arminta_company_profiles/${fileName}`; + const filePath = `arminta_company_profiles/${data.filename}`; - // Create a write stream to the destination file in the bucket - const writeStream = storage.bucket(bucketName).file(filePath).createWriteStream(); + const file = storage.bucket(bucketName).file(filePath); + const writeStream = file.createWriteStream(); - // Pipe the file data to the write stream data.file.pipe(writeStream); - writeStream.on('finish', async () => { - try { - // Make the uploaded file publicly accessible - await storage.bucket(bucketName).file(filePath).makePublic(); + await new Promise((resolve, reject) => { + writeStream.on('finish', resolve); + writeStream.on('error', reject); + }); - const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; + // Make file public + await file.makePublic(); + const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; - CompanyProfilePicture.findOneAndUpdate( - { customerId }, - { picture: publicUrl }, - { new: true, upsert: true }, - (error, picture) => { - 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' }); - } - }); + // Update DB with async/await + const picture = await CompanyProfilePicture.findOneAndUpdate( + { customerId }, + { picture: publicUrl }, + { new: true, upsert: true } + ); - writeStream.on('error', (err) => { - reply.code(500).send({ error: 'Failed to move file' }); - }); + reply.send({ picture: publicUrl }); } catch (err) { - reply.code(500).send({ error: 'An error occurred' }); + console.error(err); + reply.code(500).send({ error: 'An error occurred', details: err.message }); } }); diff --git a/src/models/admin.js b/src/models/admin.js index c386110a..5d07e5e7 100644 --- a/src/models/admin.js +++ b/src/models/admin.js @@ -29,6 +29,9 @@ const adminSchema = new mongoose.Schema({ required: true, // Customer ID is now required unique: true, }, + picture:{ + type: String, + }, date: { type: Date, default: Date.now,