api/resources/setting/MobileApp/MobileAppController.js
2024-11-06 14:05:16 +05:30

185 lines
6.3 KiB
JavaScript

import MobileApp from "./MobileAppModel.js";
import { ref, uploadBytes, getDownloadURL, deleteObject } from "firebase/storage";
import { storage } from "../../../uploads/firebaseConfig.js";
export const addMobileApp = async (req, res) => {
try {
const application = await MobileApp.findOne();
const folderPath = "MobileApps/";
let pdapp, rdapp, scapp, tmapp;
// Helper function to upload and get `url` for an APK
const uploadApp = async (file, currentPublicId) => {
// Delete old file if it exists
if (currentPublicId) {
const oldFileRef = ref(storage, `${folderPath}${currentPublicId}`);
await deleteObject(oldFileRef);
}
// Create a storage reference
const storageRef = ref(storage, `${folderPath}${file.name}`);
// Upload the file
await uploadBytes(storageRef, file.data);
// Get the download URL
const url = await getDownloadURL(storageRef);
return { public_id: file.name, url }; // Use file.name as public_id
};
// Upload APKs conditionally based on their presence in `req.files`
if (req.files.PDApp) {
pdapp = await uploadApp(req.files.PDApp, application?.PDApp?.public_id);
}
if (req.files.RDApp) {
rdapp = await uploadApp(req.files.RDApp, application?.RDApp?.public_id);
}
if (req.files.SCApp) {
scapp = await uploadApp(req.files.SCApp, application?.SCApp?.public_id);
}
if (req.files.TMApp) {
tmapp = await uploadApp(req.files.TMApp, application?.TMApp?.public_id);
}
// Prepare update object only for fields that have new uploads
const updateData = {
...(pdapp && { PDApp: pdapp }),
...(rdapp && { RDApp: rdapp }),
...(scapp && { SCApp: scapp }),
...(tmapp && { TMApp: tmapp }),
};
if (!application) {
// Create new record if it doesn't exist
const newApplication = await MobileApp.create(updateData);
return res.status(200).json({
status: "success",
message: "Uploaded Apps Successfully",
PDAppUrl: newApplication.PDApp?.url,
RDAppUrl: newApplication.RDApp?.url,
SCAppUrl: newApplication.SCApp?.url,
TMAppUrl: newApplication.TMApp?.url,
});
} else {
// Update existing record
await MobileApp.updateOne({}, { $set: updateData });
return res.status(200).json({
status: "success",
message: "Updated Apps Successfully",
PDAppUrl: pdapp?.url || application.PDApp.url,
RDAppUrl: rdapp?.url || application.RDApp.url,
SCAppUrl: scapp?.url || application.SCApp.url,
TMAppUrl: tmapp?.url || application.TMApp.url,
});
}
} catch (error) {
console.error("Error uploading APKs:", error);
return res.status(500).json({
status: "error",
message: "An error occurred while uploading APKs.",
});
}
};
export const getMobileApp = async (req, res) => {
try {
const application = await MobileApp.findOne();
if (!application) {
return res.status(404).json({
status: "error",
message: "No Mobile App found",
});
}
return res.status(200).json({
status: "success",
PDAppUrl: application.PDApp?.url,
RDAppUrl: application.RDApp?.url,
SCAppUrl: application.SCApp?.url,
TMAppUrl: application.TMApp?.url,
});
} catch (error) {
console.error("Error fetching Mobile App:", error);
return res.status(500).json({
status: "error",
message: "An error occurred while fetching Mobile App.",
});
}
};
// import cloudinary from "../../../Utils/cloudinary.js";
// export const addMobileApp = async (req, res) => {
// try {
// const application = await MobileApp.findOne();
// const folderPath = "MobileApps";
// let pdapp, rdapp, scapp, tmapp;
// console.log("1", req.files);
// console.log("3", application);
// // Helper function to upload and get `url` and `public_id` for an APK
// const uploadApp = async (file, currentPublicId) => {
// // Delete old file if it exists
// if (currentPublicId) {
// await cloudinary.v2.uploader.destroy(currentPublicId);
// }
// // Upload new file
// const result = await cloudinary.v2.uploader.upload(file.tempFilePath, {
// folder: folderPath,
// resource_type: "auto",
// });
// return { public_id: result.public_id, url: result.secure_url };
// };
// // Upload APKs conditionally based on their presence in `req.files`
// if (req.files.PDApp) {
// pdapp = await uploadApp(req.files.PDApp, application?.PDApp?.public_id);
// }
// if (req.files.RDApp) {
// rdapp = await uploadApp(req.files.RDApp, application?.RDApp?.public_id);
// }
// if (req.files.SCApp) {
// scapp = await uploadApp(req.files.SCApp, application?.SCApp?.public_id);
// }
// if (req.files.TMApp) {
// tmapp = await uploadApp(req.files.TMApp, application?.TMApp?.public_id);
// }
// // Prepare update object only for fields that have new uploads
// const updateData = {
// ...(pdapp && { PDApp: pdapp }),
// ...(rdapp && { RDApp: rdapp }),
// ...(scapp && { SCApp: scapp }),
// ...(tmapp && { TMApp: tmapp }),
// };
// if (!application) {
// // Create new record if it doesn't exist
// const newApplication = await MobileApp.create(updateData);
// return res.status(200).json({
// status: "success",
// message: "Uploaded Apps Successfully",
// PDAppUrl: newApplication.PDApp?.url,
// RDAppUrl: newApplication.RDApp?.url,
// SCAppUrl: newApplication.SCApp?.url,
// TMAppUrl: newApplication.TMApp?.url,
// });
// } else {
// // Update existing record
// await MobileApp.updateOne({}, { $set: updateData });
// return res.status(200).json({
// status: "success",
// message: "Updated Apps Successfully",
// PDAppUrl: pdapp?.url || application.PDApp.url,
// RDAppUrl: rdapp?.url || application.RDApp.url,
// SCAppUrl: scapp?.url || application.SCApp.url,
// TMAppUrl: tmapp?.url || application.TMApp.url,
// });
// }
// } catch (error) {
// console.error("Error uploading APKs:", error);
// return res.status(500).json({
// status: "error",
// message: "An error occurred while uploading APKs.",
// });
// }
// };