diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index cdfa6edb..f7239cdc 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -10,39 +10,11 @@ const fastify = require("fastify")({ return uuidv4(); }, }); -const {Iotprice, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart, Sales} = require("../models/store"); +const { 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') -const populateIotPrices = async () => { - try { - const pricingData = [ - { name: "master", type: "master", cost: 20000 }, - { name: "slave", type: "slave", cost: 10000 }, - { name: "sensor", type: "sensor", cost: 10000 }, - { name: "wire", type: "1core", cost: 100 }, - { name: "wire", type: "2core", cost: 150 }, - { name: "wire", type: "3core", cost: 200 }, - { name: "switch", type: "15A", cost: 20 }, - { name: "switch", type: "20A", cost: 30 }, - ]; - - // Insert or update pricing data - for (const item of pricingData) { - await Iotprice.findOneAndUpdate( - { name: item.name, type: item.type }, // Match existing entries - { $set: { cost: item.cost } }, // Update cost if exists - { upsert: true, new: true } // Insert if not found - ); - } - - console.log("Pricing data populated successfully"); - } catch (error) { - console.error("Error populating pricing data:", error); - } -}; -populateIotPrices(); @@ -1268,7 +1240,7 @@ exports.updateSensorQC = async (req, reply) => { const { _id } = req.params; let updateData = req.body; - const allowedFields = ["qccheck", "qcby", "comments", "status"]; + const allowedFields = ["qccheck", "qcby", "comment","status"]; // Filter only allowed fields const filteredUpdateData = Object.keys(updateData) @@ -1281,11 +1253,6 @@ exports.updateSensorQC = async (req, reply) => { // Update qccheckdate with the current date in "DD-MMM-YYYY - HH:MM" format filteredUpdateData.qccheckdate = moment().format("DD-MMM-YYYY - HH:mm"); - // Update status based on qccheck value - if (filteredUpdateData.qccheck) { - filteredUpdateData.status = filteredUpdateData.qccheck.toLowerCase() === "ok" ? "available" : "rejected"; - } - const updatedSensor = await Insensors.findByIdAndUpdate( _id, filteredUpdateData, @@ -1553,49 +1520,6 @@ exports.createquotationforSensor = async (req, reply) => { }; - -exports.createQuotationForSensor = async (req, reply) => { - try { - const items = req.body; - - if (!Array.isArray(items) || items.length === 0) { - return reply.code(400).send({ message: "Invalid request body. Expecting an array of items." }); - } - - // Fetch prices from IotPrice based on name and type - const itemNamesAndTypes = items.map((item) => ({ name: item.name, type: item.type })); - const prices = await Iotprice.find({ $or: itemNamesAndTypes }); - - let totalAmount = 0; - let itemDetails = []; - - for (const item of items) { - const priceEntry = prices.find((p) => p.name === item.name && p.type === item.type); - const unitPrice = priceEntry ? priceEntry.cost : 0; - const totalCost = unitPrice * item.quantity; - totalAmount += totalCost; - - itemDetails.push({ - name: item.name, - type: item.type, - quantity: item.quantity, - unitPrice, - totalCost - }); - } - - return reply.code(200).send({ - items: itemDetails, - totalEstimatedAmount: totalAmount - }); - - } catch (error) { - console.error("Error calculating quotation:", error); - return reply.code(500).send({ message: "Failed to calculate quotation" }); - } -}; - - exports.editQuotationForSensor = async (req, reply) => { try { const { quatationId } = req.params; // Get the ID of the quotation to edit @@ -1646,6 +1570,59 @@ exports.editQuotationForSensor = async (req, reply) => { }; +exports.createEstimationPrice = async (req, reply) => { + try { + const { items } = req.body; + + if (!Array.isArray(items) || items.length === 0) { + return reply.code(400).send({ message: "Items array is required and cannot be empty" }); + } + + let totalEstimation = 0; + const itemDetails = []; + + for (const item of items) { + const { name, type, quantity } = item; + + // Fetch the unit price from IotPrice collection + const priceEntry = await IotPrice.findOne({ name, type }); + + if (!priceEntry) { + itemDetails.push({ + name, + type, + quantity, + unitPrice: null, + totalCost: null, + message: "Price not found" + }); + continue; + } + + const unitPrice = priceEntry.cost; + const totalCost = unitPrice * quantity; + totalEstimation += totalCost; + + itemDetails.push({ + name, + type, + quantity, + unitPrice, + totalCost + }); + } + + return reply.code(200).send({ + items: itemDetails, + estimatedTotal: totalEstimation + }); + + } catch (error) { + console.error("Error calculating estimation:", error); + return reply.code(500).send({ message: "Failed to calculate estimation" }); + } +}; + exports.getallquotationdata = async (req, reply) => { try { diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 03190e79..2ec56e7e 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1310,28 +1310,34 @@ fastify.post("/api/createquotationforSensor/:installationId", { handler: storeController.createquotationforSensor, }); -fastify.post("/api/createquotationforSensor", { +fastify.post("/api/createEstimationPrice", { schema: { - description: "Create a quotation for sensors", + description: "Calculate estimated cost for a list of items", tags: ["Install"], - summary: "Calculate estimated cost for sensors", + summary: "Fetch unit prices from IotPrice and calculate total price for all items", body: { - type: "array", - items: { - type: "object", - properties: { - name: { type: "string", description: "Item name" }, - type: { type: "string", description: "Item type" }, - quantity: { type: "integer", description: "Quantity of the item" } - }, - required: ["name", "type", "quantity"] - } + type: "object", + properties: { + items: { + type: "array", + description: "List of items", + items: { + type: "object", + properties: { + name: { type: "string", description: "Item name" }, + type: { type: "string", description: "Item type" }, + quantity: { type: "integer", description: "Quantity of the item" } + }, + required: ["name", "type", "quantity"] + } + } + }, + required: ["items"] } }, - handler: storeController.createQuotationForSensor, + handler: estimationController.createEstimationPrice }); - fastify.post("/api/editQuotationForSensor/:quatationId", { schema: { description: "This is for edit quotation for sensors",