mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08:00
6dcdb30bf4
* ✏️ Alphabetize lint rules * 🔥 Remove duplicates * ⚡ Update `lintfix` script * 👕 Apply `node-param-operation-without-no-data-expression` (#3329) * 👕 Apply `node-param-operation-without-no-data-expression` * 👕 Add exceptions * 👕 Apply `node-param-description-weak` (#3328) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-value-duplicate` (#3331) * 👕 Apply `node-param-description-miscased-json` (#3337) * 👕 Apply `node-param-display-name-excess-inner-whitespace` (#3335) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-type-options-missing-from-limit` (#3336) * Rule workig as intended * ✏️ Uncomment rules Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-name-duplicate` (#3338) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-description-wrong-for-simplify` (#3334) * ⚡ fix * ⚡ exceptions * ⚡ changed rule ignoring from file to line * 👕 Apply `node-param-resource-without-no-data-expression` (#3339) * 👕 Apply `node-param-display-name-untrimmed` (#3341) * 👕 Apply `node-param-display-name-miscased-id` (#3340) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-resource-with-plural-option` (#3342) * 👕 Apply `node-param-description-wrong-for-upsert` (#3333) * ⚡ fix * ⚡ replaced record with contact in description * ⚡ fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-description-identical-to-name` (#3343) * 👕 Apply `node-param-option-name-containing-star` (#3347) * 👕 Apply `node-param-display-name-wrong-for-update-fields` (#3348) * 👕 Apply `node-param-option-name-wrong-for-get-all` (#3345) * ⚡ fix * ⚡ exceptions * 👕 Apply node-param-display-name-wrong-for-simplify (#3344) * Rule working as intended * Uncomented other rules * 👕 Undo and add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * ⚡ Alphabetize lint rules * ⚡ Restore `lintfix` script Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
824 lines
26 KiB
TypeScript
824 lines
26 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
ILoadOptionsFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
INodeExecutionData,
|
|
INodePropertyOptions,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
JsonObject,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
mapEndpoint,
|
|
serviceNowApiRequest,
|
|
serviceNowRequestAllItems,
|
|
sortData
|
|
} from './GenericFunctions';
|
|
|
|
import {
|
|
businessServiceFields,
|
|
businessServiceOperations,
|
|
} from './BusinessServiceDescription';
|
|
|
|
import {
|
|
configurationItemsFields,
|
|
configurationItemsOperations,
|
|
} from './ConfigurationItemsDescription';
|
|
|
|
import {
|
|
departmentFields,
|
|
departmentOperations,
|
|
} from './DepartmentDescription';
|
|
|
|
import {
|
|
dictionaryFields,
|
|
dictionaryOperations,
|
|
} from './DictionaryDescription';
|
|
|
|
import {
|
|
incidentFields,
|
|
incidentOperations,
|
|
} from './IncidentDescription';
|
|
|
|
import {
|
|
tableRecordFields,
|
|
tableRecordOperations,
|
|
} from './TableRecordDescription';
|
|
|
|
import {
|
|
userFields,
|
|
userOperations,
|
|
} from './UserDescription';
|
|
|
|
import {
|
|
userGroupFields,
|
|
userGroupOperations,
|
|
} from './UserGroupDescription';
|
|
|
|
import {
|
|
userRoleFields,
|
|
userRoleOperations,
|
|
} from './UserRoleDescription';
|
|
|
|
export class ServiceNow implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'ServiceNow',
|
|
name: 'serviceNow',
|
|
icon: 'file:servicenow.svg',
|
|
group: ['output'],
|
|
version: 1,
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
description: 'Consume ServiceNow API',
|
|
defaults: {
|
|
name: 'ServiceNow',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'serviceNowOAuth2Api',
|
|
required: true,
|
|
displayOptions: {
|
|
show: {
|
|
authentication: [
|
|
'oAuth2',
|
|
],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: 'serviceNowBasicApi',
|
|
required: true,
|
|
displayOptions: {
|
|
show: {
|
|
authentication: [
|
|
'basicAuth',
|
|
],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Authentication',
|
|
name: 'authentication',
|
|
type: 'options',
|
|
options: [
|
|
{
|
|
name: 'Basic Auth',
|
|
value: 'basicAuth',
|
|
},
|
|
{
|
|
name: 'OAuth2',
|
|
value: 'oAuth2',
|
|
},
|
|
],
|
|
default: 'oAuth2',
|
|
description: 'Authentication method to use',
|
|
},
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'options',
|
|
noDataExpression: true,
|
|
options: [
|
|
{
|
|
name: 'Business Service',
|
|
value: 'businessService',
|
|
},
|
|
{
|
|
name: 'Configuration Item',
|
|
value: 'configurationItems',
|
|
},
|
|
{
|
|
name: 'Department',
|
|
value: 'department',
|
|
},
|
|
{
|
|
name: 'Dictionary',
|
|
value: 'dictionary',
|
|
},
|
|
{
|
|
name: 'Incident',
|
|
value: 'incident',
|
|
},
|
|
{
|
|
name: 'Table Record',
|
|
value: 'tableRecord',
|
|
},
|
|
{
|
|
name: 'User',
|
|
value: 'user',
|
|
},
|
|
{
|
|
name: 'User Group',
|
|
value: 'userGroup',
|
|
},
|
|
{
|
|
name: 'User Role',
|
|
value: 'userRole',
|
|
},
|
|
],
|
|
default: 'user',
|
|
},
|
|
|
|
// BUSINESS SERVICE
|
|
...businessServiceOperations,
|
|
...businessServiceFields,
|
|
// CONFIGURATION ITEMS
|
|
...configurationItemsOperations,
|
|
...configurationItemsFields,
|
|
// DEPARTMENT
|
|
...departmentOperations,
|
|
...departmentFields,
|
|
// DICTIONARY
|
|
...dictionaryOperations,
|
|
...dictionaryFields,
|
|
// INCIDENT
|
|
...incidentOperations,
|
|
...incidentFields,
|
|
// TABLE RECORD
|
|
...tableRecordOperations,
|
|
...tableRecordFields,
|
|
// USER
|
|
...userOperations,
|
|
...userFields,
|
|
// USER GROUP
|
|
...userGroupOperations,
|
|
...userGroupFields,
|
|
// USER ROLE
|
|
...userRoleOperations,
|
|
...userRoleFields,
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
loadOptions: {
|
|
async getTables(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const response = await serviceNowApiRequest.call(this, 'GET', `/now/doc/table/schema`, {}, {});
|
|
for (const table of response.result) {
|
|
returnData.push({
|
|
name: table.label,
|
|
value: table.value,
|
|
description: table.value,
|
|
});
|
|
}
|
|
return sortData(returnData);
|
|
},
|
|
// Get all the table column to display them to user
|
|
async getColumns(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
const returnData: INodePropertyOptions[] = [];
|
|
let tableName;
|
|
if (resource === 'tableRecord') {
|
|
tableName = this.getNodeParameter('tableName') as string;
|
|
} else {
|
|
tableName = mapEndpoint(resource, operation);
|
|
}
|
|
|
|
const qs = {
|
|
sysparm_query: `name=${tableName}`,
|
|
sysparm_fields: 'column_label,element',
|
|
};
|
|
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/sys_dictionary`, {}, qs);
|
|
for (const column of response.result) {
|
|
if (column.element) {
|
|
returnData.push({
|
|
name: column.column_label,
|
|
value: column.element,
|
|
});
|
|
}
|
|
}
|
|
return sortData(returnData);
|
|
},
|
|
async getBusinessServices(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const qs = {
|
|
sysparm_fields: 'name,sys_id',
|
|
};
|
|
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/cmdb_ci_service`, {}, qs);
|
|
|
|
for (const column of response.result) {
|
|
returnData.push({
|
|
name: column.name,
|
|
value: column.sys_id,
|
|
});
|
|
}
|
|
return sortData(returnData);
|
|
},
|
|
async getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
const qs = {
|
|
sysparm_fields: 'sys_id,user_name',
|
|
};
|
|
if (resource === 'incident' && operation === 'create') {
|
|
const additionalFields = this.getNodeParameter('additionalFields') as IDataObject;
|
|
const group = additionalFields.assignment_group;
|
|
|
|
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user_grmember', {}, {
|
|
sysparm_query: `group=${group}^`,
|
|
});
|
|
|
|
for (const column of response) {
|
|
if (column.user) {
|
|
|
|
const responseData = await serviceNowApiRequest.call(this, 'GET', `/now/table/sys_user/${column.user.value}`, {}, {});
|
|
const user = responseData.result;
|
|
|
|
returnData.push({
|
|
name: user.user_name,
|
|
value: user.sys_id,
|
|
});
|
|
}
|
|
}
|
|
} else {
|
|
|
|
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user', {}, qs);
|
|
|
|
for (const column of response) {
|
|
if (column.user_name) {
|
|
returnData.push({
|
|
name: column.user_name,
|
|
value: column.sys_id,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
return sortData(returnData);
|
|
},
|
|
async getAssignmentGroups(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const qs = {
|
|
sysparm_fields: 'sys_id,name',
|
|
};
|
|
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user_group', {}, qs);
|
|
|
|
for (const column of response) {
|
|
if (column.name) {
|
|
returnData.push({
|
|
name: column.name,
|
|
value: column.sys_id,
|
|
});
|
|
}
|
|
}
|
|
return sortData(returnData);
|
|
},
|
|
async getUserRoles(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const qs = {
|
|
sysparm_fields: 'sys_id,name',
|
|
};
|
|
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user_role', {}, qs);
|
|
|
|
for (const column of response) {
|
|
if (column.name) {
|
|
returnData.push({
|
|
name: column.name,
|
|
value: column.sys_id,
|
|
});
|
|
}
|
|
}
|
|
return sortData(returnData);
|
|
},
|
|
async getConfigurationItems(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const qs = {
|
|
sysparm_fields: 'sys_id,name,sys_class_name',
|
|
};
|
|
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/cmdb_ci', {}, qs);
|
|
|
|
for (const column of response) {
|
|
if (column.name) {
|
|
returnData.push({
|
|
name: column.name,
|
|
value: column.sys_id,
|
|
description: column.sys_class_name,
|
|
});
|
|
}
|
|
}
|
|
return sortData(returnData);
|
|
},
|
|
async getIncidentCategories(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const qs = {
|
|
sysparm_fields: 'label,value',
|
|
sysparm_query: 'element=category^name=incident',
|
|
};
|
|
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_choice', {}, qs);
|
|
|
|
for (const column of response) {
|
|
returnData.push({
|
|
name: column.label,
|
|
value: column.value,
|
|
});
|
|
|
|
}
|
|
return sortData(returnData);
|
|
},
|
|
async getIncidentSubcategories(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const operation = this.getNodeParameter('operation');
|
|
let category;
|
|
if (operation === 'update') {
|
|
const updateFields = this.getNodeParameter('updateFields') as IDataObject;
|
|
category = updateFields.category;
|
|
} else {
|
|
const additionalFields = this.getNodeParameter('additionalFields') as IDataObject;
|
|
category = additionalFields.category;
|
|
}
|
|
const qs = {
|
|
sysparm_fields: 'label,value',
|
|
sysparm_query: `name=incident^element=subcategory^dependent_value=${category}`,
|
|
};
|
|
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_choice', {}, qs);
|
|
|
|
for (const column of response) {
|
|
returnData.push({
|
|
name: column.label,
|
|
value: column.value,
|
|
});
|
|
}
|
|
|
|
return sortData(returnData);
|
|
},
|
|
async getIncidentStates(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const qs = {
|
|
sysparm_fields: 'label,value',
|
|
sysparm_query: 'element=state^name=incident',
|
|
};
|
|
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_choice', {}, qs);
|
|
|
|
for (const column of response) {
|
|
returnData.push({
|
|
name: column.label,
|
|
value: column.value,
|
|
});
|
|
|
|
}
|
|
return sortData(returnData);
|
|
},
|
|
async getIncidentResolutionCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const qs = {
|
|
sysparm_fields: 'label,value',
|
|
sysparm_query: 'element=close_code^name=incident',
|
|
};
|
|
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_choice', {}, qs);
|
|
|
|
for (const column of response) {
|
|
returnData.push({
|
|
name: column.label,
|
|
value: column.value,
|
|
});
|
|
|
|
}
|
|
return sortData(returnData);
|
|
},
|
|
async getIncidentHoldReasons(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const qs = {
|
|
sysparm_fields: 'label,value',
|
|
sysparm_query: 'element=hold_reason^name=incident',
|
|
};
|
|
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_choice', {}, qs);
|
|
|
|
for (const column of response) {
|
|
returnData.push({
|
|
name: column.label,
|
|
value: column.value,
|
|
});
|
|
|
|
}
|
|
return sortData(returnData);
|
|
},
|
|
},
|
|
};
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
const returnData: IDataObject[] = [];
|
|
const length = items.length;
|
|
let responseData = {};
|
|
let qs: IDataObject;
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
try {
|
|
if (resource === 'businessService') {
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
qs.sysparm_limit = limit;
|
|
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/cmdb_ci_service', {}, qs);
|
|
responseData = response.result;
|
|
} else {
|
|
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/cmdb_ci_service', {}, qs);
|
|
}
|
|
|
|
}
|
|
} else if (resource === 'configurationItems') {
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
qs.sysparm_limit = limit;
|
|
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/cmdb_ci', {}, qs);
|
|
responseData = response.result;
|
|
} else {
|
|
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/cmdb_ci', {}, qs);
|
|
}
|
|
|
|
}
|
|
} else if (resource === 'department') {
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
qs.sysparm_limit = limit;
|
|
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/cmn_department', {}, qs);
|
|
responseData = response.result;
|
|
} else {
|
|
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/cmn_department', {}, qs);
|
|
}
|
|
|
|
}
|
|
} else if (resource === 'dictionary') {
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
qs.sysparm_limit = limit;
|
|
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/sys_dictionary', {}, qs);
|
|
responseData = response.result;
|
|
} else {
|
|
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_dictionary', {}, qs);
|
|
}
|
|
|
|
}
|
|
} else if (resource === 'incident') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const shortDescription = this.getNodeParameter('short_description', i) as string;
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
const body = {
|
|
short_description: shortDescription,
|
|
...additionalFields,
|
|
};
|
|
|
|
const response = await serviceNowApiRequest.call(this, 'POST', `/now/table/incident`, body);
|
|
responseData = response.result;
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
responseData = await serviceNowApiRequest.call(this, 'DELETE', `/now/table/incident/${id}`);
|
|
responseData = { success: true };
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/incident/${id}`, {}, qs);
|
|
responseData = response.result;
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
qs.sysparm_limit = limit;
|
|
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/incident`, {}, qs);
|
|
responseData = response.result;
|
|
} else {
|
|
responseData = await serviceNowRequestAllItems.call(this, 'GET', `/now/table/incident`, {}, qs);
|
|
}
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
const body = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
const response = await serviceNowApiRequest.call(this, 'PATCH', `/now/table/incident/${id}`, body);
|
|
responseData = response.result;
|
|
|
|
} else {
|
|
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
|
|
}
|
|
} else if (resource === 'tableRecord') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
|
const dataToSend = this.getNodeParameter('dataToSend', i) as string;
|
|
let body = {};
|
|
|
|
if (dataToSend === 'mapInput') {
|
|
const inputsToIgnore = (this.getNodeParameter('inputsToIgnore', i) as string).split(',').map(field => field.trim());
|
|
body = Object.entries(items[i].json)
|
|
.filter(([key]) => !inputsToIgnore.includes(key))
|
|
.reduce((obj, [key, val]) => Object.assign(obj, { [key]: val }), {});
|
|
} else if (dataToSend === 'columns') {
|
|
const fieldsToSend = this.getNodeParameter('fieldsToSend', i) as {
|
|
field: IDataObject[]
|
|
};
|
|
body = fieldsToSend.field.reduce((obj, field) => {
|
|
obj[field.column as string] = field.value;
|
|
return obj;
|
|
}, {});
|
|
}
|
|
|
|
const response = await serviceNowApiRequest.call(this, 'POST', `/now/table/${tableName}`, body);
|
|
responseData = response.result;
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
responseData = await serviceNowApiRequest.call(this, 'DELETE', `/now/table/${tableName}/${id}`);
|
|
responseData = { success: true };
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/${tableName}/${id}`, {}, qs);
|
|
responseData = response.result;
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
qs.sysparm_limit = limit;
|
|
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/${tableName}`, {}, qs);
|
|
responseData = response.result;
|
|
} else {
|
|
responseData = await serviceNowRequestAllItems.call(this, 'GET', `/now/table/${tableName}`, {}, qs);
|
|
}
|
|
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
const dataToSend = this.getNodeParameter('dataToSend', i) as string;
|
|
let body = {};
|
|
|
|
if (dataToSend === 'mapInput') {
|
|
const inputsToIgnore = (this.getNodeParameter('inputsToIgnore', i) as string).split(',').map(field => field.trim());
|
|
body = Object.entries(items[i].json)
|
|
.filter(([key]) => !inputsToIgnore.includes(key))
|
|
.reduce((obj, [key, val]) => Object.assign(obj, { [key]: val }), {});
|
|
} else if (dataToSend === 'columns') {
|
|
const fieldsToSend = this.getNodeParameter('fieldsToSend', i) as {
|
|
field: IDataObject[]
|
|
};
|
|
body = fieldsToSend.field.reduce((obj, field) => {
|
|
obj[field.column as string] = field.value;
|
|
return obj;
|
|
}, {});
|
|
}
|
|
|
|
const response = await serviceNowApiRequest.call(this, 'PATCH', `/now/table/${tableName}/${id}`, body);
|
|
responseData = response.result;
|
|
|
|
} else {
|
|
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
|
|
}
|
|
} else if (resource === 'user') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const body = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
const response = await serviceNowApiRequest.call(this, 'POST', '/now/table/sys_user', body);
|
|
responseData = response.result;
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
responseData = await serviceNowApiRequest.call(this, 'DELETE', `/now/table/sys_user/${id}`);
|
|
responseData = { success: true };
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
const getOption = this.getNodeParameter('getOption', i) as string;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
if (getOption === 'id') {
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/sys_user/${id}`, {}, qs);
|
|
responseData = response.result;
|
|
} else {
|
|
const userName = this.getNodeParameter('user_name', i) as string;
|
|
qs.sysparm_query = `user_name=${userName}`;
|
|
qs.sysparm_limit = 1;
|
|
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/sys_user', {}, qs);
|
|
responseData = response.result;
|
|
}
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
qs.sysparm_limit = limit;
|
|
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/sys_user', {}, qs);
|
|
responseData = response.result;
|
|
} else {
|
|
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user', {}, qs);
|
|
}
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
const body = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
const response = await serviceNowApiRequest.call(this, 'PATCH', `/now/table/sys_user/${id}`, body);
|
|
responseData = response.result;
|
|
|
|
} else {
|
|
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
|
|
}
|
|
} else if (resource === 'userGroup') {
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
qs.sysparm_limit = limit;
|
|
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/sys_user_group', {}, qs);
|
|
responseData = response.result;
|
|
} else {
|
|
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user_group', {}, qs);
|
|
}
|
|
} else {
|
|
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
|
|
}
|
|
} else if (resource === 'userRole') {
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
qs = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
}
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
qs.sysparm_limit = limit;
|
|
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/sys_user_role', {}, qs);
|
|
responseData = response.result;
|
|
} else {
|
|
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user_role', {}, qs);
|
|
}
|
|
} else {
|
|
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
|
|
}
|
|
} else {
|
|
throw new NodeOperationError(this.getNode(), `The resource "${resource}" is not known!`);
|
|
}
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
returnData.push({ error: (error as JsonObject).message });
|
|
continue;
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
|
|
Array.isArray(responseData)
|
|
? returnData.push(...responseData)
|
|
: returnData.push(responseData);
|
|
}
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|