added order to iots with edit

master^2
Varun 7 months ago
parent 5b2c4aac47
commit b63486484d

@ -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" });
}
};

@ -280,7 +280,13 @@ const iotpriceSchema = new mongoose.Schema({
cost: { type: Number, default: null },
});
const orderSchema = new mongoose.Schema({
orderId: { type: String, unique: true, required: true },
customerId: { type: String, required: true },
items: { type: Array, required: true },
estimatedTotal: { type: Number, required: true },
status: { type: String, default: "pending" },
}, { timestamps: true });
const sensorquotationSchema = new mongoose.Schema({
@ -412,6 +418,8 @@ const salesSchema = new mongoose.Schema({
const Iotprice = mongoose.model('Iotprice', iotpriceSchema);
const Insensors = mongoose.model('Insensors', insensorsSchema);
const Orders = mongoose.model('Orders', orderSchema);
const Store = mongoose.model("Store", storeSchema);
const WaterLeverSensor = mongoose.model('WaterLeverSensor', waterLeverSensorInSchema);
const ProfilePictureStore = mongoose.model('ProfilePictureStore', profilePictureStoreSchema);
@ -425,4 +433,4 @@ const Iotprice = mongoose.model('Iotprice', iotpriceSchema);
const Sales = mongoose.model("Sales", salesSchema);
module.exports = {Iotprice,Sales, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart};
module.exports = {Orders,Iotprice,Sales, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart};

@ -1338,6 +1338,74 @@ fastify.post("/api/createEstimationPrice", {
handler: storeController.createEstimationPrice
});
fastify.post("/api/handleEstimation", {
schema: {
description: "Accept or reject the estimated price",
tags: ["Install"],
summary: "Handles user action for estimated price",
body: {
type: "object",
properties: {
customerId: { type: "string", description: "Customer ID" },
items: {
type: "array",
description: "List of items with price details",
items: {
type: "object",
properties: {
name: { type: "string", description: "Item name" },
type: { type: "string", description: "Item type" },
quantity: { type: "string", description: "Quantity of the item" },
unitPrice: { type: "number", description: "Unit price" },
totalCost: { type: "number", description: "Total cost" }
},
required: ["name", "type", "quantity", "unitPrice", "totalCost"]
}
},
estimatedTotal: { type: "number", description: "Total estimated cost" },
action: { type: "string", enum: ["accept", "reject"], description: "User decision" }
},
required: ["customerId", "items", "estimatedTotal", "action"]
}
},
handler: storeController.handleEstimation
});
fastify.put("/api/editOrder", {
schema: {
description: "Edit an existing order based on orderId",
tags: ["Orders"],
summary: "Modify items and update the estimated total",
body: {
type: "object",
properties: {
orderId: { type: "string", description: "Order ID to be updated" },
customerId: { type: "string", description: "Customer ID associated with the order" },
items: {
type: "array",
description: "Updated list of items",
items: {
type: "object",
properties: {
name: { type: "string", description: "Item name" },
type: { type: "string", description: "Item type" },
quantity: { type: "string", description: "Quantity of the item" },
unitPrice: { type: "number", description: "Unit price of the item" },
totalPrice: { type: "number", description: "Total price of the item" }
},
required: ["name", "type", "quantity", "unitPrice", "totalPrice"]
}
},
estimatedTotal: { type: "number", description: "Updated estimated total cost" }
},
required: ["orderId", "customerId", "items", "estimatedTotal"]
}
},
handler: storeController.editOrder
});
fastify.post("/api/editQuotationForSensor/:quatationId", {
schema: {
description: "This is for edit quotation for sensors",

Loading…
Cancel
Save