mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 18:41:14 +00:00
⚡ Zendesk improvements
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
validateJSON,
|
||||
zendeskApiRequest,
|
||||
zendeskApiRequestAllItems,
|
||||
} from './GenericFunctions';
|
||||
@@ -30,6 +31,7 @@ import {
|
||||
ITicket,
|
||||
IComment,
|
||||
} from './TicketInterface';
|
||||
import { response } from 'express';
|
||||
|
||||
export class Zendesk implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
@@ -83,6 +85,33 @@ export class Zendesk implements INodeType {
|
||||
|
||||
methods = {
|
||||
loadOptions: {
|
||||
// Get all the custom fields to display them to user so that he can
|
||||
// select them easily
|
||||
async getCustomFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const customFields = [
|
||||
'text',
|
||||
'textarea',
|
||||
'date',
|
||||
'integer',
|
||||
'decimal',
|
||||
'regexp',
|
||||
'multiselect',
|
||||
'tagger',
|
||||
];
|
||||
const fields = await zendeskApiRequestAllItems.call(this, 'ticket_fields', 'GET', '/ticket_fields');
|
||||
for (const field of fields) {
|
||||
if (customFields.includes(field.type)) {
|
||||
const fieldName = field.title;
|
||||
const fieldId = field.id;
|
||||
returnData.push({
|
||||
name: fieldName,
|
||||
value: fieldId,
|
||||
});
|
||||
}
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
// Get all the groups to display them to user so that he can
|
||||
// select them easily
|
||||
async getGroups(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
@@ -131,42 +160,71 @@ export class Zendesk implements INodeType {
|
||||
if (operation === 'create') {
|
||||
const description = this.getNodeParameter('description', i) as string;
|
||||
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
|
||||
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[];
|
||||
}
|
||||
if (jsonParameters) {
|
||||
|
||||
const customFieldsJson = this.getNodeParameter('customFieldsJson', i) as string;
|
||||
try {
|
||||
JSON.parse(customFieldsJson);
|
||||
} catch(err) {
|
||||
throw new Error('Custom fields must be a valid JSON');
|
||||
|
||||
if (customFieldsJson !== '' ) {
|
||||
|
||||
if (validateJSON(customFieldsJson) !== undefined) {
|
||||
|
||||
body.custom_fields = JSON.parse(customFieldsJson);
|
||||
|
||||
} else {
|
||||
throw new Error('Custom fields must be a valid JSON');
|
||||
}
|
||||
}
|
||||
|
||||
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
|
||||
|
||||
if (additionalFieldsJson !== '' ) {
|
||||
|
||||
if (validateJSON(additionalFieldsJson) !== undefined) {
|
||||
|
||||
Object.assign(body, JSON.parse(additionalFieldsJson));
|
||||
|
||||
} else {
|
||||
throw new Error('Additional fields must be a valid JSON');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||
|
||||
const customFields = (this.getNodeParameter('customFieldsUi', i) as IDataObject).customFieldsValues as IDataObject[];
|
||||
|
||||
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[];
|
||||
}
|
||||
if (customFields) {
|
||||
body.custom_fields = customFields;
|
||||
}
|
||||
body.custom_fields = JSON.parse(customFieldsJson);
|
||||
}
|
||||
responseData = await zendeskApiRequest.call(this, 'POST', '/tickets', { ticket: body });
|
||||
responseData = responseData.ticket;
|
||||
@@ -175,37 +233,66 @@ export class Zendesk implements INodeType {
|
||||
if (operation === 'update') {
|
||||
const ticketId = this.getNodeParameter('id', i) as string;
|
||||
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
|
||||
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[];
|
||||
}
|
||||
|
||||
if (jsonParameters) {
|
||||
|
||||
const customFieldsJson = this.getNodeParameter('customFieldsJson', i) as string;
|
||||
try {
|
||||
JSON.parse(customFieldsJson);
|
||||
} catch(err) {
|
||||
throw new Error('Custom fields must be a valid JSON');
|
||||
|
||||
if (customFieldsJson !== '' ) {
|
||||
|
||||
if (validateJSON(customFieldsJson) !== undefined) {
|
||||
|
||||
body.custom_fields = JSON.parse(customFieldsJson);
|
||||
|
||||
} else {
|
||||
throw new Error('Custom fields must be a valid JSON');
|
||||
}
|
||||
}
|
||||
|
||||
const updateFieldsJson = this.getNodeParameter('updateFieldsJson', i) as string;
|
||||
|
||||
if (updateFieldsJson !== '' ) {
|
||||
|
||||
if (validateJSON(updateFieldsJson) !== undefined) {
|
||||
|
||||
Object.assign(body, JSON.parse(updateFieldsJson));
|
||||
|
||||
} else {
|
||||
throw new Error('Additional fields must be a valid JSON');
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
||||
|
||||
const customFields = (this.getNodeParameter('customFieldsUi', i) as IDataObject).customFieldsValues as IDataObject[];
|
||||
|
||||
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[];
|
||||
}
|
||||
if (customFields) {
|
||||
body.custom_fields = customFields;
|
||||
}
|
||||
body.custom_fields = JSON.parse(customFieldsJson);
|
||||
}
|
||||
responseData = await zendeskApiRequest.call(this, 'PUT', `/tickets/${ticketId}`, { ticket: body });
|
||||
responseData = responseData.ticket;
|
||||
@@ -259,8 +346,15 @@ export class Zendesk implements INodeType {
|
||||
}
|
||||
//https://developer.zendesk.com/rest_api/docs/support/ticket_fields#list-ticket-fields
|
||||
if (operation === 'getAll') {
|
||||
responseData = await zendeskApiRequest.call(this, 'GET', '/ticket_fields', {}, qs);
|
||||
responseData = responseData.ticket_fields;
|
||||
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
||||
if (returnAll) {
|
||||
responseData = await zendeskApiRequestAllItems.call(this, 'ticket_fields', 'GET', '/ticket_fields', {}, qs);
|
||||
} else {
|
||||
const limit = this.getNodeParameter('limit', i) as number;
|
||||
qs.limit = limit;
|
||||
responseData = await zendeskApiRequestAllItems.call(this, 'ticket_fields', 'GET', '/ticket_fields', {}, qs);
|
||||
responseData = responseData.slice(0, limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Array.isArray(responseData)) {
|
||||
|
||||
Reference in New Issue
Block a user