From 00835cf64d22a244ed32cb0ca8f6289d901124fb Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 5 Feb 2025 13:51:27 +0530 Subject: [PATCH] team member profile picture --- src/index.js | 51 ++++++++++++++++++++++++++++++++++++++++ src/models/Department.js | 23 +++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 8456bc61..9eb431e2 100644 --- a/src/index.js +++ b/src/index.js @@ -616,6 +616,7 @@ const {Storage} = require('@google-cloud/storage'); const { Supplier, profilePictureSupplier } = require("./models/supplier"); const multer = require('fastify-multer'); const { ProfilePictureInstall, Install } = require("./models/store.js"); +const { TeamMemberProfilePicture } = require("./models/Department.js"); fastify.register(require('fastify-formbody')); // fastify.register(multer.contentParser); // const multipart = require('fastify-multipart'); @@ -740,6 +741,56 @@ fastify.register(require('fastify-multipart')); // } // }); +fastify.post('/api/uploads_team_member/:departmentId', async (request, reply) => { + try { + const departmentId = request.params.departmentId; + 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}`; + + // Create a write stream to the destination file in the bucket + const writeStream = storage.bucket(bucketName).file(filePath).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(); + + const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; + + TeamMemberProfilePicture.findOneAndUpdate( + { departmentId }, + { 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' }); + } + }); + + writeStream.on('error', (err) => { + reply.code(500).send({ error: 'Failed to move file' }); + }); + } catch (err) { + reply.code(500).send({ error: 'An error occurred' }); + } +}); fastify.post('/api/uploads/:supplierId', async (request, reply) => { diff --git a/src/models/Department.js b/src/models/Department.js index b91fdb81..1f851293 100644 --- a/src/models/Department.js +++ b/src/models/Department.js @@ -116,11 +116,32 @@ const citySchema = new mongoose.Schema( }, { versionKey: false } ); + + const teamMemberProfilePictureSchema = new Schema({ + departmentId: { + type: String, + unique: true, + required: true + }, + picture: { + type: String, // Change the type to String + required: true, + validate: { + validator: function (value) { + const supportedFormats = ['jpg', 'jpeg', 'png']; + const fileExtension = value.split('.').pop().toLowerCase(); + return supportedFormats.includes(fileExtension); + }, + message: 'Picture must be a JPEG, PNG, or JPG image' + } + } + }); const City = mongoose.model('City', citySchema); const Deparments = mongoose.model('Deparments', departmentsSchema); const Branch = mongoose.model('Branch', branchSchema); + const TeamMemberProfilePicture = mongoose.model('TeamMemberProfilePicture', teamMemberProfilePictureSchema); - module.exports = { City,Deparments,Branch}; + module.exports = { City,Deparments,Branch,TeamMemberProfilePicture};