n8n/packages/nodes-base/nodes/Kitemaker/Kitemaker.node.ts
Iván Ovejero 6dcdb30bf4
refactor: Apply more nodelinting rules (#3324)
* ✏️ 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>
2022-05-20 23:47:24 +02:00

327 lines
7.9 KiB
TypeScript

import {
IExecuteFunctions
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription
} from 'n8n-workflow';
import {
organizationOperations,
spaceFields,
spaceOperations,
userFields,
userOperations,
workItemFields,
workItemOperations,
} from './descriptions';
import {
createLoadOptions,
kitemakerRequest,
kitemakerRequestAllItems,
} from './GenericFunctions';
import {
getAllSpaces,
getAllUsers,
getAllWorkItems,
getLabels,
getOrganization,
getSpaces,
getStatuses,
getUsers,
getWorkItem,
getWorkItems,
} from './queries';
import {
createWorkItem,
editWorkItem,
} from './mutations';
export class Kitemaker implements INodeType {
description: INodeTypeDescription = {
displayName: 'Kitemaker',
name: 'kitemaker',
icon: 'file:kitemaker.svg',
group: ['input'],
version: 1,
subtitle: '={{$parameter["resource"] + ": " + $parameter["operation"]}}',
description: 'Consume the Kitemaker GraphQL API',
defaults: {
name: 'Kitemaker',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'kitemakerApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Organization',
value: 'organization',
},
{
name: 'Space',
value: 'space',
},
{
name: 'User',
value: 'user',
},
{
name: 'Work Item',
value: 'workItem',
},
],
default: 'workItem',
required: true,
},
...organizationOperations,
...spaceOperations,
...spaceFields,
...userOperations,
...userFields,
...workItemOperations,
...workItemFields,
],
};
methods = {
loadOptions: {
async getLabels(this: ILoadOptionsFunctions) {
const responseData = await kitemakerRequest.call(this, { query: getLabels });
const { data: { organization: { spaces } } } = responseData;
return createLoadOptions(spaces[0].labels);
},
async getSpaces(this: ILoadOptionsFunctions) {
const responseData = await kitemakerRequest.call(this, { query: getSpaces });
const { data: { organization: { spaces } } } = responseData;
return createLoadOptions(spaces);
},
async getStatuses(this: ILoadOptionsFunctions) {
const spaceId = this.getNodeParameter('spaceId', 0) as string;
if (!spaceId.length) {
throw new Error('Please choose a space to set for the work item to create.');
}
const responseData = await kitemakerRequest.call(this, { query: getStatuses });
const { data: { organization: { spaces } } } = responseData;
const space = spaces.find((e: { [x: string]: string; }) => e.id === spaceId);
return createLoadOptions(space.statuses);
},
async getUsers(this: ILoadOptionsFunctions) {
const responseData = await kitemakerRequest.call(this, { query: getUsers });
const { data: { organization: { users } } } = responseData;
return createLoadOptions(users);
},
async getWorkItems(this: ILoadOptionsFunctions) {
const spaceId = this.getNodeParameter('spaceId', 0) as string;
const responseData = await kitemakerRequest.call(this, {
query: getWorkItems,
variables: { spaceId },
});
const { data: { workItems: { workItems } } } = responseData;
return createLoadOptions(workItems);
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
let responseData;
const returnData: IDataObject[] = [];
// https://github.com/kitemakerhq/docs/blob/main/kitemaker.graphql
for (let i = 0; i < items.length; i++) {
if (resource === 'organization') {
// *********************************************************************
// organization
// *********************************************************************
if (operation === 'get') {
// ----------------------------------
// organization: get
// ----------------------------------
responseData = await kitemakerRequest.call(this, {
query: getOrganization,
});
returnData.push(responseData.data.organization);
}
} else if (resource === 'space') {
// *********************************************************************
// space
// *********************************************************************
if (operation === 'getAll') {
// ----------------------------------
// space: getAll
// ----------------------------------
const allItems = await kitemakerRequestAllItems.call(this, {
query: getAllSpaces,
variables: {},
});
returnData.push(...allItems);
}
} else if (resource === 'user') {
// *********************************************************************
// user
// *********************************************************************
if (operation === 'getAll') {
// ----------------------------------
// user: getAll
// ----------------------------------
const allItems = await kitemakerRequestAllItems.call(this, {
query: getAllUsers,
variables: {},
});
returnData.push(...allItems);
}
} else if (resource === 'workItem') {
// *********************************************************************
// workItem
// *********************************************************************
if (operation === 'create') {
// ----------------------------------
// workItem: create
// ----------------------------------
const input = {
title: this.getNodeParameter('title', i) as string,
statusId: this.getNodeParameter('statusId', i) as string[],
};
if (!input.statusId.length) {
throw new Error('Please enter a status to set for the work item to create.');
}
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (Object.keys(additionalFields).length) {
Object.assign(input, additionalFields);
}
responseData = await kitemakerRequest.call(this, {
query: createWorkItem,
variables: { input },
});
returnData.push(responseData.data.createWorkItem.workItem);
} else if (operation === 'get') {
// ----------------------------------
// workItem: get
// ----------------------------------
const workItemId = this.getNodeParameter('workItemId', i) as string;
responseData = await kitemakerRequest.call(this, {
query: getWorkItem,
variables: { workItemId },
});
returnData.push(responseData.data.workItem);
} else if (operation === 'getAll') {
// ----------------------------------
// workItem: getAll
// ----------------------------------
const allItems = await kitemakerRequestAllItems.call(this, {
query: getAllWorkItems,
variables: {
spaceId: this.getNodeParameter('spaceId', i) as string,
},
});
returnData.push(...allItems);
} else if (operation === 'update') {
// ----------------------------------
// workItem: update
// ----------------------------------
const input = {
id: this.getNodeParameter('workItemId', i),
};
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
if (!Object.keys(updateFields).length) {
throw new Error('Please enter at least one field to update for the work item.');
}
Object.assign(input, updateFields);
responseData = await kitemakerRequest.call(this, {
query: editWorkItem,
variables: { input },
});
returnData.push(responseData.data.editWorkItem.workItem);
}
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}