n8n/packages/nodes-base/nodes/Salesforce/GenericFunctions.ts

244 lines
6.4 KiB
TypeScript
Raw Normal View History

import type { OptionsWithUri } from 'request';
import type { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
import type { IDataObject, INodePropertyOptions } from 'n8n-workflow';
import { LoggerProxy as Logger, NodeApiError } from 'n8n-workflow';
2020-02-10 12:55:28 -08:00
import moment from 'moment-timezone';
import jwt from 'jsonwebtoken';
function getOptions(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: any,
qs: IDataObject,
instanceUrl: string,
): OptionsWithUri {
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
},
method,
body,
qs,
uri: `${instanceUrl}/services/data/v39.0${endpoint}`,
json: true,
};
if (!Object.keys(options.body).length) {
delete options.body;
}
//@ts-ignore
return options;
}
async function getAccessToken(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
credentials: IDataObject,
): Promise<IDataObject> {
const now = moment().unix();
const authUrl =
credentials.environment === 'sandbox'
? 'https://test.salesforce.com'
: 'https://login.salesforce.com';
const signature = jwt.sign(
{
iss: credentials.clientId as string,
sub: credentials.username as string,
aud: authUrl,
exp: now + 3 * 60,
},
credentials.privateKey as string,
{
algorithm: 'RS256',
header: {
alg: 'RS256',
},
},
);
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method: 'POST',
form: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: signature,
},
uri: `${authUrl}/services/oauth2/token`,
json: true,
};
return this.helpers.request(options);
}
export async function salesforceApiRequest(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: any = {},
qs: IDataObject = {},
uri?: string,
option: IDataObject = {},
): Promise<any> {
const authenticationMethod = this.getNodeParameter('authentication', 0, 'oAuth2') as string;
2020-02-10 12:55:28 -08:00
try {
if (authenticationMethod === 'jwt') {
// https://help.salesforce.com/articleView?id=remoteaccess_oauth_jwt_flow.htm&type=5
const credentialsType = 'salesforceJwtApi';
const credentials = await this.getCredentials(credentialsType);
const response = await getAccessToken.call(this, credentials);
const { instance_url, access_token } = response;
const options = getOptions.call(
this,
method,
uri || endpoint,
body,
qs,
instance_url as string,
);
Logger.debug(
`Authentication for "Salesforce" node is using "jwt". Invoking URI ${options.uri}`,
);
options.headers!.Authorization = `Bearer ${access_token}`;
Object.assign(options, option);
//@ts-ignore
return await this.helpers.request(options);
} else {
// https://help.salesforce.com/articleView?id=remoteaccess_oauth_web_server_flow.htm&type=5
const credentialsType = 'salesforceOAuth2Api';
const credentials = (await this.getCredentials(credentialsType)) as {
oauthTokenData: { instance_url: string };
};
const options = getOptions.call(
this,
method,
uri || endpoint,
body,
qs,
credentials.oauthTokenData.instance_url,
);
Logger.debug(
`Authentication for "Salesforce" node is using "OAuth2". Invoking URI ${options.uri}`,
);
Object.assign(options, option);
//@ts-ignore
return await this.helpers.requestOAuth2.call(this, credentialsType, options);
}
2020-02-10 12:55:28 -08:00
} catch (error) {
:sparkles: Improve node error handling (#1309) * Add path mapping and response error interfaces * Add error handling and throwing functionality * Refactor error handling into a single function * Re-implement error handling in Hacker News node * Fix linting details * Re-implement error handling in Spotify node * Re-implement error handling in G Suite Admin node * :construction: create basic setup NodeError * :construction: add httpCodes * :construction: add path priolist * :construction: handle statusCode in error, adjust interfaces * :construction: fixing type issues w/Ivan * :construction: add error exploration * 👔 fix linter issues * :wrench: improve object check * :construction: remove path passing from NodeApiError * :construction: add multi error + refactor findProperty method * 👔 allow any * :wrench: handle multi error message callback * :zap: change return type of callback * :zap: add customCallback to MultiError * :construction: refactor to use INode * :hammer: handle arrays, continue search after first null property found * 🚫 refactor method access * :construction: setup NodeErrorView * :zap: change timestamp to Date.now * :books: Add documentation for methods and constants * :construction: change message setting * 🚚 move NodeErrors to workflow * :sparkles: add new ErrorView for Nodes * :art: improve error notification * :art: refactor interfaces * :zap: add WorkflowOperationError, refactor error throwing * 👕 fix linter issues * :art: rename param * :bug: fix handling normal errors * :zap: add usage of NodeApiError * :art: fix throw new error instead of constructor * :art: remove unnecessary code/comments * :art: adjusted spacing + updated status messages * :art: fix tab indentation * ✨ Replace current errors with custom errors (#1576) * :zap: Introduce NodeApiError in catch blocks * :zap: Introduce NodeOperationError in nodes * :zap: Add missing errors and remove incompatible * :zap: Fix NodeOperationError in incompatible nodes * :wrench: Adjust error handling in missed nodes PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes * :hammer: Adjust Strava Trigger node error handling * :hammer: Adjust AWS nodes error handling * :hammer: Remove duplicate instantiation of NodeApiError * :bug: fix strava trigger node error handling * Add XML parsing to NodeApiError constructor (#1633) * :bug: Remove type annotation from catch variable * :sparkles: Add XML parsing to NodeApiError * :zap: Simplify error handling in Rekognition node * :zap: Pass in XML flag in generic functions * :fire: Remove try/catch wrappers at call sites * :hammer: Refactor setting description from XML * :hammer: Refactor let to const in resource loaders * :zap: Find property in parsed XML * :zap: Change let to const * :fire: Remove unneeded try/catch block * :shirt: Fix linting issues * :bug: Fix errors from merge conflict resolution * :zap: Add custom errors to latest contributions * :shirt: Fix linting issues * :zap: Refactor MongoDB helpers for custom errors * :bug: Correct custom error type * :zap: Apply feedback to A nodes * :zap: Apply feedback to missed A node * :zap: Apply feedback to B-D nodes * :zap: Apply feedback to E-F nodes * :zap: Apply feedback to G nodes * :zap: Apply feedback to H-L nodes * :zap: Apply feedback to M nodes * :zap: Apply feedback to P nodes * :zap: Apply feedback to R nodes * :zap: Apply feedback to S nodes * :zap: Apply feedback to T nodes * :zap: Apply feedback to V-Z nodes * :zap: Add HTTP code to iterable node error * :hammer: Standardize e as error * :hammer: Standardize err as error * :zap: Fix error handling for non-standard nodes Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com> Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
2021-04-16 09:33:36 -07:00
throw new NodeApiError(this.getNode(), error);
2020-02-10 12:55:28 -08:00
}
}
export async function salesforceApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions,
propertyName: string,
method: string,
endpoint: string,
body: any = {},
query: IDataObject = {},
): Promise<any> {
2020-02-10 12:55:28 -08:00
const returnData: IDataObject[] = [];
let responseData;
let uri: string | undefined;
do {
responseData = await salesforceApiRequest.call(this, method, endpoint, body, query, uri);
uri = `${endpoint}/${responseData.nextRecordsUrl?.split('/')?.pop()}`;
2020-02-10 12:55:28 -08:00
returnData.push.apply(returnData, responseData[propertyName]);
} while (responseData.nextRecordsUrl !== undefined && responseData.nextRecordsUrl !== null);
2020-02-10 12:55:28 -08:00
return returnData;
}
2020-10-16 02:13:04 -07:00
/**
* Sorts the given options alphabetically
*
*/
export function sortOptions(options: INodePropertyOptions[]): void {
options.sort((a, b) => {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
2020-10-16 02:13:04 -07:00
return 0;
});
}
export function getValue(value: any) {
if (moment(value).isValid()) {
return value;
} else if (typeof value === 'string') {
return `'${value}'`;
} else {
return value;
}
}
export function getConditions(options: IDataObject) {
const conditions = (options.conditionsUi as IDataObject)?.conditionValues as IDataObject[];
let data = undefined;
if (Array.isArray(conditions) && conditions.length !== 0) {
data = conditions.map(
(condition: IDataObject) =>
`${condition.field} ${
condition.operation === 'equal' ? '=' : condition.operation
} ${getValue(condition.value)}`,
);
data = `WHERE ${data.join(' AND ')}`;
}
return data;
}
export function getDefaultFields(sobject: string) {
return (
{
Account: 'id,name,type',
Lead: 'id,company,firstname,lastname,street,postalCode,city,email,status',
Contact: 'id,firstname,lastname,email',
Opportunity: 'id,accountId,amount,probability,type',
Case: 'id,accountId,contactId,priority,status,subject,type',
Task: 'id,subject,status,priority',
Attachment: 'id,name',
User: 'id,name,email',
} as IDataObject
)[sobject];
}
export function getQuery(options: IDataObject, sobject: string, returnAll: boolean, limit = 0) {
const fields: string[] = [];
if (options.fields) {
// options.fields is comma separated in standard Salesforce objects and array in custom Salesforce objects -- handle both cases
if (typeof options.fields === 'string') {
fields.push.apply(fields, options.fields.split(','));
} else {
fields.push.apply(fields, options.fields as string[]);
}
} else {
fields.push.apply(fields, ((getDefaultFields(sobject) as string) || 'id').split(','));
}
const conditions = getConditions(options);
let query = `SELECT ${fields.join(',')} FROM ${sobject} ${conditions ? conditions : ''}`;
if (!returnAll) {
query = `SELECT ${fields.join(',')} FROM ${sobject} ${
conditions ? conditions : ''
} LIMIT ${limit}`;
}
return query;
}