-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: retrieve post actions and save it in appInstance
- Loading branch information
Showing
10 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const ObjectId = require('bson-objectid'); | ||
const { POST_ACTION_CHANNEL } = require('../config/channels'); | ||
|
||
const postAction = (mainWindow, db) => (event, payload = {}) => { | ||
try { | ||
const { | ||
userId, | ||
appInstanceId, | ||
spaceId, | ||
subSpaceId, | ||
format, | ||
verb, | ||
data, | ||
visibility = 'private', | ||
} = payload; | ||
|
||
// prepare the timestamp | ||
const now = new Date(); | ||
|
||
// prepare the resource that we will create | ||
const actionToWrite = { | ||
appInstance: appInstanceId, | ||
createdAt: now, | ||
updatedAt: now, | ||
data, | ||
format, | ||
verb, | ||
visibility, | ||
user: userId, | ||
id: ObjectId().str, | ||
}; | ||
|
||
// write the resource to the database | ||
const appInstanceElement = db | ||
.get('spaces') | ||
.find({ id: spaceId }) | ||
.get('phases') | ||
.find({ id: subSpaceId }) | ||
.get('items') | ||
.filter(item => item.appInstance) | ||
.map(item => item.appInstance) | ||
.find({ id: appInstanceId }); | ||
|
||
// add the actions key if it does not exist | ||
const hasActions = appInstanceElement.has('actions').value(); | ||
if (!hasActions) { | ||
appInstanceElement.set('actions', [actionToWrite]).write(); | ||
} else { | ||
appInstanceElement | ||
.get('actions') | ||
.push(actionToWrite) | ||
.write(); | ||
} | ||
|
||
// send back the resource | ||
mainWindow.webContents.send(POST_ACTION_CHANNEL, actionToWrite); | ||
} catch (e) { | ||
console.error(e); | ||
mainWindow.webContents.send(POST_ACTION_CHANNEL, null); | ||
} | ||
}; | ||
|
||
module.exports = postAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { POST_ACTION_SUCCEEDED } from '../types'; | ||
import { POST_ACTION_CHANNEL } from '../config/channels'; | ||
|
||
const postAction = async (ddd = {}, callback) => () => { | ||
const { | ||
userId, | ||
id: appInstanceId, | ||
spaceId, | ||
subSpaceId, | ||
format, | ||
data, | ||
verb, | ||
visibility, | ||
} = ddd; | ||
try { | ||
window.ipcRenderer.send(POST_ACTION_CHANNEL, { | ||
userId, | ||
appInstanceId, | ||
spaceId, | ||
subSpaceId, | ||
format, | ||
data, | ||
verb, | ||
visibility, | ||
}); | ||
|
||
window.ipcRenderer.once(POST_ACTION_CHANNEL, async (event, response) => { | ||
callback({ | ||
// have to include the appInstanceId to avoid broadcasting | ||
appInstanceId, | ||
type: POST_ACTION_SUCCEEDED, | ||
payload: response, | ||
}); | ||
}); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export { postAction }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const POST_ACTION = 'POST_ACTION'; | ||
export const POST_ACTION_SUCCEEDED = 'POST_ACTION_SUCCEEDED'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters