diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index a20f362f..cdfa6edb 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -10,11 +10,39 @@ const fastify = require("fastify")({ return uuidv4(); }, }); -const { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart, Sales} = require("../models/store"); +const {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') +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(); @@ -1525,6 +1553,49 @@ 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 diff --git a/src/models/store.js b/src/models/store.js index 382dd07a..00261d61 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -274,6 +274,11 @@ const insensorsSchema = new mongoose.Schema({ }); +const iotpriceSchema = new mongoose.Schema({ + name: { type: String }, + type: { type: String ,default:null}, + cost: { type: Number, default: null }, +}); @@ -405,6 +410,7 @@ const salesSchema = new mongoose.Schema({ updatedBy: ObjectId, }, { versionKey: false }); +const Iotprice = mongoose.model('Iotprice', iotpriceSchema); const Insensors = mongoose.model('Insensors', insensorsSchema); const Store = mongoose.model("Store", storeSchema); const WaterLeverSensor = mongoose.model('WaterLeverSensor', waterLeverSensorInSchema); @@ -419,4 +425,4 @@ const salesSchema = new mongoose.Schema({ const Sales = mongoose.model("Sales", salesSchema); - module.exports = {Sales, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart}; + module.exports = {Iotprice,Sales, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart}; diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 4731289f..03190e79 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1310,6 +1310,26 @@ fastify.post("/api/createquotationforSensor/:installationId", { handler: storeController.createquotationforSensor, }); +fastify.post("/api/createquotationforSensor", { + schema: { + description: "Create a quotation for sensors", + tags: ["Install"], + summary: "Calculate estimated cost for sensors", + 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"] + } + } + }, + handler: storeController.createQuotationForSensor, +}); fastify.post("/api/editQuotationForSensor/:quatationId", {