diff --git a/src/index.js b/src/index.js index d2573000..bb89d120 100644 --- a/src/index.js +++ b/src/index.js @@ -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: { diff --git a/src/models/admin.js b/src/models/admin.js index 76780cb3..2161e132 100644 --- a/src/models/admin.js +++ b/src/models/admin.js @@ -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 \ No newline at end of file +module.exports = {Admin,AdminProfilePicture} \ No newline at end of file