Skip to content

Commit

Permalink
feat(api): read api settings from firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Jun 17, 2023
1 parent 45028d5 commit ad2a67e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import { logger } from './src/utils/logger.js';
// load classes
import { users } from './src/classes/Users.js';
import { congregations } from './src/classes/Congregations.js';
import { initializeAPI } from './src/config/cpe.db-config.js';

const PORT = process.env.PORT || 8000;
const APP_VERSION = process.env.npm_package_version;

// define global variables
global.requestTracker = [];

await initializeAPI();
logger('info', JSON.stringify({ details: `API: minimum CPE client version set to ${global.minimumVersionCPE}` }));

await users.loadAll();
await congregations.loadAll();

Expand Down
26 changes: 26 additions & 0 deletions src/config/cpe.db-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getFirestore } from 'firebase-admin/firestore';

const db = getFirestore();

export const initializeAPI = async () => {
try {
let settingID;
const apiSettings = db.collection('api_settings');
const snapshot = await apiSettings.get();
snapshot.forEach((doc) => {
settingID = doc.id;
global.minimumVersionCPE = doc.data().minimum_version;
});

if (!settingID) {
const data = {
minimum_version: '1.0.0',
};

await db.collection('api_settings').add(data);
global.minimumVersionCPE = data.minimum_version;
}
} catch (err) {
throw new Error(err);
}
};
2 changes: 1 addition & 1 deletion src/middleware/app-version-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const appVersionChecker = () => {
return;
}

const cpeMinimum = '2.85.0';
const cpeMinimum = global.minimumVersionCPE;

if (appVersion >= cpeMinimum) {
next();
Expand Down
2 changes: 2 additions & 0 deletions src/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ export const logger = (level, message) => {
console.error(message);
if (isProd) logtail.error(message);
}

if (isProd) logtail.flush();
}
};

0 comments on commit ad2a67e

Please sign in to comment.