|
1 |
| -const _ = require('lodash'); |
2 | 1 | const { CLEARED_USER_INPUT_CHANNEL } = require('../config/channels');
|
3 | 2 | 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'); |
5 | 7 | const logger = require('../logger');
|
6 | 8 |
|
7 |
| -const clearUserInput = (mainWindow, db) => async (event, { id }) => { |
| 9 | +const clearUserInput = (mainWindow, db) => async ( |
| 10 | + event, |
| 11 | + { spaceId, userId } |
| 12 | +) => { |
8 | 13 | try {
|
9 |
| - logger.debug(`clearing user input for space ${id}`); |
| 14 | + logger.debug(`clearing user input for space ${spaceId} of user ${userId}`); |
10 | 15 |
|
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(); |
45 | 19 |
|
46 | 20 | // we need to return the value of the mutated
|
47 | 21 | // space object to the frontend
|
| 22 | + // get handle to the space |
| 23 | + const spaceHandle = db.get(SPACES_COLLECTION).find({ id: spaceId }); |
48 | 24 | const space = spaceHandle.value();
|
49 | 25 |
|
50 | 26 | mainWindow.webContents.send(CLEARED_USER_INPUT_CHANNEL, space);
|
|
0 commit comments