Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 4 additions & 22 deletions packages/v1-ready/zoho-crm/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const LOCATION_CONFIG: Record<ZohoLocation, { accounts: string; api: string }> =
in: { accounts: 'https://accounts.zoho.in', api: 'https://www.zohoapis.in' },
au: { accounts: 'https://accounts.zoho.com.au', api: 'https://www.zohoapis.com.au' },
cn: { accounts: 'https://accounts.zoho.com.cn', api: 'https://www.zohoapis.com.cn' },
ca: { accounts: 'https://accounts.zoho.ca', api: 'https://www.zohoapis.ca' },
ca: { accounts: 'https://accounts.zohocloud.ca', api: 'https://www.zohoapis.ca' },
jp: { accounts: 'https://accounts.zoho.jp', api: 'https://www.zohoapis.jp' },
sa: { accounts: 'https://accounts.zoho.sa', api: 'https://www.zohoapis.sa' },
};
Expand All @@ -46,7 +46,6 @@ const DEFAULT_LOCATION: ZohoLocation = 'us';
export class Api extends OAuth2Requester {
public URLs: Record<string, string | ((id: string) => string)>;
public location: ZohoLocation;
public accountsServer: string | null;

private static readonly CONTACTS_DEFAULT_FIELDS = 'id,First_Name,Last_Name,Email,Phone,Mobile,Account_Name,Company,Owner,Lead_Source,Created_Time,Modified_Time';
private static readonly LEADS_DEFAULT_FIELDS = 'id,First_Name,Last_Name,Email,Phone,Mobile,Company,Industry,Lead_Source,Lead_Status,Owner,Created_Time,Modified_Time,Converted__s,Converted_Date_Time';
Expand All @@ -55,17 +54,14 @@ export class Api extends OAuth2Requester {
constructor(params: ZohoConfig) {
super(params);

this.accountsServer = get(params, 'accountsServer', null) as string | null;
this.location = get(params, 'location', DEFAULT_LOCATION) as ZohoLocation;
if (!LOCATION_CONFIG[this.location]) {
this.location = DEFAULT_LOCATION;
}
const locationConfig = LOCATION_CONFIG[this.location];

this.baseUrl = `${locationConfig.api}/crm/v8`;
this.tokenUri = this.accountsServer
? `${this.accountsServer}/oauth/v2/token`
: `${locationConfig.accounts}/oauth/v2/token`;
this.tokenUri = `${locationConfig.accounts}/oauth/v2/token`;
this.authorizationUri = encodeURI(
`${locationConfig.accounts}/oauth/v2/auth?scope=${this.scope}&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&response_type=code&access_type=offline`
);
Expand Down Expand Up @@ -97,10 +93,6 @@ export class Api extends OAuth2Requester {
return this.authorizationUri;
}

/**
* Sets the datacenter location and updates URLs accordingly.
* Note: tokenUri is only updated if accountsServer is not set.
*/
setLocation(location: ZohoLocation): void {
if (!LOCATION_CONFIG[location]) {
throw new Error(
Expand All @@ -111,23 +103,12 @@ export class Api extends OAuth2Requester {
this.location = location;
const locationConfig = LOCATION_CONFIG[location];
this.baseUrl = `${locationConfig.api}/crm/v8`;
if (!this.accountsServer) {
this.tokenUri = `${locationConfig.accounts}/oauth/v2/token`;
}
this.tokenUri = `${locationConfig.accounts}/oauth/v2/token`;
this.authorizationUri = encodeURI(
`${locationConfig.accounts}/oauth/v2/auth?scope=${this.scope}&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&response_type=code&access_type=offline`
);
}

/**
* Sets the accounts server URL for token operations.
* Call this when accounts-server is provided in OAuth callback.
*/
setAccountsServer(accountsServer: string): void {
this.accountsServer = accountsServer;
this.tokenUri = `${accountsServer}/oauth/v2/token`;
}

async getTokenFromCode(code: string): Promise<TokenResponse> {
const formData = new FormData();
formData.append('grant_type', 'authorization_code');
Expand All @@ -141,6 +122,7 @@ export class Api extends OAuth2Requester {
headers: (formData as any).getHeaders(),
url: this.tokenUri,
};

const response = await this._post(options, false);
await this.setTokens(response);
return response;
Expand Down
7 changes: 1 addition & 6 deletions packages/v1-ready/zoho-crm/src/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ export const Definition = {
getToken: async function(api: Api, params: any): Promise<void> {
const code = get(params, 'code');
const location = get(params, 'location', null) as ZohoLocation | null;
const accountsServer = get(params, 'accounts-server', null) as string | null;

if (location) {
api.setLocation(location);
}
if (accountsServer) {
api.setAccountsServer(accountsServer);
}

await api.getTokenFromCode(code);
},
apiPropertiesToPersist: {
credential: ['access_token', 'refresh_token'],
entity: ['location', 'accountsServer'],
entity: ['location'],
},
getCredentialDetails: async function (api: Api, userId: string): Promise<any> {
const response = await api.listUsers({type: 'CurrentUser'});
Expand All @@ -46,7 +42,6 @@ export const Definition = {
details: {
name: currentUser.email,
location: api.location,
accountsServer: api.accountsServer,
},
};
},
Expand Down
1 change: 0 additions & 1 deletion packages/v1-ready/zoho-crm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export interface ZohoConfig {
access_token?: string | null;
refresh_token?: string | null;
location?: ZohoLocation;
accountsServer?: string | null;
}

export interface PaginationInfo {
Expand Down
Loading