Skip to content

Commit 1d39da8

Browse files
committed
feat: factor out appInstanceResources
adapt clearUserInput
1 parent e1fb99f commit 1d39da8

12 files changed

+117
-211
lines changed

public/app/db.js

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const fsPromises = fs.promises;
1111
const SPACES_COLLECTION = 'spaces';
1212
const ACTIONS_COLLECTION = 'actions';
1313
const USERS_COLLECTION = 'users';
14+
const APP_INSTANCE_RESOURCES_COLLECTION = 'resources';
1415

1516
// bootstrap database
1617
const ensureDatabaseExists = async (dbPath = DATABASE_PATH) => {
@@ -37,13 +38,15 @@ const bootstrapDatabase = (dbPath = DATABASE_PATH) => {
3738
[USERS_COLLECTION]: [],
3839
user: { lang: DEFAULT_LANG },
3940
[ACTIONS_COLLECTION]: [],
41+
[APP_INSTANCE_RESOURCES_COLLECTION]: [],
4042
}).write();
4143
return db;
4244
};
4345

4446
module.exports = {
4547
SPACES_COLLECTION,
4648
USERS_COLLECTION,
49+
APP_INSTANCE_RESOURCES_COLLECTION,
4750
ensureDatabaseExists,
4851
bootstrapDatabase,
4952
};

public/app/listeners/clearUserInput.js

+14-38
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,26 @@
1-
const _ = require('lodash');
21
const { CLEARED_USER_INPUT_CHANNEL } = require('../config/channels');
32
const { ERROR_GENERAL } = require('../config/errors');
4-
const { SPACES_COLLECTION } = require('../db');
3+
const {
4+
SPACES_COLLECTION,
5+
APP_INSTANCE_RESOURCES_COLLECTION,
6+
} = require('../db');
57
const logger = require('../logger');
68

