diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 3f1c9a22..48a3068b 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -1541,6 +1541,7 @@ exports.createquotationforSensor = async (req, reply) => { const newQuotation = new SensorQuotation({ quatationId, customerId, + surveyId, quote_status: "sentfromsurvey", masters, @@ -1559,6 +1560,7 @@ exports.createquotationforSensor = async (req, reply) => { message: 'Quotation for sensors created successfully.', data: savedQuotation, }); + } catch (error) { console.error('Error creating quotation:', error); reply.code(500).send({ @@ -1679,17 +1681,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 }); } };