upload admin profile picture

master^2
Bhaskar 3 weeks ago
parent 06a933c92d
commit 7d2ade113d

@ -1016,6 +1016,41 @@ fastify.post('/api/uploads_team_profile/:customerId', {
return reply.code(500).send({ error: 'Upload failed', details: err.message });
}
});
fastify.post('/api/uploads_admin_profile/:customerId', {
preHandler: upload.single('file')
}, async (request, reply) => {
try {
const { customerId } = request.params;
const file = await request.file; // Uncomment this line
const formData = new FormData();
formData.append('file', file);
const bucketName = 'arminta_profile_pictures';
const filePath = `arminta_team_profiles/${file.originalname}`;
const fileBuffer = await fs.promises.readFile(file.path);
await storage.bucket(bucketName).file(filePath).save(fileBuffer);
// make file public
await storage.bucket(bucketName).file(filePath).makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// save in DB
const picture = await AdminProfilePicture.findOneAndUpdate(
{ customerId },
{ picture: publicUrl },
{ new: true, upsert: true }
);
return reply.send({ picture: publicUrl });
} catch (err) {
request.log.error(err);
return reply.code(500).send({ error: 'Upload failed', details: err.message });
}
});
// fastify.post('/api/uploads_team_profile/:customerId', async (request, reply) => {
// try {
// const { customerId } = request.params;
@ -2219,6 +2254,7 @@ fastify.post("/api/teamMemberLogin", {
});
const moment = require("moment-timezone");
const { AdminProfilePicture } = require("./models/admin.js");
fastify.post("/api/supportLogin", {
schema: {

@ -38,6 +38,28 @@ const adminSchema = new mongoose.Schema({
},
})
const adminProfilePictureSchema = new mongoose.Schema({
customerId: {
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 Admin = mongoose.model('Admin', adminSchema)
const AdminProfilePicture = mongoose.model('AdminProfilePicture', adminProfilePictureSchema);
module.exports = Admin
module.exports = {Admin,AdminProfilePicture}
Loading…
Cancel
Save