Add customer resource to WooCommerce node (#2031)

This commit is contained in:
Iván Ovejero 2021-08-01 21:33:21 +02:00 committed by GitHub
parent d5418044bc
commit 597305945f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 586 additions and 5 deletions

View file

@ -32,6 +32,10 @@ import {
snakeCase,
} from 'change-case';
import {
omit
} from 'lodash';
export async function woocommerceApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('wooCommerceApi');
if (credentials === undefined) {
@ -144,3 +148,18 @@ export function toSnakeCase(data:
}
}
}
export function adjustMetadata(fields: IDataObject & Metadata) {
if (!fields.meta_data) return fields;
return {
...omit(fields, ['meta_data']),
meta_data: fields.meta_data.meta_data_fields,
};
}
type Metadata = {
meta_data?: {
meta_data_fields: Array<{ key: string; value: string }>;
}
};

View file

@ -10,6 +10,7 @@ import {
INodeTypeDescription,
} from 'n8n-workflow';
import {
adjustMetadata,
setMetadata,
toSnakeCase,
woocommerceApiRequest,
@ -37,11 +38,16 @@ import {
IShoppingLine,
} from './OrderInterface';
import {
customerFields,
customerOperations,
} from './descriptions';
export class WooCommerce implements INodeType {
description: INodeTypeDescription = {
displayName: 'WooCommerce',
name: 'wooCommerce',
icon: 'file:wooCommerce.png',
icon: 'file:wooCommerce.svg',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
@ -64,6 +70,10 @@ export class WooCommerce implements INodeType {
name: 'resource',
type: 'options',
options: [
{
name: 'Customer',
value: 'customer',
},
{
name: 'Order',
value: 'order',
@ -76,6 +86,8 @@ export class WooCommerce implements INodeType {
default: 'product',
description: 'Resource to consume.',
},
...customerOperations,
...customerFields,
...productOperations,
...productFields,
...orderOperations,
@ -128,7 +140,111 @@ export class WooCommerce implements INodeType {
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) {
if (resource === 'product') {
if (resource === 'customer') {
// **********************************************************************
// customer
// **********************************************************************
// https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#customer-properties
if (operation === 'create') {
// ----------------------------------------
// customer: create
// ----------------------------------------
// https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#create-a-customer
const body = {
email: this.getNodeParameter('email', i),
} as IDataObject;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (Object.keys(additionalFields).length) {
Object.assign(body, adjustMetadata(additionalFields));
}
responseData = await woocommerceApiRequest.call(this, 'POST', '/customers', body);
} else if (operation === 'delete') {
// ----------------------------------------
// customer: delete
// ----------------------------------------
// https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#delete-a-customer
const customerId = this.getNodeParameter('customerId', i);
const qs: IDataObject = {
force: true, // required, customers do not support trashing
};
const endpoint = `/customers/${customerId}`;
responseData = await woocommerceApiRequest.call(this, 'DELETE', endpoint, {}, qs);
} else if (operation === 'get') {
// ----------------------------------------
// customer: get
// ----------------------------------------
// https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#retrieve-a-customer
const customerId = this.getNodeParameter('customerId', i);
const endpoint = `/customers/${customerId}`;
responseData = await woocommerceApiRequest.call(this, 'GET', endpoint);
} else if (operation === 'getAll') {
// ----------------------------------------
// customer: getAll
// ----------------------------------------
// https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#list-all-customers
const qs = {} as IDataObject;
const filters = this.getNodeParameter('filters', i) as IDataObject;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (Object.keys(filters).length) {
Object.assign(qs, filters);
}
if (returnAll) {
responseData = await woocommerceApiRequestAllItems.call(this, 'GET', '/customers', {}, qs);
} else {
qs.per_page = this.getNodeParameter('limit', i) as number;
responseData = await woocommerceApiRequest.call(this, 'GET', '/customers', {}, qs);
}
} else if (operation === 'update') {
// ----------------------------------------
// customer: update
// ----------------------------------------
// https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#update-a-customer
const body = {} as IDataObject;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
if (Object.keys(updateFields).length) {
Object.assign(body, adjustMetadata(updateFields));
}
const customerId = this.getNodeParameter('customerId', i);
const endpoint = `/customers/${customerId}`;
responseData = await woocommerceApiRequest.call(this, 'PUT', endpoint, body);
}
} else if (resource === 'product') {
//https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product
if (operation === 'create') {
const name = this.getNodeParameter('name', i) as string;

View file

@ -23,7 +23,7 @@ export class WooCommerceTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'WooCommerce Trigger',
name: 'wooCommerceTrigger',
icon: 'file:wooCommerce.png',
icon: 'file:wooCommerce.svg',
group: ['trigger'],
version: 1,
description: 'Handle WooCommerce events via webhooks',

View file

@ -0,0 +1,254 @@
import {
INodeProperties,
} from 'n8n-workflow';
import {
customerCreateFields,
customerUpdateFields,
} from './shared';
export const customerOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'customer',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a customer',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a customer',
},
{
name: 'Get',
value: 'get',
description: 'Retrieve a customer',
},
{
name: 'Get All',
value: 'getAll',
description: 'Retrieve all customers',
},
{
name: 'Update',
value: 'update',
description: 'Update a customer',
},
],
default: 'create',
},
] as INodeProperties[];
export const customerFields = [
// ----------------------------------------
// customer: create
// ----------------------------------------
{
displayName: 'Email',
name: 'email',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'customer',
],
operation: [
'create',
],
},
},
},
customerCreateFields,
// ----------------------------------------
// customer: delete
// ----------------------------------------
{
displayName: 'Customer ID',
name: 'customerId',
description: 'ID of the customer to delete',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'customer',
],
operation: [
'delete',
],
},
},
},
// ----------------------------------------
// customer: get
// ----------------------------------------
{
displayName: 'Customer ID',
name: 'customerId',
description: 'ID of the customer to retrieve',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'customer',
],
operation: [
'get',
],
},
},
},
// ----------------------------------------
// customer: getAll
// ----------------------------------------
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
default: false,
description: 'Whether to return all results or only up to a given limit',
displayOptions: {
show: {
resource: [
'customer',
],
operation: [
'getAll',
],
},
},
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
default: 50,
description: 'How many results to return',
typeOptions: {
minValue: 1,
},
displayOptions: {
show: {
resource: [
'customer',
],
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
},
{
displayName: 'Filters',
name: 'filters',
type: 'collection',
placeholder: 'Add Filter',
default: {},
displayOptions: {
show: {
resource: [
'customer',
],
operation: [
'getAll',
],
},
},
options: [
{
displayName: 'Email',
name: 'email',
type: 'string',
default: '',
description: 'Email address to filter customers by',
},
{
displayName: 'Sort Order',
name: 'order',
description: 'Order to sort customers in',
type: 'options',
options: [
{
name: 'Ascending',
value: 'asc',
},
{
name: 'Descending',
value: 'desc',
},
],
default: 'asc',
},
{
displayName: 'Order By',
name: 'orderby',
description: 'Field to sort customers by',
type: 'options',
options: [
{
name: 'ID',
value: 'id',
},
{
name: 'Include',
value: 'include',
},
{
name: 'Name',
value: 'name',
},
{
name: 'Registered Date',
value: 'registered_date',
},
],
default: 'id',
},
],
},
// ----------------------------------------
// customer: update
// ----------------------------------------
{
displayName: 'Customer ID',
name: 'customerId',
description: 'ID of the customer to update',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'customer',
],
operation: [
'update',
],
},
},
},
customerUpdateFields,
] as INodeProperties[];

