Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firestore track search #229

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions assets/constants.js
Original file line number Diff line number Diff line change
@@ -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
'`': '\'',
'´': '\'',
Expand All @@ -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;
41 changes: 41 additions & 0 deletions assets/firestore.js
Original file line number Diff line number Diff line change
@@ -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
};
24 changes: 7 additions & 17 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 17 additions & 9 deletions renderer/item_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 16 additions & 3 deletions renderer/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]}`);
Expand All @@ -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);
}
}


Expand Down
Loading