feat(pipedrive): add create person action
This commit is contained in:
@@ -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,
|
||||
});
|
||||
},
|
||||
});
|
@@ -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,
|
||||
];
|
||||
|
@@ -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,
|
||||
];
|
||||
|
@@ -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;
|
||||
},
|
||||
};
|
@@ -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>
|
||||
|
Reference in New Issue
Block a user