2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
JsonObject,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2022-02-11 23:40:54 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2022-02-11 23:40:54 -08:00
|
|
|
|
2023-02-23 07:16:05 -08:00
|
|
|
import flow from 'lodash.flow';
|
2022-02-11 23:40:54 -08:00
|
|
|
|
|
|
|
import type { Zammad } from './types';
|
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
export function tolerateTrailingSlash(url: string) {
|
|
|
|
return url.endsWith('/') ? url.substr(0, url.length - 1) : url;
|
|
|
|
}
|
|
|
|
|
2022-02-11 23:40:54 -08:00
|
|
|
export async function zammadApiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
) {
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: '',
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const authentication = this.getNodeParameter('authentication', 0) as 'basicAuth' | 'tokenAuth';
|
|
|
|
|
|
|
|
if (authentication === 'basicAuth') {
|
2022-08-17 08:50:24 -07:00
|
|
|
const credentials = (await this.getCredentials(
|
|
|
|
'zammadBasicAuthApi',
|
|
|
|
)) as Zammad.BasicAuthCredentials;
|
2022-02-11 23:40:54 -08:00
|
|
|
|
|
|
|
const baseUrl = tolerateTrailingSlash(credentials.baseUrl);
|
|
|
|
|
|
|
|
options.uri = `${baseUrl}/api/v1${endpoint}`;
|
|
|
|
|
|
|
|
options.auth = {
|
|
|
|
user: credentials.username,
|
|
|
|
pass: credentials.password,
|
|
|
|
};
|
|
|
|
|
|
|
|
options.rejectUnauthorized = !credentials.allowUnauthorizedCerts;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const credentials = (await this.getCredentials(
|
|
|
|
'zammadTokenAuthApi',
|
|
|
|
)) as Zammad.TokenAuthCredentials;
|
2022-02-11 23:40:54 -08:00
|
|
|
|
|
|
|
const baseUrl = tolerateTrailingSlash(credentials.baseUrl);
|
|
|
|
|
|
|
|
options.uri = `${baseUrl}/api/v1${endpoint}`;
|
|
|
|
|
|
|
|
options.headers = {
|
|
|
|
Authorization: `Token token=${credentials.accessToken}`,
|
|
|
|
};
|
|
|
|
|
|
|
|
options.rejectUnauthorized = !credentials.allowUnauthorizedCerts;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(qs).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2022-02-11 23:40:54 -08:00
|
|
|
} catch (error) {
|
|
|
|
if (error.error.error === 'Object already exists!') {
|
|
|
|
error.error.error = 'An entity with this name already exists.';
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2022-02-11 23:40:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function zammadApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
limit = 0,
|
|
|
|
) {
|
|
|
|
// https://docs.zammad.org/en/latest/api/intro.html#pagination
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
qs.per_page = 20;
|
|
|
|
qs.page = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await zammadApiRequest.call(this, method, endpoint, body, qs);
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push(...(responseData as IDataObject[]));
|
2022-02-11 23:40:54 -08:00
|
|
|
|
|
|
|
if (limit && returnData.length > limit) {
|
|
|
|
return returnData.slice(0, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
qs.page++;
|
|
|
|
} while (responseData.length);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function throwOnEmptyUpdate(this: IExecuteFunctions, resource: string) {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one field to update for the ${resource}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// loadOptions utils
|
|
|
|
// ----------------------------------
|
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
export const prettifyDisplayName = (fieldName: string) => fieldName.replace('name', ' Name');
|
|
|
|
|
2022-02-11 23:40:54 -08:00
|
|
|
export const fieldToLoadOption = (i: Zammad.Field) => {
|
|
|
|
return { name: i.display ? prettifyDisplayName(i.display) : i.name, value: i.name };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isCustomer = (user: Zammad.User) =>
|
|
|
|
user.role_ids.includes(3) && !user.email.endsWith('@zammad.org');
|
|
|
|
|
|
|
|
export async function getAllFields(this: ILoadOptionsFunctions) {
|
2022-08-17 08:50:24 -07:00
|
|
|
return (await zammadApiRequest.call(this, 'GET', '/object_manager_attributes')) as Zammad.Field[];
|
2022-02-11 23:40:54 -08:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const isTypeField =
|
|
|
|
(resource: 'Group' | 'Organization' | 'Ticket' | 'User') => (arr: Zammad.Field[]) =>
|
|
|
|
arr.filter((i) => i.object === resource);
|
2022-02-11 23:40:54 -08:00
|
|
|
|
|
|
|
export const getGroupFields = isTypeField('Group');
|
|
|
|
export const getOrganizationFields = isTypeField('Organization');
|
|
|
|
export const getUserFields = isTypeField('User');
|
|
|
|
export const getTicketFields = isTypeField('Ticket');
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const getCustomFields = (arr: Zammad.Field[]) => arr.filter((i) => i.created_by_id !== 1);
|
2022-02-11 23:40:54 -08:00
|
|
|
|
|
|
|
export const getGroupCustomFields = flow(getGroupFields, getCustomFields);
|
|
|
|
export const getOrganizationCustomFields = flow(getOrganizationFields, getCustomFields);
|
|
|
|
export const getUserCustomFields = flow(getUserFields, getCustomFields);
|
|
|
|
export const getTicketCustomFields = flow(getTicketFields, getCustomFields);
|
|
|
|
|
|
|
|
export const isNotZammadFoundation = (i: Zammad.Organization) => i.name !== 'Zammad Foundation';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export const doesNotBelongToZammad = (i: Zammad.User) =>
|
|
|
|
!i.email.endsWith('@zammad.org') && i.login !== '-';
|