From f53909355fa2941d53cb65bd465fa7ecf63b2867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Thu, 21 Sep 2023 16:25:36 +0300 Subject: [PATCH 1/7] feat(pipedrive): add pipedrive integration --- .../src/apps/pipedrive/assets/favicon.svg | 1 + .../apps/pipedrive/auth/generate-auth-url.ts | 19 +++++++ .../backend/src/apps/pipedrive/auth/index.ts | 48 ++++++++++++++++ .../apps/pipedrive/auth/is-still-verified.ts | 9 +++ .../src/apps/pipedrive/auth/refresh-token.ts | 32 +++++++++++ .../apps/pipedrive/auth/verify-credentials.ts | 55 +++++++++++++++++++ .../apps/pipedrive/common/add-auth-header.ts | 11 ++++ .../apps/pipedrive/common/get-current-user.ts | 10 ++++ .../backend/src/apps/pipedrive/index.d.ts | 0 packages/backend/src/apps/pipedrive/index.ts | 16 ++++++ 10 files changed, 201 insertions(+) create mode 100644 packages/backend/src/apps/pipedrive/assets/favicon.svg create mode 100644 packages/backend/src/apps/pipedrive/auth/generate-auth-url.ts create mode 100644 packages/backend/src/apps/pipedrive/auth/index.ts create mode 100644 packages/backend/src/apps/pipedrive/auth/is-still-verified.ts create mode 100644 packages/backend/src/apps/pipedrive/auth/refresh-token.ts create mode 100644 packages/backend/src/apps/pipedrive/auth/verify-credentials.ts create mode 100644 packages/backend/src/apps/pipedrive/common/add-auth-header.ts create mode 100644 packages/backend/src/apps/pipedrive/common/get-current-user.ts create mode 100644 packages/backend/src/apps/pipedrive/index.d.ts create mode 100644 packages/backend/src/apps/pipedrive/index.ts diff --git a/packages/backend/src/apps/pipedrive/assets/favicon.svg b/packages/backend/src/apps/pipedrive/assets/favicon.svg new file mode 100644 index 00000000..5efad428 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/assets/favicon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/packages/backend/src/apps/pipedrive/auth/generate-auth-url.ts b/packages/backend/src/apps/pipedrive/auth/generate-auth-url.ts new file mode 100644 index 00000000..1e35bada --- /dev/null +++ b/packages/backend/src/apps/pipedrive/auth/generate-auth-url.ts @@ -0,0 +1,19 @@ +import { IField, IGlobalVariable } from '@automatisch/types'; +import { URLSearchParams } from 'url'; + +export default async function generateAuthUrl($: IGlobalVariable) { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value as string; + const searchParams = new URLSearchParams({ + client_id: $.auth.data.clientId as string, + redirect_uri: redirectUri, + }); + + const url = `https://oauth.pipedrive.com/oauth/authorize?${searchParams.toString()}`; + + await $.auth.set({ + url, + }); +} diff --git a/packages/backend/src/apps/pipedrive/auth/index.ts b/packages/backend/src/apps/pipedrive/auth/index.ts new file mode 100644 index 00000000..efd31fd5 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/auth/index.ts @@ -0,0 +1,48 @@ +import generateAuthUrl from './generate-auth-url'; +import verifyCredentials from './verify-credentials'; +import refreshToken from './refresh-token'; +import isStillVerified from './is-still-verified'; + +export default { + fields: [ + { + key: 'oAuthRedirectUrl', + label: 'OAuth Redirect URL', + type: 'string' as const, + required: true, + readOnly: true, + value: '{WEB_APP_URL}/app/pipedrive/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in Google Cloud, enter the URL above.', + clickToCopy: true, + }, + { + key: 'clientId', + label: 'Client ID', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + { + key: 'clientSecret', + label: 'Client Secret', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + ], + + generateAuthUrl, + verifyCredentials, + isStillVerified, + refreshToken, +}; diff --git a/packages/backend/src/apps/pipedrive/auth/is-still-verified.ts b/packages/backend/src/apps/pipedrive/auth/is-still-verified.ts new file mode 100644 index 00000000..93a01099 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/auth/is-still-verified.ts @@ -0,0 +1,9 @@ +import { IGlobalVariable } from '@automatisch/types'; +import getCurrentUser from '../common/get-current-user'; + +const isStillVerified = async ($: IGlobalVariable) => { + const currentUser = await getCurrentUser($); + return !!currentUser; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/pipedrive/auth/refresh-token.ts b/packages/backend/src/apps/pipedrive/auth/refresh-token.ts new file mode 100644 index 00000000..89a3b71f --- /dev/null +++ b/packages/backend/src/apps/pipedrive/auth/refresh-token.ts @@ -0,0 +1,32 @@ +import { URLSearchParams } from 'node:url'; +import { IGlobalVariable } from '@automatisch/types'; + +const refreshToken = async ($: IGlobalVariable) => { + const params = new URLSearchParams({ + grant_type: 'refresh_token', + refresh_token: $.auth.data.refreshToken as string, + }); + + const headers = { + Authorization: `Basic ${Buffer.from( + $.auth.data.clientId + ':' + $.auth.data.clientSecret + ).toString('base64')}`, + 'Content-Type': 'application/x-www-form-urlencoded', + }; + + const response = await $.http.post( + 'https://oauth.pipedrive.com/oauth/token', + params.toString(), + { + headers, + } + ); + + await $.auth.set({ + accessToken: response.data.access_token, + tokenType: response.data.token_type, + expiresIn: response.data.expires_in, + }); +}; + +export default refreshToken; diff --git a/packages/backend/src/apps/pipedrive/auth/verify-credentials.ts b/packages/backend/src/apps/pipedrive/auth/verify-credentials.ts new file mode 100644 index 00000000..70f87630 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/auth/verify-credentials.ts @@ -0,0 +1,55 @@ +import { IField, IGlobalVariable } from '@automatisch/types'; +import { URLSearchParams } from 'url'; +import getCurrentUser from '../common/get-current-user'; + +const verifyCredentials = async ($: IGlobalVariable) => { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value as string; + const params = new URLSearchParams({ + grant_type: 'authorization_code', + code: $.auth.data.code as string, + redirect_uri: redirectUri, + }); + + const headers = { + Authorization: `Basic ${Buffer.from( + $.auth.data.clientId + ':' + $.auth.data.clientSecret + ).toString('base64')}`, + 'Content-Type': 'application/x-www-form-urlencoded', + }; + + const response = await $.http.post( + `https://oauth.pipedrive.com/oauth/token`, + params.toString(), + { headers } + ); + + const { + access_token: accessToken, + api_domain: apiDomain, + expires_in: expiresIn, + refresh_token: refreshToken, + scope: scope, + token_type: tokenType, + } = response.data; + + await $.auth.set({ + accessToken, + apiDomain, + expiresIn, + refreshToken, + scope, + tokenType, + }); + + const user = await getCurrentUser($); + + await $.auth.set({ + userId: user.id, + screenName: user.name, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/pipedrive/common/add-auth-header.ts b/packages/backend/src/apps/pipedrive/common/add-auth-header.ts new file mode 100644 index 00000000..8e7798b8 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/common/add-auth-header.ts @@ -0,0 +1,11 @@ +import { TBeforeRequest } from '@automatisch/types'; + +const addAuthHeader: TBeforeRequest = ($, requestConfig) => { + if ($.auth.data?.accessToken) { + requestConfig.headers.Authorization = `${$.auth.data.tokenType} ${$.auth.data.accessToken}`; + } + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/pipedrive/common/get-current-user.ts b/packages/backend/src/apps/pipedrive/common/get-current-user.ts new file mode 100644 index 00000000..024ede73 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/common/get-current-user.ts @@ -0,0 +1,10 @@ +import { IGlobalVariable, IJSONObject } from '@automatisch/types'; + +const getCurrentUser = async ($: IGlobalVariable): Promise => { + const response = await $.http.get(`${$.auth.data.apiDomain}/api/v1/users/me`); + const currentUser = response.data.data; + + return currentUser; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/pipedrive/index.d.ts b/packages/backend/src/apps/pipedrive/index.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/backend/src/apps/pipedrive/index.ts b/packages/backend/src/apps/pipedrive/index.ts new file mode 100644 index 00000000..9999729e --- /dev/null +++ b/packages/backend/src/apps/pipedrive/index.ts @@ -0,0 +1,16 @@ +import defineApp from '../../helpers/define-app'; +import addAuthHeader from './common/add-auth-header'; +import auth from './auth'; + +export default defineApp({ + name: 'Pipedrive', + key: 'pipedrive', + baseUrl: '', + apiBaseUrl: '', + iconUrl: '{BASE_URL}/apps/pipedrive/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/pipedrive/connection', + primaryColor: 'FFFFFF', + supportsConnections: true, + beforeRequest: [addAuthHeader], + auth, +}); From 251885d4bee80c06573180cc2892ba0d34c85c0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Thu, 21 Sep 2023 18:51:18 +0300 Subject: [PATCH 2/7] feat(pipedrive): add new deals trigger --- .../src/apps/pipedrive/auth/refresh-token.ts | 1 + packages/backend/src/apps/pipedrive/index.ts | 2 + .../src/apps/pipedrive/triggers/index.ts | 3 + .../pipedrive/triggers/new-deals/index.ts | 56 +++++++++++++++++++ packages/docs/pages/.vitepress/config.js | 9 +++ .../docs/pages/apps/pipedrive/connection.md | 17 ++++++ .../docs/pages/apps/pipedrive/triggers.md | 12 ++++ .../docs/pages/public/favicons/pipedrive.svg | 1 + 8 files changed, 101 insertions(+) create mode 100644 packages/backend/src/apps/pipedrive/triggers/index.ts create mode 100644 packages/backend/src/apps/pipedrive/triggers/new-deals/index.ts create mode 100644 packages/docs/pages/apps/pipedrive/connection.md create mode 100644 packages/docs/pages/apps/pipedrive/triggers.md create mode 100644 packages/docs/pages/public/favicons/pipedrive.svg diff --git a/packages/backend/src/apps/pipedrive/auth/refresh-token.ts b/packages/backend/src/apps/pipedrive/auth/refresh-token.ts index 89a3b71f..42994e38 100644 --- a/packages/backend/src/apps/pipedrive/auth/refresh-token.ts +++ b/packages/backend/src/apps/pipedrive/auth/refresh-token.ts @@ -24,6 +24,7 @@ const refreshToken = async ($: IGlobalVariable) => { await $.auth.set({ accessToken: response.data.access_token, + refreshToken: response.data.refresh_token, tokenType: response.data.token_type, expiresIn: response.data.expires_in, }); diff --git a/packages/backend/src/apps/pipedrive/index.ts b/packages/backend/src/apps/pipedrive/index.ts index 9999729e..3e01bfa7 100644 --- a/packages/backend/src/apps/pipedrive/index.ts +++ b/packages/backend/src/apps/pipedrive/index.ts @@ -1,6 +1,7 @@ import defineApp from '../../helpers/define-app'; import addAuthHeader from './common/add-auth-header'; import auth from './auth'; +import triggers from './triggers'; export default defineApp({ name: 'Pipedrive', @@ -13,4 +14,5 @@ export default defineApp({ supportsConnections: true, beforeRequest: [addAuthHeader], auth, + triggers, }); diff --git a/packages/backend/src/apps/pipedrive/triggers/index.ts b/packages/backend/src/apps/pipedrive/triggers/index.ts new file mode 100644 index 00000000..4e595290 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/triggers/index.ts @@ -0,0 +1,3 @@ +import newDeals from './new-deals'; + +export default [newDeals]; diff --git a/packages/backend/src/apps/pipedrive/triggers/new-deals/index.ts b/packages/backend/src/apps/pipedrive/triggers/new-deals/index.ts new file mode 100644 index 00000000..204d01c3 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/triggers/new-deals/index.ts @@ -0,0 +1,56 @@ +import defineTrigger from '../../../../helpers/define-trigger'; + +type Payload = { + start: number; + limit: number; + sort: string; +}; + +type ResponseData = { + data: { + id: number; + }[]; + additional_data: { + pagination: { + next_start: number; + }; + }; +}; + +export default defineTrigger({ + name: 'New deals', + key: 'newDeals', + pollInterval: 15, + description: 'Triggers when a new deal is created.', + arguments: [], + + async run($) { + const params: Payload = { + start: 0, + limit: 100, + sort: 'add_time DESC', + }; + + do { + const { data } = await $.http.get( + `${$.auth.data.apiDomain}/api/v1/deals`, + { params } + ); + + if (!data.data.length) { + return; + } + + params.start = data.additional_data?.pagination?.next_start; + + for (const deal of data.data) { + $.pushTriggerItem({ + raw: deal, + meta: { + internalId: deal.id.toString(), + }, + }); + } + } while (params.start); + }, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 71676c16..3058a946 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -215,6 +215,15 @@ export default defineConfig({ { text: 'Connection', link: '/apps/openai/connection' }, ], }, + { + text: 'Pipedrive', + collapsible: true, + collapsed: true, + items: [ + { text: 'Triggers', link: '/apps/pipedrive/triggers' }, + { text: 'Connection', link: '/apps/pipedrive/connection' }, + ], + }, { text: 'PostgreSQL', collapsible: true, diff --git a/packages/docs/pages/apps/pipedrive/connection.md b/packages/docs/pages/apps/pipedrive/connection.md new file mode 100644 index 00000000..aa94cfbc --- /dev/null +++ b/packages/docs/pages/apps/pipedrive/connection.md @@ -0,0 +1,17 @@ +# Pipedrive + +:::info +This page explains the steps you need to follow to set up the Pipedrive +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Go to [Pipedrive developers page](https://developers.pipedrive.com/). +2. Sign up for a **Sandbox account** in order to create an app. +3. Click create an app button and then choose **Create private app** option. +4. Write any app name to be displayed in Automatisch. +5. Copy **OAuth Redirect URL** from Automatisch to **Callback URL** field, and click on the **Save** button. +6. Check all options in **OAuth & Access scopes** with full access. +7. Click on the **Save** button. +8. Copy the **Client ID** value to the `Client ID` field on Automatisch. +9. Copy the **Client Secret** value to the `Client Secret` field on Automatisch. +10. Start using Pipedrive integration with Automatisch! diff --git a/packages/docs/pages/apps/pipedrive/triggers.md b/packages/docs/pages/apps/pipedrive/triggers.md new file mode 100644 index 00000000..e6b3b94d --- /dev/null +++ b/packages/docs/pages/apps/pipedrive/triggers.md @@ -0,0 +1,12 @@ +--- +favicon: /favicons/pipedrive.svg +items: + - name: New deals + desc: Triggers when a new deal is created. +--- + + + + diff --git a/packages/docs/pages/public/favicons/pipedrive.svg b/packages/docs/pages/public/favicons/pipedrive.svg new file mode 100644 index 00000000..5efad428 --- /dev/null +++ b/packages/docs/pages/public/favicons/pipedrive.svg @@ -0,0 +1 @@ + \ No newline at end of file From 6378e6264511ca4ef991d27ef12c715798112a8d Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 28 Sep 2023 13:51:08 +0200 Subject: [PATCH 3/7] docs(pipedrive): Add to available apps --- packages/docs/pages/guide/available-apps.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/docs/pages/guide/available-apps.md b/packages/docs/pages/guide/available-apps.md index 2f5a189a..41da849b 100644 --- a/packages/docs/pages/guide/available-apps.md +++ b/packages/docs/pages/guide/available-apps.md @@ -23,6 +23,7 @@ The following integrations are currently supported by Automatisch. - [Odoo](/apps/odoo/actions) - [OpenAI](/apps/openai/actions) - [PostgreSQL](/apps/postgresql/actions) +- [Pipedrive](/apps/pipedrive/triggers) - [RSS](/apps/rss/triggers) - [Salesforce](/apps/salesforce/triggers) - [Scheduler](/apps/scheduler/triggers) From 398938f27e16537ff360e18726186cb49fd8db46 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 28 Sep 2023 14:41:24 +0200 Subject: [PATCH 4/7] feat(pipedrive): Use also company domain for screen name --- .../backend/src/apps/pipedrive/auth/verify-credentials.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/apps/pipedrive/auth/verify-credentials.ts b/packages/backend/src/apps/pipedrive/auth/verify-credentials.ts index 70f87630..40d11f9d 100644 --- a/packages/backend/src/apps/pipedrive/auth/verify-credentials.ts +++ b/packages/backend/src/apps/pipedrive/auth/verify-credentials.ts @@ -46,9 +46,13 @@ const verifyCredentials = async ($: IGlobalVariable) => { const user = await getCurrentUser($); + const screenName = [user.name, user.company_domain] + .filter(Boolean) + .join(' @ '); + await $.auth.set({ userId: user.id, - screenName: user.name, + screenName, }); }; From 213c8096d2c2e92a8e46c844c634300d369fd654 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 28 Sep 2023 14:52:23 +0200 Subject: [PATCH 5/7] fix(pipedrive): Change the order in available apps --- packages/docs/pages/guide/available-apps.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docs/pages/guide/available-apps.md b/packages/docs/pages/guide/available-apps.md index 41da849b..95884361 100644 --- a/packages/docs/pages/guide/available-apps.md +++ b/packages/docs/pages/guide/available-apps.md @@ -22,8 +22,8 @@ The following integrations are currently supported by Automatisch. - [Ntfy](/apps/ntfy/actions) - [Odoo](/apps/odoo/actions) - [OpenAI](/apps/openai/actions) -- [PostgreSQL](/apps/postgresql/actions) - [Pipedrive](/apps/pipedrive/triggers) +- [PostgreSQL](/apps/postgresql/actions) - [RSS](/apps/rss/triggers) - [Salesforce](/apps/salesforce/triggers) - [Scheduler](/apps/scheduler/triggers) From 98649dcba6c6f0c51590d232e77928be3328b934 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 28 Sep 2023 14:57:46 +0200 Subject: [PATCH 6/7] fix(pipedrive): Guard new deals response in case there is none --- packages/backend/src/apps/pipedrive/triggers/new-deals/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend/src/apps/pipedrive/triggers/new-deals/index.ts b/packages/backend/src/apps/pipedrive/triggers/new-deals/index.ts index 204d01c3..6a54a46a 100644 --- a/packages/backend/src/apps/pipedrive/triggers/new-deals/index.ts +++ b/packages/backend/src/apps/pipedrive/triggers/new-deals/index.ts @@ -37,7 +37,7 @@ export default defineTrigger({ { params } ); - if (!data.data.length) { + if (!data?.data?.length) { return; } From 365ae656f2917fc9237fcc4550d7270cc174cea8 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 28 Sep 2023 15:01:35 +0200 Subject: [PATCH 7/7] fix(pipedrive): Adjust description of OAuth redirect URL --- packages/backend/src/apps/pipedrive/auth/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend/src/apps/pipedrive/auth/index.ts b/packages/backend/src/apps/pipedrive/auth/index.ts index efd31fd5..5e0ee567 100644 --- a/packages/backend/src/apps/pipedrive/auth/index.ts +++ b/packages/backend/src/apps/pipedrive/auth/index.ts @@ -14,7 +14,7 @@ export default { value: '{WEB_APP_URL}/app/pipedrive/connections/add', placeholder: null, description: - 'When asked to input a redirect URL in Google Cloud, enter the URL above.', + 'When asked to input a redirect URL in Pipedrive, enter the URL above.', clickToCopy: true, }, {