View file

@ -0,0 +1 @@
export * from './CustomerDescription';

View file

@ -0,0 +1,177 @@
const customerAddressOptions = [
{
displayName: 'First Name',
name: 'first_name',
type: 'string',
default: '',
},
{
displayName: 'Last Name',
name: 'last_name',
type: 'string',
default: '',
},
{
displayName: 'Company',
name: 'company',
type: 'string',
default: '',
},
{
displayName: 'Address 1',
name: 'address_1',
type: 'string',
default: '',
},
{
displayName: 'Address 2',
name: 'address_2',
type: 'string',
default: '',
},
{
displayName: 'City',
name: 'city',
type: 'string',
default: '',
},
{
displayName: 'State',
name: 'state',
type: 'string',
default: '',
},
{
displayName: 'Postcode',
name: 'postcode',
type: 'string',
default: '',
},
{
displayName: 'Country',
name: 'country',
type: 'string',
default: '',
},
{
displayName: 'Email',
name: 'email',
type: 'string',
default: '',
},
{
displayName: 'Phone',
name: 'phone',
type: 'string',
default: '',
},
];
const customerUpdateOptions = [
{
displayName: 'Billing Address',
name: 'billing',
type: 'collection',
default: {},
placeholder: 'Add Field',
options: customerAddressOptions,
},
{
displayName: 'First Name',
name: 'first_name',
type: 'string',
default: '',
},
{
displayName: 'Last Name',
name: 'last_name',
type: 'string',
default: '',
},
{
displayName: 'Metadata',
name: 'meta_data',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
default: {},
placeholder: 'Add Metadata Field',
options: [
{
displayName: 'Metadata Fields',
name: 'meta_data_fields',
values: [
{
displayName: 'Key',
name: 'key',
type: 'string',
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
},
],
},
],
},
{
displayName: 'Shipping Address',
name: 'shipping',
type: 'collection',
default: {},
placeholder: 'Add Field',
options: customerAddressOptions,
},
];
const customerCreateOptions = [
...customerUpdateOptions,
{
displayName: 'Username',
name: 'username',
type: 'string',
default: '',
},
];
export const customerCreateFields = {
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'customer',
],
operation: [
'create',
],
},
},
options: customerCreateOptions,
};
export const customerUpdateFields = {
displayName: 'Update Fields',
name: 'updateFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'customer',
],
operation: [
'update',
],
},
},
options: customerUpdateOptions,
};

