From 770dd8b25c56ff7388c7017b0ef3e832ad34c2b0 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Fri, 3 Jan 2020 14:37:15 -0500 Subject: [PATCH] :sparkles: user resource done --- .../nodes/Wordpress/PostDescription.ts | 2 +- .../nodes/Wordpress/UserDescription.ts | 567 ++++++++++++++++++ .../nodes/Wordpress/UserInterface.ts | 14 + .../nodes/Wordpress/Wordpress.node.ts | 158 ++++- 4 files changed, 737 insertions(+), 4 deletions(-) create mode 100644 packages/nodes-base/nodes/Wordpress/UserDescription.ts create mode 100644 packages/nodes-base/nodes/Wordpress/UserInterface.ts diff --git a/packages/nodes-base/nodes/Wordpress/PostDescription.ts b/packages/nodes-base/nodes/Wordpress/PostDescription.ts index 727312ed1e..752667748c 100644 --- a/packages/nodes-base/nodes/Wordpress/PostDescription.ts +++ b/packages/nodes-base/nodes/Wordpress/PostDescription.ts @@ -652,7 +652,7 @@ export const postFields = [ }, { name: 'Include Slugs', - value: 'includeSlugs', + value: 'include_slugs', }, { name: 'Title', diff --git a/packages/nodes-base/nodes/Wordpress/UserDescription.ts b/packages/nodes-base/nodes/Wordpress/UserDescription.ts new file mode 100644 index 0000000000..62b1f1b2b1 --- /dev/null +++ b/packages/nodes-base/nodes/Wordpress/UserDescription.ts @@ -0,0 +1,567 @@ +import { INodeProperties } from 'n8n-workflow'; + +export const userOperations = [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'user', + ], + }, + }, + options: [ + { + name: 'Create', + value: 'create', + description: 'Create a user', + }, + { + name: 'Update', + value: 'update', + description: 'Update a user', + }, + { + name: 'Get', + value: 'get', + description: 'Get a user', + }, + { + name: 'Get All', + value: 'getAll', + description: 'Get all users', + }, + { + name: 'Delete', + value: 'delete', + description: 'Delete a user', + }, + ], + default: 'create', + description: 'The operation to perform.', + }, +] as INodeProperties[]; + +export const userFields = [ + +/* -------------------------------------------------------------------------- */ +/* user:create */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'Username', + name: 'username', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ] + }, + }, + description: 'Login name for the user.', + }, + { + displayName: 'Name', + name: 'name', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ] + }, + }, + description: 'Display name for the user.', + }, + { + displayName: 'Fistname', + name: 'firstName', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ] + }, + }, + description: 'First name for the user.', + }, + { + displayName: 'Lastname', + name: 'lastName', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ] + }, + }, + description: 'Last name for the user.', + }, + { + displayName: 'Email', + name: 'email', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ] + }, + }, + description: 'The email address for the user.', + }, + { + displayName: 'Password', + name: 'password', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ] + }, + }, + description: 'Password for the user (never included)', + }, + { + displayName: 'Additional Fields', + name: 'additionalFields', + type: 'collection', + placeholder: 'Add Field', + default: {}, + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ], + }, + }, + options: [ + { + displayName: 'URL', + name: 'url', + type: 'string', + default: '', + description: 'URL of the user.', + }, + { + displayName: 'Description', + name: 'description', + typeOptions: { + alwaysOpenEditWindow: true, + }, + type: 'string', + default: '', + description: 'Description of the user.', + }, + { + displayName: 'Nickname', + name: 'nickname', + type: 'string', + default: '', + description: 'The nickname for the user.', + }, + { + displayName: 'Slug', + name: 'slug', + type: 'string', + default: '', + description: 'An alphanumeric identifier for the user.', + }, + ] + }, +/* -------------------------------------------------------------------------- */ +/* user:update */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'User ID', + name: 'userId', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'update', + ] + }, + }, + description: 'Unique identifier for the user.', + }, + { + displayName: 'Update Fields', + name: 'updateFields', + type: 'collection', + placeholder: 'Add Field', + default: {}, + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'update', + ], + }, + }, + options: [ + { + displayName: 'Username', + name: 'username', + type: 'string', + default: '', + description: 'Login name for the user.', + }, + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + description: 'Display name for the user.', + }, + { + displayName: 'Fistname', + name: 'firstName', + type: 'string', + default: '', + description: 'First name for the user.', + }, + { + displayName: 'Lastname', + name: 'lastName', + type: 'string', + default: '', + description: 'Last name for the user.', + }, + { + displayName: 'Email', + name: 'email', + type: 'string', + default: '', + description: 'The email address for the user.', + }, + { + displayName: 'Password', + name: 'password', + type: 'password', + default: '', + description: 'Password for the user (never included)', + }, + { + displayName: 'URL', + name: 'url', + type: 'string', + default: '', + description: 'URL of the user.', + }, + { + displayName: 'Description', + name: 'description', + typeOptions: { + alwaysOpenEditWindow: true, + }, + type: 'string', + default: '', + description: 'Description of the user.', + }, + { + displayName: 'Nickname', + name: 'nickname', + type: 'string', + default: '', + description: 'The nickname for the user.', + }, + { + displayName: 'Slug', + name: 'slug', + type: 'string', + default: '', + description: 'An alphanumeric identifier for the user.', + }, + ] + }, +/* -------------------------------------------------------------------------- */ +/* user:get */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'User ID', + name: 'userId', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'get', + ] + }, + }, + description: 'Unique identifier for the user.', + }, + { + displayName: 'Options', + name: 'options', + type: 'collection', + placeholder: 'Add Option', + default: {}, + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'get', + ], + }, + }, + options: [ + { + displayName: 'Context', + name: 'context', + type: 'options', + options: [ + { + name: 'View', + value: 'view', + }, + { + name: 'Embed', + value: 'embed', + }, + { + name: 'Edit', + value: 'edit', + }, + ], + default: 'view', + description: 'Scope under which the request is made; determines fields present in response.', + }, + ] + }, +/* -------------------------------------------------------------------------- */ +/* user:getAll */ +/* -------------------------------------------------------------------------- */ +{ + displayName: 'Return All', + name: 'returnAll', + type: 'boolean', + displayOptions: { + show: { + resource: [ + 'user', + ], + 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: [ + 'user', + ], + operation: [ + 'getAll', + ], + returnAll: [ + false, + ], + }, + }, + typeOptions: { + minValue: 1, + maxValue: 10, + }, + default: 5, + description: 'How many results to return.', +}, +{ + displayName: 'Filters', + name: 'filters', + type: 'collection', + placeholder: 'Add Filter', + default: {}, + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'getAll', + ], + }, + }, + options: [ + { + displayName: 'Context', + name: 'context', + type: 'options', + options: [ + { + name: 'View', + value: 'view', + }, + { + name: 'Embed', + value: 'embed', + }, + { + name: 'Edit', + value: 'edit', + }, + ], + default: [], + description: 'Scope under which the request is made; determines fields present in response.', + }, + { + displayName: 'Order By', + name: 'orderBy', + type: 'options', + options: [ + { + name: 'Name', + value: 'name', + }, + { + name: 'ID', + value: 'id', + }, + { + name: 'Include', + value: 'include', + }, + { + name: 'Registered Date', + value: 'registered_date', + }, + { + name: 'Slug', + value: 'slug', + }, + { + name: 'Include Slugs', + value: 'include_slugs', + }, + { + name: 'Email', + value: 'email', + }, + { + name: 'URL', + value: 'url', + }, + ], + default: [], + description: 'Sort collection by object attribute.', + }, + { + displayName: 'Order', + name: 'order', + type: 'options', + options: [ + { + name: 'Desc', + value: 'desc', + }, + { + name: 'Asc', + value: 'asc', + }, + ], + default: 'desc', + description: 'Order sort attribute ascending or descending.', + }, + { + displayName: 'Search', + name: 'search', + type: 'string', + default: '', + description: 'Limit results to those matching a string.', + }, + { + displayName: 'Who', + name: 'who', + type: 'options', + options: [ + { + name: 'Authors', + value: 'authors', + }, + ], + default: 'authors', + description: 'Limit result set to users who are considered authors.', + }, + ] +}, +/* -------------------------------------------------------------------------- */ +/* user:delete */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'Reassign', + name: 'reassign', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'delete', + ] + }, + }, + description: `Reassign the deleted user's posts and links to this user ID.`, + }, +] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Wordpress/UserInterface.ts b/packages/nodes-base/nodes/Wordpress/UserInterface.ts new file mode 100644 index 0000000000..9bcf3cdeac --- /dev/null +++ b/packages/nodes-base/nodes/Wordpress/UserInterface.ts @@ -0,0 +1,14 @@ + +export interface IUser { + id?: number; + username?: string; + name?: string; + first_name?: string; + nickname?: string; + slug?: string; + last_name?: string; + email?: string; + url?: string; + description?: string; + password?: string; +} diff --git a/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts b/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts index 488664ed8c..f8e784c16d 100644 --- a/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts +++ b/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts @@ -10,15 +10,23 @@ import { INodePropertyOptions, } from 'n8n-workflow'; import { - wordpressApiRequest, wordpressApiRequestAllItems, + wordpressApiRequest, + wordpressApiRequestAllItems, } from './GenericFunctions'; import { postOperations, postFields, } from './PostDescription'; +import { + userOperations, + userFields, +} from './UserDescription'; import { IPost, } from './PostInterface'; +import { + IUser, +} from './UserInterface'; export class Wordpress implements INodeType { description: INodeTypeDescription = { @@ -50,7 +58,12 @@ export class Wordpress implements INodeType { { name: 'Post', value: 'post', - description: ``, + description: '', + }, + { + name: 'User', + value: 'user', + description: '', }, ], default: 'post', @@ -58,6 +71,8 @@ export class Wordpress implements INodeType { }, ...postOperations, ...postFields, + ...userOperations, + ...userFields, ], }; @@ -233,7 +248,6 @@ export class Wordpress implements INodeType { if (operation === 'get') { const postId = this.getNodeParameter('postId', i) as string; const options = this.getNodeParameter('options', i) as IDataObject; - options.id = postId; if (options.password) { qs.password = options.password as string; } @@ -308,6 +322,144 @@ export class Wordpress implements INodeType { } } } + if (resource === 'user') { + //https://developer.wordpress.org/rest-api/reference/users/#create-a-user + if (operation === 'create') { + const name = this.getNodeParameter('name', i) as string; + const username = this.getNodeParameter('username', i) as string; + const firstName = this.getNodeParameter('firstName', i) as string; + const lastName = this.getNodeParameter('lastName', i) as string; + const email = this.getNodeParameter('email', i) as string; + const password = this.getNodeParameter('password', i) as string; + const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; + const body: IUser = { + name, + username, + first_name: firstName, + last_name: lastName, + email, + password, + }; + // if (operation === 'update') { + // const userId = this.getNodeParameter('userId', i) as number; + // body.id = userId; + // } + if (additionalFields.url) { + body.url = additionalFields.url as string; + } + if (additionalFields.description) { + body.description = additionalFields.description as string; + } + if (additionalFields.nickname) { + body.nickname = additionalFields.nickname as string; + } + if (additionalFields.slug) { + body.slug = additionalFields.slug as string; + } + try{ + responseData = await wordpressApiRequest.call(this, 'POST', '/users', body); + } catch (err) { + throw new Error(`Wordpress Error: ${err.message}`); + } + } + //https://developer.wordpress.org/rest-api/reference/users/#update-a-user + if (operation === 'update') { + const userId = this.getNodeParameter('userId', i) as number; + const updateFields = this.getNodeParameter('updateFields', i) as IDataObject; + const body: IUser = { + id: userId, + }; + if (updateFields.name) { + body.name = updateFields.name as string; + } + if (updateFields.firstName) { + body.first_name = updateFields.firstName as string; + } + if (updateFields.lastName) { + body.last_name = updateFields.lastName as string; + } + if (updateFields.email) { + body.email = updateFields.email as string; + } + if (updateFields.password) { + body.password = updateFields.password as string; + } + if (updateFields.username) { + body.username = updateFields.username as string; + } + if (updateFields.url) { + body.url = updateFields.url as string; + } + if (updateFields.description) { + body.description = updateFields.description as string; + } + if (updateFields.nickname) { + body.nickname = updateFields.nickname as string; + } + if (updateFields.slug) { + body.slug = updateFields.slug as string; + } + try{ + responseData = await wordpressApiRequest.call(this, 'POST', `/users/${userId}`, body); + } catch (err) { + throw new Error(`Wordpress Error: ${err.message}`); + } + } + //https://developer.wordpress.org/rest-api/reference/users/#retrieve-a-user + if (operation === 'get') { + const userId = this.getNodeParameter('userId', i) as string; + const options = this.getNodeParameter('options', i) as IDataObject; + if (options.context) { + qs.context = options.context as string; + } + try { + responseData = await wordpressApiRequest.call(this,'GET', `/users/${userId}`, {}, qs); + } catch (err) { + throw new Error(`Wordpress Error: ${err.message}`); + } + } + //https://developer.wordpress.org/rest-api/reference/users/#list-users + if (operation === 'getAll') { + const returnAll = this.getNodeParameter('returnAll', i) as boolean; + const filters = this.getNodeParameter('filters', i) as IDataObject; + if (filters.context) { + qs.context = filters.context as string; + } + if (filters.orderBy) { + qs.orderby = filters.orderBy as string; + } + if (filters.order) { + qs.order = filters.order as string; + } + if (filters.search) { + qs.search = filters.search as string; + } + if (filters.who) { + qs.who = filters.who as string; + } + try { + if (returnAll === true) { + responseData = await wordpressApiRequestAllItems.call(this, 'GET', '/users', {}, qs); + } else { + qs.per_page = this.getNodeParameter('limit', i) as number; + responseData = await wordpressApiRequest.call(this, 'GET', '/users', {}, qs); + } + } catch (err) { + throw new Error(`Wordpress Error: ${err.message}`); + } + } + //https://developer.wordpress.org/rest-api/reference/users/#delete-a-user + if (operation === 'delete') { + const reassign = this.getNodeParameter('reassign', i) as string; + qs.reassign = reassign; + qs.force = true; + try { + responseData = await wordpressApiRequest.call(this, 'DELETE', `/users/me`, {}, qs); + } catch (err) { + throw new Error(`Wordpress Error: ${err.message}`); + } + } + } if (Array.isArray(responseData)) { returnData.push.apply(returnData, responseData as IDataObject[]); } else {