From 73dc35c9d3dc3991adcbda5ca04089d948eccab2 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 5 Feb 2025 13:58:34 +0530 Subject: [PATCH] company profile picture --- src/controllers/departmentController.js | 4 +- src/index.js | 53 ++++++++++++++++++++++++- src/models/Department.js | 23 ++++++++++- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index c9159149..7e8de3f5 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -81,7 +81,9 @@ const generateDepartmentId = async (city, departmentName) => { // Generate departmentId based on departmentName // const prefix = departmentName.substring(0, 2).toUpperCase(); // Extract first two letters and convert to uppercase - const cityId = await generateCityId(); + const c_id = await generateCityId(); + const cityId = `AWCI${c_id}`; + // Check for existing department const existingStore = await City.findOne({ cityId }); diff --git a/src/index.js b/src/index.js index 9eb431e2..456f613b 100644 --- a/src/index.js +++ b/src/index.js @@ -616,7 +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"); +const { TeamMemberProfilePicture, CompanyProfilePicture } = require("./models/Department.js"); fastify.register(require('fastify-formbody')); // fastify.register(multer.contentParser); // const multipart = require('fastify-multipart'); @@ -792,6 +792,57 @@ fastify.post('/api/uploads_team_member/:departmentId', async (request, reply) => } }); +fastify.post('/api/uploads_company_profile/:cityId', async (request, reply) => { + try { + const cityId = request.params.cityId; + 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_company_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}`; + + CompanyProfilePicture.findOneAndUpdate( + { cityId }, + { 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) => { try { diff --git a/src/models/Department.js b/src/models/Department.js index 1f851293..273b47cf 100644 --- a/src/models/Department.js +++ b/src/models/Department.js @@ -136,12 +136,33 @@ const citySchema = new mongoose.Schema( } } }); + + const companyProfilePictureSchema = new Schema({ + cityId: { + 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); + const CompanyProfilePicture = mongoose.model('CompanyProfilePicture', companyProfilePictureSchema); - module.exports = { City,Deparments,Branch,TeamMemberProfilePicture}; + module.exports = { City,Deparments,Branch,TeamMemberProfilePicture,CompanyProfilePicture};