diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 3f1c9a22..e6baac8a 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -1499,7 +1499,7 @@ exports.createquotationforSensor = async (req, reply) => { const i_id = await generatequatationId(); const quatationId = `AWQU${i_id}`; const { surveyId } = req.params; - const { customerId, masters, slaves, sensors, motor_switches, electricals } = req.body; + const { customerId, masters, slaves, sensors, motor_switches, electricals,storeId } = req.body; // Format electricals field const formattedElectricals = electricals.map((item) => ({ @@ -1541,6 +1541,7 @@ exports.createquotationforSensor = async (req, reply) => { const newQuotation = new SensorQuotation({ quatationId, customerId, + storeId, surveyId, quote_status: "sentfromsurvey", masters, @@ -1679,17 +1680,30 @@ exports.createEstimationPrice = async (req, reply) => { exports.getallquotationdata = async (req, reply) => { try { - await SensorQuotation.find({}) - .exec() - .then((docs) => { - reply.send({ status_code: 200, data: docs, count: docs.length }); - }) - .catch((err) => { - console.log(err); - reply.send({ error: err }); - }); + const quotations = await SensorQuotation.find({}).lean(); // Use lean() for better performance + + // Extract unique customerIds from quotations + const customerIds = [...new Set(quotations.map((q) => q.customerId).filter(Boolean))]; + + // Fetch customer details for all unique customerIds + const customers = await User.find({ customerId: { $in: customerIds } }).lean(); + + // Convert customer array to a dictionary for quick lookup + const customerMap = customers.reduce((acc, customer) => { + acc[customer.customerId] = customer; + return acc; + }, {}); + + // Attach customer details to quotations + const enrichedQuotations = quotations.map((quotation) => ({ + ...quotation, + customerDetails: customerMap[quotation.customerId] || null, // Attach customer details if found + })); + + reply.send({ status_code: 200, data: enrichedQuotations, count: enrichedQuotations.length }); } catch (err) { - throw boom.boomify(err); + console.error(err); + reply.send({ error: err.message }); } };