Skip to content

Commit

Permalink
feat: factor out appInstanceResources
Browse files Browse the repository at this point in the history
adapt clearUserInput
  • Loading branch information
pyphilia committed Apr 7, 2020
1 parent e1fb99f commit 1d39da8
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 211 deletions.
3 changes: 3 additions & 0 deletions public/app/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const fsPromises = fs.promises;
const SPACES_COLLECTION = 'spaces';
const ACTIONS_COLLECTION = 'actions';
const USERS_COLLECTION = 'users';
const APP_INSTANCE_RESOURCES_COLLECTION = 'resources';

// bootstrap database
const ensureDatabaseExists = async (dbPath = DATABASE_PATH) => {
Expand All @@ -37,13 +38,15 @@ const bootstrapDatabase = (dbPath = DATABASE_PATH) => {
[USERS_COLLECTION]: [],
user: { lang: DEFAULT_LANG },
[ACTIONS_COLLECTION]: [],
[APP_INSTANCE_RESOURCES_COLLECTION]: [],
}).write();
return db;
};

module.exports = {
SPACES_COLLECTION,
USERS_COLLECTION,
APP_INSTANCE_RESOURCES_COLLECTION,
ensureDatabaseExists,
bootstrapDatabase,
};
52 changes: 14 additions & 38 deletions public/app/listeners/clearUserInput.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,26 @@
const _ = require('lodash');
const { CLEARED_USER_INPUT_CHANNEL } = require('../config/channels');
const { ERROR_GENERAL } = require('../config/errors');
const { SPACES_COLLECTION } = require('../db');
const {
SPACES_COLLECTION,
APP_INSTANCE_RESOURCES_COLLECTION,
} = require('../db');
const logger = require('../logger');

