Add Organization resource to Zendesk node (#2152)

* Added Organization options. Create, Update, Get, GetAll, Delete, Count and Related

* Fixed Zendesk Node user alias typo

* Updated Zendesk documentation links for future maintainers

* Added Related for Users in Zendesk Node

* Added fetching organizations for users

* 🔨 Refactor Zendesk expansion

*  Improvements

Co-authored-by: Jonathan <jonathan.bennetts@gmail.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
This commit is contained in:
Iván Ovejero 2021-09-05 15:13:25 +02:00 committed by GitHub
parent 2c561507f7
commit 1cc58171dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 597 additions and 23 deletions

View file

@ -33,7 +33,6 @@ export async function zendeskApiRequest(this: IHookFunctions | IExecuteFunctions
if (Object.keys(options.body).length === 0) {
delete options.body;
}
try {
if (authenticationMethod === 'apiToken') {
const credentials = await this.getCredentials('zendeskApi');
@ -45,7 +44,6 @@ export async function zendeskApiRequest(this: IHookFunctions | IExecuteFunctions
const base64Key = Buffer.from(`${credentials.email}/token:${credentials.apiToken}`).toString('base64');
options.uri = `https://${credentials.subdomain}.zendesk.com/api/v2${resource}.json`;
options.headers!['Authorization'] = `Basic ${base64Key}`;
return await this.helpers.request!(options);
} else {
const credentials = await this.getCredentials('zendeskOAuth2Api');

View file

@ -0,0 +1,378 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const organizationOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'organization',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create an organization',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete an organization',
},
{
name: 'Count',
value: 'count',
description: 'Count organizations',
},
{
name: 'Get',
value: 'get',
description: 'Get an organization',
},
{
name: 'Get All',
value: 'getAll',
description: 'Get all organizations',
},
{
name: 'Get Related Data',
value: 'getRelatedData',
description: 'Get data related to the organization',
},
{
name: 'Update',
value: 'update',
description: 'Update a organization',
},
],
default: 'create',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const organizationFields = [
/* -------------------------------------------------------------------------- */
/* organization:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
displayOptions: {
show: {
resource: [
'organization',
],
operation: [
'create',
],
},
},
required: true,
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'organization',
],
operation: [
'create',
],
},
},
options: [
{
displayName: 'Details',
name: 'details',
type: 'string',
default: '',
description: 'Details obout the organization, such as the address',
},
{
displayName: 'Domain Names',
name: 'domain_names',
type: 'string',
default: '',
description: 'Comma-separated domain names associated with this organization',
},
{
displayName: 'Notes',
name: 'notes',
type: 'string',
default: '',
},
{
displayName: 'Organization Fields',
name: 'organizationFieldsUi',
placeholder: 'Add Organization Field',
description: 'Values of custom fields in the organization\'s profile',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
default: {},
options: [
{
name: 'organizationFieldValues',
displayName: 'Field',
values: [
{
displayName: 'Field',
name: 'field',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getOrganizationFields',
},
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
},
],
},
],
},
{
displayName: 'Tags',
name: 'tags',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getTags',
},
default: [],
description: 'IDs of tags applied to this organization',
},
],
},
/* -------------------------------------------------------------------------- */
/* organization:update */
/* -------------------------------------------------------------------------- */
{
displayName: 'Organization ID',
name: 'id',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'organization',
],
operation: [
'update',
],
},
},
},
{
displayName: 'Update Fields',
name: 'updateFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'organization',
],
operation: [
'update',
],
},
},
options: [
{
displayName: 'Details',
name: 'details',
type: 'string',
default: '',
description: 'Details obout the organization, such as the address',
},
{
displayName: 'Domain Names',
name: 'domain_names',
type: 'string',
default: '',
description: 'Comma-separated domain names associated with this organization',
},
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
},
{
displayName: 'Notes',
name: 'notes',
type: 'string',
default: '',
},
{
displayName: 'Organization Fields',
name: 'organizationFieldsUi',
placeholder: 'Add Organization Field',
description: 'Values of custom fields in the organization\'s profile',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
default: {},
options: [
{
name: 'organizationFieldValues',
displayName: 'Field',
values: [
{
displayName: 'Field',
name: 'field',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getOrganizationFields',
},
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
},
],
},
],
},
{
displayName: 'Tags',
name: 'tags',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getTags',
},
default: [],
description: 'IDs of tags applied to this organization',
},
],
},
/* -------------------------------------------------------------------------- */
/* organization:get */
/* -------------------------------------------------------------------------- */
{
displayName: 'Organization ID',
name: 'id',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'organization',
],
operation: [
'get',
],
},
},
description: 'Organization ID',
},
/* -------------------------------------------------------------------------- */
/* organization:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: [
'organization',
],
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: [
'organization',
],
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 100,
},
default: 100,
description: 'How many results to return',
},
/* -------------------------------------------------------------------------- */
/* organization:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'Organization ID',
name: 'id',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'organization',
],
operation: [
'delete',
],
},
},
},
/* -------------------------------------------------------------------------- */
/* organization:getRelatedData */
/* -------------------------------------------------------------------------- */
{
displayName: 'Organization ID',
name: 'id',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'organization',
],
operation: [
'getRelatedData',
],
},
},
},
] as INodeProperties[];

