Skip to content

Commit

Permalink
feat(Freshworks CRM Node): Add Search + Lookup functionality (#3131)
Browse files Browse the repository at this point in the history
* Add fields and Ops for Lookup Search

* Adds Search (Search + Lookup) operations

* 🔨 credentials update

* 🔨 improvements

* ⚡ clean up and linter fixes

* ⚡ merged search and query, more hints

* ⚡ Improvements

* ⚡ Add generic type to authentication method

Co-authored-by: Michael Kret <[email protected]>
Co-authored-by: ricardo <[email protected]>
  • Loading branch information
3 people authored Jul 10, 2022
1 parent af45a07 commit dbc0280
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 5 deletions.
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

0 comments on commit dbc0280

Please sign in to comment.