diff --git a/public/app/listeners/getAppInstanceResources.js b/public/app/listeners/getAppInstanceResources.js new file mode 100644 index 00000000..b67e0dce --- /dev/null +++ b/public/app/listeners/getAppInstanceResources.js @@ -0,0 +1,70 @@ +const GET_APP_INSTANCE_RESOURCES_CHANNEL = require('../config/channels'); + +const getAppInstanceResources = (mainWindow, db) => async (data = {}) => { + const defaultResponse = []; + const { userId, appInstanceId, spaceId, subSpaceId, type } = data; + try { + // tools live on the parent + const tool = spaceId === subSpaceId; + + let appInstanceResourcesHandle; + + // if not a tool, we need to go one step further into the phase + if (!tool) { + appInstanceResourcesHandle = db + .get('spaces') + .find({ id: spaceId }) + .get('phases') + .find({ id: subSpaceId }) + .get('items') + .filter(item => item.appInstance) + .map(item => item.appInstance) + .find({ id: appInstanceId }) + .get('resources'); + } else { + appInstanceResourcesHandle = db + .get('spaces') + .find({ id: spaceId }) + .get('items') + .filter(item => item.appInstance) + .map(item => item.appInstance) + .find({ id: appInstanceId }) + .get('resources'); + } + + // only filter by type if provided + if (type) { + appInstanceResourcesHandle.filter({ type }); + } + + // only filter by user if provided + if (userId) { + appInstanceResourcesHandle.filter({ user: userId }); + } + + const appInstanceResources = appInstanceResourcesHandle.value(); + + const response = appInstanceResources || defaultResponse; + + // response is sent back to channel specific for this app instance + mainWindow.webContents.send( + `${GET_APP_INSTANCE_RESOURCES_CHANNEL}_${appInstanceId}`, + { + appInstanceId, + payload: response, + } + ); + } catch (e) { + console.error(e); + // error is sent back to channel specific for this app instance + mainWindow.webContents.send( + `${GET_APP_INSTANCE_RESOURCES_CHANNEL}_${appInstanceId}`, + { + appInstanceId, + payload: defaultResponse, + } + ); + } +}; + +module.exports = getAppInstanceResources; diff --git a/public/app/listeners/index.js b/public/app/listeners/index.js index 6ddd12f3..1d028f87 100644 --- a/public/app/listeners/index.js +++ b/public/app/listeners/index.js @@ -18,6 +18,7 @@ const getDeveloperMode = require('./getDeveloperMode'); const setDeveloperMode = require('./setDeveloperMode'); const clearUserInput = require('./clearUserInput'); const showClearUserInputPrompt = require('./showClearUserInputPrompt'); +const getAppInstanceResources = require('./getAppInstanceResources'); module.exports = { loadSpace, @@ -40,4 +41,5 @@ module.exports = { setDeveloperMode, clearUserInput, showClearUserInputPrompt, + getAppInstanceResources, }; diff --git a/public/electron.js b/public/electron.js index b2471ea7..9602834c 100644 --- a/public/electron.js +++ b/public/electron.js @@ -72,6 +72,7 @@ const { setDeveloperMode, clearUserInput, showClearUserInputPrompt, + getAppInstanceResources, } = require('./app/listeners'); const isMac = require('./app/utils/isMac'); @@ -362,72 +363,10 @@ app.on('ready', async () => { ); // called when getting AppInstanceResources - ipcMain.on(GET_APP_INSTANCE_RESOURCES_CHANNEL, (event, data = {}) => { - const defaultResponse = []; - const { userId, appInstanceId, spaceId, subSpaceId, type } = data; - try { - // tools live on the parent - const tool = spaceId === subSpaceId; - - let appInstanceResourcesHandle; - - // if not a tool, we need to go one step further into the phase - if (!tool) { - appInstanceResourcesHandle = db - .get('spaces') - .find({ id: spaceId }) - .get('phases') - .find({ id: subSpaceId }) - .get('items') - .filter(item => item.appInstance) - .map(item => item.appInstance) - .find({ id: appInstanceId }) - .get('resources'); - } else { - appInstanceResourcesHandle = db - .get('spaces') - .find({ id: spaceId }) - .get('items') - .filter(item => item.appInstance) - .map(item => item.appInstance) - .find({ id: appInstanceId }) - .get('resources'); - } - - // only filter by type if provided - if (type) { - appInstanceResourcesHandle.filter({ type }); - } - - // only filter by user if provided - if (userId) { - appInstanceResourcesHandle.filter({ user: userId }); - } - - const appInstanceResources = appInstanceResourcesHandle.value(); - - const response = appInstanceResources || defaultResponse; - - // response is sent back to channel specific for this app instance - mainWindow.webContents.send( - `${GET_APP_INSTANCE_RESOURCES_CHANNEL}_${appInstanceId}`, - { - appInstanceId, - payload: response, - } - ); - } catch (e) { - console.error(e); - // error is sent back to channel specific for this app instance - mainWindow.webContents.send( - `${GET_APP_INSTANCE_RESOURCES_CHANNEL}_${appInstanceId}`, - { - appInstanceId, - payload: defaultResponse, - } - ); - } - }); + ipcMain.on( + GET_APP_INSTANCE_RESOURCES_CHANNEL, + getAppInstanceResources(mainWindow, db) + ); // called when creating an AppInstanceResource ipcMain.on(POST_APP_INSTANCE_RESOURCE_CHANNEL, (event, payload = {}) => {