Skip to content

Commit e03895a

Browse files
committed
refactor: save actions in actions collection
1 parent 47b4fc0 commit e03895a

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

public/app/db.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { DATABASE_PATH, VAR_FOLDER, DEFAULT_LANG } = require('./config/config');
99
const fsPromises = fs.promises;
1010

1111
const SPACES_COLLECTION = 'spaces';
12+
const ACTIONS_COLLECTION = 'actions';
1213

1314
// bootstrap database
1415
const ensureDatabaseExists = async (dbPath = DATABASE_PATH) => {
@@ -33,6 +34,7 @@ const bootstrapDatabase = (dbPath = DATABASE_PATH) => {
3334
db.defaults({
3435
[SPACES_COLLECTION]: [],
3536
user: { lang: DEFAULT_LANG },
37+
[ACTIONS_COLLECTION]: [],
3638
}).write();
3739
return db;
3840
};

public/app/listeners/postAction.js

+7-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ const postAction = (mainWindow, db) => (event, payload = {}) => {
1919

2020
// prepare the resource that we will create
2121
const actionToWrite = {
22-
appInstance: appInstanceId,
22+
spaceId,
23+
subSpaceId,
24+
appInstanceId,
2325
createdAt: now,
2426
updatedAt: now,
2527
data,
@@ -30,27 +32,10 @@ const postAction = (mainWindow, db) => (event, payload = {}) => {
3032
id: ObjectId().str,
3133
};
3234

33-
// write the resource to the database
34-
const appInstanceElement = db
35-
.get('spaces')
36-
.find({ id: spaceId })
37-
.get('phases')
38-
.find({ id: subSpaceId })
39-
.get('items')
40-
.filter(item => item.appInstance)
41-
.map(item => item.appInstance)
42-
.find({ id: appInstanceId });
43-
44-
// add the actions key if it does not exist
45-
const hasActions = appInstanceElement.has('actions').value();
46-
if (!hasActions) {
47-
appInstanceElement.set('actions', [actionToWrite]).write();
48-
} else {
49-
appInstanceElement
50-
.get('actions')
51-
.push(actionToWrite)
52-
.write();
53-
}
35+
// write the action to the database
36+
db.get('actions')
37+
.push(actionToWrite)
38+
.write();
5439

5540
// send back the resource
5641
mainWindow.webContents.send(POST_ACTION_CHANNEL, actionToWrite);

0 commit comments

Comments
 (0)