diff --git a/packages/pieces/community/invoiceninja/package.json b/packages/pieces/community/invoiceninja/package.json index 7c10b7752a..0607290d23 100644 --- a/packages/pieces/community/invoiceninja/package.json +++ b/packages/pieces/community/invoiceninja/package.json @@ -1,4 +1,4 @@ { "name": "@activepieces/piece-invoiceninja", - "version": "0.2.1" + "version": "0.2.2" } diff --git a/packages/pieces/community/invoiceninja/src/index.ts b/packages/pieces/community/invoiceninja/src/index.ts index 29bfdc31a9..20318519de 100644 --- a/packages/pieces/community/invoiceninja/src/index.ts +++ b/packages/pieces/community/invoiceninja/src/index.ts @@ -9,6 +9,7 @@ import { getClient } from './lib/actions/get-client'; import { getInvoices } from './lib/actions/get-invoices'; import { getReport } from './lib/actions/get-report'; import { createInvoice } from './lib/actions/create-invoice'; +import { createClient } from './lib/actions/create-client'; export const invoiceninjaAuth = PieceAuth.CustomAuth({ props: { @@ -34,6 +35,6 @@ export const invoiceninja = createPiece({ logoUrl: 'https://cdn.activepieces.com/pieces/invoiceninja.png', authors: ['buttonsbond'], auth: invoiceninjaAuth, - actions: [createTask, existsTask, getClient, getInvoices, getReport, createInvoice], + actions: [createTask, existsTask, getClient, getInvoices, getReport, createInvoice, createClient], triggers: [], }); diff --git a/packages/pieces/community/invoiceninja/src/lib/actions/create-client.ts b/packages/pieces/community/invoiceninja/src/lib/actions/create-client.ts new file mode 100644 index 0000000000..2d3a83089d --- /dev/null +++ b/packages/pieces/community/invoiceninja/src/lib/actions/create-client.ts @@ -0,0 +1,132 @@ +import { + createAction, + Property, + } from '@activepieces/pieces-framework'; + import { httpClient, HttpMethod } from '@activepieces/pieces-common'; + import { invoiceninjaAuth } from '../..'; + + export const createClient = createAction({ + auth: invoiceninjaAuth, + name: 'create_client', + displayName: 'Create Client', + description: 'Creates a new client in InvoiceNinja.', + + props: { + client_first_name: Property.LongText({ + displayName: 'Client First Name (alphanumeric)', + description: 'The contact first name for this client (optional)', + required: false, + }), + client_last_name: Property.LongText({ + displayName: 'Client Last Name (alphanumeric)', + description: 'The contact last name for this client (optional)', + required: false, + }), + client_phone: Property.LongText({ + displayName: 'Client Contact No (alphanumeric)', + description: 'The contact number for this client (optional)', + required: false, + }), + client_email: Property.LongText({ + displayName: 'Client e-mail (alphanumeric)', + description: 'The contact email for this client (compulsory)', + required: true, + }), + client_send_email: Property.Checkbox({ + displayName: 'Send invoices to the client', + description: 'Should we send invoices to the client by e-mail?', + defaultValue: false, + required: true, + }), + client_business_name: Property.LongText({ + displayName: 'Business Name (alphanumeric)', + description: 'Name of this business or natural person (compulsory)', + required: true, + }), + client_tax_no: Property.LongText({ + displayName: 'Client Tax Number (alphanumeric)', + description: 'Leave blank if not a business (optional)', + required: false, + }), + client_private_notes: Property.LongText({ + displayName: 'Private notes for client', + description: 'Text not visible for clients (optional)', + required: false, + }), + client_address1: Property.LongText({ + displayName: 'Client address 1 (alphanumeric)', + description: 'Usually street name and number (compulsory)', + required: true, + }), + client_address2: Property.LongText({ + displayName: 'Client address 2 (alphanumeric)', + description: 'Additional address details (optional)', + required: false, + }), + client_city: Property.LongText({ + displayName: 'Client City/Town (alphanumeric)', + description: 'City or Town name (compulsory)', + required: true, + }), + client_state: Property.LongText({ + displayName: 'Client State (alphanumeric)', + description: 'State or county or similar (optional)', + required: false, + }), + client_postcode: Property.LongText({ + displayName: 'Client Postcode (alphanumeric)', + description: 'Postal code (optional)', + required: false, + }), + }, + + async run(context) { + const INapiToken = context.auth.access_token; + const headers = { + 'X-Api-Token': INapiToken, + 'Content-Type': 'application/json', + }; + + const queryParams = new URLSearchParams(); + queryParams.append('name', context.propsValue.client_business_name); + queryParams.append('private_notes', context.propsValue.client_private_notes || ''); + queryParams.append('address1', context.propsValue.client_address1 || ''); + queryParams.append('address2', context.propsValue.client_address2 || ''); + queryParams.append('city', context.propsValue.client_city || ''); + queryParams.append('state', context.propsValue.client_state || ''); + queryParams.append('postal_code', context.propsValue.client_postcode || ''); + queryParams.append('vat_number', context.propsValue.client_tax_no || ''); + const body = {"contacts": { "first_name":context.propsValue.client_first_name || "", "last_name":context.propsValue.client_last_name || "", + "phone":context.propsValue.client_phone || "","email":context.propsValue.client_email || "", "send_email":context.propsValue.client_send_email || false }}; + + const baseUrl = context.auth.base_url.replace(/\/$/, ''); + const url = `${baseUrl}/api/v1/clients/?${queryParams.toString()}`; + + let errorMessages = ''; + const httprequestdata = { + method: HttpMethod.POST, + url, + headers, + body, + }; + + try { + const response = await httpClient.sendRequest(httprequestdata); + errorMessages += response.body; + return response.body; + + + + } catch (error) { + console.error('Error creating client:', error); + if (errorMessages) { + // If there are error messages, throw an error with the accumulated messages + throw new Error(errorMessages.trim()); + } else { + // If there are no error messages, throw the original error + throw error; + } + } + }, + }); + \ No newline at end of file