const clearUserInput = (mainWindow, db) => async (event, { id }) => {
const clearUserInput = (mainWindow, db) => async (
event,
{ spaceId, userId }
) => {
try {
logger.debug(`clearing user input for space ${id}`);
logger.debug(`clearing user input for space ${spaceId} of user ${userId}`);

// get handle to the space
const spaceHandle = db.get(SPACES_COLLECTION).find({ id });

// get handle to regular items
const regularItems = spaceHandle
.get('phases')
// we only care about phases with items
.filter(phase => phase.items)
// ensure all items are in the same level of the array
.flatMap(phase => phase.items);

// get handle to tools
const tools = spaceHandle.get('items');

// remove user input in items within phases then
// remove user input in tools
[regularItems, tools].forEach(handle => {
// we only care about items with app instances
handle
.filter(item => item.appInstance)
// ensure all app instances are in the same level
.flatMap(item => item.appInstance)
// user input is saved inside resources
.filter(appInstance => appInstance.resources)
// iterate through app instances to be able to delete
// reference to the original resource array and thus
// mutate the db object
.forEach(appInstance => {
const resources = _.get(appInstance, 'resources');
// we should not remove resources marked as public
_.remove(resources, resource => resource.visibility !== 'public');
})
.write();
});
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
.remove({ visibility: 'private', spaceId, user: userId })
.write();

// we need to return the value of the mutated
// space object to the frontend
// get handle to the space
const spaceHandle = db.get(SPACES_COLLECTION).find({ id: spaceId });
const space = spaceHandle.value();

mainWindow.webContents.send(CLEARED_USER_INPUT_CHANNEL, space);
Expand Down
42 changes: 42 additions & 0 deletions public/app/listeners/getAppInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { GET_APP_INSTANCE_CHANNEL } = require('../config/channels');

const getAppInstance = (mainWindow, db) => (event, payload = {}) => {
try {
const { spaceId, subSpaceId, id } = payload;

let appInstance;

// tools live on the parent
const tool = spaceId === subSpaceId;

// if not a tool, we need to go one step further into the phase
if (!tool) {
appInstance = db
.get('spaces')
.find({ id: spaceId })
.get('phases')
.find({ id: subSpaceId })
.get('items')
.filter(item => item.appInstance)
.map(item => item.appInstance)
.find({ id })
.value();
} else {
appInstance = db
.get('spaces')
.find({ id: spaceId })
.get('items')
.filter(item => item.appInstance)
.map(item => item.appInstance)
.find({ id })
.value();
}

mainWindow.webContents.send(GET_APP_INSTANCE_CHANNEL, appInstance);
} catch (e) {
console.error(e);
mainWindow.webContents.send(GET_APP_INSTANCE_CHANNEL, null);
}
};

module.exports = getAppInstance;
38 changes: 6 additions & 32 deletions public/app/listeners/getAppInstanceResources.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,13 @@
const { GET_APP_INSTANCE_RESOURCES_CHANNEL } = require('../config/channels');
const { APP_INSTANCE_RESOURCES_COLLECTION } = require('../db');

const getAppInstanceResources = (mainWindow, db) => async (
event,
data = {}
) => {
const getAppInstanceResources = (mainWindow, db) => (event, data = {}) => {
const defaultResponse = [];
const { userId, appInstanceId, spaceId, subSpaceId, type } = data;
const { userId, appInstanceId, 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');
}
const appInstanceResourcesHandle = db
.get(APP_INSTANCE_RESOURCES_COLLECTION)
.filter({ appInstance: appInstanceId });

// only filter by type if provided
if (type) {
Expand Down
2 changes: 2 additions & 0 deletions public/app/listeners/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const isAuthenticated = require('./isAuthenticated');
const getAppInstanceResources = require('./getAppInstanceResources');
const postAppInstanceResource = require('./postAppInstanceResource');
const patchAppInstanceResource = require('./patchAppInstanceResource');
const getAppInstance = require('./getAppInstance');

module.exports = {
loadSpace,
Expand Down Expand Up @@ -54,4 +55,5 @@ module.exports = {
getAppInstanceResources,
postAppInstanceResource,
patchAppInstanceResource,
getAppInstance,
};
46 changes: 8 additions & 38 deletions public/app/listeners/patchAppInstanceResource.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,20 @@
const { PATCH_APP_INSTANCE_RESOURCE_CHANNEL } = require('../config/channels');
const { APP_INSTANCE_RESOURCES_COLLECTION } = require('../db');

const patchAppInstanceResource = (mainWindow, db) => async (
event,
payload = {}
) => {
const patchAppInstanceResource = (mainWindow, db) => (event, payload = {}) => {
try {
const { appInstanceId, spaceId, subSpaceId, data, id } = payload;
const { appInstanceId: appInstance, data, id } = payload;
const now = new Date();
const fieldsToUpdate = {
updatedAt: now,
data,
};

let resource;

// tools live on the parent
const tool = spaceId === subSpaceId;

// if not a tool, we need to go one step further into the phase
if (!tool) {
resource = 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')
.find({ id })
.assign(fieldsToUpdate)
.value();
} else {
resource = db
.get('spaces')
.find({ id: spaceId })
.get('items')
.filter(item => item.appInstance)
.map(item => item.appInstance)
.find({ id: appInstanceId })
.get('resources')
.find({ id })
.assign(fieldsToUpdate)
.value();
}
const resource = db
.get(APP_INSTANCE_RESOURCES_COLLECTION)
.find({ appInstance, id })
.assign(fieldsToUpdate)
.value();

db.write();
mainWindow.webContents.send(PATCH_APP_INSTANCE_RESOURCE_CHANNEL, resource);
Expand Down
38 changes: 6 additions & 32 deletions public/app/listeners/postAppInstanceResource.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
const ObjectId = require('bson-objectid');
const { POST_APP_INSTANCE_RESOURCE_CHANNEL } = require('../config/channels');
const { APP_INSTANCE_RESOURCES_COLLECTION } = require('../db');

const postAppInstanceResource = (mainWindow, db) => async (
event,
payload = {}
) => {
const postAppInstanceResource = (mainWindow, db) => (event, payload = {}) => {
try {
const {
userId,
appInstanceId,
spaceId,
subSpaceId,
format,
type,
data,
Expand All @@ -23,6 +20,7 @@ const postAppInstanceResource = (mainWindow, db) => async (
// prepare the resource that we will create
const resourceToWrite = {
appInstance: appInstanceId,
spaceId,
createdAt: now,
updatedAt: now,
data,
Expand All @@ -33,34 +31,10 @@ const postAppInstanceResource = (mainWindow, db) => async (
id: ObjectId().str,
};

// tools live on the parent
const tool = spaceId === subSpaceId;

// write the resource to the database
// if not a tool, we need to go one step further into the phase
if (!tool) {
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')
.push(resourceToWrite)
.write();
} else {
db.get('spaces')
.find({ id: spaceId })
.get('items')
.filter(item => item.appInstance)
.map(item => item.appInstance)
.find({ id: appInstanceId })
.get('resources')
.push(resourceToWrite)
.write();
}
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
.push(resourceToWrite)
.write();

// send back the resource
mainWindow.webContents.send(
Expand Down
40 changes: 2 additions & 38 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const {
getAppInstanceResources,
postAppInstanceResource,
patchAppInstanceResource,
getAppInstance,
} = require('./app/listeners');
const isMac = require('./app/utils/isMac');

Expand Down Expand Up @@ -433,44 +434,7 @@ app.on('ready', async () => {
);

// called when getting an AppInstance
ipcMain.on(GET_APP_INSTANCE_CHANNEL, (event, payload = {}) => {
try {
const { spaceId, subSpaceId, id } = payload;

let appInstance;

// tools live on the parent
const tool = spaceId === subSpaceId;

// if not a tool, we need to go one step further into the phase
if (!tool) {
appInstance = db
.get('spaces')
.find({ id: spaceId })
.get('phases')
.find({ id: subSpaceId })
.get('items')
.filter(item => item.appInstance)
.map(item => item.appInstance)
.find({ id })
.value();
} else {
appInstance = db
.get('spaces')
.find({ id: spaceId })
.get('items')
.filter(item => item.appInstance)
.map(item => item.appInstance)
.find({ id })
.value();
}

mainWindow.webContents.send(GET_APP_INSTANCE_CHANNEL, appInstance);
} catch (e) {
console.error(e);
mainWindow.webContents.send(GET_APP_INSTANCE_CHANNEL, null);
}
});
ipcMain.on(GET_APP_INSTANCE_CHANNEL, getAppInstance(mainWindow, db));

// called when getting the database
ipcMain.on(GET_DATABASE_CHANNEL, async () => {
Expand Down
8 changes: 6 additions & 2 deletions src/actions/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ const deleteSpace = ({ id }) => dispatch => {
});
};

const clearUserInput = async ({ id }) => async dispatch => {
const clearUserInput = async ({ spaceId, userId }) => async dispatch => {
try {
// show confirmation prompt
window.ipcRenderer.send(SHOW_CLEAR_USER_INPUT_PROMPT_CHANNEL);
Expand All @@ -304,13 +304,17 @@ const clearUserInput = async ({ id }) => async dispatch => {
(event, response) => {
if (response === 1) {
dispatch(flagClearingUserInput(true));
window.ipcRenderer.send(CLEAR_USER_INPUT_CHANNEL, { id });
window.ipcRenderer.send(CLEAR_USER_INPUT_CHANNEL, {
spaceId,
userId,
});
}
}
);

// listen for response from backend
window.ipcRenderer.once(CLEARED_USER_INPUT_CHANNEL, (event, response) => {
console.log('response', response);
if (response === ERROR_GENERAL) {
toastr.error(ERROR_MESSAGE_HEADER, ERROR_CLEARING_USER_INPUT_MESSAGE);
} else {
Expand Down
Loading

0 comments on commit 1d39da8

Please sign in to comment.