diff --git a/packages/backend/src/apps/pipedrive/actions/create-person/index.ts b/packages/backend/src/apps/pipedrive/actions/create-person/index.ts new file mode 100644 index 00000000..ed26859f --- /dev/null +++ b/packages/backend/src/apps/pipedrive/actions/create-person/index.ts @@ -0,0 +1,151 @@ +import defineAction from '../../../../helpers/define-action'; + +type TEmail = { + __id: string; + email: string; +}[]; + +type TPhone = { + __id: string; + phone: string; +}[]; + +function filterProvidedFields(body: Record) { + return Object.keys(body).reduce>((result, key) => { + if (body[key]) { + result[key] = body[key]; + } + return result; + }, {}); +} + +export default defineAction({ + name: 'Create person', + key: 'createPerson', + description: 'Creates a new person.', + arguments: [ + { + label: 'Name', + key: 'name', + type: 'string' as const, + required: true, + description: '', + variables: true, + }, + { + label: 'Owner', + key: 'ownerId', + type: 'dropdown' as const, + required: false, + description: '', + variables: true, + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listUsers', + }, + ], + }, + }, + { + label: 'Organization', + key: 'organizationId', + type: 'dropdown' as const, + required: false, + description: '', + variables: true, + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listOrganizations', + }, + ], + }, + }, + { + label: 'Emails', + key: 'emails', + type: 'dynamic' as const, + required: false, + description: '', + fields: [ + { + label: 'Email', + key: 'email', + type: 'string' as const, + required: false, + description: '', + variables: true, + }, + ], + }, + { + label: 'Phones', + key: 'phones', + type: 'dynamic' as const, + required: false, + description: '', + fields: [ + { + label: 'Phone', + key: 'phone', + type: 'string' as const, + required: false, + description: '', + variables: true, + }, + ], + }, + { + label: 'Label', + key: 'labelId', + type: 'dropdown' as const, + required: false, + description: '', + variables: true, + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listPersonLabelField', + }, + ], + }, + }, + ], + + async run($) { + const { name, ownerId, organizationId, labelId } = $.step.parameters; + const emails = $.step.parameters.emails as TEmail; + const emailValues = emails.map((entry) => entry.email); + const phones = $.step.parameters.phones as TPhone; + const phoneValues = phones.map((entry) => entry.phone); + + const fields = { + name: name, + owner_id: ownerId, + org_id: organizationId, + email: emailValues, + phone: phoneValues, + label: labelId, + }; + + const body = filterProvidedFields(fields); + + const { + data: { data }, + } = await $.http.post(`${$.auth.data.apiDomain}/api/v1/persons`, body); + + $.setActionItem({ + raw: data, + }); + }, +}); diff --git a/packages/backend/src/apps/pipedrive/actions/index.ts b/packages/backend/src/apps/pipedrive/actions/index.ts index 950d7e95..ffabeaa5 100644 --- a/packages/backend/src/apps/pipedrive/actions/index.ts +++ b/packages/backend/src/apps/pipedrive/actions/index.ts @@ -3,6 +3,7 @@ import createDeal from './create-deal'; import createLead from './create-lead'; import createNote from './create-note'; import createOrganization from './create-organization'; +import createPerson from './create-person'; export default [ createActivity, @@ -10,4 +11,5 @@ export default [ createLead, createNote, createOrganization, + createPerson, ]; diff --git a/packages/backend/src/apps/pipedrive/dynamic-data/index.ts b/packages/backend/src/apps/pipedrive/dynamic-data/index.ts index 76bdc182..dfc4118a 100644 --- a/packages/backend/src/apps/pipedrive/dynamic-data/index.ts +++ b/packages/backend/src/apps/pipedrive/dynamic-data/index.ts @@ -5,6 +5,7 @@ import listLeads from './list-leads'; import listLeadLabels from './list-lead-labels'; import listOrganizations from './list-organizations'; import listOrganizationLabelField from './list-organization-label-field'; +import listPersonLabelField from './list-person-label-field'; import listPersons from './list-persons'; import listUsers from './list-users'; @@ -16,6 +17,7 @@ export default [ listLeadLabels, listOrganizations, listOrganizationLabelField, + listPersonLabelField, listPersons, listUsers, ]; diff --git a/packages/backend/src/apps/pipedrive/dynamic-data/list-person-label-field/index.ts b/packages/backend/src/apps/pipedrive/dynamic-data/list-person-label-field/index.ts new file mode 100644 index 00000000..f162eefe --- /dev/null +++ b/packages/backend/src/apps/pipedrive/dynamic-data/list-person-label-field/index.ts @@ -0,0 +1,42 @@ +import { IGlobalVariable, IJSONObject } from '@automatisch/types'; + +export default { + name: 'List person label field', + key: 'listPersonLabelField', + + async run($: IGlobalVariable) { + const personFields: { + data: IJSONObject[]; + } = { + data: [], + }; + + const params = { + start: 0, + limit: 100, + }; + + do { + const { data } = await $.http.get( + `${$.auth.data.apiDomain}/api/v1/personFields`, + { params } + ); + params.start = data.additional_data?.pagination?.next_start; + + const labelField = data.data?.filter( + (personField: IJSONObject) => personField.key === 'label' + ); + const labelOptions = labelField[0].options; + + if (labelOptions?.length) { + for (const label of labelOptions) { + personFields.data.push({ + value: label.id, + name: label.label, + }); + } + } + } while (params.start); + return personFields; + }, +}; diff --git a/packages/docs/pages/apps/pipedrive/actions.md b/packages/docs/pages/apps/pipedrive/actions.md index 98d8c1bd..c27b131f 100644 --- a/packages/docs/pages/apps/pipedrive/actions.md +++ b/packages/docs/pages/apps/pipedrive/actions.md @@ -11,6 +11,8 @@ items: desc: Creates a new note. - name: Create organization desc: Creates a new organization. + - name: Create person + desc: Creates a new person. ---