-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(firefly-iii): add transaction updated trigger
- Loading branch information
1 parent
1c76b02
commit cf78c89
Showing
3 changed files
with
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import transactionCreated from './transaction-created/index.js'; | ||
import transactionUpdated from './transaction-updated/index.js'; | ||
|
||
export default [transactionCreated]; | ||
export default [transactionCreated, transactionUpdated]; |
85 changes: 85 additions & 0 deletions
85
packages/backend/src/apps/firefly-iii/triggers/transaction-updated/index.js
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,85 @@ | ||
import Crypto from 'crypto'; | ||
import defineTrigger from '../../../../helpers/define-trigger.js'; | ||
|
||
export default defineTrigger({ | ||
name: 'Transaction updated', | ||
key: 'transactionUpdated', | ||
type: 'webhook', | ||
description: 'Triggers when a transaction is updated.', | ||
arguments: [ | ||
{ | ||
label: 'Title of the webhook', | ||
key: 'title', | ||
type: 'string', | ||
required: false, | ||
description: '', | ||
variables: true, | ||
}, | ||
], | ||
|
||
async run($) { | ||
const dataItem = { | ||
raw: $.request.body, | ||
meta: { | ||
internalId: Crypto.randomUUID(), | ||
}, | ||
}; | ||
|
||
$.pushTriggerItem(dataItem); | ||
}, | ||
|
||
async testRun($) { | ||
const { data: transactions } = await $.http.get(`/api/v1/transactions`); | ||
|
||
const { data: transaction } = await $.http.get( | ||
`/api/v1/transactions/${transactions.data[0].id}` | ||
); | ||
|
||
const lastResponse = transaction.data; | ||
|
||
if (!lastResponse) { | ||
return; | ||
} | ||
|
||
const computedWebhookEvent = { | ||
url: '', | ||
uuid: Crypto.randomUUID(), | ||
content: lastResponse.attributes, | ||
trigger: 'UPDATE_TRANSACTION', | ||
user_id: lastResponse.attributes.user, | ||
version: '', | ||
response: 'TRANSACTIONS', | ||
}; | ||
|
||
const dataItem = { | ||
raw: computedWebhookEvent, | ||
meta: { | ||
internalId: computedWebhookEvent.uuid, | ||
}, | ||
}; | ||
|
||
$.pushTriggerItem(dataItem); | ||
}, | ||
|
||
async registerHook($) { | ||
const title = $.step.parameters.title; | ||
|
||
const payload = { | ||
active: true, | ||
title: title || `Flow ID: ${$.flow.id}`, | ||
trigger: 'UPDATE_TRANSACTION', | ||
response: 'TRANSACTIONS', | ||
delivery: 'JSON', | ||
url: $.webhookUrl, | ||
}; | ||
|
||
const response = await $.http.post('/api/v1/webhooks', payload); | ||
const id = response.data.data.id; | ||
|
||
await $.flow.setRemoteWebhookId(id); | ||
}, | ||
|
||
async unregisterHook($) { | ||
await $.http.delete(`/api/v1/webhooks/${$.flow.remoteWebhookId}`); | ||
}, | ||
}); |
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