2021-11-04 02:58:31 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
2020-01-07 11:56:42 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
2020-04-06 16:11:50 -07:00
|
|
|
|
2020-01-07 11:56:42 -08:00
|
|
|
import {
|
2021-11-04 02:58:31 -07:00
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialTestFunctions,
|
2020-01-07 11:56:42 -08:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeExecutionData,
|
2020-01-07 11:56:42 -08:00
|
|
|
INodePropertyOptions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
2021-11-04 02:58:31 -07:00
|
|
|
NodeCredentialTestResult,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2020-01-07 11:56:42 -08:00
|
|
|
} from 'n8n-workflow';
|
2020-04-06 16:11:50 -07:00
|
|
|
|
2020-01-07 11:56:42 -08:00
|
|
|
import {
|
2020-04-09 13:13:14 -07:00
|
|
|
validateJSON,
|
2020-01-07 11:56:42 -08:00
|
|
|
zendeskApiRequest,
|
|
|
|
zendeskApiRequestAllItems,
|
|
|
|
} from './GenericFunctions';
|
2020-04-06 16:11:50 -07:00
|
|
|
|
2020-01-07 11:56:42 -08:00
|
|
|
import {
|
|
|
|
ticketFields,
|
|
|
|
ticketOperations
|
|
|
|
} from './TicketDescription';
|
2020-04-06 16:11:50 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
ticketFieldFields,
|
|
|
|
ticketFieldOperations
|
|
|
|
} from './TicketFieldDescription';
|
|
|
|
|
2020-08-26 23:53:23 -07:00
|
|
|
import {
|
|
|
|
userFields,
|
|
|
|
userOperations
|
|
|
|
} from './UserDescription';
|
|
|
|
|
2021-09-05 06:13:25 -07:00
|
|
|
import {
|
|
|
|
organizationFields,
|
|
|
|
organizationOperations
|
|
|
|
} from './OrganizationDescription';
|
|
|
|
|
2020-01-07 11:56:42 -08:00
|
|
|
import {
|
|
|
|
IComment,
|
2020-10-01 05:01:39 -07:00
|
|
|
ITicket,
|
2021-10-13 16:45:53 -07:00
|
|
|
} from './TicketInterface';
|
2020-01-07 11:56:42 -08:00
|
|
|
|
|
|
|
export class Zendesk implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Zendesk',
|
|
|
|
name: 'zendesk',
|
2021-09-05 06:13:25 -07:00
|
|
|
icon: 'file:zendesk.svg',
|
2020-01-07 11:56:42 -08:00
|
|
|
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,
|
2020-06-09 05:00:41 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: [
|
2020-06-14 15:19:35 -07:00
|
|
|
'apiToken',
|
2020-06-09 05:00:41 -07:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2021-11-04 02:58:31 -07:00
|
|
|
testedBy: 'zendeskSoftwareApiTest',
|
2020-06-09 05:00:41 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'zendeskOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: [
|
|
|
|
'oAuth2',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-01-07 11:56:42 -08:00
|
|
|
],
|
|
|
|
properties: [
|
2020-06-09 05:00:41 -07:00
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
2020-06-14 15:19:35 -07:00
|
|
|
name: 'API Token',
|
|
|
|
value: 'apiToken',
|
2020-06-09 05:00:41 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'OAuth2',
|
|
|
|
value: 'oAuth2',
|
|
|
|
},
|
|
|
|
],
|
2020-06-14 15:19:35 -07:00
|
|
|
default: 'apiToken',
|
2021-10-13 16:45:53 -07:00
|
|
|
description: 'The resource to operate on',
|
2020-06-09 05:00:41 -07:00
|
|
|
},
|
2020-01-07 11:56:42 -08:00
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Ticket',
|
|
|
|
value: 'ticket',
|
2021-10-13 16:45:53 -07:00
|
|
|
description: 'Tickets are the means through which your end users (customers) communicate with agents in Zendesk Support',
|
2020-01-07 11:56:42 -08:00
|
|
|
},
|
2020-04-06 16:11:50 -07:00
|
|
|
{
|
|
|
|
name: 'Ticket Field',
|
|
|
|
value: 'ticketField',
|
|
|
|
description: 'Manage system and custom ticket fields',
|
|
|
|
},
|
2020-08-26 23:53:23 -07:00
|
|
|
{
|
|
|
|
name: 'User',
|
|
|
|
value: 'user',
|
|
|
|
description: 'Manage users',
|
|
|
|
},
|
2021-09-05 06:13:25 -07:00
|
|
|
{
|
|
|
|
name: 'Organization',
|
|
|
|
value: 'organization',
|
|
|
|
description: 'Manage organizations',
|
|
|
|
},
|
2020-01-07 11:56:42 -08:00
|
|
|
],
|
|
|
|
default: 'ticket',
|
2021-10-13 16:45:53 -07:00
|
|
|
description: 'Resource to consume',
|
2020-01-07 11:56:42 -08:00
|
|
|
},
|
2020-04-06 16:11:50 -07:00
|
|
|
// TICKET
|
2020-01-07 11:56:42 -08:00
|
|
|
...ticketOperations,
|
|
|
|
...ticketFields,
|
2020-08-26 23:53:23 -07:00
|
|
|
// TICKET FIELD
|
2020-04-06 16:11:50 -07:00
|
|
|
...ticketFieldOperations,
|
|
|
|
...ticketFieldFields,
|
2020-08-26 23:53:23 -07:00
|
|
|
// USER
|
|
|
|
...userOperations,
|
|
|
|
...userFields,
|
2021-09-05 06:13:25 -07:00
|
|
|
// ORGANIZATION
|
|
|
|
...organizationOperations,
|
|
|
|
...organizationFields,
|
2020-01-07 11:56:42 -08:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
2021-11-04 02:58:31 -07:00
|
|
|
credentialTest: {
|
|
|
|
async zendeskSoftwareApiTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<NodeCredentialTestResult> {
|
|
|
|
const credentials = credential.data;
|
|
|
|
const subdomain = credentials!.subdomain;
|
|
|
|
const email = credentials!.email;
|
|
|
|
const apiToken = credentials!.apiToken;
|
|
|
|
|
|
|
|
const base64Key = Buffer.from(`${email}/token:${apiToken}`).toString('base64');
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Basic ${base64Key}`,
|
|
|
|
},
|
|
|
|
method: 'GET',
|
|
|
|
uri: `https://${subdomain}.zendesk.com/api/v2/ticket_fields.json`,
|
|
|
|
qs: {
|
|
|
|
recent: 0,
|
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
timeout: 5000,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2021-11-11 03:08:05 -08:00
|
|
|
await this.helpers.request!(options);
|
2021-11-04 02:58:31 -07:00
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
2021-11-11 03:08:05 -08:00
|
|
|
message: `Connection details not valid: ${error.message}`,
|
2021-11-04 02:58:31 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Authentication successful!',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
2020-01-07 11:56:42 -08:00
|
|
|
loadOptions: {
|
2020-04-09 13:13:14 -07:00
|
|
|
// 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;
|
|
|
|
},
|
2020-01-07 11:56:42 -08:00
|
|
|
// 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;
|
|
|
|
},
|
2020-08-26 23:53:23 -07:00
|
|
|
|
|
|
|
// Get all the locales to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getLocales(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const locales = await zendeskApiRequestAllItems.call(this, 'locales', 'GET', '/locales');
|
|
|
|
for (const locale of locales) {
|
|
|
|
const localeName = `${locale.locale} - ${locale.name}`;
|
|
|
|
const localeId = locale.locale;
|
|
|
|
returnData.push({
|
|
|
|
name: localeName,
|
|
|
|
value: localeId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get all the user fields to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getUserFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const fields = await zendeskApiRequestAllItems.call(this, 'user_fields', 'GET', '/user_fields');
|
|
|
|
for (const field of fields) {
|
|
|
|
const fieldName = field.title;
|
|
|
|
const fieldId = field.key;
|
|
|
|
returnData.push({
|
|
|
|
name: fieldName,
|
|
|
|
value: fieldId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2021-09-05 06:13:25 -07:00
|
|
|
|
|
|
|
// Get all the organization fields to display them to the user for easy selection
|
|
|
|
async getOrganizationFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const fields = await zendeskApiRequestAllItems.call(this, 'organization_fields', 'GET', '/organization_fields');
|
|
|
|
for (const field of fields) {
|
|
|
|
const fieldName = field.title;
|
|
|
|
const fieldId = field.key;
|
|
|
|
returnData.push({
|
|
|
|
name: fieldName,
|
|
|
|
value: fieldId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
|
|
|
|
async getOrganizations(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const fields = await zendeskApiRequestAllItems.call(this, 'organizations', 'GET', `/organizations`, {}, {});
|
|
|
|
for (const field of fields) {
|
|
|
|
returnData.push({
|
|
|
|
name: field.name,
|
|
|
|
value: field.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-01-07 11:56:42 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
const length = items.length as unknown as number;
|
2020-01-07 19:15:37 -08:00
|
|
|
const qs: IDataObject = {};
|
2020-01-07 11:56:42 -08:00
|
|
|
let responseData;
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
2021-09-05 06:13:25 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/introduction/
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'ticket') {
|
|
|
|
//https://developer.zendesk.com/rest_api/docs/support/tickets
|
|
|
|
if (operation === 'create') {
|
|
|
|
const description = this.getNodeParameter('description', i) as string;
|
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
|
|
|
|
const comment: IComment = {
|
|
|
|
body: description,
|
|
|
|
};
|
|
|
|
const body: ITicket = {
|
2021-10-13 16:45:53 -07:00
|
|
|
comment,
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
|
|
|
if (jsonParameters) {
|
|
|
|
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
|
|
|
|
|
2021-10-13 16:45:53 -07:00
|
|
|
if (additionalFieldsJson !== '') {
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
|
|
|
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be a valid JSON');
|
|
|
|
}
|
2020-04-09 13:13:14 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2020-04-09 13:13:14 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-04-09 13:13:14 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
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 (additionalFields.customFieldsUi) {
|
|
|
|
body.custom_fields = (additionalFields.customFieldsUi as IDataObject).customFieldsValues as IDataObject[];
|
|
|
|
}
|
2020-04-06 16:11:50 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await zendeskApiRequest.call(this, 'POST', '/tickets', { ticket: body });
|
|
|
|
responseData = responseData.ticket;
|
2020-01-07 11:56:42 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.zendesk.com/rest_api/docs/support/tickets#update-ticket
|
|
|
|
if (operation === 'update') {
|
|
|
|
const ticketId = this.getNodeParameter('id', i) as string;
|
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
|
|
|
|
const body: ITicket = {};
|
2020-04-09 13:13:14 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (jsonParameters) {
|
|
|
|
const updateFieldsJson = this.getNodeParameter('updateFieldsJson', i) as string;
|
2020-04-09 13:13:14 -07:00
|
|
|
|
2021-10-13 16:45:53 -07:00
|
|
|
if (updateFieldsJson !== '') {
|
2020-04-09 13:13:14 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (validateJSON(updateFieldsJson) !== undefined) {
|
2020-04-09 13:13:14 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, JSON.parse(updateFieldsJson));
|
2020-04-09 13:13:14 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be a valid JSON');
|
|
|
|
}
|
2020-04-09 13:13:14 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2020-04-09 13:13:14 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2020-04-09 13:13:14 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
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 (updateFields.customFieldsUi) {
|
|
|
|
body.custom_fields = (updateFields.customFieldsUi as IDataObject).customFieldsValues as IDataObject[];
|
|
|
|
}
|
2021-10-13 16:45:53 -07:00
|
|
|
if (updateFields.assigneeEmail) {
|
|
|
|
body.assignee_email = updateFields.assigneeEmail as string;
|
|
|
|
}
|
|
|
|
if (updateFields.internalNote) {
|
|
|
|
const comment: IComment = {
|
|
|
|
html_body: updateFields.internalNote as string,
|
|
|
|
public: false,
|
|
|
|
};
|
|
|
|
body.comment = comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateFields.publicReply) {
|
|
|
|
const comment: IComment = {
|
|
|
|
body: updateFields.publicReply as string,
|
|
|
|
public: true,
|
|
|
|
};
|
|
|
|
body.comment = comment;
|
|
|
|
}
|
|
|
|
|
2020-04-09 13:13:14 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await zendeskApiRequest.call(this, 'PUT', `/tickets/${ticketId}`, { ticket: body });
|
|
|
|
responseData = responseData.ticket;
|
|
|
|
}
|
|
|
|
//https://developer.zendesk.com/rest_api/docs/support/tickets#show-ticket
|
2021-10-13 16:45:53 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/tickets/suspended_tickets/#show-suspended-ticket
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
2021-10-13 16:45:53 -07:00
|
|
|
const ticketType = this.getNodeParameter('ticketType', i) as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
const ticketId = this.getNodeParameter('id', i) as string;
|
2021-10-13 16:45:53 -07:00
|
|
|
const endpoint = (ticketType === 'regular') ? `/tickets/${ticketId}` : `/suspended_tickets/${ticketId}`;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
responseData = responseData.ticket || responseData.suspended_ticket;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
//https://developer.zendesk.com/rest_api/docs/support/search#list-search-results
|
2021-10-13 16:45:53 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/tickets/suspended_tickets/#list-suspended-tickets
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2021-10-13 16:45:53 -07:00
|
|
|
const ticketType = this.getNodeParameter('ticketType', i) as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
qs.query = 'type:ticket';
|
2021-10-13 16:45:53 -07:00
|
|
|
if (options.query) {
|
|
|
|
qs.query += ` ${options.query}`;
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.status) {
|
|
|
|
qs.query += ` status:${options.status}`;
|
2020-04-09 13:13:14 -07:00
|
|
|
}
|
2021-10-13 16:45:53 -07:00
|
|
|
if (options.group) {
|
|
|
|
qs.query += ` group:${options.group}`;
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.sortBy) {
|
|
|
|
qs.sort_by = options.sortBy;
|
2020-04-09 13:13:14 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.sortOrder) {
|
|
|
|
qs.sort_order = options.sortOrder;
|
2020-04-09 13:13:14 -07:00
|
|
|
}
|
2021-10-13 16:45:53 -07:00
|
|
|
const endpoint = (ticketType === 'regular') ? `/search` : `/suspended_tickets`;
|
|
|
|
const property = (ticketType === 'regular') ? 'results' : 'suspended_tickets';
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
2021-10-13 16:45:53 -07:00
|
|
|
responseData = await zendeskApiRequestAllItems.call(this, property, 'GET', endpoint, {}, qs);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
qs.per_page = limit;
|
2021-10-13 16:45:53 -07:00
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', endpoint, {}, qs);
|
|
|
|
responseData = responseData.results || responseData.suspended_tickets;
|
2020-04-06 16:11:50 -07:00
|
|
|
}
|
2020-01-07 11:56:42 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.zendesk.com/rest_api/docs/support/tickets#delete-ticket
|
2021-10-13 16:45:53 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/tickets/suspended_tickets/#delete-suspended-ticket
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
2021-10-13 16:45:53 -07:00
|
|
|
const ticketType = this.getNodeParameter('ticketType', i) as string;
|
|
|
|
const ticketId = this.getNodeParameter('id', i) as string;
|
|
|
|
const endpoint = (ticketType === 'regular') ? `/tickets/${ticketId}` : `/suspended_tickets/${ticketId}`;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'DELETE', endpoint, {});
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/tickets/suspended_tickets/#recover-suspended-ticket
|
|
|
|
if (operation === 'recover') {
|
2021-07-19 23:58:54 -07:00
|
|
|
const ticketId = this.getNodeParameter('id', i) as string;
|
|
|
|
try {
|
2021-10-13 16:45:53 -07:00
|
|
|
responseData = await zendeskApiRequest.call(this, 'PUT', `/suspended_tickets/${ticketId}/recover`, {});
|
|
|
|
responseData = responseData.ticket;
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
throw new NodeApiError(this.getNode(), error);
|
|
|
|
}
|
2020-01-07 11:56:42 -08:00
|
|
|
}
|
|
|
|
}
|
2021-09-05 06:13:25 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/tickets/ticket_fields/
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'ticketField') {
|
|
|
|
//https://developer.zendesk.com/rest_api/docs/support/tickets#show-ticket
|
|
|
|
if (operation === 'get') {
|
|
|
|
const ticketFieldId = this.getNodeParameter('ticketFieldId', i) as string;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', `/ticket_fields/${ticketFieldId}`, {});
|
|
|
|
responseData = responseData.ticket_field;
|
2020-01-07 11:56:42 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.zendesk.com/rest_api/docs/support/ticket_fields#list-ticket-fields
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
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);
|
|
|
|
}
|
2020-04-09 13:13:14 -07:00
|
|
|
}
|
2020-04-06 16:11:50 -07:00
|
|
|
}
|
2021-09-05 06:13:25 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/users/users/
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'user') {
|
2021-09-05 06:13:25 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/users/users/#create-user
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(body, additionalFields);
|
|
|
|
|
|
|
|
if (body.userFieldsUi) {
|
|
|
|
const userFields = (body.userFieldsUi as IDataObject).userFieldValues as IDataObject[];
|
|
|
|
if (userFields) {
|
|
|
|
body.user_fields = {};
|
|
|
|
for (const userField of userFields) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.user_fields[userField.field] = userField.value;
|
|
|
|
}
|
|
|
|
delete body.userFieldsUi;
|
2020-08-26 23:53:23 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await zendeskApiRequest.call(this, 'POST', '/users', { user: body });
|
|
|
|
responseData = responseData.user;
|
2020-08-26 23:53:23 -07:00
|
|
|
}
|
2021-09-05 06:13:25 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/users/users/#update-user
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
|
|
|
const userId = this.getNodeParameter('id', i) as string;
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2020-08-26 23:53:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
|
|
|
|
|
|
|
Object.assign(body, updateFields);
|
|
|
|
|
|
|
|
if (body.userFieldsUi) {
|
|
|
|
const userFields = (body.userFieldsUi as IDataObject).userFieldValues as IDataObject[];
|
|
|
|
if (userFields) {
|
|
|
|
body.user_fields = {};
|
|
|
|
for (const userField of userFields) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.user_fields[userField.field] = userField.value;
|
|
|
|
}
|
|
|
|
delete body.userFieldsUi;
|
2020-08-26 23:53:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await zendeskApiRequest.call(this, 'PUT', `/users/${userId}`, { user: body });
|
|
|
|
responseData = responseData.user;
|
2020-08-26 23:53:23 -07:00
|
|
|
}
|
2021-09-05 06:13:25 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/users/users/#show-user
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
const userId = this.getNodeParameter('id', i) as string;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', `/users/${userId}`, {});
|
|
|
|
responseData = responseData.user;
|
|
|
|
}
|
2021-09-05 06:13:25 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/users/users/#list-users
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const options = this.getNodeParameter('filters', i) as IDataObject;
|
|
|
|
|
|
|
|
Object.assign(qs, options);
|
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await zendeskApiRequestAllItems.call(this, 'users', 'GET', `/users`, {}, qs);
|
|
|
|
} else {
|
|
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
qs.per_page = limit;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', `/users`, {}, qs);
|
|
|
|
responseData = responseData.users;
|
|
|
|
}
|
|
|
|
}
|
2021-09-05 06:13:25 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/organizations/organizations/#list-organizations
|
|
|
|
if (operation === 'getOrganizations') {
|
|
|
|
const userId = this.getNodeParameter('id', i) as string;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', `/users/${userId}/organizations`, {});
|
|
|
|
responseData = responseData.organizations;
|
|
|
|
}
|
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/users/users/#search-users
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'search') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const options = this.getNodeParameter('filters', i) as IDataObject;
|
|
|
|
|
|
|
|
Object.assign(qs, options);
|
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await zendeskApiRequestAllItems.call(this, 'users', 'GET', `/users/search`, {}, qs);
|
|
|
|
} else {
|
|
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
qs.per_page = limit;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', `/users/search`, {}, qs);
|
|
|
|
responseData = responseData.users;
|
|
|
|
}
|
|
|
|
}
|
2021-09-05 06:13:25 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/users/users/#delete-user
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
|
|
|
const userId = this.getNodeParameter('id', i) as string;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'DELETE', `/users/${userId}`, {});
|
|
|
|
responseData = responseData.user;
|
2020-08-31 03:26:11 -07:00
|
|
|
}
|
2021-09-05 06:13:25 -07:00
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/users/users/#show-user-related-information
|
|
|
|
if (operation === 'getRelatedData') {
|
|
|
|
const userId = this.getNodeParameter('id', i) as string;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', `/users/${userId}/related`, {});
|
|
|
|
responseData = responseData.user_related;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/organizations/organizations/
|
|
|
|
if (resource === 'organization') {
|
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/organizations/organizations/#create-organization
|
|
|
|
if (operation === 'create') {
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
|
|
|
|
|
|
|
const body: IDataObject & { name: string; organization_fields?: { [key: string]: object | string } } = {
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
|
|
|
|
const { organizationFieldsUi, ...rest } = this.getNodeParameter('additionalFields', i) as IDataObject & { organizationFieldsUi?: { organizationFieldValues: Array<{ field: string; value: string; }> } };
|
|
|
|
|
|
|
|
Object.assign(body, rest);
|
|
|
|
|
|
|
|
if (organizationFieldsUi?.organizationFieldValues.length) {
|
|
|
|
const organizationFields = organizationFieldsUi.organizationFieldValues;
|
|
|
|
if (organizationFields.length) {
|
|
|
|
body.organization_fields = {};
|
|
|
|
for (const organizationField of organizationFields) {
|
|
|
|
body.organization_fields[organizationField.field] = organizationField.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'POST', '/organizations', { organization: body });
|
|
|
|
responseData = responseData.organization;
|
|
|
|
}
|
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/organizations/organizations/#delete-organization
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const organizationId = this.getNodeParameter('id', i) as string;
|
|
|
|
await zendeskApiRequest.call(this, 'DELETE', `/organizations/${organizationId}`, {});
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/organizations/organizations/#count-organizations
|
|
|
|
if (operation === 'count') {
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', `/organizations/count`, {});
|
|
|
|
responseData = responseData.count;
|
|
|
|
}
|
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/organizations/organizations/#show-organization
|
|
|
|
if (operation === 'get') {
|
|
|
|
const organizationId = this.getNodeParameter('id', i) as string;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', `/organizations/${organizationId}`, {});
|
|
|
|
responseData = responseData.organization;
|
|
|
|
}
|
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/organizations/organizations/#list-organizations
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await zendeskApiRequestAllItems.call(this, 'organizations', 'GET', `/organizations`, {}, qs);
|
|
|
|
} else {
|
|
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
qs.per_page = limit;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', `/organizations`, {}, qs);
|
|
|
|
responseData = responseData.organizations;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/organizations/organizations/#show-organizations-related-information
|
|
|
|
if (operation === 'getRelatedData') {
|
|
|
|
const organizationId = this.getNodeParameter('id', i) as string;
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'GET', `/organizations/${organizationId}/related`, {});
|
|
|
|
responseData = responseData.organization_related;
|
|
|
|
}
|
|
|
|
//https://developer.zendesk.com/api-reference/ticketing/organizations/organizations/#update-organization
|
|
|
|
if (operation === 'update') {
|
|
|
|
const organizationId = this.getNodeParameter('id', i) as string;
|
|
|
|
|
|
|
|
const body: IDataObject & { organization_fields?: { [key: string]: object | string } } = {};
|
|
|
|
|
|
|
|
const { organizationFieldsUi, ...rest } = this.getNodeParameter('updateFields', i) as IDataObject & { organizationFieldsUi?: { organizationFieldValues: Array<{ field: string; value: string; }> } };
|
|
|
|
|
|
|
|
Object.assign(body, rest);
|
|
|
|
|
|
|
|
if (organizationFieldsUi?.organizationFieldValues.length) {
|
|
|
|
const organizationFields = organizationFieldsUi.organizationFieldValues;
|
|
|
|
if (organizationFields.length) {
|
|
|
|
body.organization_fields = {};
|
|
|
|
for (const organizationField of organizationFields) {
|
|
|
|
body.organization_fields[organizationField.field] = organizationField.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await zendeskApiRequest.call(this, 'PUT', `/organizations/${organizationId}`, { organization: body });
|
|
|
|
responseData = responseData.organization;
|
|
|
|
}
|
|
|
|
|
2020-08-31 03:26:11 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else {
|
|
|
|
returnData.push(responseData as IDataObject);
|
2020-08-26 23:53:23 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-01-07 11:56:42 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|