View file

@ -35,6 +35,16 @@ export const userOperations = [
value: 'getAll',
description: 'Get all users',
},
{
name: 'Get Organizations',
value: 'getOrganizations',
description: 'Get a user\'s organizations',
},
{
name: 'Get Related Data',
value: 'getRelatedData',
description: 'Get data related to the user',
},
{
name: 'Search',
value: 'search',
@ -47,7 +57,6 @@ export const userOperations = [
},
],
default: 'create',
description: 'The operation to perform.',
},
] as INodeProperties[];
@ -93,7 +102,7 @@ export const userFields = [
options: [
{
displayName: 'Alias',
name: 'alis',
name: 'alias',
type: 'string',
default: '',
description: `An alias displayed to end users`,
@ -159,9 +168,12 @@ export const userFields = [
},
{
displayName: 'Organization ID',
name: 'organizationId',
type: 'number',
default: 0,
name: 'organization_id',
typeOptions: {
loadOptionsMethod: 'getOrganizations',
},
type: 'options',
default: '',
description: `The id of the user's organization. If the user has more than one organization memberships, the id of the user's default organization`,
},
{
@ -347,7 +359,7 @@ export const userFields = [
options: [
{
displayName: 'Alias',
name: 'alis',
name: 'alias',
type: 'string',
default: '',
description: `An alias displayed to end users`,
@ -420,9 +432,12 @@ export const userFields = [
},
{
displayName: 'Organization ID',
name: 'organizationId',
type: 'number',
default: 0,
name: 'organization_id',
typeOptions: {
loadOptionsMethod: 'getOrganizations',
},
type: 'options',
default: '',
description: `The id of the user's organization. If the user has more than one organization memberships, the id of the user's default organization`,
},
{
@ -768,4 +783,44 @@ export const userFields = [
},
description: 'User ID',
},
/* -------------------------------------------------------------------------- */
/* user:getRelatedData */
/* -------------------------------------------------------------------------- */
{
displayName: 'User ID',
name: 'id',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'getRelatedData',
],
},
},
},
/* -------------------------------------------------------------------------- */
/* user:getOrganizations */
/* -------------------------------------------------------------------------- */
{
displayName: 'User ID',
name: 'id',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'getOrganizations',
],
},
},
},
] as INodeProperties[];

View file

