mirror of
https://github.com/n8n-io/n8n.git
synced 2024-09-20 14:57:31 -07:00
done
This commit is contained in:
parent
7d2e857613
commit
f92a42dfe1
|
@ -18,47 +18,20 @@ export async function zendeskApiRequest(this: IHookFunctions | IExecuteFunctions
|
||||||
method,
|
method,
|
||||||
qs,
|
qs,
|
||||||
body,
|
body,
|
||||||
uri: uri ||`${credentials.domain}/api/v2${resource}`,
|
uri: uri ||`${credentials.domain}/api/v2${resource}.json`,
|
||||||
json: true
|
json: true
|
||||||
};
|
};
|
||||||
options = Object.assign({}, options, option);
|
options = Object.assign({}, options, option);
|
||||||
if (Object.keys(options.body).length === 0) {
|
if (Object.keys(options.body).length === 0) {
|
||||||
delete options.body;
|
delete options.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await this.helpers.request!(options);
|
return await this.helpers.request!(options);
|
||||||
} catch (error) {
|
} catch (err) {
|
||||||
let errorMessage = error.message;
|
let errorMessage = '';
|
||||||
if (error.response.body) {
|
if (err.error && err.description) {
|
||||||
errorMessage = error.response.body.message || error.response.body.Message || error.message;
|
errorMessage = err.description;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(errorMessage);
|
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import {
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
INodeType,
|
INodeType,
|
||||||
IWebhookResponseData,
|
IWebhookResponseData,
|
||||||
|
IDataObject,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
@ -59,7 +60,7 @@ export class ZendeskTrigger implements INodeType {
|
||||||
{
|
{
|
||||||
displayName: 'Events',
|
displayName: 'Events',
|
||||||
name: 'events',
|
name: 'events',
|
||||||
type: 'multiOptions',
|
type: 'options',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
service: [
|
service: [
|
||||||
|
@ -69,12 +70,12 @@ export class ZendeskTrigger implements INodeType {
|
||||||
},
|
},
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'ticket.status.open',
|
name: 'ticket.created',
|
||||||
value: 'ticket.status.open'
|
value: 'ticket.created',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
required: true,
|
required: true,
|
||||||
default: [],
|
default: '',
|
||||||
description: '',
|
description: '',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
@ -84,55 +85,72 @@ export class ZendeskTrigger implements INodeType {
|
||||||
webhookMethods = {
|
webhookMethods = {
|
||||||
default: {
|
default: {
|
||||||
async checkExists(this: IHookFunctions): Promise<boolean> {
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
||||||
let webhooks;
|
|
||||||
const webhookData = this.getWorkflowStaticData('node');
|
const webhookData = this.getWorkflowStaticData('node');
|
||||||
if (webhookData.webhookId === undefined) {
|
if (webhookData.webhookId === undefined) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const endpoint = `/webhooks/${webhookData.webhookId}/`;
|
const endpoint = `/triggers/${webhookData.webhookId}`;
|
||||||
try {
|
try {
|
||||||
webhooks = await zendeskApiRequest.call(this, 'GET', endpoint);
|
await zendeskApiRequest.call(this, 'GET', endpoint);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
async create(this: IHookFunctions): Promise<boolean> {
|
async create(this: IHookFunctions): Promise<boolean> {
|
||||||
let body, responseData;
|
let condition: IDataObject = {};
|
||||||
const webhookUrl = this.getNodeWebhookUrl('default');
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
||||||
const webhookData = this.getWorkflowStaticData('node');
|
const webhookData = this.getWorkflowStaticData('node');
|
||||||
const event = this.getNodeParameter('event') as string;
|
const event = this.getNodeParameter('event') as string;
|
||||||
const actions = this.getNodeParameter('actions') as string[];
|
if (event === 'ticket.created') {
|
||||||
const endpoint = `/webhooks/`;
|
condition = {
|
||||||
// @ts-ignore
|
all: [
|
||||||
body = {
|
{
|
||||||
endpoint_url: webhookUrl,
|
field: 'status',
|
||||||
actions: actions.join(','),
|
value: 'open',
|
||||||
event_id: event,
|
},
|
||||||
};
|
],
|
||||||
try {
|
}
|
||||||
responseData = await zendeskApiRequest.call(this, 'POST', endpoint, body);
|
|
||||||
} catch(error) {
|
|
||||||
console.log(error)
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
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
|
// @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;
|
return true;
|
||||||
},
|
},
|
||||||
async delete(this: IHookFunctions): Promise<boolean> {
|
async delete(this: IHookFunctions): Promise<boolean> {
|
||||||
let responseData;
|
|
||||||
const webhookData = this.getWorkflowStaticData('node');
|
const webhookData = this.getWorkflowStaticData('node');
|
||||||
const endpoint = `/webhooks/${webhookData.webhookId}/`;
|
|
||||||
try {
|
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) {
|
} catch(error) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!responseData.success) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
delete webhookData.webhookId;
|
delete webhookData.webhookId;
|
||||||
|
delete webhookData.targetId
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue