From 5009b999789005fd0ef73c4f9f8a295315e15daf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Wed, 11 Oct 2023 15:51:13 +0300 Subject: [PATCH] feat(invoice-ninja): add create payment action --- .../actions/create-payment/fields.ts | 111 ++++++++++++++++++ .../actions/create-payment/index.ts | 42 +++++++ .../src/apps/invoice-ninja/actions/index.ts | 3 +- .../apps/invoice-ninja/dynamic-data/index.ts | 3 +- .../dynamic-data/list-invoices/index.ts | 35 ++++++ .../docs/pages/apps/invoice-ninja/actions.md | 2 + 6 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 packages/backend/src/apps/invoice-ninja/actions/create-payment/fields.ts create mode 100644 packages/backend/src/apps/invoice-ninja/actions/create-payment/index.ts create mode 100644 packages/backend/src/apps/invoice-ninja/dynamic-data/list-invoices/index.ts diff --git a/packages/backend/src/apps/invoice-ninja/actions/create-payment/fields.ts b/packages/backend/src/apps/invoice-ninja/actions/create-payment/fields.ts new file mode 100644 index 0000000000..ba9ec5f697 --- /dev/null +++ b/packages/backend/src/apps/invoice-ninja/actions/create-payment/fields.ts @@ -0,0 +1,111 @@ +export const fields = [ + { + label: 'Client ID', + key: 'clientId', + type: 'dropdown' as const, + required: true, + description: 'The ID of the client, not the name or email address.', + variables: true, + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listClients', + }, + ], + }, + }, + { + label: 'Payment Date', + key: 'paymentDate', + type: 'string' as const, + required: false, + description: '', + variables: true, + }, + { + label: 'Invoice', + key: 'invoiceId', + type: 'dropdown' as const, + required: false, + description: '', + variables: true, + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listInvoices', + }, + ], + }, + }, + { + label: 'Invoice Amount', + key: 'invoiceAmount', + type: 'string' as const, + required: false, + description: '', + variables: true, + }, + { + label: 'Payment Type', + key: 'paymentType', + type: 'dropdown' as const, + required: false, + description: '', + variables: true, + options: [ + { label: 'Bank Transfer', value: '1' }, + { label: 'Cash', value: '2' }, + { label: 'Debit', value: '3' }, + { label: 'ACH', value: '4' }, + { label: 'Visa Card', value: '5' }, + { label: 'MasterCard', value: '6' }, + { label: 'American Express', value: '7' }, + { label: 'Discover Card', value: '8' }, + { label: 'Diners Card', value: '9' }, + { label: 'EuroCard', value: '10' }, + { label: 'Nova', value: '11' }, + { label: 'Credit Card Other', value: '12' }, + { label: 'PayPal', value: '13' }, + { label: 'Google Wallet', value: '14' }, + { label: 'Check', value: '15' }, + { label: 'Carte Blanche', value: '16' }, + { label: 'UnionPay', value: '17' }, + { label: 'JCB', value: '18' }, + { label: 'Laser', value: '19' }, + { label: 'Maestro', value: '20' }, + { label: 'Solo', value: '21' }, + { label: 'Switch', value: '22' }, + { label: 'iZettle', value: '23' }, + { label: 'Swish', value: '24' }, + { label: 'Venmo', value: '25' }, + { label: 'Money Order', value: '26' }, + { label: 'Alipay', value: '27' }, + { label: 'Sofort', value: '28' }, + { label: 'SEPA', value: '29' }, + { label: 'GoCardless', value: '30' }, + { label: 'Bitcoin', value: '31' }, + ], + }, + { + label: 'Transfer Reference', + key: 'transferReference', + type: 'string' as const, + required: false, + description: '', + variables: true, + }, + { + label: 'Private Notes', + key: 'privateNotes', + type: 'string' as const, + required: false, + description: '', + variables: true, + }, +]; diff --git a/packages/backend/src/apps/invoice-ninja/actions/create-payment/index.ts b/packages/backend/src/apps/invoice-ninja/actions/create-payment/index.ts new file mode 100644 index 0000000000..54a87eef96 --- /dev/null +++ b/packages/backend/src/apps/invoice-ninja/actions/create-payment/index.ts @@ -0,0 +1,42 @@ +import defineAction from '../../../../helpers/define-action'; +import { filterProvidedFields } from '../../common/filter-provided-fields'; +import { fields } from './fields'; + +export default defineAction({ + name: 'Create payment', + key: 'createPayment', + description: 'Creates a new payment.', + arguments: fields, + + async run($) { + const { + clientId, + paymentDate, + invoiceId, + invoiceAmount, + paymentType, + transferReference, + privateNotes, + } = $.step.parameters; + + const bodyFields = { + client_id: clientId, + date: paymentDate, + invoices: [ + { + invoice_id: invoiceId, + amount: invoiceAmount, + }, + ], + type_id: paymentType, + transaction_reference: transferReference, + private_notes: privateNotes, + }; + + const body = filterProvidedFields(bodyFields); + + const response = await $.http.post('/v1/payments', body); + + $.setActionItem({ raw: response.data.data }); + }, +}); diff --git a/packages/backend/src/apps/invoice-ninja/actions/index.ts b/packages/backend/src/apps/invoice-ninja/actions/index.ts index 0cfd721927..5a9dc32c2c 100644 --- a/packages/backend/src/apps/invoice-ninja/actions/index.ts +++ b/packages/backend/src/apps/invoice-ninja/actions/index.ts @@ -1,4 +1,5 @@ import createClient from './create-client'; import createInvoice from './create-invoice'; +import createPayment from './create-payment'; -export default [createClient, createInvoice]; +export default [createClient, createInvoice, createPayment]; diff --git a/packages/backend/src/apps/invoice-ninja/dynamic-data/index.ts b/packages/backend/src/apps/invoice-ninja/dynamic-data/index.ts index e7a5c21cf5..172f6d249f 100644 --- a/packages/backend/src/apps/invoice-ninja/dynamic-data/index.ts +++ b/packages/backend/src/apps/invoice-ninja/dynamic-data/index.ts @@ -1,3 +1,4 @@ import listClients from './list-clients'; +import listInvoices from './list-invoices'; -export default [listClients]; +export default [listClients, listInvoices]; diff --git a/packages/backend/src/apps/invoice-ninja/dynamic-data/list-invoices/index.ts b/packages/backend/src/apps/invoice-ninja/dynamic-data/list-invoices/index.ts new file mode 100644 index 0000000000..ac7b510734 --- /dev/null +++ b/packages/backend/src/apps/invoice-ninja/dynamic-data/list-invoices/index.ts @@ -0,0 +1,35 @@ +import { IGlobalVariable, IJSONObject } from '@automatisch/types'; + +export default { + name: 'List invoices', + key: 'listInvoices', + + async run($: IGlobalVariable) { + const invoices: { + data: IJSONObject[]; + } = { + data: [], + }; + + const params = { + sort: 'created_at|desc', + }; + + const { + data: { data }, + } = await $.http.get('/v1/invoices', { params }); + + if (!data?.length) { + return; + } + + for (const invoice of data) { + invoices.data.push({ + value: invoice.id, + name: invoice.number, + }); + } + + return invoices; + }, +}; diff --git a/packages/docs/pages/apps/invoice-ninja/actions.md b/packages/docs/pages/apps/invoice-ninja/actions.md index bf23f1d42e..be4e38fb62 100644 --- a/packages/docs/pages/apps/invoice-ninja/actions.md +++ b/packages/docs/pages/apps/invoice-ninja/actions.md @@ -5,6 +5,8 @@ items: desc: Creates a new client. - name: Create invoice desc: Creates a new invoice. + - name: Create payment + desc: Creates a new payment. ---