Skip to content

Commit

Permalink
feat(firefly-iii): add transaction updated trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
ridvanakca committed May 21, 2024
1 parent 1c76b02 commit cf78c89
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/backend/src/apps/firefly-iii/triggers/index.js
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];
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}`);
},
});
2 changes: 2 additions & 0 deletions packages/docs/pages/apps/firefly-iii/triggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ favicon: /favicons/firefly-iii.svg
items:
- name: Transaction created
desc: Triggers when a new transaction is created.
- name: Transaction updated
desc: Triggers when a transaction is updated.
---

<script setup>
Expand Down

0 comments on commit cf78c89

Please sign in to comment.