diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index f7d5bd65..875f392d 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -11,7 +11,7 @@ const fastify = require("fastify")({ }, }); -const {Order,Iotprice, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart, Sales} = require("../models/store"); +const {Order,EstimationOrder,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') @@ -1695,7 +1695,7 @@ exports.getallquotationdata = async (req, reply) => { return acc; }, {}); - + // Attach customer details to quotations const enrichedQuotations = quotations.map((quotation) => ({ ...quotation, @@ -1711,6 +1711,8 @@ exports.getallquotationdata = async (req, reply) => { + + exports.saveQuotationData = async (req, reply) => { try { const { quotationId } = req.params; // Retrieve the quotationId from the request parameters @@ -1918,7 +1920,7 @@ exports.handleEstimation = async (req, reply) => { } // If accepted, generate unique Order ID - const lastOrder = await Order.findOne().sort({ createdAt: -1 }); + const lastOrder = await EstimationOrder.findOne().sort({ createdAt: -1 }); let orderId = "AWS001"; if (lastOrder) { const lastNumber = parseInt(lastOrder.orderId.replace("AWS", ""), 10) + 1; @@ -1926,7 +1928,7 @@ exports.handleEstimation = async (req, reply) => { } // Create a new order in the database - const newOrder = new Order({ + const newOrder = new EstimationOrder({ orderId, customerId, items, @@ -1960,7 +1962,7 @@ exports.editOrder = async (req, reply) => { } // Find the existing order - const existingOrder = await Order.findOne({ orderId }); + const existingOrder = await EstimationOrder.findOne({ orderId }); if (!existingOrder) { return reply.code(404).send({ message: "Order not found" }); @@ -1995,7 +1997,7 @@ exports.getOrdersByCustomer = async (req, reply) => { } // Fetch orders with status 'pending' or 'accepted' for the given customer - const orders = await Order.find({ + const orders = await EstimationOrder.find({ customerId, status: { $in: ["pending", "accepted"] } }); @@ -2016,3 +2018,42 @@ exports.getOrdersByCustomer = async (req, reply) => { } }; +exports.acceptQuotation = async (req, reply) => { + try { + const { quotationId } = req.params; + const { action } = req.body; + + if (action !== "accept") { + return reply.status(400).send({ error: "Invalid action" }); + } + + // Find the quotation by ID + const quotation = await SensorQuotation.findOne({ quatationId: quotationId }); + + if (!quotation) { + return reply.status(404).send({ error: "Quotation not found" }); + } + + // Convert quotation to an order object + const newOrder = new Order({ + ...quotation.toObject(), // Copy all fields + status: "pending", // Set status to "pending" + }); + + // Save to the Orders collection + await newOrder.save(); + + // Delete the record from SensorQuotation + await SensorQuotation.deleteOne({ quatationId: quotationId }); + + return reply.send({ + status_code: 200, + message: "Quotation accepted and moved to Orders", + data: newOrder, + }); + + } catch (err) { + console.error("Error accepting quotation:", err); + return reply.status(500).send({ error: "Internal server error" }); + } +}; \ No newline at end of file diff --git a/src/models/store.js b/src/models/store.js index cc2a7031..68f52458 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -370,7 +370,7 @@ const iotpriceSchema = new mongoose.Schema({ cost: { type: Number, default: null }, }); -const orderSchema = new mongoose.Schema({ +const estimationorderSchema = new mongoose.Schema({ orderId: { type: String, unique: true, required: true }, customerId: { type: String, required: true }, items: { type: Array, required: true }, @@ -382,6 +382,7 @@ const orderSchema = new mongoose.Schema({ const sensorquotationSchema = new mongoose.Schema({ customerId: { type: String }, surveyId: { type: String, default: null }, + storeId: { type: String, default: null }, installationId: { type: String, default: null }, quatationId: { type: String, default: null }, masters: { type: String }, @@ -405,9 +406,13 @@ const sensorquotationSchema = new mongoose.Schema({ wire: { type: String, default: null }, switch: { type: String, default: null }, text: { type: String, default: null }, + available_quantity: { type: String, default: null }, }, ], master_type_quantity_price: { type: String, default: null }, + master_available_quantity: { type: String, default: null }, + slave_available_quantity: { type: String, default: null }, + sensor_available_quantity: { type: String, default: null }, master_type_total_price: { type: String, default: null }, sensor_type_quantity_price: { type: String , default: null}, sensor_type_total_price: { type: String , default: null}, @@ -416,6 +421,56 @@ const sensorquotationSchema = new mongoose.Schema({ qutation_total_price: { type: String, default: null }, }); + + + + +const orderSchema = new mongoose.Schema({ + customerId: { type: String }, + surveyId: { type: String, default: null }, + storeId: { type: String, default: null }, + installationId: { type: String, default: null }, + quatationId: { type: String, default: null }, + masters: { type: String }, + masters_quantity_price: { type: String }, + masters_total_price: { type: String }, + slaves: { type: String }, + sensors: { type: String }, + slaves_quantity_price: { type: String }, + slaves_total_price: { type: String }, + motor_switches: { type: String }, + motor_switches_quantity_price: { type: String }, + motor_switches_total_price: { type: String }, + quote_status: { type: String, default: null }, + quoted_amount: { type: String, default: null }, + comments: { type: String, default: null }, + datetime: { type: String, default: null }, + updated_at: { type: String, default: null }, + electricals: [ + { + type: { type: String, default: null }, + wire: { type: String, default: null }, + switch: { type: String, default: null }, + text: { type: String, default: null }, + available_quantity: { type: String, default: null }, + }, + ], + master_type_quantity_price: { type: String, default: null }, + master_available_quantity: { type: String, default: null }, + slave_available_quantity: { type: String, default: null }, + sensor_available_quantity: { type: String, default: null }, + master_type_total_price: { type: String, default: null }, + sensor_type_quantity_price: { type: String, default: null }, + sensor_type_total_price: { type: String, default: null }, + switch_type_quantity_price: { type: String, default: null }, + switch_type_total_price: { type: String, default: null }, + qutation_total_price: { type: String, default: null }, + status: { type: String, default: "pending" }, // Status field added +}); + + + + const hardwareCartSchema = new mongoose.Schema({ productId: { type: String}, productName: { type: String }, @@ -510,7 +565,9 @@ const salesSchema = new mongoose.Schema({ const Iotprice = mongoose.model('Iotprice', iotpriceSchema); const Insensors = mongoose.model('Insensors', insensorsSchema); - const Order = mongoose.model('Orders', orderSchema); + const Order = mongoose.model('Order', orderSchema); + const EstimationOrder = mongoose.model('EstimationOrder', estimationorderSchema); + const Store = mongoose.model("Store", storeSchema); const WaterLeverSensor = mongoose.model('WaterLeverSensor', waterLeverSensorInSchema); const ProfilePictureStore = mongoose.model('ProfilePictureStore', profilePictureStoreSchema); @@ -527,4 +584,4 @@ const Iotprice = mongoose.model('Iotprice', iotpriceSchema); - module.exports = {Order,Iotprice,Sales, Install,Survey, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart}; + module.exports = {Order,EstimationOrder,Iotprice,Sales, Install,Survey, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart}; diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index e788bcd1..791f26c2 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1654,5 +1654,34 @@ fastify.post("/api/cart/installationService", { }, handler: storeController.addToCartService }); + + +fastify.post("/api/acceptquotation/:quotationId", { + schema: { + tags: ["Install"], + description: "Accepts a quotation and moves it to the Orders database", + summary: "Accepts a quotation", + params: { + type: "object", + properties: { + quotationId: { type: "string" }, + }, + }, + body: { + type: "object", + properties: { + action: { type: "string" }, + }, + required: ["action"], + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: fastify.auth([fastify.authenticate]), // Uncomment if authentication is needed + handler: storeController.acceptQuotation, +}); next(); };