From 6b9e817084557fdf515f6d3deae9dc5635723d9a Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 7 Nov 2024 12:57:38 +0530 Subject: [PATCH] Hardware cart and service cart --- src/controllers/storeController.js | 60 +++++++++++++++++++++++++++++- src/models/store.js | 35 ++++++++++++++++- src/routes/storeRoute.js | 47 +++++++++++++++++++++++ 3 files changed, 140 insertions(+), 2 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 2385621e..5465460f 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -10,7 +10,7 @@ const fastify = require("fastify")({ return uuidv4(); }, }); -const { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId} = require("../models/store"); +const { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart} = require("../models/store"); const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture} = require('../models/User') @@ -1228,3 +1228,61 @@ exports.getSinleQuotationData = async (req, reply) => { }); } }; + + +exports.addToCartHardwareItems = async (req, reply) => { + try { + const { productId, productName, description, GST, unitPrice, quantity, totalAmount, serialId } = req.body; + + const gstAmount = (GST / 100) * totalAmount; + const grandTotal = totalAmount + gstAmount; + + const newCartItem = new HardwareCart({ + productId, + productName, + description, + GST, + unitPrice, + quantity, + totalAmount, + grandTotal, + serialId + }); + + const savedItem = await newCartItem.save(); + + reply.code(201).send({ message: 'Item added to cart', data: savedItem }); + } catch (error) { + console.error(error); + reply.code(500).send({ message: 'Error adding item to cart', error }); + } +}; + +exports.addToCartService = async (req, reply) => { + try { + const { productId, productName, description, GST, unitPrice, quantity, totalAmount, serialId,installationId } = req.body; + + const gstAmount = (GST / 100) * totalAmount; + const grandTotal = totalAmount + gstAmount; + + const newCartItem = new ServiceCart({ + installationId, + productId, + productName, + description, + GST, + unitPrice, + quantity, + totalAmount, + grandTotal, + serialId + }); + + const savedItem = await newCartItem.save(); + + reply.code(201).send({ message: 'Item added to cart', data: savedItem }); + } catch (error) { + console.error(error); + reply.code(500).send({ message: 'Error adding item to cart', error }); + } +}; \ No newline at end of file diff --git a/src/models/store.js b/src/models/store.js index 15146ae1..838e2387 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -289,7 +289,38 @@ const sensorquotationSchema = new mongoose.Schema({ qutation_total_price: { type: String, default: null }, }); +const hardwareCartSchema = new mongoose.Schema({ + productId: { type: String}, + productName: { type: String }, + description: { type: String, default: null }, + GST: { type: Number, min: 0 }, + unitPrice: { type: Number, min: 0 }, + quantity: { type: Number, min: 1 }, + grandTotal: { type: Number, min: 0 }, + totalAmount: { type: Number, min: 0 }, // Amount before GST + serialId: { type: String, default: null }, + category: { type: String, enum: ['slaves', 'master', 'switches'], default: 'slaves' }, + discount: { type: Number, default: 0, min: 0 }, +}, { + timestamps: true, +}); +const serviceCartSchema = new mongoose.Schema({ + installationId: {type: String}, + productId: { type: String}, + productName: { type: String }, + description: { type: String, default: null }, + GST: { type: Number, min: 0 }, + unitPrice: { type: Number, min: 0 }, + quantity: { type: Number, min: 1 }, + grandTotal: { type: Number, min: 0 }, + totalAmount: { type: Number, min: 0 }, // Amount before GST + serialId: { type: String, default: null }, + category: { type: String, enum: ['slaves', 'master', 'switches'], default: 'slaves' }, + discount: { type: Number, default: 0, min: 0 }, +}, { + timestamps: true, +}); const Insensors = mongoose.model('Insensors', insensorsSchema); const Store = mongoose.model("Store", storeSchema); @@ -300,6 +331,8 @@ const sensorquotationSchema = new mongoose.Schema({ const SensorQuotation = mongoose.model('SensorQuotationSchema', sensorquotationSchema); const Install = mongoose.model("Install", installationschema); + const HardwareCart = mongoose.model("HardwareCart", hardwareCartSchema); + const ServiceCart = mongoose.model("ServiceCart", serviceCartSchema); - module.exports = { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId}; + module.exports = { 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 a179b77a..b48cc9b1 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -881,5 +881,52 @@ fastify.get("/api/getSingleQuotationData/:quotationId", { handler: storeController.getSinleQuotationData, }); +fastify.post("/api/cart/hardwareItem", { + schema: { + description: "To add items to the Hardwarecart", + tags: ["Cart"], + summary: "Add item to Hardwarecart", + body: { + type: "object", + properties: { + productId: { type: "string", description: "Unique identifier for the product" }, + productName: { type: "string", description: "Name of the product" }, + description: { type: "string", description: "Product description", default: null }, + GST: { type: "number", description: "GST applied to the product", minimum: 0 }, + unitPrice: { type: "number", description: "Unit price of the product", minimum: 0 }, + quantity: { type: "integer", description: "Quantity of the product", minimum: 1 }, + totalAmount: { type: "number", description: "Total amount before GST", minimum: 0 }, + serialId: { type: "string", description: "Serial identifier for the cart item", default: null } + }, + //required: ["productId", "productName", "GST", "unitPrice", "quantity", "totalAmount"] + }, + }, + handler: storeController.addToCartHardwareItems +}); + +fastify.post("/api/cart/installationService", { + schema: { + description: "To add items to the Installation Service", + tags: ["Cart"], + summary: "Add item to Installation Service", + body: { + type: "object", + properties: { + installationId: { type: "string" }, + + productId: { type: "string"}, + productName: { type: "string" }, + description: { type: "string" }, + GST: { type: "number", description: "GST applied to the product", minimum: 0 }, + unitPrice: { type: "number", description: "Unit price of the product", minimum: 0 }, + quantity: { type: "integer", description: "Quantity of the product", minimum: 1 }, + totalAmount: { type: "number", description: "Total amount before GST", minimum: 0 }, + serialId: { type: "string", description: "Serial identifier for the cart item", default: null } + }, + //required: ["productId", "productName", "GST", "unitPrice", "quantity", "totalAmount"] + }, + }, + handler: storeController.addToCartService +}); next(); };