Skip to content

Commit

Permalink
update firebase functions to use record generator url stored in the a…
Browse files Browse the repository at this point in the history
…dmin section of the firebase db
  • Loading branch information
fostermh committed Jun 6, 2024
1 parent d4429ad commit 3089929
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
63 changes: 47 additions & 16 deletions firebase-functions/functions/updates.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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,
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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);
});
2 changes: 1 addition & 1 deletion src/components/FormComponents/MetadataRecordListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Pages/MetadataForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 3089929

Please sign in to comment.