From d085e0f979dca7e7201db98d28e5f878b6851e66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Tue, 21 May 2024 14:49:19 +0200 Subject: [PATCH] feat(firefly-iii): add transaction deleted trigger --- .../src/apps/firefly-iii/triggers/index.js | 3 +- .../triggers/transaction-deleted/index.js | 89 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 packages/backend/src/apps/firefly-iii/triggers/transaction-deleted/index.js diff --git a/packages/backend/src/apps/firefly-iii/triggers/index.js b/packages/backend/src/apps/firefly-iii/triggers/index.js index 7f7bbaaf..acc79adb 100644 --- a/packages/backend/src/apps/firefly-iii/triggers/index.js +++ b/packages/backend/src/apps/firefly-iii/triggers/index.js @@ -1,4 +1,5 @@ import transactionCreated from './transaction-created/index.js'; +import transactionDeleted from './transaction-deleted/index.js'; import transactionUpdated from './transaction-updated/index.js'; -export default [transactionCreated, transactionUpdated]; +export default [transactionCreated, transactionDeleted, transactionUpdated]; diff --git a/packages/backend/src/apps/firefly-iii/triggers/transaction-deleted/index.js b/packages/backend/src/apps/firefly-iii/triggers/transaction-deleted/index.js new file mode 100644 index 00000000..c68fefe4 --- /dev/null +++ b/packages/backend/src/apps/firefly-iii/triggers/transaction-deleted/index.js @@ -0,0 +1,89 @@ +import Crypto from 'crypto'; +import defineTrigger from '../../../../helpers/define-trigger.js'; + +export default defineTrigger({ + name: 'Transaction deleted', + key: 'transactionDeleted', + type: 'webhook', + description: 'Triggers when a transaction is deleted.', + 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`); + + if (transactions.data.length === 0) { + return; + } + + const { data: transaction } = await $.http.get( + `/api/v1/transactions/${transactions.data[0].id}` + ); + + const lastTransaction = transaction.data; + + if (!lastTransaction) { + return; + } + + const computedWebhookEvent = { + url: '', + uuid: Crypto.randomUUID(), + content: lastTransaction.attributes, + trigger: 'DESTROY_TRANSACTION', + user_id: lastTransaction.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: 'DESTROY_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}`); + }, +});