2020-01-29 17:53:01 -08:00
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
import { createHash } from 'crypto';
|
2020-02-24 16:19:04 -08:00
|
|
|
import { snakeCase } from 'change-case';
|
|
|
|
|
2020-01-29 17:53:01 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
2020-02-24 16:19:04 -08:00
|
|
|
import {
|
2020-10-01 05:01:39 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
2020-02-24 16:19:04 -08:00
|
|
|
IDataObject,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
2020-10-01 05:01:39 -07:00
|
|
|
ICouponLine,
|
|
|
|
IFeeLine,
|
|
|
|
ILineItem,
|
|
|
|
IShoppingLine,
|
2020-02-24 16:19:04 -08:00
|
|
|
} from './OrderInterface';
|
2020-01-29 17:53:01 -08:00
|
|
|
|
|
|
|
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) {
|
|
|
|
throw new Error('No credentials got returned!');
|
|
|
|
}
|
|
|
|
let options: OptionsWithUri = {
|
2020-01-29 20:06:47 -08:00
|
|
|
auth: {
|
|
|
|
user: credentials.consumerKey as string,
|
|
|
|
password: credentials.consumerSecret as string,
|
2020-01-29 17:53:01 -08:00
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2020-10-01 05:01:39 -07:00
|
|
|
uri: uri || `${credentials.url}/wp-json/wc/v3${resource}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-01-29 17:53:01 -08:00
|
|
|
};
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.form;
|
|
|
|
}
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2020-01-29 20:06:47 -08:00
|
|
|
if (error.statusCode === 401) {
|
|
|
|
// Return a clear error
|
|
|
|
throw new Error('The WooCommerce credentials are not valid!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.response.body && error.response.body.message) {
|
|
|
|
// Try to return the error prettier
|
|
|
|
throw new Error(`WooCommerce Error [${error.statusCode}]: ${error.response.body.message}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If that data does not exist for some reason return the actual error
|
|
|
|
throw new Error('WooCommerce Error: ' + error.message);
|
2020-01-29 17:53:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 10:43:51 -08:00
|
|
|
export async function woocommerceApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
let uri: string | undefined;
|
|
|
|
query.per_page = 100;
|
|
|
|
do {
|
|
|
|
responseData = await woocommerceApiRequest.call(this, method, endpoint, body, query, uri, { resolveWithFullResponse: true });
|
2020-10-01 05:01:39 -07:00
|
|
|
uri = responseData.headers['link'].split(';')[0].replace('<', '').replace('>', '');
|
2020-02-16 10:43:51 -08:00
|
|
|
returnData.push.apply(returnData, responseData.body);
|
|
|
|
} while (
|
|
|
|
responseData.headers['link'] !== undefined &&
|
|
|
|
responseData.headers['link'].includes('rel="next"')
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:53:01 -08:00
|
|
|
/**
|
|
|
|
* Creates a secret from the credentials
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {ICredentialDataDecryptedObject} credentials
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function getAutomaticSecret(credentials: ICredentialDataDecryptedObject) {
|
|
|
|
const data = `${credentials.consumerKey},${credentials.consumerSecret}`;
|
2020-01-29 20:06:47 -08:00
|
|
|
return createHash('md5').update(data).digest('hex');
|
2020-01-29 17:53:01 -08:00
|
|
|
}
|
2020-02-24 16:19:04 -08:00
|
|
|
|
|
|
|
export function setMetadata(data:
|
|
|
|
IShoppingLine[] |
|
|
|
|
IShoppingLine[] |
|
|
|
|
IFeeLine[] |
|
|
|
|
ILineItem[] |
|
|
|
|
ICouponLine[]) {
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
|
|
//@ts-ignore\
|
|
|
|
if (data[i].metadataUi && data[i].metadataUi.metadataValues) {
|
|
|
|
//@ts-ignore
|
|
|
|
data[i].meta_data = data[i].metadataUi.metadataValues;
|
|
|
|
//@ts-ignore
|
2020-02-25 02:01:13 -08:00
|
|
|
delete data[i].metadataUi;
|
2020-02-24 16:19:04 -08:00
|
|
|
} else {
|
|
|
|
//@ts-ignore
|
|
|
|
delete data[i].metadataUi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function toSnakeCase(data:
|
|
|
|
IShoppingLine[] |
|
|
|
|
IShoppingLine[] |
|
|
|
|
IFeeLine[] |
|
|
|
|
ILineItem[] |
|
|
|
|
ICouponLine[] |
|
|
|
|
IDataObject) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
data = [data];
|
|
|
|
}
|
|
|
|
let remove = false;
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
|
|
for (const key of Object.keys(data[i])) {
|
|
|
|
//@ts-ignore
|
|
|
|
if (data[i][snakeCase(key)] === undefined) {
|
|
|
|
remove = true;
|
|
|
|
}
|
|
|
|
//@ts-ignore
|
2020-02-25 02:01:13 -08:00
|
|
|
data[i][snakeCase(key)] = data[i][key];
|
2020-02-24 16:19:04 -08:00
|
|
|
if (remove) {
|
|
|
|
//@ts-ignore
|
|
|
|
delete data[i][key];
|
|
|
|
remove = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|