From 9295367025334fea80def9b62d76ad0a3112fac3 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 10 Sep 2025 17:02:17 +0530 Subject: [PATCH] installation team member upload profile --- src/index.js | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 1caf0421..9fa69d76 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ const createConnectionController = require("./controllers/createConnectionContro const storeController = require("./controllers/storeController.js") const boom = require("boom"); const bcrypt = require('bcrypt'); -const { ProfilePictureStore,generateinstallationId,Store, Survey, PlumbingWorkPictures, ElectrictyWorkPictures, MaterialRecievedPictures, Support, ManualTestVideo} = require("./models/store"); +const { ProfilePictureStore,generateinstallationId,Store, Survey, PlumbingWorkPictures, ElectrictyWorkPictures, MaterialRecievedPictures, Support, ManualTestVideo, ProfilePictureInstallTeamMember} = require("./models/store"); const cors = require('fastify-cors'); @@ -1232,6 +1232,102 @@ fastify.post('/api/uploads_company_profile/:customerId', async (request, reply) } }); +fastify.post("/api/uploads_installation_TeamMember_profile/:installationId/:teamMemberId", { + preHandler: upload.single("file"), +}, async (request, reply) => { + try { + const { installationId ,teamMemberId} = request.params; + //const teamMemberId = request.body?.teamMemberId; // OPTIONAL + const file = request.file; + + if (!file) { + return reply.code(400).send({ error: "No file uploaded (field name 'file')." }); + } + + // Validate image type + const allowed = ["image/jpeg", "image/jpg", "image/png"]; + if (!allowed.includes(file.mimetype)) { + return reply.code(400).send({ error: "Only JPEG/PNG images are allowed." }); + } + + // Build GCS path + const ext = (mime.extension(file.mimetype) || path.extname(file.originalname).slice(1) || "png").toLowerCase(); + const safeBase = path.parse(file.originalname).name.replace(/[^\w.-]/g, "_"); + const filePath = `arminta_team_profiles/${safeBase}-${Date.now()}.${ext}`; + const bucketName = "arminta_profile_pictures"; + + // Upload + const buffer = await fs.promises.readFile(file.path); + const bucket = storage.bucket(bucketName); + const gcsFile = bucket.file(filePath); + await gcsFile.save(buffer, { + resumable: false, + public: true, + contentType: file.mimetype, + metadata: { cacheControl: "public, max-age=31536000" }, + }); + await gcsFile.makePublic(); + + const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; + + // Always upsert the installation-level picture doc + await ProfilePictureInstall.findOneAndUpdate( + { installationId }, + { picture: publicUrl }, + { new: true, upsert: true } + ); + + // Update department picture where departmentId === installationId + const deptUpdate = await Deparments.findOneAndUpdate( + { departmentId: installationId }, + { picture: publicUrl }, + { new: true } + ); + + let teamMemberUpdated = false; + if (teamMemberId) { + // 1) Upsert team-member picture collection + await ProfilePictureInstallTeamMember.findOneAndUpdate( + { teamMemberId }, + { picture: publicUrl }, + { new: true, upsert: true } + ); + + // 2) Update nested item inside Installations.team_member.team_member[] + // Using arrayFilters to match the correct team member element + const res = await Install.updateOne( + { installationId }, + { + $set: { + "team_member.team_member.$[tm].picture": publicUrl + } + }, + { + arrayFilters: [{ "tm.teamMemberId": teamMemberId }] + } + ); + teamMemberUpdated = res.modifiedCount > 0; + } + + return reply.send({ + installationId, + teamMemberId: teamMemberId || null, + picture: publicUrl, + departmentUpdated: Boolean(deptUpdate), + teamMemberUpdated, + message: teamMemberId + ? (teamMemberUpdated + ? "Upload successful. Installation + team member picture updated." + : "Upload successful. Team member not found under this installation.") + : "Upload successful. Installation picture updated.", + }); + } catch (err) { + request.log.error(err); + return reply.code(500).send({ error: "Upload failed", details: err.message }); + } finally { + try { if (request.file?.path) await fs.promises.unlink(request.file.path); } catch {} + } +}); fastify.post('/api/uploads/:supplierId', async (request, reply) => { try {