diff --git a/packages/backend/src/apps/hubspot/actions/create-contact/index.ts b/packages/backend/src/apps/hubspot/actions/create-contact/index.ts index 8c6c6317..4ca0ef67 100644 --- a/packages/backend/src/apps/hubspot/actions/create-contact/index.ts +++ b/packages/backend/src/apps/hubspot/actions/create-contact/index.ts @@ -64,28 +64,25 @@ export default defineAction({ ], async run($) { - const company = $.step.parameters.company as string || undefined; - const email = $.step.parameters.email as string || undefined; - const firstname = $.step.parameters.firstname as string || undefined; - const lastname = $.step.parameters.lastname as string || undefined; - const phone = $.step.parameters.phone as string || undefined; - const website = $.step.parameters.website as string || undefined; - const hubspot_owner_id = $.step.parameters.hubspot_owner_id as number || undefined; + const company = $.step.parameters.company as string; + const email = $.step.parameters.email as string; + const firstname = $.step.parameters.firstname as string; + const lastname = $.step.parameters.lastname as string; + const phone = $.step.parameters.phone as string; + const website = $.step.parameters.website as string; + const hubspot_owner_id = $.step.parameters.hubspot_owner_id as string; - const response = await $.http.post( - `crm/v3/objects/contacts`, - { - properties: { - company, - email, - firstname, - lastname, - phone, - website, - hubspot_owner_id, - } - } - ); + const response = await $.http.post(`crm/v3/objects/contacts`, { + properties: { + company, + email, + firstname, + lastname, + phone, + website, + hubspot_owner_id, + }, + }); $.setActionItem({ raw: response.data }); }, diff --git a/packages/backend/src/apps/hubspot/auth/generate-auth-url.ts b/packages/backend/src/apps/hubspot/auth/generate-auth-url.ts index 33ecbb09..d8f33dd4 100644 --- a/packages/backend/src/apps/hubspot/auth/generate-auth-url.ts +++ b/packages/backend/src/apps/hubspot/auth/generate-auth-url.ts @@ -1,7 +1,6 @@ import { IField, IGlobalVariable } from '@automatisch/types'; import { URLSearchParams } from 'url'; - -const scopes = ['crm.objects.contacts.read', 'crm.objects.contacts.write']; +import scopes from '../common/scopes'; export default async function generateAuthUrl($: IGlobalVariable) { const oauthRedirectUrlField = $.app.auth.fields.find( diff --git a/packages/backend/src/apps/hubspot/auth/index.ts b/packages/backend/src/apps/hubspot/auth/index.ts index a5509677..c7f57dbf 100644 --- a/packages/backend/src/apps/hubspot/auth/index.ts +++ b/packages/backend/src/apps/hubspot/auth/index.ts @@ -1,6 +1,7 @@ import generateAuthUrl from './generate-auth-url'; import verifyCredentials from './verify-credentials'; import isStillVerified from './is-still-verified'; +import refreshToken from './refresh-token'; export default { fields: [ @@ -43,4 +44,5 @@ export default { generateAuthUrl, verifyCredentials, isStillVerified, + refreshToken, }; diff --git a/packages/backend/src/apps/hubspot/auth/is-still-verified.ts b/packages/backend/src/apps/hubspot/auth/is-still-verified.ts index 4470a643..cccd73be 100644 --- a/packages/backend/src/apps/hubspot/auth/is-still-verified.ts +++ b/packages/backend/src/apps/hubspot/auth/is-still-verified.ts @@ -1,8 +1,9 @@ import { IGlobalVariable } from '@automatisch/types'; -import verifyCredentials from "./verify-credentials"; +import getAccessTokenInfo from '../common/get-access-token-info'; const isStillVerified = async ($: IGlobalVariable) => { - await verifyCredentials($); + await getAccessTokenInfo($); + return true; }; diff --git a/packages/backend/src/apps/hubspot/auth/refresh-token.ts b/packages/backend/src/apps/hubspot/auth/refresh-token.ts new file mode 100644 index 00000000..c1fc8a9d --- /dev/null +++ b/packages/backend/src/apps/hubspot/auth/refresh-token.ts @@ -0,0 +1,28 @@ +import { IGlobalVariable, IField } from '@automatisch/types'; +import { URLSearchParams } from 'url'; + +const refreshToken = async ($: IGlobalVariable) => { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' + ); + + const callbackUrl = oauthRedirectUrlField.value as string; + + const params = new URLSearchParams({ + grant_type: 'refresh_token', + client_id: $.auth.data.clientId as string, + client_secret: $.auth.data.clientSecret as string, + redirect_uri: callbackUrl, + refresh_token: $.auth.data.refreshToken as string, + }); + + const { data } = await $.http.post('/oauth/v1/token', params.toString()); + + await $.auth.set({ + accessToken: data.access_token, + expiresIn: data.expires_in, + refreshToken: data.refresh_token, + }); +}; + +export default refreshToken; diff --git a/packages/backend/src/apps/hubspot/auth/verify-credentials.ts b/packages/backend/src/apps/hubspot/auth/verify-credentials.ts index cff860ba..130bf171 100644 --- a/packages/backend/src/apps/hubspot/auth/verify-credentials.ts +++ b/packages/backend/src/apps/hubspot/auth/verify-credentials.ts @@ -1,11 +1,51 @@ -import { IGlobalVariable } from '@automatisch/types'; +import { IGlobalVariable, IField } from '@automatisch/types'; +import { URLSearchParams } from 'url'; +import getAccessTokenInfo from '../common/get-access-token-info'; const verifyCredentials = async ($: IGlobalVariable) => { - await $.http.get( - `/crm/v3/objects/contacts?limit=1`, + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' ); + const callbackUrl = oauthRedirectUrlField.value as string; + const params = new URLSearchParams({ + grant_type: 'authorization_code', + client_id: $.auth.data.clientId as string, + client_secret: $.auth.data.clientSecret as string, + redirect_uri: callbackUrl, + code: $.auth.data.code as string, + }); + + const { data: verifiedCredentials } = await $.http.post( + '/oauth/v1/token', + params.toString() + ); + + const { + access_token: accessToken, + refresh_token: refreshToken, + expires_in: expiresIn, + } = verifiedCredentials; + await $.auth.set({ - screenName: $.auth.data?.displayName, + accessToken, + refreshToken, + expiresIn, + }); + + const accessTokenInfo = await getAccessTokenInfo($); + + await $.auth.set({ + screenName: accessTokenInfo.user, + hubDomain: accessTokenInfo.hub_domain, + scopes: accessTokenInfo.scopes, + scopeToScopeGroupPks: accessTokenInfo.scope_to_scope_group_pks, + trialScopes: accessTokenInfo.trial_scopes, + trialScopeToScoreGroupPks: accessTokenInfo.trial_scope_to_scope_group_pks, + hubId: accessTokenInfo.hub_id, + appId: accessTokenInfo.app_id, + userId: accessTokenInfo.user_id, + expiresIn: accessTokenInfo.expires_in, + tokenType: accessTokenInfo.token_type, }); }; diff --git a/packages/backend/src/apps/hubspot/common/get-access-token-info.ts b/packages/backend/src/apps/hubspot/common/get-access-token-info.ts new file mode 100644 index 00000000..55ed8ff2 --- /dev/null +++ b/packages/backend/src/apps/hubspot/common/get-access-token-info.ts @@ -0,0 +1,11 @@ +import { IGlobalVariable, IJSONObject } from '@automatisch/types'; + +const getAccessTokenInfo = async ($: IGlobalVariable): Promise => { + const response = await $.http.get( + `/oauth/v1/access-tokens/${$.auth.data.accessToken}` + ); + + return response.data; +}; + +export default getAccessTokenInfo; diff --git a/packages/backend/src/apps/hubspot/common/scopes.ts b/packages/backend/src/apps/hubspot/common/scopes.ts new file mode 100644 index 00000000..38cb30a3 --- /dev/null +++ b/packages/backend/src/apps/hubspot/common/scopes.ts @@ -0,0 +1,3 @@ +const scopes = ['crm.objects.contacts.read', 'crm.objects.contacts.write']; + +export default scopes;