team member profile picture

master^2
Bhaskar 8 months ago
parent 70b25df6c0
commit 00835cf64d

@ -616,6 +616,7 @@ const {Storage} = require('@google-cloud/storage');
const { Supplier, profilePictureSupplier } = require("./models/supplier"); const { Supplier, profilePictureSupplier } = require("./models/supplier");
const multer = require('fastify-multer'); const multer = require('fastify-multer');
const { ProfilePictureInstall, Install } = require("./models/store.js"); const { ProfilePictureInstall, Install } = require("./models/store.js");
const { TeamMemberProfilePicture } = require("./models/Department.js");
fastify.register(require('fastify-formbody')); fastify.register(require('fastify-formbody'));
// fastify.register(multer.contentParser); // fastify.register(multer.contentParser);
// const multipart = require('fastify-multipart'); // 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) => { fastify.post('/api/uploads/:supplierId', async (request, reply) => {

@ -117,10 +117,31 @@ const citySchema = new mongoose.Schema(
{ versionKey: false } { 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 City = mongoose.model('City', citySchema);
const Deparments = mongoose.model('Deparments', departmentsSchema); const Deparments = mongoose.model('Deparments', departmentsSchema);
const Branch = mongoose.model('Branch', branchSchema); const Branch = mongoose.model('Branch', branchSchema);
const TeamMemberProfilePicture = mongoose.model('TeamMemberProfilePicture', teamMemberProfilePictureSchema);
module.exports = { City,Deparments,Branch}; module.exports = { City,Deparments,Branch,TeamMemberProfilePicture};

Loading…
Cancel
Save