|  |  |  | @ -10,7 +10,7 @@ const fastify = require("fastify")({ | 
			
		
	
		
			
				
					|  |  |  |  |     return uuidv4(); | 
			
		
	
		
			
				
					|  |  |  |  |   }, | 
			
		
	
		
			
				
					|  |  |  |  | }); | 
			
		
	
		
			
				
					|  |  |  |  | const {Iotprice, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart, Sales} = require("../models/store"); | 
			
		
	
		
			
				
					|  |  |  |  | const {Orders,Iotprice, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart, Sales} = require("../models/store"); | 
			
		
	
		
			
				
					|  |  |  |  | const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture} = require('../models/User') | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
	
		
			
				
					|  |  |  | @ -1495,7 +1495,7 @@ exports.createquotationforSensor = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |     const masterPrice = await getPrice("master", "master"); | 
			
		
	
		
			
				
					|  |  |  |  |     const slavePrice = await getPrice("slave", "slave"); | 
			
		
	
		
			
				
					|  |  |  |  |     const sensorPrice = await getPrice("sensor", "sensor"); | 
			
		
	
		
			
				
					|  |  |  |  |     const motorSwitchPrice = await getPrice("motor_switch", "motor_switche"); | 
			
		
	
		
			
				
					|  |  |  |  |     const motorSwitchPrice = await getPrice("motor_switch", "motor_switches"); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // Calculate price for electricals
 | 
			
		
	
		
			
				
					|  |  |  |  |     let electricalPrice = 0; | 
			
		
	
	
		
			
				
					|  |  |  | @ -1850,4 +1850,96 @@ exports.getquotationofinstalleranduser = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |   } catch (err) { | 
			
		
	
		
			
				
					|  |  |  |  |     throw boom.boomify(err); | 
			
		
	
		
			
				
					|  |  |  |  |   } | 
			
		
	
		
			
				
					|  |  |  |  | }; | 
			
		
	
		
			
				
					|  |  |  |  | }; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | exports.handleEstimation = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |   try { | 
			
		
	
		
			
				
					|  |  |  |  |     const { customerId, items, estimatedTotal, action } = req.body; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (!customerId) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(400).send({ message: "customerId is required" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  |     if (!Array.isArray(items) || items.length === 0) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(400).send({ message: "Items array is required and cannot be empty" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  |     if (!estimatedTotal) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(400).send({ message: "Estimated total is required" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  |     if (!["accept", "reject"].includes(action)) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(400).send({ message: "Invalid action, must be 'accept' or 'reject'" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // If rejected, return a response without creating an order
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (action === "reject") { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(200).send({ message: "Estimation rejected" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // If accepted, generate unique Order ID
 | 
			
		
	
		
			
				
					|  |  |  |  |     const lastOrder = await Order.findOne().sort({ createdAt: -1 }); | 
			
		
	
		
			
				
					|  |  |  |  |     let orderId = "AWS001"; | 
			
		
	
		
			
				
					|  |  |  |  |     if (lastOrder) { | 
			
		
	
		
			
				
					|  |  |  |  |       const lastNumber = parseInt(lastOrder.orderId.replace("AWS", ""), 10) + 1; | 
			
		
	
		
			
				
					|  |  |  |  |       orderId = `AWS${String(lastNumber).padStart(3, "0")}`; | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // Create a new order in the database
 | 
			
		
	
		
			
				
					|  |  |  |  |     const newOrder = new Order({ | 
			
		
	
		
			
				
					|  |  |  |  |       orderId, | 
			
		
	
		
			
				
					|  |  |  |  |       customerId, | 
			
		
	
		
			
				
					|  |  |  |  |       items, | 
			
		
	
		
			
				
					|  |  |  |  |       estimatedTotal, | 
			
		
	
		
			
				
					|  |  |  |  |       status: "pending" | 
			
		
	
		
			
				
					|  |  |  |  |     }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     await newOrder.save(); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     return reply.code(201).send({ | 
			
		
	
		
			
				
					|  |  |  |  |       message: "Order created successfully", | 
			
		
	
		
			
				
					|  |  |  |  |       orderId, | 
			
		
	
		
			
				
					|  |  |  |  |       customerId, | 
			
		
	
		
			
				
					|  |  |  |  |       estimatedTotal, | 
			
		
	
		
			
				
					|  |  |  |  |       items, | 
			
		
	
		
			
				
					|  |  |  |  |       status: "pending" | 
			
		
	
		
			
				
					|  |  |  |  |     }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   } catch (error) { | 
			
		
	
		
			
				
					|  |  |  |  |     console.error("Error handling estimation:", error); | 
			
		
	
		
			
				
					|  |  |  |  |     return reply.code(500).send({ message: "Failed to process estimation" }); | 
			
		
	
		
			
				
					|  |  |  |  |   } | 
			
		
	
		
			
				
					|  |  |  |  | }; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | exports.editOrder = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |   try { | 
			
		
	
		
			
				
					|  |  |  |  |     const { orderId, customerId, items, estimatedTotal } = req.body; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (!orderId) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(400).send({ message: "orderId is required" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // Find the existing order
 | 
			
		
	
		
			
				
					|  |  |  |  |     const existingOrder = await Order.findOne({ orderId }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (!existingOrder) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(404).send({ message: "Order not found" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // Update the order in the database
 | 
			
		
	
		
			
				
					|  |  |  |  |     existingOrder.customerId = customerId; | 
			
		
	
		
			
				
					|  |  |  |  |     existingOrder.items = items; | 
			
		
	
		
			
				
					|  |  |  |  |     existingOrder.estimatedTotal = estimatedTotal; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     await existingOrder.save(); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     return reply.code(200).send({ | 
			
		
	
		
			
				
					|  |  |  |  |       message: "Order updated successfully", | 
			
		
	
		
			
				
					|  |  |  |  |       orderId, | 
			
		
	
		
			
				
					|  |  |  |  |       updatedItems: items, | 
			
		
	
		
			
				
					|  |  |  |  |       estimatedTotal | 
			
		
	
		
			
				
					|  |  |  |  |     }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   } catch (error) { | 
			
		
	
		
			
				
					|  |  |  |  |     console.error("Error updating order:", error); | 
			
		
	
		
			
				
					|  |  |  |  |     return reply.code(500).send({ message: "Failed to update order" }); | 
			
		
	
		
			
				
					|  |  |  |  |   } | 
			
		
	
		
			
				
					|  |  |  |  | }; | 
			
		
	
	
		
			
				
					|  |  |  | 
 |