@ -34,6 +34,11 @@ import {
userOperations
} from './UserDescription';
import {
organizationFields,
organizationOperations
} from './OrganizationDescription';
import {
IComment,
ITicket,
@ -43,7 +48,7 @@ export class Zendesk implements INodeType {
description: INodeTypeDescription = {
displayName: 'Zendesk',
name: 'zendesk',
icon: 'file:zendesk.png',
icon: 'file:zendesk.svg',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
@ -116,6 +121,11 @@ export class Zendesk implements INodeType {
value: 'user',
description: 'Manage users',
},
{
name: 'Organization',
value: 'organization',
description: 'Manage organizations',
},
],
default: 'ticket',
description: 'Resource to consume.',
@ -129,6 +139,9 @@ export class Zendesk implements INodeType {
// USER
...userOperations,
...userFields,
// ORGANIZATION
...organizationOperations,
...organizationFields,
],
};
@ -223,6 +236,33 @@ export class Zendesk implements INodeType {
}
return returnData;
},
// 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;
},
},
};
@ -236,7 +276,7 @@ export class Zendesk implements INodeType {
try {
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
//https://developer.zendesk.com/api-reference/ticketing/introduction/
if (resource === 'ticket') {
//https://developer.zendesk.com/rest_api/docs/support/tickets
if (operation === 'create') {
@ -385,7 +425,7 @@ export class Zendesk implements INodeType {
}
}
}
//https://developer.zendesk.com/rest_api/docs/support/ticket_fields
//https://developer.zendesk.com/api-reference/ticketing/tickets/ticket_fields/
if (resource === 'ticketField') {
//https://developer.zendesk.com/rest_api/docs/support/tickets#show-ticket
if (operation === 'get') {
@ -406,9 +446,9 @@ export class Zendesk implements INodeType {
}
}
}
//https://developer.zendesk.com/rest_api/docs/support/users
//https://developer.zendesk.com/api-reference/ticketing/users/users/
if (resource === 'user') {
//https://developer.zendesk.com/rest_api/docs/support/users#create-user
//https://developer.zendesk.com/api-reference/ticketing/users/users/#create-user
if (operation === 'create') {
const name = this.getNodeParameter('name', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
@ -430,11 +470,10 @@ export class Zendesk implements INodeType {
delete body.userFieldsUi;
}
}
responseData = await zendeskApiRequest.call(this, 'POST', '/users', { user: body });
responseData = responseData.user;
}
//https://developer.zendesk.com/rest_api/docs/support/tickets#update-ticket
//https://developer.zendesk.com/api-reference/ticketing/users/users/#update-user
if (operation === 'update') {
const userId = this.getNodeParameter('id', i) as string;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
@ -458,13 +497,13 @@ export class Zendesk implements INodeType {
responseData = await zendeskApiRequest.call(this, 'PUT', `/users/${userId}`, { user: body });
responseData = responseData.user;
}
//https://developer.zendesk.com/rest_api/docs/support/users#show-user
//https://developer.zendesk.com/api-reference/ticketing/users/users/#show-user
if (operation === 'get') {
const userId = this.getNodeParameter('id', i) as string;
responseData = await zendeskApiRequest.call(this, 'GET', `/users/${userId}`, {});
responseData = responseData.user;
}
//https://developer.zendesk.com/rest_api/docs/support/users#list-users
//https://developer.zendesk.com/api-reference/ticketing/users/users/#list-users
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('filters', i) as IDataObject;
@ -480,7 +519,13 @@ export class Zendesk implements INodeType {
responseData = responseData.users;
}
}
//https://developer.zendesk.com/rest_api/docs/support/users#search-users
//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
if (operation === 'search') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('filters', i) as IDataObject;
@ -496,12 +541,106 @@ export class Zendesk implements INodeType {
responseData = responseData.users;
}
}
//https://developer.zendesk.com/rest_api/docs/support/users#delete-user
//https://developer.zendesk.com/api-reference/ticketing/users/users/#delete-user
if (operation === 'delete') {
const userId = this.getNodeParameter('id', i) as string;
responseData = await zendeskApiRequest.call(this, 'DELETE', `/users/${userId}`, {});
responseData = responseData.user;
}
//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;
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);

View file

@ -33,7 +33,7 @@ export class ZendeskTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Zendesk Trigger',
name: 'zendeskTrigger',
icon: 'file:zendesk.png',
icon: 'file:zendesk.svg',
group: ['trigger'],
version: 1,
description: 'Handle Zendesk events via webhooks',

Binary file not shown.

Before

Width:  |  Height:  |  Size: 930 B

View file

@ -0,0 +1,4 @@
<svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M0 24C0 10.7452 10.7452 0 24 0C37.2548 0 48 10.7452 48 24C48 37.2548 37.2548 48 24 48C10.7452 48 0 37.2548 0 24Z" fill="white"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M15.4479 18.6532C19.4971 18.6532 22.7796 15.278 22.7796 11.1145H8.11609C8.11609 15.278 11.3986 18.6532 15.4479 18.6532ZM22.7796 35.8515V17.6483L8.11609 35.8515H22.7796ZM25.1953 35.8516C25.1953 31.6881 28.4779 28.3129 32.5271 28.3129C36.5763 28.3129 39.8588 31.6881 39.8588 35.8516H25.1953ZM25.1953 11.1145V29.3177L39.8604 11.1145H25.1953Z" fill="#03363D"/>
</svg>

After

Width:  |  Height:  |  Size: 647 B