diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index df750161..22e81a3b 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -3,7 +3,7 @@ const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const customJwtAuth = require("../customAuthJwt"); const { Deparments } = require("../models/Department"); -const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData, ElectrictyWorkPictures, PlumbingWorkPictures, MaterialRecievedPictures, Support } = require("../models/store"); +const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData, ElectrictyWorkPictures, PlumbingWorkPictures, MaterialRecievedPictures, Support, Repairorder } = require("../models/store"); const { Counter, User } = require("../models/User"); const { IotData, Tank } = require("../models/tanks"); const moment = require('moment-timezone'); @@ -10923,3 +10923,48 @@ exports.resolveIssueIfAllConnected = async (req, reply) => { return reply.code(500).send({ error: "Internal Server Error" }); } }; + + + +exports.createRepairOrder = async (req, reply) => { + try { + const { supportId, customerId } = req.params; + const { storeId, replacements, status } = req.body; + + // Validate required path params + if (!supportId || !customerId) { + return reply.code(400).send({ error: "supportId and customerId are required in path params" }); + } + + // Validate replacements + if (!Array.isArray(replacements) || replacements.length === 0) { + return reply.code(400).send({ error: "Replacements array is required in body and must contain at least one item" }); + } + + for (const r of replacements) { + if (!["master", "slave", "sensor"].includes(r.type) || !r.oldHardwareId || !r.newHardwareId) { + return reply.code(400).send({ error: "Each replacement must have valid type, oldHardwareId, and newHardwareId" }); + } + } + + + // Create the repair order + const newRepairOrder = await Repairorder.create({ + customerId, + supportId, + storeId, + status: status || "pending", + replacements + }); + + return reply.send({ + status_code: 201, + message: "Repair order created successfully", + repairOrder: newRepairOrder + }); + + } catch (error) { + console.error("Error creating repair order:", error); + return reply.code(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 4d71e225..372805cb 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -1079,8 +1079,11 @@ const materialRecievedPicturesSchema = new Schema({ const RepairorderSchema = new mongoose.Schema({ customerId: { type: String, required: true }, - packageId: { type: String, required: true }, - otp: { type: String, required: true }, + supportId: { type: String }, + storeId: { type: String }, + status: { type: String, default: "pending" }, + packageId: { type: String,}, + otp: { type: String, }, replacements: [ { type: { @@ -1089,7 +1092,7 @@ const RepairorderSchema = new mongoose.Schema({ required: true }, oldHardwareId: { type: String, required: true }, - newHardwareId: { type: String, required: true } + newHardwareId: { type: String } } ], createdAt: { type: Date, default: Date.now } diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 40aff23f..f6c48103 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -918,6 +918,55 @@ module.exports = function (fastify, opts, next) { handler: installationController.sendToStoreHardwareList }); + fastify.post('/api/repair-order/:supportId/:customerId',{ + schema: { + summary: 'Create a new Repair Order', + description: 'Creates a repair order for the given supportId and customerId with replacements.', + tags: ['Repair Orders'], + params: { + type: 'object', + required: ['supportId', 'customerId'], + properties: { + supportId: { type: 'string', description: 'Support ID' }, + customerId: { type: 'string', description: 'Customer ID' } + } + }, + body: { + type: 'object', + required: ['replacements'], + properties: { + storeId: { type: 'string', description: 'Store ID where the repair is logged' }, + status: { + type: 'string', + enum: ['pending', 'completed'], + default: 'pending', + description: 'Status of the repair order' + }, + replacements: { + type: 'array', + description: 'List of hardware replacements', + items: { + type: 'object', + required: ['type', 'oldHardwareId', 'newHardwareId'], + properties: { + type: { + type: 'string', + enum: ['master', 'slave', 'sensor'], + description: 'Type of the hardware being replaced' + }, + oldHardwareId: { type: 'string', description: 'Old hardware ID' }, + newHardwareId: { type: 'string', description: 'New hardware ID' } + } + }, + minItems: 1 + } + } + }, + }, + handler: installationController.createRepairOrder + + }); + fastify.post("/api/assignTeamMemberIssueToCategory/:supportId", { schema: {