This commit is contained in:
Ricardo Espinoza 2020-01-05 21:32:22 -05:00
parent 7d2e857613
commit f92a42dfe1
2 changed files with 51 additions and 60 deletions

View file

@ -18,47 +18,20 @@ export async function zendeskApiRequest(this: IHookFunctions | IExecuteFunctions
method,
qs,
body,
uri: uri ||`${credentials.domain}/api/v2${resource}`,
uri: uri ||`${credentials.domain}/api/v2${resource}.json`,
json: true
};
options = Object.assign({}, options, option);
if (Object.keys(options.body).length === 0) {
delete options.body;
}
try {
return await this.helpers.request!(options);
} catch (error) {
let errorMessage = error.message;
if (error.response.body) {
errorMessage = error.response.body.message || error.response.body.Message || error.message;
} catch (err) {
let errorMessage = '';
if (err.error && err.description) {
errorMessage = err.description;
}
throw new Error(errorMessage);
}
}
/**
* Make an API request to paginated flow endpoint
* and return all results
*/
export async function zendeskApiRequestAllItems(this: IHookFunctions | IExecuteFunctions| ILoadOptionsFunctions, propertyName: string, method: string, resource: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
let uri: string | undefined;
do {
responseData = await zendeskApiRequest.call(this, method, resource, body, query, uri);
query.continuation = responseData.pagination.continuation;
returnData.push.apply(returnData, responseData[propertyName]);
} while (
responseData.pagination !== undefined &&
responseData.pagination.has_more_items !== undefined &&
responseData.pagination.has_more_items !== false
);
return returnData;
}

View file

@ -7,6 +7,7 @@ import {
INodeTypeDescription,
INodeType,
IWebhookResponseData,
IDataObject,
} from 'n8n-workflow';
import {
@ -59,7 +60,7 @@ export class ZendeskTrigger implements INodeType {
{
displayName: 'Events',
name: 'events',
type: 'multiOptions',
type: 'options',
displayOptions: {
show: {
service: [
@ -69,12 +70,12 @@ export class ZendeskTrigger implements INodeType {
},
options: [
{
name: 'ticket.status.open',
value: 'ticket.status.open'
name: 'ticket.created',
value: 'ticket.created',
},
],
required: true,
default: [],
default: '',
description: '',
},
],
@ -84,55 +85,72 @@ export class ZendeskTrigger implements INodeType {
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
let webhooks;
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId === undefined) {
return false;
}
const endpoint = `/webhooks/${webhookData.webhookId}/`;
const endpoint = `/triggers/${webhookData.webhookId}`;
try {
webhooks = await zendeskApiRequest.call(this, 'GET', endpoint);
await zendeskApiRequest.call(this, 'GET', endpoint);
} catch (e) {
return false;
}
return true;
},
async create(this: IHookFunctions): Promise<boolean> {
let body, responseData;
let condition: IDataObject = {};
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const event = this.getNodeParameter('event') as string;
const actions = this.getNodeParameter('actions') as string[];
const endpoint = `/webhooks/`;
// @ts-ignore
body = {
endpoint_url: webhookUrl,
actions: actions.join(','),
event_id: event,
};
try {
responseData = await zendeskApiRequest.call(this, 'POST', endpoint, body);
} catch(error) {
console.log(error)
return false;
if (event === 'ticket.created') {
condition = {
all: [
{
field: 'status',
value: 'open',
},
],
}
}
const bodyTrigger: IDataObject = {
trigger: {
conditions: { ...condition },
actions: [
{
field: 'notification_target',
value: [],
}
]
},
}
const bodyTarget: IDataObject = {
target: {
title: 'N8N webhook',
type: 'http_target',
target_url: webhookUrl,
method: 'POST',
active: true,
content_type: 'application/json',
},
}
const { target } = await zendeskApiRequest.call(this, 'POST', '/targets', bodyTarget);
// @ts-ignore
webhookData.webhookId = responseData.id;
bodyTrigger.trigger.actions[0].value = [target.id, ''];
const { trigger } = await zendeskApiRequest.call(this, 'POST', '/triggers', bodyTrigger);
webhookData.webhookId = trigger.id;
webhookData.targetId = target.id;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
let responseData;
const webhookData = this.getWorkflowStaticData('node');
const endpoint = `/webhooks/${webhookData.webhookId}/`;
try {
responseData = await zendeskApiRequest.call(this, 'DELETE', endpoint);
await zendeskApiRequest.call(this, 'DELETE', `/triggers/${webhookData.webhookId}`);
await zendeskApiRequest.call(this, 'DELETE', `/targets/${webhookData.targetId}`);
} catch(error) {
return false;
}
if (!responseData.success) {
return false;
}
delete webhookData.webhookId;
delete webhookData.targetId
return true;
},
},