From a9282ad1180843f8be6589dff35a765676091e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Mon, 2 Oct 2023 15:11:09 +0300 Subject: [PATCH] feat(pipedrive): add create note action --- .../pipedrive/actions/create-note/index.ts | 206 ++++++++++++++++++ .../src/apps/pipedrive/actions/index.ts | 3 +- .../src/apps/pipedrive/dynamic-data/index.ts | 11 +- .../dynamic-data/list-deals/index.ts | 37 ++++ .../dynamic-data/list-leads/index.ts | 37 ++++ packages/docs/pages/apps/pipedrive/actions.md | 2 + 6 files changed, 294 insertions(+), 2 deletions(-) create mode 100644 packages/backend/src/apps/pipedrive/actions/create-note/index.ts create mode 100644 packages/backend/src/apps/pipedrive/dynamic-data/list-deals/index.ts create mode 100644 packages/backend/src/apps/pipedrive/dynamic-data/list-leads/index.ts diff --git a/packages/backend/src/apps/pipedrive/actions/create-note/index.ts b/packages/backend/src/apps/pipedrive/actions/create-note/index.ts new file mode 100644 index 00000000..335381e1 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/actions/create-note/index.ts @@ -0,0 +1,206 @@ +import defineAction from '../../../../helpers/define-action'; + +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 note', + key: 'createNote', + description: 'Creates a new note.', + arguments: [ + { + label: 'Content', + key: 'content', + type: 'string' as const, + required: true, + description: 'Supports some HTML formatting.', + variables: true, + }, + { + label: 'Deal', + key: 'dealId', + type: 'dropdown' as const, + required: false, + description: + 'Note must be associated with at least one deal, person, organization, or lead.', + variables: true, + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listDeals', + }, + ], + }, + }, + { + label: 'Pin note on specified deal?', + key: 'pinnedDeal', + type: 'dropdown' as const, + required: false, + description: '', + options: [ + { + label: 'No', + value: 0, + }, + { + label: 'Yes', + value: 1, + }, + ], + }, + { + label: 'Person', + key: 'personId', + type: 'dropdown' as const, + required: false, + description: + 'Note must be associated with at least one deal, person, organization, or lead.', + variables: true, + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listPersons', + }, + ], + }, + }, + { + label: 'Pin note on specified person?', + key: 'pinnedPerson', + type: 'dropdown' as const, + required: false, + description: '', + options: [ + { + label: 'No', + value: 0, + }, + { + label: 'Yes', + value: 1, + }, + ], + }, + { + label: 'Organization', + key: 'organizationId', + type: 'dropdown' as const, + required: false, + description: + 'Note must be associated with at least one deal, person, organization, or lead.', + variables: true, + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listOrganizations', + }, + ], + }, + }, + { + label: 'Pin note on specified organization?', + key: 'pinnedOrganization', + type: 'dropdown' as const, + required: false, + description: '', + options: [ + { + label: 'No', + value: 0, + }, + { + label: 'Yes', + value: 1, + }, + ], + }, + { + label: 'Lead', + key: 'leadId', + type: 'dropdown' as const, + required: false, + description: + 'Note must be associated with at least one deal, person, organization, or lead.', + variables: true, + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listLeads', + }, + ], + }, + }, + { + label: 'Pin note on specified lead?', + key: 'pinnedLead', + type: 'dropdown' as const, + required: false, + description: '', + options: [ + { + label: 'No', + value: 0, + }, + { + label: 'Yes', + value: 1, + }, + ], + }, + ], + + async run($) { + const { + content, + dealId, + pinnedDeal, + personId, + pinnedPerson, + organizationId, + pinnedOrganization, + leadId, + pinnedLead, + } = $.step.parameters; + + const fields = { + content: content as string, + deal_id: dealId as number, + pinned_to_deal_flag: pinnedDeal as number, + person_id: personId as number, + pinned_to_person_flag: pinnedPerson as number, + org_id: organizationId as number, + pinned_to_organization_flag: pinnedOrganization as number, + lead_id: leadId as string, + pinned_to_lead_flag: pinnedLead as number, + }; + + const body = filterProvidedFields(fields); + + const { + data: { data }, + } = await $.http.post(`${$.auth.data.apiDomain}/api/v1/notes`, 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 04bb1d37..516066d7 100644 --- a/packages/backend/src/apps/pipedrive/actions/index.ts +++ b/packages/backend/src/apps/pipedrive/actions/index.ts @@ -1,3 +1,4 @@ import createDeal from './create-deal'; +import createNote from './create-note'; -export default [createDeal]; +export default [createDeal, createNote]; diff --git a/packages/backend/src/apps/pipedrive/dynamic-data/index.ts b/packages/backend/src/apps/pipedrive/dynamic-data/index.ts index 78a520fe..7af49dcc 100644 --- a/packages/backend/src/apps/pipedrive/dynamic-data/index.ts +++ b/packages/backend/src/apps/pipedrive/dynamic-data/index.ts @@ -1,6 +1,15 @@ import listCurrencies from './list-currencies'; +import listDeals from './list-deals'; +import listLeads from './list-leads'; import listOrganizations from './list-organizations'; import listPersons from './list-persons'; import listUsers from './list-users'; -export default [listCurrencies, listOrganizations, listPersons, listUsers]; +export default [ + listCurrencies, + listDeals, + listLeads, + listOrganizations, + listPersons, + listUsers, +]; diff --git a/packages/backend/src/apps/pipedrive/dynamic-data/list-deals/index.ts b/packages/backend/src/apps/pipedrive/dynamic-data/list-deals/index.ts new file mode 100644 index 00000000..d93655fe --- /dev/null +++ b/packages/backend/src/apps/pipedrive/dynamic-data/list-deals/index.ts @@ -0,0 +1,37 @@ +import { IGlobalVariable, IJSONObject } from '@automatisch/types'; + +export default { + name: 'List deals', + key: 'listDeals', + + async run($: IGlobalVariable) { + const deals: { + data: IJSONObject[]; + } = { + data: [], + }; + + const params = { + sort: 'add_time DESC', + }; + + const { data } = await $.http.get(`${$.auth.data.apiDomain}/api/v1/deals`, { + params, + }); + + if (!data?.data) { + return { data: [] }; + } + + if (data.data.length) { + for (const deal of data.data) { + deals.data.push({ + value: deal.id, + name: deal.title, + }); + } + } + + return deals; + }, +}; diff --git a/packages/backend/src/apps/pipedrive/dynamic-data/list-leads/index.ts b/packages/backend/src/apps/pipedrive/dynamic-data/list-leads/index.ts new file mode 100644 index 00000000..e1896b16 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/dynamic-data/list-leads/index.ts @@ -0,0 +1,37 @@ +import { IGlobalVariable, IJSONObject } from '@automatisch/types'; + +export default { + name: 'List leads', + key: 'listLeads', + + async run($: IGlobalVariable) { + const leads: { + data: IJSONObject[]; + } = { + data: [], + }; + + const params = { + sort: 'add_time DESC', + }; + + const { data } = await $.http.get(`${$.auth.data.apiDomain}/api/v1/leads`, { + params, + }); + + if (!data?.data) { + return { data: [] }; + } + + if (data.data.length) { + for (const lead of data.data) { + leads.data.push({ + value: lead.id, + name: lead.title, + }); + } + } + + return leads; + }, +}; diff --git a/packages/docs/pages/apps/pipedrive/actions.md b/packages/docs/pages/apps/pipedrive/actions.md index 1a135b02..996b2ebe 100644 --- a/packages/docs/pages/apps/pipedrive/actions.md +++ b/packages/docs/pages/apps/pipedrive/actions.md @@ -3,6 +3,8 @@ favicon: /favicons/pipedrive.svg items: - name: Create deal desc: Creates a new deal. + - name: Create note + desc: Creates a new note. ---