diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 33f8e266..d47afff5 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1688,13 +1688,59 @@ return { }; +// exports.addMediaToInsensor = async (req, reply) => { +// try { +// const { hardwareId, customerId, type } = req.params; +// const { urls, mediaType } = req.body; + +// if (!hardwareId || !customerId || !type || !urls || !mediaType) { +// return reply.status(400).send({ success: false, message: "Missing required fields" }); +// } + +// const insensor = await Insensors.findOne({ hardwareId, customerId, type }); +// if (!insensor) { +// return reply.status(404).send({ success: false, message: "Insensor not found" }); +// } + +// const mediaItems = urls.map(url => ({ url, createdAt: new Date() })); + +// if (mediaType === 'video') { +// insensor.manualTestVideos.push(...mediaItems); +// } else if (mediaType === 'material') { +// insensor.materialReceivedPictures.push(...mediaItems); +// } else if (mediaType === 'workStatus') { +// insensor.workStatusPictures.push(...mediaItems); +// } else { +// return reply.status(400).send({ success: false, message: "Invalid mediaType" }); +// } + +// await insensor.save(); + +// // ✅ fetch the updated document as plain object +// const updatedInsensor = await Insensors.findOne({ hardwareId, customerId, type }).lean(); +// console.log("updatedInsensor",updatedInsensor) +// return reply.send({ +// success: true, +// message: "Media saved successfully", +// data: updatedInsensor +// }); +// } catch (error) { +// console.error("Error adding media to insensor:", error); +// return reply.status(500).send({ success: false, message: "Internal server error" }); +// } +// }; + + exports.addMediaToInsensor = async (req, reply) => { try { - const { hardwareId, customerId, type } = req.params; - const { urls, mediaType } = req.body; + // const { hardwareId, customerId, type } = req.params; + // const { video, material, workStatus, product_status } = req.body; + const { customerId } = req.params; + const { hardwareId, type, video, material, workStatus, product_status } = req.body; + - if (!hardwareId || !customerId || !type || !urls || !mediaType) { - return reply.status(400).send({ success: false, message: "Missing required fields" }); + if (!hardwareId || !customerId || !type) { + return reply.status(400).send({ success: false, message: "Missing required params" }); } const insensor = await Insensors.findOne({ hardwareId, customerId, type }); @@ -1702,27 +1748,34 @@ exports.addMediaToInsensor = async (req, reply) => { return reply.status(404).send({ success: false, message: "Insensor not found" }); } - const mediaItems = urls.map(url => ({ url, createdAt: new Date() })); + // Append if arrays are provided + if (Array.isArray(video) && video.length) { + const items = video.map(url => ({ url, createdAt: new Date() })); + insensor.manualTestVideos.push(...items); + } + + if (Array.isArray(material) && material.length) { + const items = material.map(url => ({ url, createdAt: new Date() })); + insensor.materialReceivedPictures.push(...items); + } - if (mediaType === 'video') { - insensor.manualTestVideos.push(...mediaItems); - } else if (mediaType === 'material') { - insensor.materialReceivedPictures.push(...mediaItems); - } else if (mediaType === 'workStatus') { - insensor.workStatusPictures.push(...mediaItems); - } else { - return reply.status(400).send({ success: false, message: "Invalid mediaType" }); + if (Array.isArray(workStatus) && workStatus.length) { + const items = workStatus.map(url => ({ url, createdAt: new Date() })); + insensor.workStatusPictures.push(...items); + } + + // Update product_status if provided + if (product_status && ['pending', 'complete'].includes(product_status)) { + insensor.product_status = product_status; } await insensor.save(); - // ✅ fetch the updated document as plain object - const updatedInsensor = await Insensors.findOne({ hardwareId, customerId, type }).lean(); - console.log("updatedInsensor",updatedInsensor) + const updated = await Insensors.findOne({ hardwareId, customerId, type }).lean(); return reply.send({ success: true, message: "Media saved successfully", - data: updatedInsensor + data: updated }); } catch (error) { console.error("Error adding media to insensor:", error); @@ -1732,8 +1785,6 @@ exports.addMediaToInsensor = async (req, reply) => { - - exports.mastrerList = async (req, reply) => { try { const { customerId, installationId } = req.params; diff --git a/src/models/store.js b/src/models/store.js index 5c11fc8a..43e7b539 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -694,7 +694,12 @@ workStatusPictures: [ url: String, createdAt: { type: Date, default: Date.now } } -] +], + product_status: { + type: String, + enum: ['pending', 'complete'], + default: 'pending' + }, }); diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 4ba97fc3..0159c60e 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -422,37 +422,48 @@ module.exports = function (fastify, opts, next) { handler: installationController.masterConnectedSlaveList, }); fastify.post( - '/api/insensors/:hardwareId/:customerId/:type/media', + '/api/insensors/:customerId/media', { schema: { summary: 'Add media (manual videos, material pictures, or work status pictures) to master or slave device', - description: 'Attach media files (video or images) to a specific Insensor (master or slave) for a given customer', + description: 'Attach media files (video/images) to a specific Insensor (master or slave) by customerId', tags: ['Installation'], params: { type: 'object', - required: ['hardwareId', 'customerId', 'type'], + required: ['customerId'], properties: { - hardwareId: { type: 'string', description: 'Hardware ID of the device' }, - customerId: { type: 'string', description: 'Customer ID' }, - type: { type: 'string', enum: ['master', 'slave'], description: 'Device type' } + customerId: { type: 'string', description: 'Customer ID' } } }, body: { type: 'object', - required: ['urls', 'mediaType'], + required: ['hardwareId', 'type'], // keep required fields properties: { - urls: { + hardwareId: { type: 'string', description: 'Hardware ID of the device' }, + type: { type: 'string', enum: ['master', 'slave'], description: 'Device type' }, + video: { type: 'array', - items: { type: 'string', format: 'uri', description: 'URL of media file' }, - minItems: 1, - description: 'Array of media URLs to save' + items: { type: 'string', format: 'uri' }, + description: 'URLs to save in manualTestVideos' }, - mediaType: { + material: { + type: 'array', + items: { type: 'string', format: 'uri' }, + description: 'URLs to save in materialReceivedPictures' + }, + workStatus: { + type: 'array', + items: { type: 'string', format: 'uri' }, + description: 'URLs to save in workStatusPictures' + }, + product_status: { type: 'string', - enum: ['video', 'material', 'workStatus'], - description: 'Target field to store media in: "video" → manualTestVideos, "material" → materialReceivedPictures, "workStatus" → workStatusPictures' + enum: ['pending', 'complete'], + description: 'Optional: update product_status' } - } + }, + // at least one of video, material, workStatus required + }, // response: { // 200: { @@ -480,8 +491,7 @@ fastify.post( // } // } }, - handler: installationController.addMediaToInsensor - + handler: installationController.addMediaToInsensor } );