added zendesk node

This commit is contained in:
Ricardo Espinoza 2020-01-07 14:56:42 -05:00
parent 0cb7965101
commit 3448575a20
5 changed files with 760 additions and 6 deletions

View file

@ -28,11 +28,7 @@ export async function zendeskApiRequest(this: IHookFunctions | IExecuteFunctions
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (err) { } catch (err) {
let errorMessage = ''; throw new Error(err);
if (err.message && err.error) {
errorMessage = err.message;
}
throw new Error(errorMessage);
} }
} }

View file

@ -0,0 +1,503 @@
import { INodeProperties } from 'n8n-workflow';
export const ticketOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'ticket',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a ticket',
},
{
name: 'Update',
value: 'update',
description: 'Update a ticket'
},
{
name: 'Get',
value: 'get',
description: 'Get a ticket'
},
{
name: 'Get All',
value: 'getAll',
description: 'Get all tickets'
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a ticket'
},
],
default: 'create',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const ticketFields = [
/* -------------------------------------------------------------------------- */
/* ticket:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'Description',
name: 'description',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
displayOptions: {
show: {
resource: [
'ticket',
],
operation: [
'create',
],
},
},
required: true,
description: 'The first comment on the ticket',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'ticket',
],
operation: [
'create',
],
},
},
options: [
{
displayName: 'External ID',
name: 'externalId',
type: 'string',
default: '',
description: 'An id you can use to link Zendesk Support tickets to local records',
},
{
displayName: 'Subject',
name: 'subject',
type: 'string',
default: '',
description: 'The value of the subject field for this ticket',
},
{
displayName: 'Recipient',
name: 'recipient',
type: 'string',
default: '',
description: 'The original recipient e-mail address of the ticket',
},
{
displayName: 'Group',
name: 'group',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getGroups',
},
default: '',
description: 'The group this ticket is assigned to',
},
{
displayName: 'Tags',
name: 'tags',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getTags',
},
default: [],
description: 'The array of tags applied to this ticket',
},
{
displayName: 'Type',
name: 'type',
type: 'options',
options: [
{
name: 'Question',
value: 'question',
},
{
name: 'Incident',
value: 'incident',
},
{
name: 'Problem',
value: 'problem',
},
{
name: 'Task',
value: 'task',
},
],
default: '',
description: 'The type of this ticket',
},
{
displayName: 'Status',
name: 'status',
type: 'options',
options: [
{
name: 'Open',
value: 'open',
},
{
name: 'New',
value: 'new',
},
{
name: 'Pending',
value: 'pending',
},
{
name: 'Solved',
value: 'solved',
},
{
name: 'Closed',
value: 'closed',
},
],
default: '',
description: 'The state of the ticket',
}
],
},
/* -------------------------------------------------------------------------- */
/* ticket:update */
/* -------------------------------------------------------------------------- */
{
displayName: 'ID',
name: 'id',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'ticket',
],
operation: [
'update',
],
},
},
description: 'Ticket ID',
},
{
displayName: 'Update Fields',
name: 'updateFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'ticket',
],
operation: [
'update',
],
},
},
options: [
{
displayName: 'External ID',
name: 'externalId',
type: 'string',
default: '',
description: 'An id you can use to link Zendesk Support tickets to local records',
},
{
displayName: 'Subject',
name: 'subject',
type: 'string',
default: '',
description: 'The value of the subject field for this ticket',
},
{
displayName: 'Recipient',
name: 'recipient',
type: 'string',
default: '',
description: 'The original recipient e-mail address of the ticket',
},
{
displayName: 'Group',
name: 'group',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getGroups',
},
default: '',
description: 'The group this ticket is assigned to',
},
{
displayName: 'Tags',
name: 'tags',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getTags',
},
default: [],
description: 'The array of tags applied to this ticket',
},
{
displayName: 'Type',
name: 'type',
type: 'options',
options: [
{
name: 'Question',
value: 'question',
},
{
name: 'Incident',
value: 'incident',
},
{
name: 'Problem',
value: 'problem',
},
{
name: 'Task',
value: 'task',
},
],
default: '',
description: 'The type of this ticket',
},
{
displayName: 'Status',
name: 'status',
type: 'options',
options: [
{
name: 'Open',
value: 'open',
},
{
name: 'New',
value: 'new',
},
{
name: 'Pending',
value: 'pending',
},
{
name: 'Solved',
value: 'solved',
},
{
name: 'Closed',
value: 'closed',
},
],
default: '',
description: 'The state of the ticket',
}
],
},
/* -------------------------------------------------------------------------- */
/* ticket:get */
/* -------------------------------------------------------------------------- */
{
displayName: 'ID',
name: 'id',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'ticket',
],
operation: [
'get',
],
},
},
description: 'Ticket ID',
},
/* -------------------------------------------------------------------------- */
/* ticket:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: [
'ticket',
],
operation: [
'getAll',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
resource: [
'ticket',
],
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 100,
},
default: 100,
description: 'How many results to return.',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
displayOptions: {
show: {
resource: [
'ticket',
],
operation: [
'getAll',
],
},
},
options: [
{
displayName: 'Status',
name: 'status',
type: 'options',
options: [
{
name: 'Open',
value: 'open',
},
{
name: 'New',
value: 'new',
},
{
name: 'Pending',
value: 'pending',
},
{
name: 'Solved',
value: 'solved',
},
{
name: 'Closed',
value: 'closed',
},
],
default: '',
description: 'The state of the ticket',
},
{
displayName: 'Sort By',
name: 'sortBy',
type: 'options',
options: [
{
name: 'Updated At',
value: 'updated_at',
},
{
name: 'Created At',
value: 'created_at',
},
{
name: 'Priority',
value: 'priority',
},
{
name: 'Status',
value: 'status',
},
{
name: 'Ticket Type',
value: 'ticket_type',
},
],
default: 'updated_at',
description: 'Defaults to sorting by relevance',
},
{
displayName: 'Sort Order',
name: 'sortOrder',
type: 'options',
options: [
{
name: 'Asc',
value: 'asc',
},
{
name: 'Desc',
value: 'desc',
},
],
default: 'desc',
description: 'Sort order',
}
],
},
/* -------------------------------------------------------------------------- */
/* ticket:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'ID',
name: 'id',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'ticket',
],
operation: [
'delete',
],
},
},
description: 'Ticket ID',
},
] as INodeProperties[];

View file

@ -0,0 +1,16 @@
import { IDataObject } from "n8n-workflow";
export interface ITicket {
subject?: string;
comment?: IComment;
type?: string;
group?: string;
external_id?: string;
tags?: string[];
status?: string;
recipient?: string;
}
export interface IComment {
body?: string;
}

View file

@ -0,0 +1,238 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeTypeDescription,
INodeExecutionData,
INodeType,
ILoadOptionsFunctions,
INodePropertyOptions,
} from 'n8n-workflow';
import {
zendeskApiRequest,
zendeskApiRequestAllItems,
} from './GenericFunctions';
import {
ticketFields,
ticketOperations
} from './TicketDescription';
import {
ITicket,
IComment,
} from './TicketInterface';
export class Zendesk implements INodeType {
description: INodeTypeDescription = {
displayName: 'Zendesk',
name: 'zendesk',
icon: 'file:zendesk.png',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Zendesk API',
defaults: {
name: 'Zendesk',
color: '#13353c',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'zendeskApi',
required: true,
}
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Ticket',
value: 'ticket',
description: 'Tickets are the means through which your end users (customers) communicate with agents in Zendesk Support.',
},
],
default: 'ticket',
description: 'Resource to consume.',
},
...ticketOperations,
...ticketFields,
],
};
methods = {
loadOptions: {
// Get all the groups to display them to user so that he can
// select them easily
async getGroups(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const groups = await zendeskApiRequestAllItems.call(this, 'groups', 'GET', '/groups');
for (const group of groups) {
const groupName = group.name;
const groupId = group.id;
returnData.push({
name: groupName,
value: groupId,
});
}
return returnData;
},
// Get all the tags to display them to user so that he can
// select them easily
async getTags(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const tags = await zendeskApiRequestAllItems.call(this, 'tags', 'GET', '/tags');
for (const tag of tags) {
const tagName = tag.name;
const tagId = tag.name;
returnData.push({
name: tagName,
value: tagId,
});
}
return returnData;
},
}
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = items.length as unknown as number;
let qs: IDataObject = {};
let responseData;
for (let i = 0; i < length; i++) {
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
//https://developer.zendesk.com/rest_api/docs/support/introduction
if (resource === 'ticket') {
//https://developer.zendesk.com/rest_api/docs/support/tickets
if (operation === 'create') {
const description = this.getNodeParameter('description', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const comment: IComment = {
body: description,
};
const body: ITicket = {
comment,
};
if (additionalFields.type) {
body.type = additionalFields.type as string;
}
if (additionalFields.externalId) {
body.external_id = additionalFields.externalId as string;
}
if (additionalFields.subject) {
body.subject = additionalFields.subject as string;
}
if (additionalFields.status) {
body.status = additionalFields.status as string;
}
if (additionalFields.recipient) {
body.recipient = additionalFields.recipient as string;
}
if (additionalFields.group) {
body.group = additionalFields.group as string;
}
if (additionalFields.tags) {
body.tags = additionalFields.tags as string[];
}
try {
responseData = await zendeskApiRequest.call(this, 'POST', '/tickets', { ticket: body });
responseData = responseData.ticket;
} catch (err) {
throw new Error(`Zendesk Error: ${err}`);
}
}
//https://developer.zendesk.com/rest_api/docs/support/tickets#update-ticket
if (operation === 'update') {
const ticketId = this.getNodeParameter('id', i) as string;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
const body: ITicket = {};
if (updateFields.type) {
body.type = updateFields.type as string;
}
if (updateFields.externalId) {
body.external_id = updateFields.externalId as string;
}
if (updateFields.subject) {
body.subject = updateFields.subject as string;
}
if (updateFields.status) {
body.status = updateFields.status as string;
}
if (updateFields.recipient) {
body.recipient = updateFields.recipient as string;
}
if (updateFields.group) {
body.group = updateFields.group as string;
}
if (updateFields.tags) {
body.tags = updateFields.tags as string[];
}
try {
responseData = await zendeskApiRequest.call(this, 'PUT', `/tickets/${ticketId}`, { ticket: body });
responseData = responseData.ticket;
} catch (err) {
throw new Error(`Zendesk Error: ${err}`);
}
}
//https://developer.zendesk.com/rest_api/docs/support/tickets#show-ticket
if (operation === 'get') {
const ticketId = this.getNodeParameter('id', i) as string;
try {
responseData = await zendeskApiRequest.call(this, 'GET', `/tickets/${ticketId}`, {});
responseData = responseData.ticket;
} catch (err) {
throw new Error(`Zendesk Error: ${err}`);
}
}
//https://developer.zendesk.com/rest_api/docs/support/search#list-search-results
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
qs.query = 'type:ticket'
if (options.status) {
qs.query += ` status:${options.status}`
}
if (options.sortBy) {
qs.sort_by = options.sortBy;
}
if (options.sortOrder) {
qs.sort_order = options.sortOrder;
}
try {
if (returnAll) {
responseData = await zendeskApiRequestAllItems.call(this, 'results', 'GET', `/search`, {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.per_page = limit;
responseData = await zendeskApiRequest.call(this, 'GET', `/search`, {}, qs);
responseData = responseData.results;
}
} catch (err) {
throw new Error(`Zendesk Error: ${err}`);
}
}
//https://developer.zendesk.com/rest_api/docs/support/tickets#delete-ticket
if (operation === 'delete') {
const ticketId = this.getNodeParameter('id', i) as string;
try {
responseData = await zendeskApiRequest.call(this, 'DELETE', `/tickets/${ticketId}`, {});
} catch (err) {
throw new Error(`Zendesk Error: ${err}`);
}
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -171,7 +171,8 @@
"dist/nodes/Webhook.node.js", "dist/nodes/Webhook.node.js",
"dist/nodes/Wordpress/Wordpress.node.js", "dist/nodes/Wordpress/Wordpress.node.js",
"dist/nodes/Xml.node.js", "dist/nodes/Xml.node.js",
"dist/nodes/Zendesk/ZendeskTrigger.node.js" "dist/nodes/Zendesk/ZendeskTrigger.node.js",
"dist/nodes/Zendesk/Zendesk.node.js"
] ]
}, },
"devDependencies": { "devDependencies": {