From 9a7fb64d023e15efedb6a9a9fd62521eb938105a Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 6 Feb 2025 17:05:04 +0530 Subject: [PATCH] team member profile picture --- src/index.js | 57 ++++++++++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/src/index.js b/src/index.js index 71a9c08e..3da28c46 100644 --- a/src/index.js +++ b/src/index.js @@ -741,57 +741,44 @@ fastify.register(require('fastify-multipart')); // } // }); -fastify.post('/api/uploads_team_member/:customerId', async (request, reply) => { +fastify.post('/api/uploads_team_profile/:customerId', async (request, reply) => { try { const customerId = request.params.customerId; const data = await request.file(); - // Generate a unique file name - const fileName = `${data.filename}`; - - // Define the destination bucket and file path const bucketName = 'arminta_profile_pictures'; - const filePath = `arminta_team_member_profiles/${fileName}`; + const filePath = `arminta_team_profiles/${data.filename}`; - // Create a write stream to the destination file in the bucket - const writeStream = storage.bucket(bucketName).file(filePath).createWriteStream(); + const file = storage.bucket(bucketName).file(filePath); + const writeStream = file.createWriteStream(); - // Pipe the file data to the write stream data.file.pipe(writeStream); - writeStream.on('finish', async () => { - try { - // Make the uploaded file publicly accessible - await storage.bucket(bucketName).file(filePath).makePublic(); + await new Promise((resolve, reject) => { + writeStream.on('finish', resolve); + writeStream.on('error', reject); + }); - const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; + // Make file public + await file.makePublic(); + const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; - TeamMemberProfilePicture.findOneAndUpdate( - { customerId }, - { picture: publicUrl }, - { new: true, upsert: true }, - (error, picture) => { - if (error) { - reply.code(500).send({ error: 'Failed to update database' }); - } else { - // Return the public URL - reply.send({ picture: publicUrl }); - } - } - ); - } catch (error) { - reply.code(500).send({ error: 'Failed to make file public' }); - } - }); + // Update DB with async/await + const picture = await TeamMemberProfilePicture.findOneAndUpdate( + { customerId }, + { picture: publicUrl }, + { new: true, upsert: true } + ); - writeStream.on('error', (err) => { - reply.code(500).send({ error: 'Failed to move file' }); - }); + reply.send({ picture: publicUrl }); } catch (err) { - reply.code(500).send({ error: 'An error occurred' }); + console.error(err); + reply.code(500).send({ error: 'An error occurred', details: err.message }); } }); + + fastify.post('/api/uploads_company_profile/:customerId', async (request, reply) => { try { const customerId = request.params.customerId;