feat(mailchimp): add mailchimp integration

This commit is contained in:
Rıdvan Akca
2024-01-31 12:32:15 +03:00
committed by Ali BARIN
parent c4cbc024e6
commit cbed79fbf1
12 changed files with 195 additions and 0 deletions

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

@@ -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,
});
}

View File

@@ -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,
};

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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,
});

View File

@@ -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,

View File

@@ -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.

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 5.0 KiB