From c6e9e1a59a525de64e9bdcb32de9c16afa450b92 Mon Sep 17 00:00:00 2001 From: desumai Date: Fri, 12 Apr 2024 10:10:31 -0400 Subject: [PATCH 1/2] Refactor firestore calls - Created a module for firestore so other modules can also access it --- assets/constants.js | 7 ++++++- assets/firestore.js | 43 +++++++++++++++++++++++++++++++++++++++++++ main.js | 21 ++++----------------- 3 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 assets/firestore.js diff --git a/assets/constants.js b/assets/constants.js index f83bfa3..e35455d 100644 --- a/assets/constants.js +++ b/assets/constants.js @@ -1,9 +1,10 @@ // for global constants. used for debugging + const CONSTANTS = Object.freeze({ OPEN_DEV_TOOLS: false, // set true to open dev tools on application launch ENV: this.OPEN_DEV_TOOLS ? 'development.manage.development' : 'prod.manage.prod', // set true to use production environment and set false for test environment. - REPLACEMENTS: Object.freeze({ // for replacing commonly confused illegal characters with their legal counterparts + REPLACEMENTS: Object.freeze({ // single quote '`': '\'', 'ยด': '\'', @@ -26,6 +27,10 @@ const CONSTANTS = Object.freeze({ appId: '1:36956740284:web:561e9a73a0f3f4b08fceb9', measurementId: 'G-112KCDJT5G', }, + FIRESTORE_EVENT_STARTAPP: 'Start App', + FIRESTORE_EVENT_SEARCH: 'Search Item', + FIRESTORE_EVENT_ADDTOINVENTORY: 'Add to Inventory', + FIRESTORE_EVENT_REQUESTITEM: 'Request Item', }); module.exports = CONSTANTS; diff --git a/assets/firestore.js b/assets/firestore.js new file mode 100644 index 0000000..9a515d1 --- /dev/null +++ b/assets/firestore.js @@ -0,0 +1,43 @@ +//for firestore app usage logging. Doesn't work for ES6 +const {initializeApp} = require('firebase/app'); +const {getFirestore, collection, addDoc} = require('firebase/firestore/lite'); +const { isNull } = require('lodash'); +const CONSTANTS = require('../assets/constants.js'); + + +let fireApp = null; +let fireDB = null; + +/** + * login to firestore + * can only initialize firestore once + */ +function init() { + if(isNull(fireDB)) { + fireApp = initializeApp(CONSTANTS.FIREBASECONFIG); + fireDB = getFirestore(fireApp); + } else { + console.log("firestore already initialized"); + return; + } +} + + +/** + * Add data to fireStore + * userid + date is included by default + * @param {data} data Data dictionary to be included + */ +async function fslog(data) { + if (isNull(fireDB)){ + console.log("firestore is not initialized"); + return; + } + const fireRef = await addDoc(collection(fireDB, 'events'), { + time: Date.now(), user: process.env.USERNAME, ...data, + }); +} + +module.exports = { + init, fslog +}; \ No newline at end of file diff --git a/main.js b/main.js index d9d678d..49e6cf9 100644 --- a/main.js +++ b/main.js @@ -4,26 +4,13 @@ const path = require('path'); const fs = require('fs'); const {appUpdater} = require('./assets/autoupdater'); const CONSTANTS = require('./assets/constants.js'); -const {initializeApp} = require('firebase/app'); -const {getFirestore, collection, addDoc} = require('firebase/firestore/lite'); +const firestore = require('./assets/firestore.js'); let mainWindow; let settingWindow; -const fireApp = initializeApp(CONSTANTS.FIREBASECONFIG); -const fireDB = getFirestore(fireApp); -fireStore({event: 'Start App'}); - -/** - * Add data to fireStore - * userid + date is included by default - * @param {data} data Data dictionary to be included - */ -async function fireStore(data) { - const fireRef = await addDoc(collection(fireDB, 'events'), { - time: Date.now(), user: process.env.USERNAME, ...data, - }); - console.log('added: ' + fireRef.id); -}; +firestore.init(); +firestore.fslog({event: CONSTANTS.FIRESTORE_EVENT_STARTAPP}); + if (CONSTANTS.OPEN_DEV_TOOLS) { require('electron-reload')(__dirname); From 299e56ff50d798d2759ec74883735e629da906ea Mon Sep 17 00:00:00 2001 From: desumai Date: Fri, 12 Apr 2024 10:49:27 -0400 Subject: [PATCH 2/2] Firestore log item search - Log everytime user does an item search - TODO: only log non-poweruser searches? --- renderer/worker.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/renderer/worker.js b/renderer/worker.js index 39ee13b..5466b7e 100644 --- a/renderer/worker.js +++ b/renderer/worker.js @@ -11,6 +11,7 @@ const path = require('path'); const Translation = require('../assets/item_translation/item-translation'); const fs = require('fs'); const CONSTANTS = require('../assets/constants.js'); +const {fslog} = require('../assets/firestore.js'); /** * Handles messages from the WorkerHandler * @@ -27,6 +28,7 @@ onmessage = function (e) { valid.validateSingle(e.data[1]).then((result) => { postMessage(['result', result]); }); + fslog({event: CONSTANTS.FIRESTORE_EVENT_SEARCH}); break; case 'validBatch': valid = new Validate();