feat(pipedrive): add pipedrive integration

This commit is contained in:
Rıdvan Akca
2023-09-21 16:25:36 +03:00
committed by Faruk AYDIN
parent 6a66b65f2a
commit f53909355f
10 changed files with 201 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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