company profile picture

master^2
Bhaskar 8 months ago
parent 00835cf64d
commit 73dc35c9d3

@ -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 });

@ -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 {

@ -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};

Loading…
Cancel
Save