diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index 90addfb3..d72146fc 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -1203,27 +1203,95 @@ exports.getconnectedCustomers = async (req, reply) => { }; -exports.uploadProfilePicture = async (req, reply) => { - try { - const supplierId = req.params.supplierId; - const picture = req.body.picture; +// exports.uploadProfilePicture = async (req, reply) => { +// try { +// const supplierId = req.params.supplierId; +// const picture = req.body.picture; - let profilePicture = await profilePictureSupplier.findOne({ supplierId }); +// let profilePicture = await profilePictureSupplier.findOne({ supplierId }); - console.log(profilePicture,"profile===") - if (!profilePicture) { - profilePicture = new profilePictureSupplier({ - supplierId, - picture, - }); - } else { - profilePicture.picture = picture; - } +// console.log(profilePicture,"profile===") +// if (!profilePicture) { +// profilePicture = new profilePictureSupplier({ +// supplierId, +// picture, +// }); +// } else { +// profilePicture.picture = picture; +// } + +// await profilePicture.save(); + +// reply.send({ message: 'Profile picture uploaded successfully' }); +// } catch (error) { +// reply.status(500).send({ error: error.message }); +// } +// }; + + +const multer = require('multer'); +const fs = require('fs'); + + +// Multer storage configuration +const storage = multer.diskStorage({ + destination: function (req, file, cb) { + // Specify the destination folder for storing uploaded files + cb(null, './uploads'); + }, + filename: function (req, file, cb) { + // Generate a unique filename for the uploaded file + cb(null, file.originalname); + }, +}); + +// Multer upload configuration +const upload = multer({ storage: storage }).single('picture'); - await profilePicture.save(); +// Handler for uploading profile picture +exports.uploadProfilePicture = async (req, res) => { + try { + upload(req, res, async (err) => { + if (err) { + return res.status(400).send({ error: 'Failed to upload profile picture' }); + } + + const supplierId = req.params.supplierId; + const picture = req.file.filename; // Assuming the file field in the request is named 'picture' + + let profilePicture = await ProfilePictureSupplier.findOne({ supplierId }); + + if (!profilePicture) { + profilePicture = new ProfilePictureSupplier({ + supplierId, + picture, + }); + } else { + profilePicture.picture = picture; + } + + await profilePicture.save(); - reply.send({ message: 'Profile picture uploaded successfully' }); + res.send({ message: 'Profile picture uploaded successfully' }); + }); } catch (error) { - reply.status(500).send({ error: error.message }); + res.status(500).send({ error: error.message }); } }; + +// Route for fetching profile picture data +fastify.get('/api/users/profile-picture-supplier/:supplierId', async (req, res) => { + try { + const supplierId = req.params.supplierId; + const profilePicture = await ProfilePictureSupplier.findOne({ supplierId }); + + if (!profilePicture) { + return res.status(404).send({ error: 'Profile picture not found' }); + } + + // Send the profile picture data as a response + res.send({ picture: profilePicture.picture }); + } catch (error) { + res.status(500).send({ error: error.message }); + } +}); \ No newline at end of file diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 4cfac239..9cd99b86 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -576,48 +576,32 @@ module.exports = function (fastify, opts, next) { // }); - - const multer = require('multer'); - const fs = require('fs'); - - const storage = multer.diskStorage({ - destination: function (req, file, cb) { - if (!fs.existsSync(__dirname + '/temp')) { - fs.mkdirSync(__dirname + '/temp'); - } - cb(null, './temp'); - }, - filename: function (req, file, cb) { - cb(null, file.originalname + '-' + Date.now() + '.' + file.mimetype.split('/')[1]); - }, - }); - - const upload = multer({ storage: storage }).single('picture'); - fastify.route({ method: 'POST', url: '/api/users/profile-picture-supplier/:supplierId', schema: { tags: ['Supplier'], - description: 'Upload a profile picture supplier', - summary: 'Upload a profile picture supplier', + description: 'Upload a profile picture for a supplier', + summary: 'Upload a profile picture for a supplier', params: { type: 'object', properties: { supplierId: { type: 'string', - description: 'SupplierId', + description: 'Supplier ID', }, }, }, + consumes: ['multipart/form-data'], body: { - formData: { + type: 'object', + properties: { picture: { type: 'string', - format: 'binary', description: 'Profile picture file', }, }, + required: ['picture'], }, response: { 200: { @@ -643,40 +627,111 @@ module.exports = function (fastify, opts, next) { }, }, }, - handler: async (req, reply) => { - try { - upload(req, reply, async (err) => { - if (err) { - reply.status(400).send({ error: 'Failed to upload profile picture' }); - return; - } + handler: validationHandler.uploadProfilePicture, + }); - const supplierId = req.params.supplierId; - const picturePath = req.file.path; + + + const multer = require('multer'); + const fs = require('fs'); - let profilePicture = await profilePictureSupplier.findOne({ supplierId }); + const storage = multer.diskStorage({ + destination: function (req, file, cb) { + if (!fs.existsSync(__dirname + '/temp')) { + fs.mkdirSync(__dirname + '/temp'); + } + cb(null, './temp'); + }, + filename: function (req, file, cb) { + cb(null, file.originalname + '-' + Date.now() + '.' + file.mimetype.split('/')[1]); + }, + }); + + const upload = multer({ storage: storage }).single('picture'); + + // fastify.route({ + // method: 'POST', + // url: '/api/users/profile-picture-supplier/:supplierId', + // schema: { + // tags: ['Supplier'], + // description: 'Upload a profile picture supplier', + // summary: 'Upload a profile picture supplier', + // params: { + // type: 'object', + // properties: { + // supplierId: { + // type: 'string', + // description: 'SupplierId', + // }, + // }, + // }, + // body: { + // formData: { + // picture: { + // type: 'string', + // format: 'binary', + // description: 'Profile picture file', + // }, + // }, + // }, + // response: { + // 200: { + // description: 'Profile picture uploaded successfully', + // type: 'object', + // properties: { + // message: { type: 'string' }, + // }, + // }, + // 400: { + // description: 'Failed to upload profile picture', + // type: 'object', + // properties: { + // error: { type: 'string' }, + // }, + // }, + // 500: { + // description: 'Internal server error', + // type: 'object', + // properties: { + // error: { type: 'string' }, + // }, + // }, + // }, + // }, + // handler: async (req, reply) => { + // try { + // upload(req, reply, async (err) => { + // if (err) { + // reply.status(400).send({ error: 'Failed to upload profile picture' }); + // return; + // } - if (!profilePicture) { - profilePicture = new ProfilePicture({ - supplierId, - picture: fs.readFileSync(picturePath), - }); - } else { - profilePicture.picture = fs.readFileSync(picturePath); - } + // const supplierId = req.params.supplierId; + // const picturePath = req.file.path; - await profilePicture.save(); + // let profilePicture = await profilePictureSupplier.findOne({ supplierId }); - // Delete the temporary uploaded file - fs.unlinkSync(picturePath); + // if (!profilePicture) { + // profilePicture = new ProfilePicture({ + // supplierId, + // picture: fs.readFileSync(picturePath), + // }); + // } else { + // profilePicture.picture = fs.readFileSync(picturePath); + // } - reply.send({ message: 'Profile picture uploaded successfully' }); - }); - } catch (error) { - reply.status(500).send({ error: error.message }); - } - }, - }); + // await profilePicture.save(); + + // // Delete the temporary uploaded file + // fs.unlinkSync(picturePath); + + // reply.send({ message: 'Profile picture uploaded successfully' }); + // }); + // } catch (error) { + // reply.status(500).send({ error: error.message }); + // } + // }, + // }); next(); }