Add cron functions; improve styles

This commit is contained in:
Logan Williams
2023-05-20 15:12:03 +02:00
parent 14fa178d49
commit 0a2923bfde
18 changed files with 16623 additions and 4726 deletions

28
functions/.eslintrc.js Normal file
View File

@@ -0,0 +1,28 @@
module.exports = {
env: {
es6: true,
node: true,
},
parserOptions: {
"ecmaVersion": 2018,
requireConfigFile: false,
},
extends: [
"google",
],
rules: {
"no-restricted-globals": ["error", "name", "length"],
"prefer-arrow-callback": "error",
"quotes": ["error", "double", {"allowTemplateLiterals": true}],
},
overrides: [
{
files: ["**/*.spec.*"],
env: {
mocha: true,
},
rules: {},
},
],
globals: {},
};

1
functions/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
node_modules/

57
functions/index.js Normal file
View File

@@ -0,0 +1,57 @@
/**
* Import function triggers from their respective submodules:
*
* const {onCall} = require("firebase-functions/v2/https");
* const {onDocumentWritten} = require("firebase-functions/v2/firestore");
*
* See a full list of supported triggers at https://firebase.google.com/docs/functions
*/
const { onSchedule } = require("firebase-functions/v2/scheduler");
const logger = require("firebase-functions/logger");
// The Firebase Admin SDK to access Firestore.
const { initializeApp } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");
initializeApp();
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
exports.processSheetScheduler = onSchedule(
"0,15,30,45 * * * *",
async (event) => {
const db = getFirestore();
// get all documents from firestore sheets collection
const querySnapshot = await db.collection("sheets").get();
querySnapshot.forEach(async (doc) => {
console.log("processing document: ", doc.id);
// 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 options = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization:
"Basic " +
Buffer.from(
"service:password"
).toString("base64"),
},
body: JSON.stringify(data),
};
const response = await fetch(url, options);
console.log(response);
await doc.ref.update({ lastArchived: Date.now() });
await sleep(1000);
});
}
);

11871
functions/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

26
functions/package.json Normal file
View File

@@ -0,0 +1,26 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "18"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^11.8.0",
"firebase-functions": "^4.3.1"
},
"devDependencies": {
"eslint": "^8.15.0",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^3.1.0"
},
"private": true
}