Binary file not shown.

Before

Width:  |  Height:  |  Size: 999 B

View file

@ -0,0 +1,14 @@
<svg preserveAspectRatio="xMidYMid" version="1.1" viewBox="0 -50 256 253" xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<title>WooCommerce Logo</title>
<metadata>
<rdf:RDF>
<cc:Work rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
<dc:title/>
</cc:Work>
</rdf:RDF>
</metadata>
<path d="m23.759 0h208.38c13.187 0 23.863 10.675 23.863 23.863v79.542c0 13.187-10.675 23.863-23.863 23.863h-74.727l10.257 25.118-45.109-25.118h-98.695c-13.187 0-23.863-10.675-23.863-23.863v-79.542c-0.10466-13.083 10.571-23.863 23.758-23.863z" fill="#7f54b3"/>
<path d="m14.578 21.75c1.4569-1.9772 3.6423-3.0179 6.5561-3.226 5.3073-0.41626 8.3252 2.0813 9.0537 7.4927 3.226 21.75 6.7642 40.169 10.511 55.259l22.79-43.395c2.0813-3.9545 4.6829-6.0358 7.8049-6.2439 4.5789-0.3122 7.3886 2.6016 8.5333 8.7415 2.6016 13.841 5.9317 25.6 9.8862 35.59 2.7057-26.433 7.2846-45.476 13.737-57.236 1.561-2.9138 3.8504-4.3707 6.8683-4.5789 2.3935-0.20813 4.5789 0.52033 6.5561 2.0813 1.9772 1.561 3.0179 3.5382 3.226 5.9317 0.10406 1.8732-0.20813 3.4341-1.0407 4.9951-4.0585 7.4927-7.3886 20.085-10.094 37.567-2.6016 16.963-3.5382 30.179-2.9138 39.649 0.20813 2.6016-0.20813 4.8911-1.2488 6.8683-1.2488 2.2894-3.122 3.5382-5.5154 3.7463-2.7057 0.20813-5.5154-1.0406-8.2211-3.8504-9.678-9.8862-17.379-24.663-22.998-44.332-6.7642 13.32-11.759 23.311-14.985 29.971-6.1398 11.759-11.343 17.795-15.714 18.107-2.8098 0.20813-5.2033-2.1854-7.2846-7.1805-5.3073-13.633-11.031-39.961-17.171-78.985-0.41626-2.7057 0.20813-5.0992 1.665-6.9724zm223.64 16.338c-3.7463-6.5561-9.2618-10.511-16.65-12.072-1.9772-0.41626-3.8504-0.62439-5.6195-0.62439-9.9902 0-18.107 5.2033-24.455 15.61-5.4114 8.8455-8.1171 18.628-8.1171 29.346 0 8.013 1.665 14.881 4.9951 20.605 3.7463 6.5561 9.2618 10.511 16.65 12.072 1.9772 0.41626 3.8504 0.62439 5.6195 0.62439 10.094 0 18.211-5.2033 24.455-15.61 5.4114-8.9496 8.1171-18.732 8.1171-29.45 0.10406-8.1171-1.665-14.881-4.9951-20.501zm-13.112 28.826c-1.4569 6.8683-4.0585 11.967-7.9089 15.402-3.0179 2.7057-5.8276 3.8504-8.4293 3.3301-2.4976-0.52033-4.5789-2.7057-6.1398-6.7642-1.2488-3.226-1.8732-6.452-1.8732-9.4699 0-2.6016 0.20813-5.2033 0.72846-7.5967 0.93659-4.2667 2.7057-8.4293 5.5154-12.384 3.4341-5.0992 7.0764-7.1805 10.823-6.452 2.4976 0.52033 4.5789 2.7057 6.1398 6.7642 1.2488 3.226 1.8732 6.452 1.8732 9.4699 0 2.7057-0.20813 5.3073-0.72846 7.7008zm-52.033-28.826c-3.7463-6.5561-9.3659-10.511-16.65-12.072-1.9772-0.41626-3.8504-0.62439-5.6195-0.62439-9.9902 0-18.107 5.2033-24.455 15.61-5.4114 8.8455-8.1171 18.628-8.1171 29.346 0 8.013 1.665 14.881 4.9951 20.605 3.7463 6.5561 9.2618 10.511 16.65 12.072 1.9772 0.41626 3.8504 0.62439 5.6195 0.62439 10.094 0 18.211-5.2033 24.455-15.61 5.4114-8.9496 8.1171-18.732 8.1171-29.45 0-8.1171-1.665-14.881-4.9951-20.501zm-13.216 28.826c-1.4569 6.8683-4.0585 11.967-7.9089 15.402-3.0179 2.7057-5.8276 3.8504-8.4293 3.3301-2.4976-0.52033-4.5789-2.7057-6.1398-6.7642-1.2488-3.226-1.8732-6.452-1.8732-9.4699 0-2.6016 0.20813-5.2033 0.72846-7.5967 0.93658-4.2667 2.7057-8.4293 5.5154-12.384 3.4341-5.0992 7.0764-7.1805 10.823-6.452 2.4976 0.52033 4.5789 2.7057 6.1398 6.7642 1.2488 3.226 1.8732 6.452 1.8732 9.4699 0.10406 2.7057-0.20813 5.3073-0.72846 7.7008z" fill="#fff"/>
</svg>

After

Width:  |  Height:  |  Size: 3.3 KiB