forked from activepieces/activepieces
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(invoiceninja): add action create-client
- Loading branch information
1 parent
1eed085
commit 072066d
Showing
3 changed files
with
135 additions
and
2 deletions.
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,4 +1,4 @@ | ||
{ | ||
"name": "@activepieces/piece-invoiceninja", | ||
"version": "0.2.1" | ||
"version": "0.2.2" | ||
} |
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
132 changes: 132 additions & 0 deletions
132
packages/pieces/community/invoiceninja/src/lib/actions/create-client.ts
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,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; | ||
} | ||
} | ||
}, | ||
}); | ||
|