ashok 10 months ago
commit 97b1516a1e

@ -1541,6 +1541,7 @@ exports.createquotationforSensor = async (req, reply) => {
const newQuotation = new SensorQuotation({ const newQuotation = new SensorQuotation({
quatationId, quatationId,
customerId, customerId,
surveyId, surveyId,
quote_status: "sentfromsurvey", quote_status: "sentfromsurvey",
masters, masters,
@ -1559,6 +1560,7 @@ exports.createquotationforSensor = async (req, reply) => {
message: 'Quotation for sensors created successfully.', message: 'Quotation for sensors created successfully.',
data: savedQuotation, data: savedQuotation,
}); });
} catch (error) { } catch (error) {
console.error('Error creating quotation:', error); console.error('Error creating quotation:', error);
reply.code(500).send({ reply.code(500).send({
@ -1679,17 +1681,30 @@ exports.createEstimationPrice = async (req, reply) => {
exports.getallquotationdata = async (req, reply) => { exports.getallquotationdata = async (req, reply) => {
try { try {
await SensorQuotation.find({}) const quotations = await SensorQuotation.find({}).lean(); // Use lean() for better performance
.exec()
.then((docs) => { // Extract unique customerIds from quotations
reply.send({ status_code: 200, data: docs, count: docs.length }); const customerIds = [...new Set(quotations.map((q) => q.customerId).filter(Boolean))];
})
.catch((err) => { // Fetch customer details for all unique customerIds
console.log(err); const customers = await User.find({ customerId: { $in: customerIds } }).lean();
reply.send({ error: err });
}); // 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) { } catch (err) {
throw boom.boomify(err); console.error(err);
reply.send({ error: err.message });
} }
}; };

Loading…
Cancel
Save