|
|
|
@ -616,6 +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");
|
|
|
|
|
fastify.register(require('fastify-formbody'));
|
|
|
|
|
// fastify.register(multer.contentParser);
|
|
|
|
|
// 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) => {
|
|
|
|
|