ashok 1 week ago
commit d798115402

@ -1051,81 +1051,81 @@ fastify.post('/api/uploads_admin_profile/:customerId', {
} }
}); });
fastify.post("/api/uploads_installation_profile/:installationId", { // fastify.post("/api/uploads_installation_profile/:installationId", {
preHandler: upload.single("file"), // your multer/fastify-multipart preHandler // preHandler: upload.single("file"), // your multer/fastify-multipart preHandler
}, async (request, reply) => { // }, async (request, reply) => {
try { // try {
const { installationId } = request.params; // const { installationId } = request.params;
const file = request.file; // in fastify-multer this is set by preHandler // const file = request.file; // in fastify-multer this is set by preHandler
if (!file) { // if (!file) {
return reply.code(400).send({ error: "No file uploaded (expected field name 'file')." }); // return reply.code(400).send({ error: "No file uploaded (expected field name 'file')." });
} // }
// basic file validation // // basic file validation
const allowed = ["image/jpeg", "image/jpg", "image/png"]; // const allowed = ["image/jpeg", "image/jpg", "image/png"];
if (!allowed.includes(file.mimetype)) { // if (!allowed.includes(file.mimetype)) {
return reply.code(400).send({ error: "Only JPEG/PNG images are allowed." }); // return reply.code(400).send({ error: "Only JPEG/PNG images are allowed." });
} // }
const bucketName = "arminta_profile_pictures"; // const bucketName = "arminta_profile_pictures";
const ext = mime.extension(file.mimetype) || (file.originalname.split(".").pop() || "png"); // const ext = mime.extension(file.mimetype) || (file.originalname.split(".").pop() || "png");
const safeBase = path.parse(file.originalname).name.replace(/[^\w.-]/g, "_"); // const safeBase = path.parse(file.originalname).name.replace(/[^\w.-]/g, "_");
const filePath = `arminta_team_profiles/${safeBase}-${Date.now()}.${ext}`; // const filePath = `arminta_team_profiles/${safeBase}-${Date.now()}.${ext}`;
// read temp file to buffer // // read temp file to buffer
const fileBuffer = await fs.promises.readFile(file.path); // const fileBuffer = await fs.promises.readFile(file.path);
// upload to GCS // // upload to GCS
const bucket = storage.bucket(bucketName); // const bucket = storage.bucket(bucketName);
const gcsFile = bucket.file(filePath); // const gcsFile = bucket.file(filePath);
await gcsFile.save(fileBuffer, { // await gcsFile.save(fileBuffer, {
resumable: false, // resumable: false,
contentType: file.mimetype, // contentType: file.mimetype,
public: true, // public: true,
metadata: { cacheControl: "public, max-age=31536000" }, // metadata: { cacheControl: "public, max-age=31536000" },
}); // });
await gcsFile.makePublic(); // await gcsFile.makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; // const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// 1) Upsert installation profile picture collection // // 1) Upsert installation profile picture collection
await ProfilePictureInstall.findOneAndUpdate( // await ProfilePictureInstall.findOneAndUpdate(
{ installationId }, // { installationId },
{ picture: publicUrl }, // { picture: publicUrl },
{ new: true, upsert: true } // { new: true, upsert: true }
); // );
// 2) Update department doc where departmentId === installationId // // 2) Update department doc where departmentId === installationId
const deptUpdate = await Deparments.findOneAndUpdate( // const deptUpdate = await Deparments.findOneAndUpdate(
{ departmentId: installationId }, // { departmentId: installationId },
{ picture: publicUrl }, // { picture: publicUrl },
{ new: true } // { new: true }
); // );
// 3) (Optional) also save on the installation doc itself if you keep picture there // // 3) (Optional) also save on the installation doc itself if you keep picture there
// await Installations.findOneAndUpdate( // // await Installations.findOneAndUpdate(
// { installationId }, // // { installationId },
// { picture: publicUrl }, // // { picture: publicUrl },
// { new: true } // // { new: true }
// ); // // );
return reply.send({ // return reply.send({
installationId, // installationId,
picture: publicUrl, // picture: publicUrl,
departmentUpdated: Boolean(deptUpdate), // departmentUpdated: Boolean(deptUpdate),
message: deptUpdate // message: deptUpdate
? "Upload successful. Department picture updated." // ? "Upload successful. Department picture updated."
: "Upload successful. No department matched this installationId.", // : "Upload successful. No department matched this installationId.",
}); // });
} catch (err) { // } catch (err) {
request.log.error(err); // request.log.error(err);
return reply.code(500).send({ error: "Upload failed", details: err.message }); // return reply.code(500).send({ error: "Upload failed", details: err.message });
} finally { // } finally {
// best effort: clean up temp file if your preHandler writes to disk // // best effort: clean up temp file if your preHandler writes to disk
try { if (request.file?.path) await fs.promises.unlink(request.file.path); } catch {} // try { if (request.file?.path) await fs.promises.unlink(request.file.path); } catch {}
} // }
}); // });
// fastify.post('/api/uploads_installation_profile/:installationId', { // fastify.post('/api/uploads_installation_profile/:installationId', {
// preHandler: upload.single('file') // preHandler: upload.single('file')
@ -1232,103 +1232,103 @@ fastify.post('/api/uploads_company_profile/:customerId', async (request, reply)
} }
}); });
fastify.post("/api/uploads_installation_TeamMember_profile/:installationId/:teamMemberId", { // fastify.post("/api/uploads_installation_TeamMember_profile/:installationId/:teamMemberId", {
preHandler: upload.single("file"), // preHandler: upload.single("file"),
}, async (request, reply) => { // }, async (request, reply) => {
try { // try {
const { installationId ,teamMemberId} = request.params; // const { installationId ,teamMemberId} = request.params;
//const teamMemberId = request.body?.teamMemberId; // OPTIONAL // //const teamMemberId = request.body?.teamMemberId; // OPTIONAL
const file = request.file; // 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}`; // if (!file) {
// return reply.code(400).send({ error: "No file uploaded (field name 'file')." });
// }
// Always upsert the installation-level picture doc // // Validate image type
await ProfilePictureInstallTeamMember.findOneAndUpdate( // const allowed = ["image/jpeg", "image/jpg", "image/png"];
{ installationId, teamMemberId }, // if (!allowed.includes(file.mimetype)) {
{ picture: publicUrl }, // return reply.code(400).send({ error: "Only JPEG/PNG images are allowed." });
{ new: true, upsert: true } // }
);
// // 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";
// Update department picture where departmentId === installationId // // Upload
const deptUpdate = await Deparments.findOneAndUpdate( // const buffer = await fs.promises.readFile(file.path);
{ departmentId: installationId }, // const bucket = storage.bucket(bucketName);
{ picture: publicUrl }, // const gcsFile = bucket.file(filePath);
{ new: true } // await gcsFile.save(buffer, {
); // resumable: false,
// public: true,
// contentType: file.mimetype,
// metadata: { cacheControl: "public, max-age=31536000" },
// });
// await gcsFile.makePublic();
let teamMemberUpdated = false; // const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
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[] // // Always upsert the installation-level picture doc
// Using arrayFilters to match the correct team member element // await ProfilePictureInstallTeamMember.findOneAndUpdate(
const res = await Install.updateOne( // { installationId, teamMemberId },
{ installationId }, // { picture: publicUrl },
{ // { new: true, upsert: true }
$set: { // );
"team_member.team_member.$[tm].picture": publicUrl
}
},
{
arrayFilters: [{ "tm.teamMemberId": teamMemberId }]
}
);
teamMemberUpdated = res.modifiedCount > 0;
}
return reply.send({
installationId, // // Update department picture where departmentId === installationId
teamMemberId: teamMemberId || null, // const deptUpdate = await Deparments.findOneAndUpdate(
picture: publicUrl, // { departmentId: installationId },
departmentUpdated: Boolean(deptUpdate), // { picture: publicUrl },
teamMemberUpdated, // { new: true }
message: teamMemberId // );
? (teamMemberUpdated
? "Upload successful. Installation + team member picture updated." // let teamMemberUpdated = false;
: "Upload successful. Team member not found under this installation.") // if (teamMemberId) {
: "Upload successful. Installation picture updated.", // // 1) Upsert team-member picture collection
}); // await ProfilePictureInstallTeamMember.findOneAndUpdate(
} catch (err) { // { teamMemberId },
request.log.error(err); // { picture: publicUrl },
return reply.code(500).send({ error: "Upload failed", details: err.message }); // { new: true, upsert: true }
} finally { // );
try { if (request.file?.path) await fs.promises.unlink(request.file.path); } catch {}
} // // 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) => { fastify.post('/api/uploads/:supplierId', async (request, reply) => {
try { try {
@ -1495,59 +1495,59 @@ fastify.post('/api/uploads-user/:customerId', async (request, reply) => {
// } // }
// }); // });
fastify.post( // fastify.post(
"/api/uploads-electricty-work/:customerId/:installationId", // "/api/uploads-electricty-work/:customerId/:installationId",
{ preHandler: upload.any() }, // allow multiple files // { preHandler: upload.any() }, // allow multiple files
async (request, reply) => { // async (request, reply) => {
try { // try {
const { customerId, installationId } = request.params; // const { customerId, installationId } = request.params;
const files = request.files; // const files = request.files;
if (!files || files.length === 0) { // if (!files || files.length === 0) {
return reply.code(400).send({ error: "No files uploaded" }); // return reply.code(400).send({ error: "No files uploaded" });
} // }
const bucketName = "arminta_profile_pictures"; // const bucketName = "arminta_profile_pictures";
const publicUrls = []; // const publicUrls = [];
for (const file of files) { // for (const file of files) {
const uniqueFileName = `${Date.now()}-${Math.random() // const uniqueFileName = `${Date.now()}-${Math.random()
.toString(36) // .toString(36)
.substring(7)}-${file.originalname}`; // .substring(7)}-${file.originalname}`;
const filePath = `electricty_work_picture/${uniqueFileName}`; // const filePath = `electricty_work_picture/${uniqueFileName}`;
// ✅ Handle buffer vs path depending on multer storage // // ✅ Handle buffer vs path depending on multer storage
const fileBuffer = file.buffer // const fileBuffer = file.buffer
? file.buffer // memoryStorage // ? file.buffer // memoryStorage
: await fs.promises.readFile(file.path); // diskStorage // : await fs.promises.readFile(file.path); // diskStorage
await storage.bucket(bucketName).file(filePath).save(fileBuffer); // await storage.bucket(bucketName).file(filePath).save(fileBuffer);
await storage.bucket(bucketName).file(filePath).makePublic(); // await storage.bucket(bucketName).file(filePath).makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; // const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
publicUrls.push(publicUrl); // publicUrls.push(publicUrl);
console.log(`✅ Uploaded: ${publicUrl}`); // console.log(`✅ Uploaded: ${publicUrl}`);
} // }
// Update MongoDB // // Update MongoDB
const updatedRecord = await ElectrictyWorkPictures.findOneAndUpdate( // const updatedRecord = await ElectrictyWorkPictures.findOneAndUpdate(
{ customerId, installationId }, // { customerId, installationId },
{ $push: { pictureUrl: { $each: publicUrls.map((url) => ({ url })) } } }, // { $push: { pictureUrl: { $each: publicUrls.map((url) => ({ url })) } } },
{ new: true, upsert: true } // { new: true, upsert: true }
); // );
return reply.send({ // return reply.send({
success: true, // success: true,
pictures: publicUrls, // pictures: publicUrls,
details: updatedRecord, // details: updatedRecord,
}); // });
} catch (err) { // } catch (err) {
console.error("❌ Upload Error:", err); // console.error("❌ Upload Error:", err);
return reply.code(500).send({ error: "Upload failed", details: err.message }); // return reply.code(500).send({ error: "Upload failed", details: err.message });
} // }
} // }
); // );
// fastify.post("/api/uploads-manualTestVideo-work/:customerId/:installationId", async (request, reply) => { // fastify.post("/api/uploads-manualTestVideo-work/:customerId/:installationId", async (request, reply) => {
// try { // try {
@ -1681,142 +1681,142 @@ fastify.post(
// ); // );
fastify.post( // fastify.post(
"/api/uploads-manualTestVideo-work/:customerId/:installationId", // "/api/uploads-manualTestVideo-work/:customerId/:installationId",
{ preHandler: upload.any() }, // Multer saves files to "uploads/" // { preHandler: upload.any() }, // Multer saves files to "uploads/"
async (request, reply) => { // async (request, reply) => {
try { // try {
const { customerId, installationId } = request.params; // const { customerId, installationId } = request.params;
const files = request.files; // Multer saves file info here // const files = request.files; // Multer saves file info here
if (!files || files.length === 0) { // if (!files || files.length === 0) {
return reply.code(400).send({ error: "No files uploaded" }); // return reply.code(400).send({ error: "No files uploaded" });
} // }
const bucketName = "arminta_profile_pictures"; // const bucketName = "arminta_profile_pictures";
const publicUrls = []; // const publicUrls = [];
for (const file of files) { // for (const file of files) {
const uniqueFileName = `${Date.now()}-${Math.random() // const uniqueFileName = `${Date.now()}-${Math.random()
.toString(36) // .toString(36)
.substring(7)}-${file.originalname}`; // .substring(7)}-${file.originalname}`;
const filePath = `manual_test_video/${uniqueFileName}`; // const filePath = `manual_test_video/${uniqueFileName}`;
console.log(`Uploading video: ${file.path}${filePath}`); // console.log(`Uploading video: ${file.path} → ${filePath}`);
const gcsFile = storage.bucket(bucketName).file(filePath); // const gcsFile = storage.bucket(bucketName).file(filePath);
const stream = gcsFile.createWriteStream({ // const stream = gcsFile.createWriteStream({
metadata: { // metadata: {
contentType: file.mimetype || "video/mp4", // contentType: file.mimetype || "video/mp4",
contentDisposition: "inline", // contentDisposition: "inline",
}, // },
resumable: false, // resumable: false,
}); // });
// Pipe from disk to GCS // // Pipe from disk to GCS
fs.createReadStream(file.path).pipe(stream); // fs.createReadStream(file.path).pipe(stream);
await new Promise((resolve, reject) => { // await new Promise((resolve, reject) => {
stream.on("finish", async () => { // stream.on("finish", async () => {
try { // try {
await gcsFile.makePublic(); // await gcsFile.makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; // const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
publicUrls.push(publicUrl); // publicUrls.push(publicUrl);
console.log(`✅ Uploaded: ${publicUrl}`); // console.log(`✅ Uploaded: ${publicUrl}`);
resolve(); // resolve();
} catch (err) { // } catch (err) {
reject(err); // reject(err);
} // }
}); // });
stream.on("error", (err) => { // stream.on("error", (err) => {
console.error("Upload Error:", err); // console.error("Upload Error:", err);
reject(err); // reject(err);
}); // });
}); // });
// optional: cleanup temp file // // optional: cleanup temp file
fs.unlink(file.path, (err) => { // fs.unlink(file.path, (err) => {
if (err) console.error("Temp file cleanup failed:", err); // if (err) console.error("Temp file cleanup failed:", err);
}); // });
} // }
// Update MongoDB // // Update MongoDB
const updatedRecord = await ManualTestVideo.findOneAndUpdate( // const updatedRecord = await ManualTestVideo.findOneAndUpdate(
{ customerId, installationId }, // { customerId, installationId },
{ $push: { pictureUrl: { $each: publicUrls.map((url) => ({ url })) } } }, // { $push: { pictureUrl: { $each: publicUrls.map((url) => ({ url })) } } },
{ new: true, upsert: true } // { new: true, upsert: true }
); // );
return reply.send({ // return reply.send({
success: true, // success: true,
videos: publicUrls, // videos: publicUrls,
details: updatedRecord, // details: updatedRecord,
}); // });
} catch (err) { // } catch (err) {
console.error("Upload Error:", err); // console.error("Upload Error:", err);
return reply // return reply
.code(500) // .code(500)
.send({ error: "Upload failed", details: err.message }); // .send({ error: "Upload failed", details: err.message });
} // }
} // }
); // );
fastify.post("/api/uploads-plumbing-work/:customerId/:installationId", async (request, reply) => { // fastify.post("/api/uploads-plumbing-work/:customerId/:installationId", async (request, reply) => {
try { // try {
const { customerId, installationId } = request.params; // const { customerId, installationId } = request.params;
const files = await request.files(); // Await files properly // const files = await request.files(); // Await files properly
if (!files || files.length === 0) { // if (!files || files.length === 0) {
return reply.code(400).send({ error: "No files uploaded" }); // return reply.code(400).send({ error: "No files uploaded" });
} // }
const bucketName = "arminta_profile_pictures"; // const bucketName = "arminta_profile_pictures";
const publicUrls = []; // const publicUrls = [];
for await (const file of files) { // for await (const file of files) {
const uniqueFileName = `${Date.now()}-${Math.random().toString(36).substring(7)}-${file.filename}`; // const uniqueFileName = `${Date.now()}-${Math.random().toString(36).substring(7)}-${file.filename}`;
const filePath = `plumbing_work_picture/${uniqueFileName}`; // const filePath = `plumbing_work_picture/${uniqueFileName}`;
console.log(`Uploading file: ${file.filename}${filePath}`); // console.log(`Uploading file: ${file.filename} → ${filePath}`);
const writeStream = storage.bucket(bucketName).file(filePath).createWriteStream(); // const writeStream = storage.bucket(bucketName).file(filePath).createWriteStream();
file.file.pipe(writeStream); // file.file.pipe(writeStream);
await new Promise((resolve, reject) => { // await new Promise((resolve, reject) => {
writeStream.on("finish", async () => { // writeStream.on("finish", async () => {
try { // try {
await storage.bucket(bucketName).file(filePath).makePublic(); // await storage.bucket(bucketName).file(filePath).makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; // const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
publicUrls.push(publicUrl); // publicUrls.push(publicUrl);
console.log(`File uploaded: ${publicUrl}`); // console.log(`File uploaded: ${publicUrl}`);
resolve(); // resolve();
} catch (error) { // } catch (error) {
console.error("Failed to make file public:", error); // console.error("Failed to make file public:", error);
reject(error); // reject(error);
} // }
}); // });
writeStream.on("error", (err) => { // writeStream.on("error", (err) => {
console.error("Failed to upload file:", err); // console.error("Failed to upload file:", err);
reject(err); // reject(err);
}); // });
}); // });
} // }
// Update MongoDB: Convert URLs to { url: "..." } objects // // Update MongoDB: Convert URLs to { url: "..." } objects
const updatedRecord = await PlumbingWorkPictures.findOneAndUpdate( // const updatedRecord = await PlumbingWorkPictures.findOneAndUpdate(
{ customerId, installationId }, // { customerId, installationId },
{ $push: { pictureUrl: { $each: publicUrls.map(url => ({ url })) } } }, // Append new images // { $push: { pictureUrl: { $each: publicUrls.map(url => ({ url })) } } }, // Append new images
{ new: true, upsert: true } // { new: true, upsert: true }
); // );
reply.send({ success: true, pictures: publicUrls, details: updatedRecord }); // reply.send({ success: true, pictures: publicUrls, details: updatedRecord });
} catch (err) { // } catch (err) {
console.error("Upload Error:", err); // console.error("Upload Error:", err);
reply.code(500).send({ error: "An error occurred", details: err.message }); // reply.code(500).send({ error: "An error occurred", details: err.message });
} // }
}); // });
// fastify.post("/api/uploads-material-recieved/:customerId/:installationId", async (request, reply) => { // fastify.post("/api/uploads-material-recieved/:customerId/:installationId", async (request, reply) => {
// try { // try {
@ -1988,61 +1988,61 @@ fastify.post("/api/uploads-plumbing-work/:customerId/:installationId", async (re
// }, // },
// }); // });
fastify.post( // fastify.post(
"/api/uploads-material-recieved/:customerId/:installationId", // "/api/uploads-material-recieved/:customerId/:installationId",
{ preHandler: upload.any() }, // use 'files' field for multiple uploads // { preHandler: upload.any() }, // use 'files' field for multiple uploads
async (request, reply) => { // async (request, reply) => {
try { // try {
const { customerId, installationId } = request.params; // const { customerId, installationId } = request.params;
const files = request.files; // multer-style files array // const files = request.files; // multer-style files array
if (!files || files.length === 0) { // if (!files || files.length === 0) {
return reply.code(400).send({ error: "No files uploaded" }); // return reply.code(400).send({ error: "No files uploaded" });
} // }
const bucketName = "arminta_profile_pictures"; // const bucketName = "arminta_profile_pictures";
const publicUrls = []; // const publicUrls = [];
for (const file of files) { // for (const file of files) {
// Generate unique file name // // Generate unique file name
const uniqueFileName = `${Date.now()}-${Math.random() // const uniqueFileName = `${Date.now()}-${Math.random()
.toString(36) // .toString(36)
.substring(7)}-${file.originalname}`; // .substring(7)}-${file.originalname}`;
const filePath = `plumbing_work_picture/${uniqueFileName}`; // const filePath = `plumbing_work_picture/${uniqueFileName}`;
// Read file buffer // // Read file buffer
const fileBuffer = await fs.promises.readFile(file.path); // const fileBuffer = await fs.promises.readFile(file.path);
// Upload to GCS // // Upload to GCS
await storage.bucket(bucketName).file(filePath).save(fileBuffer); // await storage.bucket(bucketName).file(filePath).save(fileBuffer);
// Make file public // // Make file public
await storage.bucket(bucketName).file(filePath).makePublic(); // await storage.bucket(bucketName).file(filePath).makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; // const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
publicUrls.push(publicUrl); // publicUrls.push(publicUrl);
} // }
// Save/Update in DB // // Save/Update in DB
const updatedRecord = await MaterialRecievedPictures.findOneAndUpdate( // const updatedRecord = await MaterialRecievedPictures.findOneAndUpdate(
{ customerId, installationId }, // { customerId, installationId },
{ $push: { pictureUrl: { $each: publicUrls.map((url) => ({ url })) } } }, // Append images // { $push: { pictureUrl: { $each: publicUrls.map((url) => ({ url })) } } }, // Append images
{ new: true, upsert: true } // { new: true, upsert: true }
); // );
return reply.send({ // return reply.send({
success: true, // success: true,
pictures: publicUrls, // pictures: publicUrls,
details: updatedRecord, // details: updatedRecord,
}); // });
} catch (err) { // } catch (err) {
request.log.error(err); // request.log.error(err);
return reply // return reply
.code(500) // .code(500)
.send({ error: "Upload failed", details: err.message }); // .send({ error: "Upload failed", details: err.message });
} // }
} // }
); // );
fastify.post("/api/installLogin", { fastify.post("/api/installLogin", {
schema: { schema: {
description: "Login as Installation Manager", description: "Login as Installation Manager",

Loading…
Cancel
Save