7-
const clearUserInput = (mainWindow, db) => async (event, { id }) => {
9+
const clearUserInput = (mainWindow, db) => async (
10+
event,
11+
{ spaceId, userId }
12+
) => {
813
try {
9-
logger.debug(`clearing user input for space ${id}`);
14+
logger.debug(`clearing user input for space ${spaceId} of user ${userId}`);
1015

11-
// get handle to the space
12-
const spaceHandle = db.get(SPACES_COLLECTION).find({ id });
13-
14-
// get handle to regular items
15-
const regularItems = spaceHandle
16-
.get('phases')
17-
// we only care about phases with items
18-
.filter(phase => phase.items)
19-
// ensure all items are in the same level of the array
20-
.flatMap(phase => phase.items);
21-
22-
// get handle to tools
23-
const tools = spaceHandle.get('items');
24-
25-
// remove user input in items within phases then
26-
// remove user input in tools
27-
[regularItems, tools].forEach(handle => {
28-
// we only care about items with app instances
29-
handle
30-
.filter(item => item.appInstance)
31-
// ensure all app instances are in the same level
32-
.flatMap(item => item.appInstance)
33-
// user input is saved inside resources
34-
.filter(appInstance => appInstance.resources)
35-
// iterate through app instances to be able to delete
36-
// reference to the original resource array and thus
37-
// mutate the db object
38-
.forEach(appInstance => {
39-
const resources = _.get(appInstance, 'resources');
40-
// we should not remove resources marked as public
41-
_.remove(resources, resource => resource.visibility !== 'public');
42-
})
43-
.write();
44-
});
16+
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
17+
.remove({ visibility: 'private', spaceId, user: userId })
18+
.write();
4519

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

5026
mainWindow.webContents.send(CLEARED_USER_INPUT_CHANNEL, space);
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const { GET_APP_INSTANCE_CHANNEL } = require('../config/channels');
2+
3+
const getAppInstance = (mainWindow, db) => (event, payload = {}) => {
4+
try {
5+
const { spaceId, subSpaceId, id } = payload;
6+
7+
let appInstance;
8+
9+
// tools live on the parent
10+
const tool = spaceId === subSpaceId;
11+
12+
// if not a tool, we need to go one step further into the phase
13+
if (!tool) {
14+
appInstance = db
15+
.get('spaces')
16+
.find({ id: spaceId })
17+
.get('phases')
18+
.find({ id: subSpaceId })
19+
.get('items')
20+
.filter(item => item.appInstance)
21+
.map(item => item.appInstance)
22+
.find({ id })
23+
.value();
24+
} else {
25+
appInstance = db
26+
.get('spaces')
27+
.find({ id: spaceId })
28+
.get('items')
29+
.filter(item => item.appInstance)
30+
.map(item => item.appInstance)
31+
.find({ id })
32+
.value();
33+
}
34+
35+
mainWindow.webContents.send(GET_APP_INSTANCE_CHANNEL, appInstance);
36+
} catch (e) {
37+
console.error(e);
38+
mainWindow.webContents.send(GET_APP_INSTANCE_CHANNEL, null);
39+
}
40+
};
41+
42+
module.exports = getAppInstance;

public/app/listeners/getAppInstanceResources.js

+6-32
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,13 @@
11
const { GET_APP_INSTANCE_RESOURCES_CHANNEL } = require('../config/channels');
2+
const { APP_INSTANCE_RESOURCES_COLLECTION } = require('../db');
23

3-
const getAppInstanceResources = (mainWindow, db) => async (
4-
event,
5-
data = {}
6-
) => {
4+
const getAppInstanceResources = (mainWindow, db) => (event, data = {}) => {
75
const defaultResponse = [];
8-
const { userId, appInstanceId, spaceId, subSpaceId, type } = data;
6+
const { userId, appInstanceId, type } = data;
97
try {
10-
// tools live on the parent
11-
const tool = spaceId === subSpaceId;
12-
13-
let appInstanceResourcesHandle;
14-
15-
// if not a tool, we need to go one step further into the phase
16-
if (!tool) {
17-
appInstanceResourcesHandle = db
18-
.get('spaces')
19-
.find({ id: spaceId })
20-
.get('phases')
21-
.find({ id: subSpaceId })
22-
.get('items')
23-
.filter(item => item.appInstance)
24-
.map(item => item.appInstance)
25-
.find({ id: appInstanceId })
26-
.get('resources');
27-
} else {
28-
appInstanceResourcesHandle = db
29-
.get('spaces')
30-
.find({ id: spaceId })
31-
.get('items')
32-
.filter(item => item.appInstance)
33-
.map(item => item.appInstance)
34-
.find({ id: appInstanceId })
35-
.get('resources');
36-
}
8+
const appInstanceResourcesHandle = db
9+
.get(APP_INSTANCE_RESOURCES_COLLECTION)
10+
.filter({ appInstance: appInstanceId });
3711

3812
// only filter by type if provided
3913
if (type) {

public/app/listeners/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const isAuthenticated = require('./isAuthenticated');
2525
const getAppInstanceResources = require('./getAppInstanceResources');
2626
const postAppInstanceResource = require('./postAppInstanceResource');
2727
const patchAppInstanceResource = require('./patchAppInstanceResource');
28+
const getAppInstance = require('./getAppInstance');
2829

2930
module.exports = {
3031
loadSpace,
@@ -54,4 +55,5 @@ module.exports = {
5455
getAppInstanceResources,
5556
postAppInstanceResource,
5657
patchAppInstanceResource,
58+
getAppInstance,
5759
};

public/app/listeners/patchAppInstanceResource.js

+8-38
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,20 @@
11
const { PATCH_APP_INSTANCE_RESOURCE_CHANNEL } = require('../config/channels');
2+
const { APP_INSTANCE_RESOURCES_COLLECTION } = require('../db');
23

3-
const patchAppInstanceResource = (mainWindow, db) => async (
4-
event,
5-
payload = {}
6-
) => {
4+
const patchAppInstanceResource = (mainWindow, db) => (event, payload = {}) => {
75
try {
8-
const { appInstanceId, spaceId, subSpaceId, data, id } = payload;
6+
const { appInstanceId: appInstance, data, id } = payload;
97
const now = new Date();
108
const fieldsToUpdate = {
119
updatedAt: now,
1210
data,
1311
};
1412

15-
let resource;
16-
17-
// tools live on the parent
18-
const tool = spaceId === subSpaceId;
19-
20-
// if not a tool, we need to go one step further into the phase
21-
if (!tool) {
22-
resource = db
23-
.get('spaces')
24-
.find({ id: spaceId })
25-
.get('phases')
26-
.find({ id: subSpaceId })
27-
.get('items')
28-
.filter(item => item.appInstance)
29-
.map(item => item.appInstance)
30-
.find({ id: appInstanceId })
31-
.get('resources')
32-
.find({ id })
33-
.assign(fieldsToUpdate)
34-
.value();
35-
} else {
36-
resource = db
37-
.get('spaces')
38-
.find({ id: spaceId })
39-
.get('items')
40-
.filter(item => item.appInstance)
41-
.map(item => item.appInstance)
42-
.find({ id: appInstanceId })
43-
.get('resources')
44-
.find({ id })
45-
.assign(fieldsToUpdate)
46-
.value();
47-
}
13+
const resource = db
14+
.get(APP_INSTANCE_RESOURCES_COLLECTION)
15+
.find({ appInstance, id })
16+
.assign(fieldsToUpdate)
17+
.value();
4818

4919
db.write();
5020
mainWindow.webContents.send(PATCH_APP_INSTANCE_RESOURCE_CHANNEL, resource);

public/app/listeners/postAppInstanceResource.js

+6-32
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
const ObjectId = require('bson-objectid');
22
const { POST_APP_INSTANCE_RESOURCE_CHANNEL } = require('../config/channels');
3+
const { APP_INSTANCE_RESOURCES_COLLECTION } = require('../db');
34

4-
const postAppInstanceResource = (mainWindow, db) => async (
5-
event,
6-
payload = {}
7-
) => {
5+
const postAppInstanceResource = (mainWindow, db) => (event, payload = {}) => {
86
try {
97
const {
108
userId,
119
appInstanceId,
1210
spaceId,
13-
subSpaceId,
1411
format,
1512
type,
1613
data,
@@ -23,6 +20,7 @@ const postAppInstanceResource = (mainWindow, db) => async (
2320
// prepare the resource that we will create
2421
const resourceToWrite = {
2522
appInstance: appInstanceId,
23+
spaceId,
2624
createdAt: now,
2725
updatedAt: now,
2826
data,
@@ -33,34 +31,10 @@ const postAppInstanceResource = (mainWindow, db) => async (
3331
id: ObjectId().str,
3432
};
3533

36-
// tools live on the parent
37-
const tool = spaceId === subSpaceId;
38-
3934
// write the resource to the database
40-
// if not a tool, we need to go one step further into the phase
41-
if (!tool) {
42-
db.get('spaces')
43-
.find({ id: spaceId })
44-
.get('phases')
45-
.find({ id: subSpaceId })
46-
.get('items')
47-
.filter(item => item.appInstance)
48-
.map(item => item.appInstance)
49-
.find({ id: appInstanceId })
50-
.get('resources')
51-
.push(resourceToWrite)
52-
.write();
53-
} else {
54-
db.get('spaces')
55-
.find({ id: spaceId })
56-
.get('items')
57-
.filter(item => item.appInstance)
58-
.map(item => item.appInstance)
59-
.find({ id: appInstanceId })
60-
.get('resources')
61-
.push(resourceToWrite)
62-
.write();
63-
}
35+
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
36+
.push(resourceToWrite)
37+
.write();
6438

6539
// send back the resource
6640
mainWindow.webContents.send(

public/electron.js

+2-38
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const {
8383
getAppInstanceResources,
8484
postAppInstanceResource,
8585
patchAppInstanceResource,
86+
getAppInstance,
8687
} = require('./app/listeners');
8788
const isMac = require('./app/utils/isMac');
8889

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

435436
// called when getting an AppInstance
436-
ipcMain.on(GET_APP_INSTANCE_CHANNEL, (event, payload = {}) => {
437-
try {
438-
const { spaceId, subSpaceId, id } = payload;
439-
440-
let appInstance;
441-
442-
// tools live on the parent
443-
const tool = spaceId === subSpaceId;
444-
445-
// if not a tool, we need to go one step further into the phase
446-
if (!tool) {
447-
appInstance = db
448-
.get('spaces')
449-
.find({ id: spaceId })
450-
.get('phases')
451-
.find({ id: subSpaceId })
452-
.get('items')
453-
.filter(item => item.appInstance)
454-
.map(item => item.appInstance)
455-
.find({ id })
456-
.value();
457-
} else {
458-
appInstance = db
459-
.get('spaces')
460-
.find({ id: spaceId })
461-
.get('items')
462-
.filter(item => item.appInstance)
463-
.map(item => item.appInstance)
464-
.find({ id })
465-
.value();
466-
}
467-
468-
mainWindow.webContents.send(GET_APP_INSTANCE_CHANNEL, appInstance);
469-
} catch (e) {
470-
console.error(e);
471-
mainWindow.webContents.send(GET_APP_INSTANCE_CHANNEL, null);
472-
}
473-
});
437+
ipcMain.on(GET_APP_INSTANCE_CHANNEL, getAppInstance(mainWindow, db));
474438

475439
// called when getting the database
476440
ipcMain.on(GET_DATABASE_CHANNEL, async () => {

src/actions/space.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ const deleteSpace = ({ id }) => dispatch => {
293293
});
294294
};
295295

296-
const clearUserInput = async ({ id }) => async dispatch => {
296+
const clearUserInput = async ({ spaceId, userId }) => async dispatch => {
297297
try {
298298
// show confirmation prompt
299299
window.ipcRenderer.send(SHOW_CLEAR_USER_INPUT_PROMPT_CHANNEL);
@@ -304,13 +304,17 @@ const clearUserInput = async ({ id }) => async dispatch => {
304304
(event, response) => {
305305
if (response === 1) {
306306
dispatch(flagClearingUserInput(true));
307-
window.ipcRenderer.send(CLEAR_USER_INPUT_CHANNEL, { id });
307+
window.ipcRenderer.send(CLEAR_USER_INPUT_CHANNEL, {
308+
spaceId,
309+
userId,
310+
});
308311
}
309312
}
310313
);
311314

312315
// listen for response from backend
313316
window.ipcRenderer.once(CLEARED_USER_INPUT_CHANNEL, (event, response) => {
317+
console.log('response', response);
314318
if (response === ERROR_GENERAL) {
315319
toastr.error(ERROR_MESSAGE_HEADER, ERROR_CLEARING_USER_INPUT_MESSAGE);
316320
} else {

0 commit comments

Comments
 (0)