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) => {
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.");
}
};

@ -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 });
}
});

@ -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,

Loading…
Cancel
Save