Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Freshworks CRM] Add Search + Lookup functionality - N8N-3413 #3131

Merged
merged 11 commits into from
Jul 10, 2022
17 changes: 17 additions & 0 deletions packages/nodes-base/credentials/FreshworksCrmApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
Expand All @@ -24,4 +26,19 @@ export class FreshworksCrmApi implements ICredentialType {
description: 'Domain in the Freshworks CRM org URL. For example, in <code>https://n8n-org.myfreshworks.com</code>, the domain is <code>n8n-org</code>.',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'Authorization': '=Token token={{$credentials?.apiKey}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '=https://{{$credentials?.domain}}.myfreshworks.com/crm/sales/api',
url: '/tasks',
method: 'GET',
},
};
}
60 changes: 60 additions & 0 deletions packages/nodes-base/nodes/FreshworksCrm/FreshworksCrm.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
noteOperations,
salesActivityFields,
salesActivityOperations,
searchFields,
searchOperations,
taskFields,
taskOperations,
} from './descriptions';
Expand Down Expand Up @@ -100,6 +102,10 @@ export class FreshworksCrm implements INodeType {
name: 'Sales Activity',
value: 'salesActivity',
},
{
name: 'Search',
value: 'search',
},
{
name: 'Task',
value: 'task',
Expand All @@ -119,6 +125,8 @@ export class FreshworksCrm implements INodeType {
...noteFields,
...salesActivityOperations,
...salesActivityFields,
...searchOperations,
...searchFields,
...taskOperations,
...taskFields,
],
Expand Down Expand Up @@ -855,6 +863,58 @@ export class FreshworksCrm implements INodeType {

}

} else if (resource === 'search') {

// **********************************************************************
// search
// **********************************************************************

if (operation === 'query') {
// https://developers.freshworks.com/crm/api/#search
const query = this.getNodeParameter('query', i) as string;
let entities = this.getNodeParameter('entities', i);
const returnAll = this.getNodeParameter('returnAll', 0, false);

if (Array.isArray(entities)) {
entities = entities.join(',');
}

const qs: IDataObject = {
q: query,
include: entities,
per_page: 100,
};

responseData = await freshworksCrmApiRequest.call(this, 'GET', '/search', {}, qs);

if (!returnAll) {
const limit = this.getNodeParameter('limit', 0);
responseData = responseData.slice(0, limit);
}
}

if (operation === 'lookup') {
// https://developers.freshworks.com/crm/api/#lookup_search
let searchField = this.getNodeParameter('searchField', i) as string;
let fieldValue = this.getNodeParameter('fieldValue', i, '') as string;
let entities = this.getNodeParameter('options.entities', i) as string;
if (Array.isArray(entities)) {
entities = entities.join(',');
}

if (searchField === 'customField') {
searchField = this.getNodeParameter('customFieldName', i) as string;
fieldValue = this.getNodeParameter('customFieldValue', i) as string;
}

const qs: IDataObject = {
q: fieldValue,
f: searchField,
entities,
};

responseData = await freshworksCrmApiRequest.call(this, 'GET', '/lookup', {}, qs);
}
} else if (resource === 'task') {

// **********************************************************************
Expand Down
8 changes: 3 additions & 5 deletions packages/nodes-base/nodes/FreshworksCrm/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ export async function freshworksCrmApiRequest(
body: IDataObject = {},
qs: IDataObject = {},
) {
const { apiKey, domain } = await this.getCredentials('freshworksCrmApi') as FreshworksCrmApiCredentials;
const { domain } = await this.getCredentials('freshworksCrmApi') as FreshworksCrmApiCredentials;

const options: OptionsWithUri = {
headers: {
Authorization: `Token token=${apiKey}`,
},
method,
body,
qs,
Expand All @@ -52,7 +49,8 @@ export async function freshworksCrmApiRequest(
delete options.qs;
}
try {
return await this.helpers.request!(options);
const credentialsType = 'freshworksCrmApi';
return await this.helpers.requestWithAuthentication.call(this, credentialsType, options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
Expand Down
Loading