From a87c76df40963900abaf8d0919f8219a766f9a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Wed, 12 Jun 2024 13:20:46 +0200 Subject: [PATCH] feat(surveymonkey): add surveymonkey integration --- .../src/apps/surveymonkey/assets/favicon.svg | 1 + .../surveymonkey/auth/generate-auth-url.js | 24 ++++++++++ .../src/apps/surveymonkey/auth/index.js | 46 ++++++++++++++++++ .../surveymonkey/auth/is-still-verified.js | 8 ++++ .../surveymonkey/auth/verify-credentials.js | 48 +++++++++++++++++++ .../surveymonkey/common/add-auth-header.js | 9 ++++ .../surveymonkey/common/get-current-user.js | 6 +++ .../apps/surveymonkey/common/set-base-url.js | 11 +++++ .../backend/src/apps/surveymonkey/index.js | 17 +++++++ packages/docs/pages/.vitepress/config.js | 8 ++++ .../pages/apps/surveymonkey/connection.md | 18 +++++++ .../pages/public/favicons/surveymonkey.svg | 1 + 12 files changed, 197 insertions(+) create mode 100644 packages/backend/src/apps/surveymonkey/assets/favicon.svg create mode 100644 packages/backend/src/apps/surveymonkey/auth/generate-auth-url.js create mode 100644 packages/backend/src/apps/surveymonkey/auth/index.js create mode 100644 packages/backend/src/apps/surveymonkey/auth/is-still-verified.js create mode 100644 packages/backend/src/apps/surveymonkey/auth/verify-credentials.js create mode 100644 packages/backend/src/apps/surveymonkey/common/add-auth-header.js create mode 100644 packages/backend/src/apps/surveymonkey/common/get-current-user.js create mode 100644 packages/backend/src/apps/surveymonkey/common/set-base-url.js create mode 100644 packages/backend/src/apps/surveymonkey/index.js create mode 100644 packages/docs/pages/apps/surveymonkey/connection.md create mode 100644 packages/docs/pages/public/favicons/surveymonkey.svg diff --git a/packages/backend/src/apps/surveymonkey/assets/favicon.svg b/packages/backend/src/apps/surveymonkey/assets/favicon.svg new file mode 100644 index 00000000..9268a24d --- /dev/null +++ b/packages/backend/src/apps/surveymonkey/assets/favicon.svg @@ -0,0 +1 @@ +Goldie_Sabaeus_RGB \ No newline at end of file diff --git a/packages/backend/src/apps/surveymonkey/auth/generate-auth-url.js b/packages/backend/src/apps/surveymonkey/auth/generate-auth-url.js new file mode 100644 index 00000000..65c4ba70 --- /dev/null +++ b/packages/backend/src/apps/surveymonkey/auth/generate-auth-url.js @@ -0,0 +1,24 @@ +import crypto from 'crypto'; +import { URLSearchParams } from 'url'; + +export default async function generateAuthUrl($) { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const state = crypto.randomBytes(100).toString('base64url'); + + const searchParams = new URLSearchParams({ + client_id: $.auth.data.clientId, + redirect_uri: redirectUri, + response_type: 'code', + state, + }); + + const url = `https://api.surveymonkey.com/oauth/authorize?${searchParams.toString()}`; + + await $.auth.set({ + url, + originalState: state, + }); +} diff --git a/packages/backend/src/apps/surveymonkey/auth/index.js b/packages/backend/src/apps/surveymonkey/auth/index.js new file mode 100644 index 00000000..36c20476 --- /dev/null +++ b/packages/backend/src/apps/surveymonkey/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/surveymonkey/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in SurveyMonkey, 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/surveymonkey/auth/is-still-verified.js b/packages/backend/src/apps/surveymonkey/auth/is-still-verified.js new file mode 100644 index 00000000..08962895 --- /dev/null +++ b/packages/backend/src/apps/surveymonkey/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.id; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/surveymonkey/auth/verify-credentials.js b/packages/backend/src/apps/surveymonkey/auth/verify-credentials.js new file mode 100644 index 00000000..fc6bd8cf --- /dev/null +++ b/packages/backend/src/apps/surveymonkey/auth/verify-credentials.js @@ -0,0 +1,48 @@ +import getCurrentUser from '../common/get-current-user.js'; + +const verifyCredentials = async ($) => { + if ($.auth.data.originalState !== $.auth.data.state) { + throw new Error("The 'state' parameter does not match."); + } + + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const { data } = await $.http.post( + 'https://api.surveymonkey.com/oauth/token', + { + client_secret: $.auth.data.clientSecret, + code: $.auth.data.code, + redirect_uri: redirectUri, + client_id: $.auth.data.clientId, + grant_type: 'authorization_code', + }, + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + } + ); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + accessUrl: data.access_url, + expiresIn: data.expires_in, + }); + + const currentUser = await getCurrentUser($); + + const screenName = [currentUser.username, currentUser.email] + .filter(Boolean) + .join(' @ '); + + await $.auth.set({ + clientId: $.auth.data.clientId, + clientSecret: $.auth.data.clientSecret, + screenName, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/surveymonkey/common/add-auth-header.js b/packages/backend/src/apps/surveymonkey/common/add-auth-header.js new file mode 100644 index 00000000..02477aa4 --- /dev/null +++ b/packages/backend/src/apps/surveymonkey/common/add-auth-header.js @@ -0,0 +1,9 @@ +const addAuthHeader = ($, 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/surveymonkey/common/get-current-user.js b/packages/backend/src/apps/surveymonkey/common/get-current-user.js new file mode 100644 index 00000000..e630f506 --- /dev/null +++ b/packages/backend/src/apps/surveymonkey/common/get-current-user.js @@ -0,0 +1,6 @@ +const getCurrentUser = async ($) => { + const { data: currentUser } = await $.http.get('/v3/users/me'); + return currentUser; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/surveymonkey/common/set-base-url.js b/packages/backend/src/apps/surveymonkey/common/set-base-url.js new file mode 100644 index 00000000..91e6ce3c --- /dev/null +++ b/packages/backend/src/apps/surveymonkey/common/set-base-url.js @@ -0,0 +1,11 @@ +const setBaseUrl = ($, requestConfig) => { + const accessUrl = $.auth.data.accessUrl; + + if (accessUrl) { + requestConfig.baseURL = accessUrl; + } + + return requestConfig; +}; + +export default setBaseUrl; diff --git a/packages/backend/src/apps/surveymonkey/index.js b/packages/backend/src/apps/surveymonkey/index.js new file mode 100644 index 00000000..25292244 --- /dev/null +++ b/packages/backend/src/apps/surveymonkey/index.js @@ -0,0 +1,17 @@ +import defineApp from '../../helpers/define-app.js'; +import addAuthHeader from './common/add-auth-header.js'; +import auth from './auth/index.js'; +import setBaseUrl from './common/set-base-url.js'; + +export default defineApp({ + name: 'SurveyMonkey', + key: 'surveymonkey', + baseUrl: 'https://www.surveymonkey.com', + apiBaseUrl: '', + iconUrl: '{BASE_URL}/apps/surveymonkey/assets/favicon.svg', + authDocUrl: '{DOCS_URL}/apps/surveymonkey/connection', + primaryColor: '00bf6f', + supportsConnections: true, + beforeRequest: [setBaseUrl, addAuthHeader], + auth, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 219a1d51..05bdb60a 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -437,6 +437,14 @@ export default defineConfig({ { text: 'Connection', link: '/apps/stripe/connection' }, ], }, + { + text: 'SurveyMonkey', + collapsible: true, + collapsed: true, + items: [ + { text: 'Connection', link: '/apps/surveymonkey/connection' }, + ], + }, { text: 'Telegram', collapsible: true, diff --git a/packages/docs/pages/apps/surveymonkey/connection.md b/packages/docs/pages/apps/surveymonkey/connection.md new file mode 100644 index 00000000..76d192a6 --- /dev/null +++ b/packages/docs/pages/apps/surveymonkey/connection.md @@ -0,0 +1,18 @@ +# SurveyMonkey + +:::info +This page explains the steps you need to follow to set up the SurveyMonkey +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Go to the [My Apps page of your SurveyMonkey account](https://developer.surveymonkey.com/apps/) to create a project. +2. Click on the **Add a New App** button. +3. Fill the form and submit it. +4. Go to **Settings** page. +5. Copy **OAuth Redirect URL** from Automatisch to **OAuth Redirect URIs** field, and click on the **Submit Changes** button. +6. In the same page, go to the **Scopes** section. +7. Select **Create/Modify Surveys**, **View Surveys**, **View Collectors**, **View Responses**, **Create/Modify Contacts**, **View Contacts**, **Create/Modify Webhooks**, **View Users** and **View Webhooks** scopes and click on the **Update Scopes** button. +8. Copy the **Client ID** value in the same page to the `Client ID` field on Automatisch. +9. Copy the **Secret** value in the same page to the `Client Secret` field on Automatisch. +10. Click **Submit** button on Automatisch. +11. Congrats! Start using your new SurveyMonkey connection within the flows. diff --git a/packages/docs/pages/public/favicons/surveymonkey.svg b/packages/docs/pages/public/favicons/surveymonkey.svg new file mode 100644 index 00000000..9268a24d --- /dev/null +++ b/packages/docs/pages/public/favicons/surveymonkey.svg @@ -0,0 +1 @@ +Goldie_Sabaeus_RGB \ No newline at end of file