diff --git a/assets/constants.js b/assets/constants.js index f83bfa3..8930327 100644 --- a/assets/constants.js +++ b/assets/constants.js @@ -1,9 +1,11 @@ // for global constants. used for debugging +const devMode = false; //set to true for testing. Remember to set it back to false when committing! + 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 + OPEN_DEV_TOOLS: devMode, // set true to open dev tools on application launch + ENV: devMode ? 'test.manage.test' : 'prod.manage.prod', // set false to use production environment and set true for test environments. + REPLACEMENTS: Object.freeze({ // single quote '`': '\'', 'ยด': '\'', @@ -26,6 +28,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..a773b84 --- /dev/null +++ b/assets/firestore.js @@ -0,0 +1,41 @@ + +const {initializeApp} = require('firebase/app'); +const {getFirestore, collection, addDoc} = require('firebase/firestore/lite'); +const CONSTANTS = require('../assets/constants.js'); + +/** + * For Firestore app usage tracking + */ +class Firestore { + #fireApp; + #fireDB; + + /** + * login to firestore + */ + constructor(){ + this.#fireApp = initializeApp(CONSTANTS.FIREBASECONFIG); + this.#fireDB = getFirestore(this.#fireApp); + } + + + /** + * Add data to fireStore + * userid + date is included by default + * @param {data} data Data dictionary to be included + */ + async log(data) { + if (this.#fireDB == undefined){ + console.log("firestore is not initialized"); + return; + } + const fireRef = await addDoc(collection(this.#fireDB, 'events'), { + time: Date.now(), user: process.env.USERNAME, ...data, + }); + console.log("added: " + fireRef.id); + } +} + +module.exports = { + Firestore +}; \ No newline at end of file diff --git a/main.js b/main.js index d9d678d..259f1de 100644 --- a/main.js +++ b/main.js @@ -4,26 +4,16 @@ 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); -}; +const firestore = new Firestore(); +firestore.log({event: CONSTANTS.FIRESTORE_EVENT_STARTAPP}); + +ipcMain.on('firestore-log', (event, data) => { + firestore.log(data); +}) if (CONSTANTS.OPEN_DEV_TOOLS) { require('electron-reload')(__dirname); diff --git a/renderer/item_main.js b/renderer/item_main.js index ed3bcf0..6c293f5 100644 --- a/renderer/item_main.js +++ b/renderer/item_main.js @@ -306,6 +306,8 @@ Content-Type: text/html; boundary=--boundary_text_string // Send string to main process to write file ipcRenderer.send('write-file', mailText); // requestModal.toggle(); + //log in firestore + ipcRenderer.send('firestore-log', {event: CONSTANTS.FIRESTORE_EVENT_REQUESTITEM}); } /* Infinite scroll @@ -512,15 +514,20 @@ document.getElementById('storeroom-btn').addEventListener('click', () => { return; } const worker = new WorkerHandler(); - const upload = { - cataloguenum: '', - issueunit: document.getElementById('storeroom-item-uom').value, - itemnumber: document.getElementById('storeroom-item-itemnum').value, - storeroomname: document.getElementById('storeroom-storeroom').value, - siteID: localStorage.getItem('userSite'), - vendorname: '', - }; - worker.work(['uploadInventory', upload]); + const upload = { + cataloguenum: '', + issueunit: document.getElementById('storeroom-item-uom').value, + itemnumber: document.getElementById('storeroom-item-itemnum').value, + storeroomname: document.getElementById('storeroom-storeroom').value, + siteID: localStorage.getItem('userSite'), + vendorname: '', + }; + worker.work(['uploadInventory', upload, true], (result) => { + //log in firestore if upload is successful + if(result[0] == 1) { + ipcRenderer.send("firestore-log", {event: CONSTANTS.FIRESTORE_EVENT_ADDTOINVENTORY}) + } + }); }); // Other @@ -1297,6 +1304,7 @@ function validSingle(isExtended = false) { worker.work(['validSingle', raw_desc], (result) => { showResult(result, isExtended); }); + ipcRenderer.send('firestore-log',{event: CONSTANTS.FIRESTORE_EVENT_SEARCH}) } function showResult(result, isExtended = false) { diff --git a/renderer/worker.js b/renderer/worker.js index 39ee13b..ae33906 100644 --- a/renderer/worker.js +++ b/renderer/worker.js @@ -112,7 +112,11 @@ onmessage = function (e) { uploadImages(e.data[1]); break; case 'uploadInventory': - uploadInventory(e.data[1]); + if(e.data.length > 2){ //only adding to storeroom + uploadInventory(e.data[1], e.data[2]).then((statuscode) => this.postMessage(['result', statuscode])); + } else { //other uses + uploadInventory(e.data[1]); + } break; default: console.log(`Unimplimented work ${e.data[0]}`); @@ -122,10 +126,19 @@ onmessage = function (e) { /** * upload item to inventory * @param {*} item data regarding uploaded item + * @param {boolean | undefined} [rtrn] optional parameter used to return information on whether adding to inventory was successful + * @returns {boolean | void} returns the status code if rtrn is true, else returns void */ -async function uploadInventory(item) { +async function uploadInventory(item, rtrn) { const maximo = new Maximo(); - console.log(await maximo.uploadToInventory(item)); + const statusCode = await maximo.uploadToInventory(item); + try{ + if(rtrn == true) { + return statusCode; + } + } catch (err){ + console.error(err); + } }