feat(pipedrive): add create person action

This commit is contained in:
Rıdvan Akca
2023-10-18 14:13:53 +03:00
committed by Ali BARIN
parent 0772308bf5
commit 3a638220af
5 changed files with 199 additions and 0 deletions

View File

@@ -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<string, unknown>) {
return Object.keys(body).reduce<Record<string, unknown>>((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,
});
},
});

View File

@@ -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,
];

View File

@@ -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,
];

View File

@@ -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;
},
};

View File

@@ -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.
---
<script setup>