From cbed79fbf130a9da7f24504a7f0128e6accfa1a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Wed, 31 Jan 2024 12:32:15 +0300 Subject: [PATCH] feat(mailchimp): add mailchimp integration --- .../src/apps/mailchimp/assets/favicon.svg | 1 + .../apps/mailchimp/auth/generate-auth-url.js | 19 ++++++++ .../backend/src/apps/mailchimp/auth/index.js | 46 +++++++++++++++++++ .../apps/mailchimp/auth/is-still-verified.js | 8 ++++ .../apps/mailchimp/auth/verify-credentials.js | 40 ++++++++++++++++ .../apps/mailchimp/common/add-auth-header.js | 12 +++++ .../apps/mailchimp/common/get-current-user.js | 18 ++++++++ .../src/apps/mailchimp/common/set-base-url.js | 10 ++++ packages/backend/src/apps/mailchimp/index.js | 17 +++++++ packages/docs/pages/.vitepress/config.js | 6 +++ .../docs/pages/apps/mailchimp/connection.md | 17 +++++++ .../docs/pages/public/favicons/mailchimp.svg | 1 + 12 files changed, 195 insertions(+) create mode 100644 packages/backend/src/apps/mailchimp/assets/favicon.svg create mode 100644 packages/backend/src/apps/mailchimp/auth/generate-auth-url.js create mode 100644 packages/backend/src/apps/mailchimp/auth/index.js create mode 100644 packages/backend/src/apps/mailchimp/auth/is-still-verified.js create mode 100644 packages/backend/src/apps/mailchimp/auth/verify-credentials.js create mode 100644 packages/backend/src/apps/mailchimp/common/add-auth-header.js create mode 100644 packages/backend/src/apps/mailchimp/common/get-current-user.js create mode 100644 packages/backend/src/apps/mailchimp/common/set-base-url.js create mode 100644 packages/backend/src/apps/mailchimp/index.js create mode 100644 packages/docs/pages/apps/mailchimp/connection.md create mode 100644 packages/docs/pages/public/favicons/mailchimp.svg diff --git a/packages/backend/src/apps/mailchimp/assets/favicon.svg b/packages/backend/src/apps/mailchimp/assets/favicon.svg new file mode 100644 index 00000000..55af85db --- /dev/null +++ b/packages/backend/src/apps/mailchimp/assets/favicon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/packages/backend/src/apps/mailchimp/auth/generate-auth-url.js b/packages/backend/src/apps/mailchimp/auth/generate-auth-url.js new file mode 100644 index 00000000..380fe61d --- /dev/null +++ b/packages/backend/src/apps/mailchimp/auth/generate-auth-url.js @@ -0,0 +1,19 @@ +import { URLSearchParams } from 'url'; + +export default async function generateAuthUrl($) { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const searchParams = new URLSearchParams({ + response_type: 'code', + client_id: $.auth.data.clientId, + redirect_uri: redirectUri, + }); + + const url = `https://login.mailchimp.com/oauth2/authorize?${searchParams.toString()}`; + + await $.auth.set({ + url, + }); +} diff --git a/packages/backend/src/apps/mailchimp/auth/index.js b/packages/backend/src/apps/mailchimp/auth/index.js new file mode 100644 index 00000000..d69cfd43 --- /dev/null +++ b/packages/backend/src/apps/mailchimp/auth/index.js @@ -0,0 +1,46 @@ +import generateAuthUrl from './generate-auth-url.js'; +import verifyCredentials from './verify-credentials.js'; +import isStillVerified from './is-still-verified.js'; + +export default { + fields: [ + { + key: 'oAuthRedirectUrl', + label: 'OAuth Redirect URL', + type: 'string', + required: true, + readOnly: true, + value: '{WEB_APP_URL}/app/mailchimp/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in Mailchimp, enter the URL above.', + clickToCopy: true, + }, + { + key: 'clientId', + label: 'Client ID', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + { + key: 'clientSecret', + label: 'Client Secret', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + ], + + generateAuthUrl, + verifyCredentials, + isStillVerified, +}; diff --git a/packages/backend/src/apps/mailchimp/auth/is-still-verified.js b/packages/backend/src/apps/mailchimp/auth/is-still-verified.js new file mode 100644 index 00000000..b750ce14 --- /dev/null +++ b/packages/backend/src/apps/mailchimp/auth/is-still-verified.js @@ -0,0 +1,8 @@ +import getCurrentUser from '../common/get-current-user.js'; + +const isStillVerified = async ($) => { + const currentUser = await getCurrentUser($); + return !!currentUser.user_id; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/mailchimp/auth/verify-credentials.js b/packages/backend/src/apps/mailchimp/auth/verify-credentials.js new file mode 100644 index 00000000..efe7a049 --- /dev/null +++ b/packages/backend/src/apps/mailchimp/auth/verify-credentials.js @@ -0,0 +1,40 @@ +import getCurrentUser from '../common/get-current-user.js'; + +const verifyCredentials = async ($) => { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const params = new URLSearchParams({ + grant_type: 'authorization_code', + client_id: $.auth.data.clientId, + client_secret: $.auth.data.clientSecret, + redirect_uri: redirectUri, + code: $.auth.data.code, + }); + + const { data } = await $.http.post( + 'https://login.mailchimp.com/oauth2/token', + params.toString() + ); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + }); + + const currentUser = await getCurrentUser($); + + await $.auth.set({ + clientId: $.auth.data.clientId, + clientSecret: $.auth.data.clientSecret, + scope: $.auth.data.scope, + idToken: data.id_token, + expiresIn: data.expires_in, + refreshToken: data.refresh_token, + serverPrefix: currentUser.dc, + screenName: currentUser.login.login_name, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/mailchimp/common/add-auth-header.js b/packages/backend/src/apps/mailchimp/common/add-auth-header.js new file mode 100644 index 00000000..141e1bdc --- /dev/null +++ b/packages/backend/src/apps/mailchimp/common/add-auth-header.js @@ -0,0 +1,12 @@ +const addAuthHeader = ($, requestConfig) => { + if ( + !requestConfig.additionalProperties?.skipAddingAuthHeader && + $.auth.data?.accessToken + ) { + requestConfig.headers.Authorization = `Bearer ${$.auth.data.accessToken}`; + } + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/mailchimp/common/get-current-user.js b/packages/backend/src/apps/mailchimp/common/get-current-user.js new file mode 100644 index 00000000..1c27f1a3 --- /dev/null +++ b/packages/backend/src/apps/mailchimp/common/get-current-user.js @@ -0,0 +1,18 @@ +const getCurrentUser = async ($) => { + const { data: currentUser } = await $.http.get( + 'https://login.mailchimp.com/oauth2/metadata', + { + headers: { + Authorization: `OAuth ${$.auth.data.accessToken}`, + }, + additionalProperties: { + skipAddingAuthHeader: true, + skipAddingBaseUrl: true, + }, + } + ); + + return currentUser; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/mailchimp/common/set-base-url.js b/packages/backend/src/apps/mailchimp/common/set-base-url.js new file mode 100644 index 00000000..6ace2347 --- /dev/null +++ b/packages/backend/src/apps/mailchimp/common/set-base-url.js @@ -0,0 +1,10 @@ +const setBaseUrl = ($, requestConfig) => { + const serverPrefix = $.auth.data.serverPrefix; + if (!requestConfig.additionalProperties?.skipAddingBaseUrl && serverPrefix) { + requestConfig.baseURL = `https://${serverPrefix}.api.mailchimp.com`; + } + + return requestConfig; +}; + +export default setBaseUrl; diff --git a/packages/backend/src/apps/mailchimp/index.js b/packages/backend/src/apps/mailchimp/index.js new file mode 100644 index 00000000..170b3e95 --- /dev/null +++ b/packages/backend/src/apps/mailchimp/index.js @@ -0,0 +1,17 @@ +import defineApp from '../../helpers/define-app.js'; +import addAuthHeader from './common/add-auth-header.js'; +import setBaseUrl from './common/set-base-url.js'; +import auth from './auth/index.js'; + +export default defineApp({ + name: 'Mailchimp', + key: 'mailchimp', + baseUrl: 'https://mailchimp.com', + apiBaseUrl: '', + iconUrl: '{BASE_URL}/apps/mailchimp/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/mailchimp/connection', + primaryColor: '000000', + supportsConnections: true, + beforeRequest: [setBaseUrl, addAuthHeader], + auth, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 7e4785ba..f71ef2a5 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -261,6 +261,12 @@ export default defineConfig({ { text: 'Connection', link: '/apps/mailerlite/connection' }, ], }, + { + text: 'Mailchimp', + collapsible: true, + collapsed: true, + items: [{ text: 'Connection', link: '/apps/mailchimp/connection' }], + }, { text: 'Mattermost', collapsible: true, diff --git a/packages/docs/pages/apps/mailchimp/connection.md b/packages/docs/pages/apps/mailchimp/connection.md new file mode 100644 index 00000000..41886994 --- /dev/null +++ b/packages/docs/pages/apps/mailchimp/connection.md @@ -0,0 +1,17 @@ +# Mailchimp + +:::info +This page explains the steps you need to follow to set up the Mailchimp +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Login to your [Mailchimp account](https://mailchimp.com/) to create an app. +2. Click on the account image and go to your **profile** page. +3. Click on the **Extras** tab and choose the **Registered apps**. +4. Click on the **Register An App** button. +5. Fill the registration form. +6. Copy **OAuth Redirect URL** from Automatisch to **Redirect URI** field, and click on the **Create** button. +7. Copy the **Your Client ID** value to the `Client ID` field on Automatisch. +8. Copy the **Your Client Secret** value to the `Client Secret` field on Automatisch. +9. Click **Submit** button on Automatisch. +10. Congrats! Start using your new Mailchimp connection within the flows. diff --git a/packages/docs/pages/public/favicons/mailchimp.svg b/packages/docs/pages/public/favicons/mailchimp.svg new file mode 100644 index 00000000..55af85db --- /dev/null +++ b/packages/docs/pages/public/favicons/mailchimp.svg @@ -0,0 +1 @@ + \ No newline at end of file