diff --git a/server.js b/server.js index 2239b520..7e45a92f 100644 --- a/server.js +++ b/server.js @@ -61,6 +61,9 @@ const allowedProcessMessagesSet = _getAllowedStatesSet({ 'READY_TRX_TAX_REPORT', 'ERROR_TRX_TAX_REPORT', + 'READY_SYNC', + 'ERROR_SYNC', + 'ALL_TABLE_HAVE_BEEN_CLEARED', 'ALL_TABLE_HAVE_NOT_BEEN_CLEARED', diff --git a/src/helpers/manage-window.js b/src/helpers/manage-window.js index 9bb72617..5f74f54d 100644 --- a/src/helpers/manage-window.js +++ b/src/helpers/manage-window.js @@ -111,8 +111,16 @@ const centerWindow = (win, workArea) => { win.setBounds(boundsOpts) } +const isWindowInvisible = (win) => { + return ( + !win?.isVisible() || + !win?.isFocused() + ) +} + module.exports = { hideWindow, showWindow, - centerWindow + centerWindow, + isWindowInvisible } diff --git a/src/manage-worker-messages.js b/src/manage-worker-messages.js index d94d9f16..2cbd7265 100644 --- a/src/manage-worker-messages.js +++ b/src/manage-worker-messages.js @@ -10,8 +10,15 @@ const showMessageModalDialog = require( const isMainWinAvailable = require( './helpers/is-main-win-available' ) -const { showWindow } = require('./helpers/manage-window') -const showNotification = require('./show-notification') +const { + showWindow +} = require('./helpers/manage-window') +const showTrxTaxReportNotification = require( + './show-notification/show-trx-tax-report-notification' +) +const showSyncNotification = require( + './show-notification/show-sync-notification' +) const PROCESS_MESSAGES = require( '../bfx-reports-framework/workers/loc.api/process.message.manager/process.messages' ) @@ -238,22 +245,16 @@ module.exports = (ipc) => { ipc.send({ state: PROCESS_STATES.REMOVE_ALL_TABLES }) } if ( - ( - state === PROCESS_MESSAGES.READY_TRX_TAX_REPORT || - state === PROCESS_MESSAGES.ERROR_TRX_TAX_REPORT - ) && - ( - !wins?.mainWindow?.isVisible() || - !wins?.mainWindow?.isFocused() - ) + state === PROCESS_MESSAGES.READY_TRX_TAX_REPORT || + state === PROCESS_MESSAGES.ERROR_TRX_TAX_REPORT ) { - const isError = state === PROCESS_MESSAGES.ERROR_TRX_TAX_REPORT - const body = isError - ? 'An unexpected error occurred while generating the tax report!' - : 'Your tax report is ready!' - const urgency = isError ? 'critical' : 'normal' - - showNotification({ body, urgency }) + showTrxTaxReportNotification(mess) + } + if ( + state === PROCESS_MESSAGES.READY_SYNC || + state === PROCESS_MESSAGES.ERROR_SYNC + ) { + showSyncNotification(mess) } } catch (err) { console.error(err) diff --git a/src/show-notification/show-sync-notification.js b/src/show-notification/show-sync-notification.js new file mode 100644 index 00000000..b63099a4 --- /dev/null +++ b/src/show-notification/show-sync-notification.js @@ -0,0 +1,43 @@ +'use strict' + +const PROCESS_MESSAGES = require( + '../../bfx-reports-framework/workers/loc.api/process.message.manager/process.messages' +) +const wins = require('../windows') +const { isWindowInvisible } = require('../helpers/manage-window') +const showNotification = require('./') + +const getBody = (params) => { + const { + isError, + isInterrupted + } = params ?? {} + + if (isError) { + return 'Data sync completed with an error!' + } + if (isInterrupted) { + return 'Data sync interrupted!' + } + + return 'Data sync completed successfully!' +} + +module.exports = (mess) => { + const { + state = '', + data = {} + } = mess ?? {} + + if (!isWindowInvisible(wins?.mainWindow)) { + return + } + + const isError = state === PROCESS_MESSAGES.ERROR_SYNC + const isInterrupted = !!data?.isInterrupted + + const body = getBody({ isError, isInterrupted }) + const urgency = isError ? 'critical' : 'normal' + + showNotification({ body, urgency }) +} diff --git a/src/show-notification/show-trx-tax-report-notification.js b/src/show-notification/show-trx-tax-report-notification.js new file mode 100644 index 00000000..02be5289 --- /dev/null +++ b/src/show-notification/show-trx-tax-report-notification.js @@ -0,0 +1,26 @@ +'use strict' + +const PROCESS_MESSAGES = require( + '../../bfx-reports-framework/workers/loc.api/process.message.manager/process.messages' +) +const wins = require('../windows') +const { isWindowInvisible } = require('../helpers/manage-window') +const showNotification = require('./') + +module.exports = (mess) => { + const { + state = '' + } = mess ?? {} + + if (!isWindowInvisible(wins?.mainWindow)) { + return + } + + const isError = state === PROCESS_MESSAGES.ERROR_TRX_TAX_REPORT + const body = isError + ? 'An unexpected error occurred while generating the tax report!' + : 'Your tax report is ready!' + const urgency = isError ? 'critical' : 'normal' + + showNotification({ body, urgency }) +}