Minor improvements to CopperTrigger-Node

This commit is contained in:
Jan Oberhauser 2020-01-18 09:28:19 -06:00
parent 6808c51c0a
commit 6b45d79d36
3 changed files with 60 additions and 35 deletions

View file

@ -21,11 +21,5 @@ export class CopperApi implements ICredentialType {
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', default: '',
}, },
{
displayName: 'Secret',
name: 'secret',
type: 'string' as NodePropertyTypes,
default: '',
},
]; ];
} }

View file

@ -12,12 +12,13 @@ import {
import { import {
copperApiRequest, copperApiRequest,
getAutomaticSecret,
} from './GenericFunctions'; } from './GenericFunctions';
export class CopperTrigger implements INodeType { export class CopperTrigger implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Copper Trigger', displayName: 'Copper Trigger',
name: 'copper', name: 'copperTrigger',
icon: 'file:copper.png', icon: 'file:copper.png',
group: ['trigger'], group: ['trigger'],
version: 1, version: 1,
@ -50,28 +51,32 @@ export class CopperTrigger implements INodeType {
required: true, required: true,
default: '', default: '',
options: [ options: [
{
name: 'Lead',
value: 'lead',
},
{
name: 'Person',
value: 'person',
},
{ {
name: 'Company', name: 'Company',
value: 'company', value: 'company',
}, },
{
name: 'Lead',
value: 'lead',
},
{ {
name: 'Opportunity', name: 'Opportunity',
value: 'opportunity', value: 'opportunity',
}, },
{
name: 'Person',
value: 'person',
},
{ {
name: 'Project', name: 'Project',
value: 'project', value: 'project',
}, },
{
name: 'Task',
value: 'task',
},
], ],
description: 'The resource is gonna fire the event', description: 'The resource which will fire the event.',
}, },
{ {
displayName: 'Event', displayName: 'Event',
@ -80,6 +85,11 @@ export class CopperTrigger implements INodeType {
required: true, required: true,
default: '', default: '',
options: [ options: [
{
name: 'Delete',
value: 'delete',
description: 'An existing record is removed',
},
{ {
name: 'New', name: 'New',
value: 'new', value: 'new',
@ -90,13 +100,8 @@ export class CopperTrigger implements INodeType {
value: 'update', value: 'update',
description: 'Any field in the existing entity record is changed', description: 'Any field in the existing entity record is changed',
}, },
{
name: 'Delete',
value: 'delete',
description: 'An existing record is removed',
},
], ],
description: 'The resource is gonna fire the event', description: 'The event to listen to.',
}, },
], ],
}; };
@ -112,7 +117,7 @@ export class CopperTrigger implements INodeType {
try { try {
await copperApiRequest.call(this, 'GET', endpoint); await copperApiRequest.call(this, 'GET', endpoint);
} catch (err) { } catch (err) {
return false return false;
} }
return true; return true;
}, },
@ -125,8 +130,14 @@ export class CopperTrigger implements INodeType {
const body: IDataObject = { const body: IDataObject = {
target: webhookUrl, target: webhookUrl,
type: resource, type: resource,
event: event, event,
}; };
const credentials = this.getCredentials('copperApi');
body.secret = {
secret: getAutomaticSecret(credentials!),
};
const { id } = await copperApiRequest.call(this, 'POST', endpoint, body); const { id } = await copperApiRequest.call(this, 'POST', endpoint, body);
webhookData.webhookId = id; webhookData.webhookId = id;
return true; return true;
@ -148,11 +159,12 @@ export class CopperTrigger implements INodeType {
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> { async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const credentials = this.getCredentials('copperApi'); const credentials = this.getCredentials('copperApi');
const req = this.getRequestObject(); const req = this.getRequestObject();
if (credentials!.secret) {
if (req.body.secret !== credentials!.secret) { // Check if the supplied secret matches. If not ignore request.
if (req.body.secret !== getAutomaticSecret(credentials!)) {
return {}; return {};
};
} }
return { return {
workflowData: [ workflowData: [
this.helpers.returnJsonArray(req.body), this.helpers.returnJsonArray(req.body),

View file

@ -1,4 +1,6 @@
import { createHash } from 'crypto';
import { OptionsWithUri } from 'request'; import { OptionsWithUri } from 'request';
import { import {
IExecuteFunctions, IExecuteFunctions,
IExecuteSingleFunctions, IExecuteSingleFunctions,
@ -6,18 +8,17 @@ import {
ILoadOptionsFunctions, ILoadOptionsFunctions,
IWebhookFunctions, IWebhookFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { IDataObject } from 'n8n-workflow'; import {
ICredentialDataDecryptedObject,
IDataObject,
} from 'n8n-workflow';
export async function copperApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any export async function copperApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('copperApi'); const credentials = this.getCredentials('copperApi');
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} }
if (credentials.secret) {
body.secret = {
secret: credentials.secret as string,
};
};
let options: OptionsWithUri = { let options: OptionsWithUri = {
headers: { headers: {
'X-PW-AccessToken': credentials.apiKey, 'X-PW-AccessToken': credentials.apiKey,
@ -39,6 +40,24 @@ export async function copperApiRequest(this: IHookFunctions | IExecuteFunctions
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
throw new Error('Copper Error: ' + error.message); let errorMessage = error.message;
if (error.response.body && error.response.body.message) {
errorMessage = error.response.body.message;
}
throw new Error('Copper Error: ' + errorMessage);
} }
} }
/**
* Creates a secret from the credentials
*
* @export
* @param {ICredentialDataDecryptedObject} credentials
* @returns
*/
export function getAutomaticSecret(credentials: ICredentialDataDecryptedObject) {
const data = `${credentials.email},${credentials.apiKey}`;
return createHash('md5').update(data).digest("hex");
}