diff --git a/firebase-functions/functions/updates.js b/firebase-functions/functions/updates.js index 3d6c2256..6b4648ea 100644 --- a/firebase-functions/functions/updates.js +++ b/firebase-functions/functions/updates.js @@ -1,10 +1,9 @@ +const admin = require("firebase-admin"); const functions = require("firebase-functions"); -const { defineString } = require('firebase-functions/params'); const https = require("https"); const axios = require("axios"); -const generatorURL = defineString('RECORD_GENERATOR_URL'); -const urlBase = process.env.RECORD_GENERATOR_URL || generatorURL.value() || "https://pac-dev1.cioos.org/cioos-xml/"; +const urlBaseDefault = "https://pac-dev1.cioos.org/cioos-xml/" function getRecordFilename(record) { @@ -19,14 +18,30 @@ function getRecordFilename(record) { // creates xml for a completed record. returns a URL to the generated XML exports.downloadRecord = functions.https.onCall( - async ({ record, fileType }, context) => { + async ({ record, fileType, region }, context) => { + + let urlBase = urlBaseDefault;; + try { + urlBase = (await admin.database().ref('admin').child(region).child("recordGeneratorURL").once("value")).val() ?? urlBaseDefault; + } catch (error) { + console.error(`Error fetching recordGeneratorURL for region ${region}, using the default value:`, error); + } + const url = `${urlBase}recordTo${fileType.toUpperCase()}`; const response = await axios.post(url, record); return response.data; } ); -async function updateXML(path, status = "", filename = "") { +async function updateXML(path, region, status = "", filename = "") { + + let urlBase = urlBaseDefault; + try { + urlBase = (await admin.database().ref('admin').child(region).child("recordGeneratorURL").once("value")).val() ?? urlBaseDefault; + } catch (error) { + console.error(`Error fetching recordGeneratorURL for region ${region}, using the default value:`, error); + } + const url = `${urlBase}record`; const urlParams = new URLSearchParams({ path, @@ -44,8 +59,8 @@ exports.regenerateXMLforRecord = functions.https.onCall( if (!context.auth || !context.auth.token) throw new functions.https.HttpsError("unauthenticated"); - const { path, status } = data; - if (["submitted", "published"].includes(status)) updateXML(path); + const { path, status, region } = data; + if (["submitted", "published"].includes(status)) updateXML(path, region); // No need to create new XML if the record is a draft. // If the record is complete, the user can still generate XML for a draft record } @@ -66,8 +81,9 @@ exports.updatesRecordCreate = functions.database // wait a second so the file has a chance to be deleted on the server before it is created // otherwise the server might delete the new files await delay(1000); - return updateXML(path); + return updateXML(path, region); } + return null; }); // if the record changes status we should trigger an update @@ -89,22 +105,37 @@ exports.updatesRecordUpdate = functions.database (status) => status === "published" || status === "submitted" ) ) { - return updateXML(path, afterStatus); + return updateXML(path, region, afterStatus); } console.log("no change"); + return null; }); + +async function deleteXML(filename, region) { + let urlBase = urlBaseDefault; + try { + urlBase = (await admin.database().ref('admin').child(region).child("recordGeneratorURL").once("value")).val() ?? urlBaseDefault; + } catch (error) { + console.error(`Error fetching recordGeneratorURL for region ${region}, using the default value:`, error); + } + + const url = `${urlBase}recordDelete`; + const urlParams = new URLSearchParams({ + filename, + }).toString(); + const urlFull = `${url}?${urlParams}`; + + return https.get(urlFull); +} + // also trigger update when record is deleted exports.updatesRecordDelete = functions.database .ref("/{region}/users/{userID}/records/{recordID}") .onDelete((snpashot, context) => { const record = snpashot.val(); const filename = getRecordFilename(record); - const url = `${urlBase}recordDelete`; - const urlParams = new URLSearchParams({ - filename, - }).toString(); - const urlFull = `${url}?${urlParams}`; - - return https.get(urlFull); + const { region } = context.params; + + return deleteXML(filename, region); }); diff --git a/src/components/FormComponents/MetadataRecordListItem.jsx b/src/components/FormComponents/MetadataRecordListItem.jsx index 9e1f0bd6..8efbdc72 100644 --- a/src/components/FormComponents/MetadataRecordListItem.jsx +++ b/src/components/FormComponents/MetadataRecordListItem.jsx @@ -104,7 +104,7 @@ const MetadataRecordListItem = ({ } else if (fileType === "json") { data = await [JSON.stringify(recordToDataCite( record, language, region, datacitePrefix ), null, 2)]; } else { - const res = await downloadRecord({ record, fileType }); + const res = await downloadRecord({ record, fileType, region }); data = Object.values(res.data.message); } const mimeTypes = { diff --git a/src/components/Pages/MetadataForm.jsx b/src/components/Pages/MetadataForm.jsx index 935ca917..a7851ff3 100644 --- a/src/components/Pages/MetadataForm.jsx +++ b/src/components/Pages/MetadataForm.jsx @@ -425,7 +425,7 @@ class MetadataForm extends FormClassTemplate { const path = `${region}/${userID}/${recordID}`; const { status, filename } = record; - regenerateXMLforRecord({ path, status, filename }); + regenerateXMLforRecord({ path, status, filename, region }); } this.setState({ saveDisabled: true });