From e2cd10615cf6b5c1bf514bc824a90b9f8e865c0f Mon Sep 17 00:00:00 2001 From: msramalho <19508417+msramalho@users.noreply.github.com> Date: Sun, 17 Dec 2023 23:28:19 +0000 Subject: [PATCH] adds email info for new sheets from cronjob --- functions/index.js | 22 +++++++++++++--------- src/store/index.js | 2 ++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/functions/index.js b/functions/index.js index 1ca5687..e22ef44 100644 --- a/functions/index.js +++ b/functions/index.js @@ -36,23 +36,27 @@ String.prototype.hashCode = function () { exports.processSheetScheduler = onSchedule( "* * * * *", async (event) => { + // get all documents from firestore sheets collection const db = getFirestore(); - // get all documents from firestore sheets collection + // each sheet runs once per hour, so we hash the sheet id and only process it if the hash % 60 matches the cron minute const querySnapshot = await db.collection("sheets").get(); - const eventDate = Date.parse(event.scheduleTime); + const eventDate = new Date(Date.parse(event.scheduleTime)); querySnapshot.forEach(async (doc) => { - const docHash = doc.id.hashCode(); - if ((docHash % 60) != eventDate.getMinutes()) { - console.log(`skipping document: ${doc.id} as its hash%60 (${docHash % 60}) does not match the cron minute (${eventDate.getMinutes()})`); + const hashToSixty = Math.abs(doc.id.hashCode() % 60); + if (hashToSixty != eventDate.getMinutes()) { + console.log(`skipping document: ${doc.id} as its hash%60 (${hashToSixty}) does not match the cron minute (${eventDate.getMinutes()})`); return; } - logger.log(`processing document ${doc.id}, its hash % 60 (${docHash % 60}) matches the cron minute (${eventDate.getMinutes()})`); + logger.log(`processing document ${doc.id}, its hash % 60 (${hashToSixty}) matches the cron minute (${eventDate.getMinutes()})`); // send POST request with sheetID to trigger sheet processing - const sheetId = doc.data().sheetId; const url = "https://auto-archiver-api.bellingcat.com/sheet_service"; - const data = { sheet_id: sheetId }; + const data = { + sheet_id: doc.data().sheetId, + author_id: doc.data().email ?? doc.data().uid, + tags: ["setup-tool"] + }; const options = { method: "POST", headers: { @@ -67,7 +71,7 @@ exports.processSheetScheduler = onSchedule( }; const response = await fetch(url, options); - logger.log(response); + console.log(response); await doc.ref.update({ lastArchived: Date.now() }); diff --git a/src/store/index.js b/src/store/index.js index 736e59e..339059e 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -313,6 +313,7 @@ export default new Vuex.Store({ url: newSheet.result.spreadsheetUrl, timestamp: Date.now(), uid: state.user.uid, + email: state.user.email, lastArchived: null, name: name, }); @@ -351,6 +352,7 @@ export default new Vuex.Store({ url: sheetToEnable.result.spreadsheetUrl, timestamp: Date.now(), uid: state.user.uid, + email: state.user.email, lastArchived: null, name: sheetToEnable.result.properties.title, });