feat(pipedrive): add create lead action
This commit is contained in:
199
packages/backend/src/apps/pipedrive/actions/create-lead/index.ts
Normal file
199
packages/backend/src/apps/pipedrive/actions/create-lead/index.ts
Normal file
@@ -0,0 +1,199 @@
|
||||
import defineAction from '../../../../helpers/define-action';
|
||||
|
||||
type LabelIds = { __id: string; leadLabelId: string }[];
|
||||
|
||||
type LabelValue = { amount?: number; currency?: 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 lead',
|
||||
key: 'createLead',
|
||||
description: 'Creates a new lead.',
|
||||
arguments: [
|
||||
{
|
||||
label: 'Title',
|
||||
key: 'title',
|
||||
type: 'string' as const,
|
||||
required: true,
|
||||
description: '',
|
||||
variables: true,
|
||||
},
|
||||
{
|
||||
label: 'Person',
|
||||
key: 'personId',
|
||||
type: 'dropdown' as const,
|
||||
required: false,
|
||||
description:
|
||||
'Lead must be associated with at least one person or organization.',
|
||||
variables: true,
|
||||
source: {
|
||||
type: 'query',
|
||||
name: 'getDynamicData',
|
||||
arguments: [
|
||||
{
|
||||
name: 'key',
|
||||
value: 'listPersons',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
label: 'Organization',
|
||||
key: 'organizationId',
|
||||
type: 'dropdown' as const,
|
||||
required: false,
|
||||
description:
|
||||
'Lead must be associated with at least one person or organization.',
|
||||
variables: true,
|
||||
source: {
|
||||
type: 'query',
|
||||
name: 'getDynamicData',
|
||||
arguments: [
|
||||
{
|
||||
name: 'key',
|
||||
value: 'listOrganizations',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
label: 'Owner',
|
||||
key: 'ownerId',
|
||||
type: 'dropdown' as const,
|
||||
required: false,
|
||||
description:
|
||||
'Select user who will be marked as the owner of this lead. If omitted, the authorized user will be used.',
|
||||
variables: true,
|
||||
source: {
|
||||
type: 'query',
|
||||
name: 'getDynamicData',
|
||||
arguments: [
|
||||
{
|
||||
name: 'key',
|
||||
value: 'listUsers',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
label: 'Lead Labels',
|
||||
key: 'labelIds',
|
||||
type: 'dynamic' as const,
|
||||
required: false,
|
||||
description: '',
|
||||
fields: [
|
||||
{
|
||||
label: 'Label',
|
||||
key: 'leadLabelId',
|
||||
type: 'dropdown' as const,
|
||||
required: false,
|
||||
variables: true,
|
||||
source: {
|
||||
type: 'query',
|
||||
name: 'getDynamicData',
|
||||
arguments: [
|
||||
{
|
||||
name: 'key',
|
||||
value: 'listLeadLabels',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
label: 'Expected Close Date',
|
||||
key: 'expectedCloseDate',
|
||||
type: 'string' as const,
|
||||
required: false,
|
||||
description: '',
|
||||
variables: true,
|
||||
},
|
||||
{
|
||||
label: 'Lead Value',
|
||||
key: 'value',
|
||||
type: 'string' as const,
|
||||
required: false,
|
||||
description: '',
|
||||
variables: true,
|
||||
},
|
||||
{
|
||||
label: 'Lead Value Currency',
|
||||
key: 'currency',
|
||||
type: 'dropdown' as const,
|
||||
required: false,
|
||||
description: 'This field is required if a Lead Value amount is provided.',
|
||||
variables: true,
|
||||
source: {
|
||||
type: 'query',
|
||||
name: 'getDynamicData',
|
||||
arguments: [
|
||||
{
|
||||
name: 'key',
|
||||
value: 'listCurrencies',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
async run($) {
|
||||
const {
|
||||
title,
|
||||
personId,
|
||||
organizationId,
|
||||
ownerId,
|
||||
labelIds,
|
||||
expectedCloseDate,
|
||||
value,
|
||||
currency,
|
||||
} = $.step.parameters;
|
||||
|
||||
const onlyLabelIds = (labelIds as LabelIds)
|
||||
.map((labelId) => labelId.leadLabelId)
|
||||
.filter(Boolean);
|
||||
|
||||
const labelValue: LabelValue = {};
|
||||
|
||||
if (value) {
|
||||
labelValue.amount = Number(value);
|
||||
}
|
||||
if (currency) {
|
||||
labelValue.currency = currency as string;
|
||||
}
|
||||
|
||||
const fields = {
|
||||
title: title as string,
|
||||
person_id: Number(personId),
|
||||
organization_id: Number(organizationId),
|
||||
owner_id: Number(ownerId),
|
||||
expected_close_date: expectedCloseDate as string,
|
||||
};
|
||||
|
||||
const body = filterProvidedFields(fields);
|
||||
|
||||
if (onlyLabelIds.length) {
|
||||
body.label_ids = onlyLabelIds;
|
||||
}
|
||||
|
||||
if (Object.keys(labelValue).length) {
|
||||
body.value = labelValue;
|
||||
}
|
||||
|
||||
const {
|
||||
data: { data },
|
||||
} = await $.http.post(`${$.auth.data.apiDomain}/api/v1/leads`, body);
|
||||
|
||||
$.setActionItem({
|
||||
raw: data,
|
||||
});
|
||||
},
|
||||
});
|
@@ -1,5 +1,6 @@
|
||||
import createActivity from './create-activity';
|
||||
import createDeal from './create-deal';
|
||||
import createLead from './create-lead';
|
||||
import createNote from './create-note';
|
||||
|
||||
export default [createActivity, createDeal, createNote];
|
||||
export default [createActivity, createDeal, createLead, createNote];
|
||||
|
@@ -2,6 +2,7 @@ import listActivityTypes from './list-activity-types';
|
||||
import listCurrencies from './list-currencies';
|
||||
import listDeals from './list-deals';
|
||||
import listLeads from './list-leads';
|
||||
import listLeadLabels from './list-lead-labels';
|
||||
import listOrganizations from './list-organizations';
|
||||
import listPersons from './list-persons';
|
||||
import listUsers from './list-users';
|
||||
@@ -11,6 +12,7 @@ export default [
|
||||
listCurrencies,
|
||||
listDeals,
|
||||
listLeads,
|
||||
listLeadLabels,
|
||||
listOrganizations,
|
||||
listPersons,
|
||||
listUsers,
|
||||
|
@@ -0,0 +1,34 @@
|
||||
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
|
||||
|
||||
export default {
|
||||
name: 'List lead labels',
|
||||
key: 'listLeadLabels',
|
||||
|
||||
async run($: IGlobalVariable) {
|
||||
const leadLabels: {
|
||||
data: IJSONObject[];
|
||||
} = {
|
||||
data: [],
|
||||
};
|
||||
|
||||
const { data } = await $.http.get(
|
||||
`${$.auth.data.apiDomain}/api/v1/leadLabels`
|
||||
);
|
||||
|
||||
if (!data?.data) {
|
||||
return { data: [] };
|
||||
}
|
||||
|
||||
if (data.data.length) {
|
||||
for (const leadLabel of data.data) {
|
||||
const name = `${leadLabel.name} (${leadLabel.color})`;
|
||||
leadLabels.data.push({
|
||||
value: leadLabel.id,
|
||||
name,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return leadLabels;
|
||||
},
|
||||
};
|
@@ -1,12 +1,14 @@
|
||||
---
|
||||
favicon: /favicons/pipedrive.svg
|
||||
items:
|
||||
- name: Create deal
|
||||
desc: Creates a new deal.
|
||||
- name: Create note
|
||||
desc: Creates a new note.
|
||||
- name: Create activity
|
||||
desc: Creates a new activity.
|
||||
- name: Create deal
|
||||
desc: Creates a new deal.
|
||||
- name: Create lead
|
||||
desc: Creates a new lead.
|
||||
- name: Create note
|
||||
desc: Creates a new note.
|
||||
---
|
||||
|
||||
<script setup>
|
||||
|
@@ -25,14 +25,13 @@ interface DynamicFieldProps {
|
||||
docUrl?: string;
|
||||
clickToCopy?: boolean;
|
||||
disabled?: boolean;
|
||||
fields: IFieldDynamic["fields"];
|
||||
fields: IFieldDynamic['fields'];
|
||||
shouldUnregister?: boolean;
|
||||
stepId?: string;
|
||||
}
|
||||
|
||||
function DynamicField(
|
||||
props: DynamicFieldProps
|
||||
): React.ReactElement {
|
||||
const { label, description, fields, name, defaultValue } = props;
|
||||
function DynamicField(props: DynamicFieldProps): React.ReactElement {
|
||||
const { label, description, fields, name, defaultValue, stepId } = props;
|
||||
const { control, setValue, getValues } = useFormContext();
|
||||
const fieldsValue = useWatch({ control, name }) as Record<string, unknown>[];
|
||||
const editorContext = React.useContext(EditorContext);
|
||||
@@ -43,7 +42,7 @@ function DynamicField(
|
||||
...previousValue,
|
||||
[field.key]: '',
|
||||
__id: uuidv4(),
|
||||
}
|
||||
};
|
||||
}, {});
|
||||
}, [fields]);
|
||||
|
||||
@@ -57,23 +56,31 @@ function DynamicField(
|
||||
}
|
||||
}, [getValues, createEmptyItem]);
|
||||
|
||||
const removeItem = React.useCallback((index) => {
|
||||
if (fieldsValue.length === 1) return;
|
||||
const removeItem = React.useCallback(
|
||||
(index) => {
|
||||
if (fieldsValue.length === 1) return;
|
||||
|
||||
const newFieldsValue = fieldsValue.filter((fieldValue, fieldIndex) => fieldIndex !== index);
|
||||
const newFieldsValue = fieldsValue.filter(
|
||||
(fieldValue, fieldIndex) => fieldIndex !== index
|
||||
);
|
||||
|
||||
setValue(name, newFieldsValue);
|
||||
}, [fieldsValue]);
|
||||
setValue(name, newFieldsValue);
|
||||
},
|
||||
[fieldsValue]
|
||||
);
|
||||
|
||||
React.useEffect(function addInitialGroupWhenEmpty() {
|
||||
const fieldValues = getValues(name);
|
||||
React.useEffect(
|
||||
function addInitialGroupWhenEmpty() {
|
||||
const fieldValues = getValues(name);
|
||||
|
||||
if (!fieldValues && defaultValue) {
|
||||
setValue(name, defaultValue);
|
||||
} else if (!fieldValues) {
|
||||
setValue(name, [createEmptyItem()]);
|
||||
}
|
||||
}, [createEmptyItem, defaultValue]);
|
||||
if (!fieldValues && defaultValue) {
|
||||
setValue(name, defaultValue);
|
||||
} else if (!fieldValues) {
|
||||
setValue(name, [createEmptyItem()]);
|
||||
}
|
||||
},
|
||||
[createEmptyItem, defaultValue]
|
||||
);
|
||||
|
||||
return (
|
||||
<React.Fragment>
|
||||
@@ -81,14 +88,22 @@ function DynamicField(
|
||||
|
||||
{fieldsValue?.map((field, index) => (
|
||||
<Stack direction="row" spacing={2} key={`fieldGroup-${field.__id}`}>
|
||||
<Stack direction={{ xs: 'column', sm: 'row' }} spacing={{ xs: 2 }} sx={{ display: 'flex', flex: 1 }}>
|
||||
<Stack
|
||||
direction={{ xs: 'column', sm: 'row' }}
|
||||
spacing={{ xs: 2 }}
|
||||
sx={{ display: 'flex', flex: 1 }}
|
||||
>
|
||||
{fields.map((fieldSchema, fieldSchemaIndex) => (
|
||||
<Box sx={{ display: 'flex', flex: '1 0 0px' }} key={`field-${field.__id}-${fieldSchemaIndex}`}>
|
||||
<Box
|
||||
sx={{ display: 'flex', flex: '1 0 0px' }}
|
||||
key={`field-${field.__id}-${fieldSchemaIndex}`}
|
||||
>
|
||||
<InputCreator
|
||||
schema={fieldSchema}
|
||||
namePrefix={`${name}.${index}`}
|
||||
disabled={editorContext.readOnly}
|
||||
shouldUnregister={false}
|
||||
stepId={stepId}
|
||||
/>
|
||||
</Box>
|
||||
))}
|
||||
|
@@ -72,6 +72,7 @@ export default function InputCreator(
|
||||
disabled={disabled}
|
||||
fields={schema.fields}
|
||||
shouldUnregister={shouldUnregister}
|
||||
stepId={stepId}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user