Skip to content

Commit

Permalink
feat(post-connection-scripts): [nan-1901] add post connection scripts (
Browse files Browse the repository at this point in the history
…#2856)

## Describe your changes
Adds post connection scripts for gusto, docusign, calendly

## Issue ticket number and link
NAN-1901

## Checklist before requesting a review (skip if just adding/editing
APIs & templates)
- [ ] I added tests, otherwise the reason is: 
- [ ] I added observability, otherwise the reason is:
- [ ] I added analytics, otherwise the reason is:
  • Loading branch information
khaliqgant authored Oct 17, 2024
1 parent c3a7e64 commit e5d9a09
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/server/lib/hooks/connection/calendly-post-connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { InternalNango as Nango } from './post-connection.js';
import type { CalendlyUser } from '../response-types/calendly.js';
import { isAxiosError } from 'axios';

export default async function execute(nango: Nango) {
const connection = await nango.getConnection();

const response = await nango.proxy<CalendlyUser>({
endpoint: '/users/me',
connectionId: connection.connection_id,
providerConfigKey: connection.provider_config_key
});

if (isAxiosError(response)) {
return;
}

const { data } = response;

const { current_organization } = data.resource;

const organizationId = current_organization.split('/').pop();

await nango.updateConnectionConfig({ organizationId });
}
35 changes: 35 additions & 0 deletions packages/server/lib/hooks/connection/docusign-post-connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { InternalNango as Nango } from './post-connection.js';
import type { UserInfoResponse, AccountInfo } from '../response-types/docusign.js';
import { isAxiosError } from 'axios';

export default async function execute(nango: Nango) {
const connection = await nango.getConnection();

const rootUrl = connection.provider_config_key.includes('sandbox') ? 'account-d.docusign.com' : 'account.docusign.com';

const response = await nango.proxy<UserInfoResponse>({
baseUrlOverride: `https://${rootUrl}`,
endpoint: '/oauth/userinfo',
connectionId: connection.connection_id,
providerConfigKey: connection.provider_config_key
});

if (isAxiosError(response)) {
return;
}

if (response.data.accounts.length <= 0) {
return;
}

const defaultAccount = response.data.accounts.find((account: AccountInfo) => account.is_default);

if (!defaultAccount) {
return;
}

await nango.updateConnectionConfig({
baseUri: defaultAccount.base_uri,
accountId: defaultAccount.account_id
});
}
28 changes: 28 additions & 0 deletions packages/server/lib/hooks/connection/gusto-post-connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { InternalNango as Nango } from './post-connection.js';
import type { GustoTokenInfoResponse } from '../response-types/gusto.js';
import { isAxiosError } from 'axios';

export default async function execute(nango: Nango) {
const connection = await nango.getConnection();
const rootUrl = connection.provider_config_key.includes('sandbox') ? 'api.gusto-demo.com' : 'api.gusto.com';

const response = await nango.proxy<GustoTokenInfoResponse>({
baseUrlOverride: `https://${rootUrl}`,
endpoint: '/v1/token_info',
connectionId: connection.connection_id,
providerConfigKey: connection.provider_config_key
});

if (isAxiosError(response)) {
return;
}

const { data } = response;

const { resource } = data;

if (resource.type === 'Company') {
const { uuid } = resource;
await nango.updateConnectionConfig({ companyUuid: uuid });
}
}
3 changes: 3 additions & 0 deletions packages/server/lib/hooks/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ export { default as salesforcePostConnection } from './salesforce-post-connectio
export { default as checkrPostConnection } from './checkr-post-connection.js';
export { default as xeroPostConnection } from './xero-post-connection.js';
export { default as microsoftTeamsPostConnection } from './microsoft-teams-post-connection.js';
export { default as calendlyPostConnection } from './calendly-post-connection.js';
export { default as docusignPostConnection } from './docusign-post-connection.js';
export { default as gustoPostConnection } from './gusto-post-connection.js';
2 changes: 1 addition & 1 deletion packages/server/lib/hooks/connection/post-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const handlers: PostConnectionHandlersMap = postConnectionHandlers as unknown as

export interface InternalNango {
getConnection: () => Promise<Connection>;
proxy: ({ method, endpoint, data }: UserProvidedProxyConfiguration) => Promise<AxiosResponse | AxiosError>;
proxy: <T = any>({ method, endpoint, data }: UserProvidedProxyConfiguration) => Promise<AxiosResponse<T> | AxiosError>;
updateConnectionConfig: (config: ConnectionConfig) => Promise<ConnectionConfig>;
}

Expand Down
16 changes: 16 additions & 0 deletions packages/server/lib/hooks/response-types/calendly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface CalendlyUser {
resource: {
uri: string;
name: string;
slug: string;
email: string;
scheduling_url: string;
timezone: string;
avatar_url: string;
created_at: string;
updated_at: string;
current_organization: string;
resource_type: string;
locale: string;
};
}
27 changes: 27 additions & 0 deletions packages/server/lib/hooks/response-types/docusign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export interface UserInfoResponse {
sub: string;
name: string;
given_name: string;
family_name: string;
created: string;
email: string;
accounts: AccountInfo[];
}

export interface AccountInfo {
account_id: string;
is_default: boolean;
account_name: string;
base_uri: string;
organization: OrganizationInfo;
}

interface OrganizationInfo {
organization_id: string;
links: OrgLinkInfo[];
}

interface OrgLinkInfo {
rel: string;
href: string;
}
11 changes: 11 additions & 0 deletions packages/server/lib/hooks/response-types/gusto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface GustoTokenInfoResponse {
scope: string;
resource: {
type: string;
uuid: string;
};
resource_owner: {
type: string;
uuid: string;
};
}
5 changes: 5 additions & 0 deletions packages/shared/providers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ calendly:
at: 'X-Ratelimit-Reset'
token_response_metadata:
- owner
post_connection_script: calendlyPostConnection
docs: https://docs.nango.dev/integrations/all/calendly

canny:
Expand Down Expand Up @@ -1498,6 +1499,7 @@ docusign:
grant_type: refresh_token
proxy:
base_url: https://www.docusign.net
post_connection_script: docusignPostConnection
docs: https://docs.nango.dev/integrations/all/docusign

docusign-sandbox:
Expand All @@ -1514,6 +1516,7 @@ docusign-sandbox:
grant_type: refresh_token
proxy:
base_url: https://demo.docusign.net
post_connection_script: docusignPostConnection
docs: https://docs.nango.dev/integrations/all/docusign

dropbox:
Expand Down Expand Up @@ -2363,6 +2366,7 @@ gusto:
grant_type: refresh_token
proxy:
base_url: https://api.gusto.com
post_connection_script: gustoPostConnection
docs: https://docs.nango.dev/integrations/all/gusto

gusto-demo:
Expand All @@ -2378,6 +2382,7 @@ gusto-demo:
grant_type: refresh_token
proxy:
base_url: https://api.gusto-demo.com
post_connection_script: gustoPostConnection
docs: https://docs.nango.dev/integrations/all/gusto

hackerrank-work:
Expand Down

0 comments on commit e5d9a09

Please sign in to comment.