PICTURES AND videos product status maintain

master^2
Bhaskar 3 months ago
parent f0302fc68b
commit fa57b26a9d

@ -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) => { exports.addMediaToInsensor = async (req, reply) => {
try { try {
const { hardwareId, customerId, type } = req.params; // const { hardwareId, customerId, type } = req.params;
const { urls, mediaType } = req.body; // 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) { if (!hardwareId || !customerId || !type) {
return reply.status(400).send({ success: false, message: "Missing required fields" }); return reply.status(400).send({ success: false, message: "Missing required params" });
} }
const insensor = await Insensors.findOne({ hardwareId, customerId, type }); 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" }); 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') { if (Array.isArray(workStatus) && workStatus.length) {
insensor.manualTestVideos.push(...mediaItems); const items = workStatus.map(url => ({ url, createdAt: new Date() }));
} else if (mediaType === 'material') { insensor.workStatusPictures.push(...items);
insensor.materialReceivedPictures.push(...mediaItems); }
} else if (mediaType === 'workStatus') {
insensor.workStatusPictures.push(...mediaItems); // Update product_status if provided
} else { if (product_status && ['pending', 'complete'].includes(product_status)) {
return reply.status(400).send({ success: false, message: "Invalid mediaType" }); insensor.product_status = product_status;
} }
await insensor.save(); await insensor.save();
// ✅ fetch the updated document as plain object const updated = await Insensors.findOne({ hardwareId, customerId, type }).lean();
const updatedInsensor = await Insensors.findOne({ hardwareId, customerId, type }).lean();
console.log("updatedInsensor",updatedInsensor)
return reply.send({ return reply.send({
success: true, success: true,
message: "Media saved successfully", message: "Media saved successfully",
data: updatedInsensor data: updated
}); });
} catch (error) { } catch (error) {
console.error("Error adding media to insensor:", error); console.error("Error adding media to insensor:", error);
@ -1732,8 +1785,6 @@ exports.addMediaToInsensor = async (req, reply) => {
exports.mastrerList = async (req, reply) => { exports.mastrerList = async (req, reply) => {
try { try {
const { customerId, installationId } = req.params; const { customerId, installationId } = req.params;

@ -694,7 +694,12 @@ workStatusPictures: [
url: String, url: String,
createdAt: { type: Date, default: Date.now } createdAt: { type: Date, default: Date.now }
} }
] ],
product_status: {
type: String,
enum: ['pending', 'complete'],
default: 'pending'
},
}); });

@ -422,37 +422,48 @@ module.exports = function (fastify, opts, next) {
handler: installationController.masterConnectedSlaveList, handler: installationController.masterConnectedSlaveList,
}); });
fastify.post( fastify.post(
'/api/insensors/:hardwareId/:customerId/:type/media', '/api/insensors/:customerId/media',
{ {
schema: { schema: {
summary: 'Add media (manual videos, material pictures, or work status pictures) to master or slave device', 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'], tags: ['Installation'],
params: { params: {
type: 'object', type: 'object',
required: ['hardwareId', 'customerId', 'type'], required: ['customerId'],
properties: { properties: {
hardwareId: { type: 'string', description: 'Hardware ID of the device' }, customerId: { type: 'string', description: 'Customer ID' }
customerId: { type: 'string', description: 'Customer ID' },
type: { type: 'string', enum: ['master', 'slave'], description: 'Device type' }
} }
}, },
body: { body: {
type: 'object', type: 'object',
required: ['urls', 'mediaType'], required: ['hardwareId', 'type'], // keep required fields
properties: { properties: {
urls: { hardwareId: { type: 'string', description: 'Hardware ID of the device' },
type: { type: 'string', enum: ['master', 'slave'], description: 'Device type' },
video: {
type: 'array', type: 'array',
items: { type: 'string', format: 'uri', description: 'URL of media file' }, items: { type: 'string', format: 'uri' },
minItems: 1, description: 'URLs to save in manualTestVideos'
description: 'Array of media URLs to save'
}, },
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', type: 'string',
enum: ['video', 'material', 'workStatus'], enum: ['pending', 'complete'],
description: 'Target field to store media in: "video" → manualTestVideos, "material" → materialReceivedPictures, "workStatus" → workStatusPictures' description: 'Optional: update product_status'
} }
} },
// at least one of video, material, workStatus required
}, },
// response: { // response: {
// 200: { // 200: {
@ -480,8 +491,7 @@ fastify.post(
// } // }
// } // }
}, },
handler: installationController.addMediaToInsensor handler: installationController.addMediaToInsensor
} }
); );

Loading…
Cancel
Save