Skip to content

Commit

Permalink
Merge pull request #793 from SSWConsulting/firebase-update-1
Browse files Browse the repository at this point in the history
Bump Firebase SDK version
  • Loading branch information
tombui99 committed Nov 28, 2023
2 parents 452c507 + 750a6d7 commit cfa8b06
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 61 deletions.
73 changes: 35 additions & 38 deletions api/functions/firestore.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
const admin = require('firebase-admin');
const { initializeApp } = require('firebase-admin/app');
const { getFirestore } = require('firebase-admin/firestore');
const { CONSTANTS } = require('./consts');

exports.getUserIdFromApiKey = (api) => {
return admin
.firestore()
.collection(CONSTANTS.users)
.where(CONSTANTS.apiKey, '==', api)
.get()
.then((x) => (x.docs.length === 1 ? x.docs[0].id : null));
initializeApp();
const db = getFirestore();

exports.getUserIdFromApiKey = async (api) => {
const q = db.collection(CONSTANTS.users);
const users = await q.where(CONSTANTS.apiKey, '==', api).limit(1).get();
return users.empty ? null : users.docs[0].id;
};

exports.updateLastBuild = (userId, apikey, runId) => {
return admin
.firestore()
.collection(CONSTANTS.users)
.doc(userId)
.update({
lastBuild: new Date(),
runId,
})
.then(() =>
admin.firestore().collection(CONSTANTS.runs).doc(runId).set({
apikey,
runId,
})
);
exports.updateLastBuild = async (userId, apikey, runId) => {
await db.collection(CONSTANTS.users).doc(userId).set(
{
lastBuild: new Date(),
runId,
},
{ merge: true }
);
await db.collection(CONSTANTS.runs).doc(runId).set(
{
apikey,
runId,
},
{ merge: true }
);
};

exports.getRun = (runId) =>
admin
.firestore()
.collection(CONSTANTS.runs)
.doc(runId)
.get()
.then((doc) => doc.data());
exports.getRun = async (runId) => {
const doc = await db.collection(CONSTANTS.runs).doc(runId).get();
return doc.data();
};

exports.getAlertEmailConfig = () => {
return admin
.firestore()
.collection(CONSTANTS.config)
.doc('alertEmailConfig')
.get()
.then((doc) => doc.data())
};
exports.getAlertEmailConfig = async () => {
const doc = await db
.collection(CONSTANTS.config)
.doc('alertEmailConfig')
.get();
return doc.data();
};
6 changes: 2 additions & 4 deletions api/functions/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const functions = require('firebase-functions');
const express = require('express');
const admin = require('firebase-admin');
const R = require('ramda');
const fetch = require('node-fetch');
const Queue = require('better-queue');
Expand Down Expand Up @@ -55,7 +54,6 @@ const {
} = require('./firestore');

var cors = require('cors');
admin.initializeApp();

const app = express();
// middlewares
Expand Down Expand Up @@ -364,5 +362,5 @@ app.post('/scanresult/:api/:buildId', async (req, res) => {
res.json(runId);
});

exports.api = functions.region('asia-east2').https.onRequest(app);
exports.api2 = functions.region('asia-northeast1').https.onRequest(app);
exports.api = functions.runWith({ timeoutSeconds: 540 }).region('asia-east2').https.onRequest(app);
exports.api2 = functions.runWith({ timeoutSeconds: 540 }).region('asia-northeast1').https.onRequest(app);
92 changes: 75 additions & 17 deletions api/functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"lint": "eslint .",
"test": "jest",
"serve": "firebase serve --only functions --port 5001",
"serve": "firebase serve -o 0.0.0.0 --only functions --port 5001",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --token $FIREBASETOKEN --only functions",
Expand All @@ -21,7 +21,7 @@
"cors": "^2.8.5",
"dotenv": "^8.6.0",
"express": "^4.18.2",
"firebase-admin": "^11.8.0",
"firebase-admin": "^11.11.1",
"firebase-functions": "^3.24.1",
"node-fetch": "^2.6.11",
"ramda": "^0.27.2",
Expand Down

0 comments on commit cfa8b06

Please sign in to comment.