From ce885e5071a7027a18aa2c6cb1bea0fbd0edeca9 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Sun, 27 Jun 2021 02:55:33 -0400 Subject: [PATCH] :sparkles: Add n8n training nodes (#1898) * :zap: n8n training node * :zap: Improvements * :zap: cosmetic changes * :zap: Improvements * :zap: Formatting fix Co-authored-by: Jan Oberhauser --- .../N8nTrainingCustomerDatastore.node.ts | 162 ++++++++++++++++++ .../N8nTrainingCustomerMessenger.node.ts | 65 +++++++ .../nodes/n8nTrainingCustomerDatastore.svg | 13 ++ .../nodes/n8nTrainingCustomerMessenger.svg | 4 + packages/nodes-base/package.json | 2 + 5 files changed, 246 insertions(+) create mode 100644 packages/nodes-base/nodes/N8nTrainingCustomerDatastore.node.ts create mode 100644 packages/nodes-base/nodes/N8nTrainingCustomerMessenger.node.ts create mode 100644 packages/nodes-base/nodes/n8nTrainingCustomerDatastore.svg create mode 100644 packages/nodes-base/nodes/n8nTrainingCustomerMessenger.svg diff --git a/packages/nodes-base/nodes/N8nTrainingCustomerDatastore.node.ts b/packages/nodes-base/nodes/N8nTrainingCustomerDatastore.node.ts new file mode 100644 index 0000000000..0ccc9042c3 --- /dev/null +++ b/packages/nodes-base/nodes/N8nTrainingCustomerDatastore.node.ts @@ -0,0 +1,162 @@ +import { + IExecuteFunctions, +} from 'n8n-core'; + +import { + IDataObject, + INodeExecutionData, + INodeType, + INodeTypeDescription, +} from 'n8n-workflow'; + +const data = [ + { + id: '23423532', + name: 'Jay Gatsby', + email: 'gatsby@west-egg.com', + notes: 'Keeps asking about a green light??', + country: 'US', + created: '1925-04-10', + }, + { + id: '23423533', + name: 'José Arcadio Buendía', + email: 'jab@macondo.co', + notes: 'Lots of people named after him. Very confusing', + country: 'CO', + created: '1967-05-05', + }, + { + id: '23423534', + name: 'Max Sendak', + email: 'info@in-and-out-of-weeks.org', + notes: 'Keeps rolling his terrible eyes', + country: 'US', + created: '1963-04-09', + }, + { + id: '23423535', + name: 'Zaphod Beeblebrox', + email: 'captain@heartofgold.com', + notes: 'Felt like I was talking to more than one person', + country: null, + created: '1979-10-12', + }, + { + id: '23423536', + name: 'Edmund Pevensie', + email: 'edmund@narnia.gov', + notes: 'Passionate sailor', + country: 'UK', + created: '1950-10-16', + }, +]; + +export class N8nTrainingCustomerDatastore implements INodeType { + description: INodeTypeDescription = { + displayName: 'Customer Datastore (n8n training)', + name: 'n8nTrainingCustomerDatastore', + icon: 'file:n8nTrainingCustomerDatastore.svg', + group: ['transform'], + version: 1, + subtitle: '={{$parameter["operation"]}}', + description: 'Dummy node used for n8n training', + defaults: { + name: 'Customer Datastore', + color: '#ff6d5a', + }, + inputs: ['main'], + outputs: ['main'], + properties: [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + options: [ + { + name: 'Get One Person', + value: 'getOnePerson', + description: 'Get one person', + }, + { + name: 'Get All People', + value: 'getAllPeople', + description: 'Get all people', + }, + ], + default: 'getOnePerson', + description: 'The operation to perform.', + }, + { + displayName: 'Return All', + name: 'returnAll', + type: 'boolean', + displayOptions: { + show: { + operation: [ + 'getAllPeople', + ], + }, + }, + default: false, + description: 'If all results should be returned or only up to a given limit.', + }, + { + displayName: 'Limit', + name: 'limit', + type: 'number', + displayOptions: { + show: { + operation: [ + 'getAllPeople', + ], + returnAll: [ + false, + ], + }, + }, + typeOptions: { + minValue: 1, + maxValue: 10, + }, + default: 5, + description: 'How many results to return.', + }, + ], + }; + + async execute(this: IExecuteFunctions): Promise { + const items = this.getInputData(); + const returnData: IDataObject[] = []; + const length = (items.length as unknown) as number; + const operation = this.getNodeParameter('operation', 0) as string; + let responseData; + + for (let i = 0; i < length; i++) { + + if (operation === 'getOnePerson') { + + responseData = data[0]; + } + + if (operation === 'getAllPeople') { + + const returnAll = this.getNodeParameter('returnAll', i) as boolean; + + if (returnAll === true) { + responseData = data; + } else { + const limit = this.getNodeParameter('limit', i) as number; + responseData = data.splice(0, limit); + } + } + + if (Array.isArray(responseData)) { + returnData.push.apply(returnData, responseData as IDataObject[]); + } else if (responseData !== undefined) { + returnData.push(responseData as IDataObject); + } + } + return [this.helpers.returnJsonArray(returnData)]; + } +} diff --git a/packages/nodes-base/nodes/N8nTrainingCustomerMessenger.node.ts b/packages/nodes-base/nodes/N8nTrainingCustomerMessenger.node.ts new file mode 100644 index 0000000000..02b2048fab --- /dev/null +++ b/packages/nodes-base/nodes/N8nTrainingCustomerMessenger.node.ts @@ -0,0 +1,65 @@ +import { + IExecuteFunctions, +} from 'n8n-core'; + +import { + IDataObject, + INodeExecutionData, + INodeType, + INodeTypeDescription, +} from 'n8n-workflow'; + +export class N8nTrainingCustomerMessenger implements INodeType { + description: INodeTypeDescription = { + displayName: 'Customer Messenger (n8n training)', + name: 'n8nTrainingCustomeMessenger', + icon: 'file:n8nTrainingCustomerMessenger.svg', + group: ['transform'], + version: 1, + description: 'Dummy node used for n8n training', + defaults: { + name: 'Customer Messenger', + color: '#ff6d5a', + }, + inputs: ['main'], + outputs: ['main'], + properties: [ + { + displayName: 'Customer ID', + name: 'customerId', + type: 'string', + required: true, + default: '', + }, + { + displayName: 'Message', + name: 'message', + type: 'string', + required: true, + typeOptions: { + rows: 4, + }, + default: '', + }, + ], + }; + + async execute(this: IExecuteFunctions): Promise { + const items = this.getInputData(); + const returnData: IDataObject[] = []; + const length = (items.length as unknown) as number; + let responseData; + + for (let i = 0; i < length; i++) { + + const customerId = this.getNodeParameter('customerId', i) as string; + + const message = this.getNodeParameter('message', i) as string; + + responseData = { output: `Sent message to customer ${customerId}: ${message}` }; + + returnData.push(responseData); + } + return [this.helpers.returnJsonArray(returnData)]; + } +} diff --git a/packages/nodes-base/nodes/n8nTrainingCustomerDatastore.svg b/packages/nodes-base/nodes/n8nTrainingCustomerDatastore.svg new file mode 100644 index 0000000000..1b053772f7 --- /dev/null +++ b/packages/nodes-base/nodes/n8nTrainingCustomerDatastore.svg @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/packages/nodes-base/nodes/n8nTrainingCustomerMessenger.svg b/packages/nodes-base/nodes/n8nTrainingCustomerMessenger.svg new file mode 100644 index 0000000000..c3165491c1 --- /dev/null +++ b/packages/nodes-base/nodes/n8nTrainingCustomerMessenger.svg @@ -0,0 +1,4 @@ + + + + diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 29835e3c49..34d8f43b62 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -463,6 +463,8 @@ "dist/nodes/NoOp.node.js", "dist/nodes/Notion/Notion.node.js", "dist/nodes/Notion/NotionTrigger.node.js", + "dist/nodes/N8nTrainingCustomerDatastore.node.js", + "dist/nodes/N8nTrainingCustomerMessenger.node.js", "dist/nodes/OpenThesaurus/OpenThesaurus.node.js", "dist/nodes/OpenWeatherMap.node.js", "dist/nodes/Orbit/Orbit.node.js",