2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2021-09-15 01:07:02 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2021-09-15 01:07:02 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { capitalCase } from 'change-case';
|
2021-09-15 01:07:02 -07:00
|
|
|
import {
|
|
|
|
adjustAddresses,
|
|
|
|
getFilterQuery,
|
|
|
|
getOrderFields,
|
|
|
|
getProductAttributes,
|
|
|
|
magentoApiRequest,
|
|
|
|
magentoApiRequestAllItems,
|
|
|
|
sort,
|
|
|
|
validateJSON,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { customerFields, customerOperations } from './CustomerDescription';
|
2021-09-15 01:07:02 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { orderFields, orderOperations } from './OrderDescription';
|
2021-09-15 01:07:02 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { productFields, productOperations } from './ProductDescription';
|
2021-09-15 01:07:02 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { invoiceFields, invoiceOperations } from './InvoiceDescription';
|
2021-09-15 01:07:02 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2021-09-15 01:07:02 -07:00
|
|
|
CustomAttribute,
|
|
|
|
CustomerAttributeMetadata,
|
|
|
|
Filter,
|
|
|
|
NewCustomer,
|
|
|
|
NewProduct,
|
|
|
|
Search,
|
2023-07-10 10:35:34 -07:00
|
|
|
} from './types';
|
2021-09-15 01:07:02 -07:00
|
|
|
|
|
|
|
export class Magento2 implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Magento 2',
|
|
|
|
name: 'magento2',
|
|
|
|
icon: 'file:magento.svg',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Magento API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Magento 2',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'magento2Api',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-09-15 01:07:02 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Customer',
|
|
|
|
value: 'customer',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Invoice',
|
|
|
|
value: 'invoice',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Order',
|
|
|
|
value: 'order',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Product',
|
|
|
|
value: 'product',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'customer',
|
|
|
|
},
|
|
|
|
...customerOperations,
|
|
|
|
...customerFields,
|
|
|
|
...invoiceOperations,
|
|
|
|
...invoiceFields,
|
|
|
|
...orderOperations,
|
|
|
|
...orderFields,
|
|
|
|
...productOperations,
|
|
|
|
...productFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getCountries(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/directorycountries
|
2022-08-17 08:50:24 -07:00
|
|
|
const countries = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/rest/default/V1/directory/countries',
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const country of countries) {
|
|
|
|
returnData.push({
|
|
|
|
name: country.full_name_english,
|
|
|
|
value: country.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
returnData.sort(sort);
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getGroups(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/customerGroupsdefault#operation/customerGroupManagementV1GetDefaultGroupGet
|
2022-08-17 08:50:24 -07:00
|
|
|
const group = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/rest/default/V1/customerGroups/default',
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
returnData.push({
|
|
|
|
name: group.code,
|
|
|
|
value: group.id,
|
|
|
|
});
|
|
|
|
returnData.sort(sort);
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getStores(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/storestoreConfigs
|
2022-08-17 08:50:24 -07:00
|
|
|
const stores = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/rest/default/V1/store/storeConfigs',
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const store of stores) {
|
|
|
|
returnData.push({
|
|
|
|
name: store.base_url,
|
|
|
|
value: store.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
returnData.sort(sort);
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getWebsites(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/storewebsites
|
2022-08-17 08:50:24 -07:00
|
|
|
const websites = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/rest/default/V1/store/websites',
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const website of websites) {
|
|
|
|
returnData.push({
|
|
|
|
name: website.name,
|
|
|
|
value: website.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
returnData.sort(sort);
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getCustomAttributes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/attributeMetadatacustomer#operation/customerCustomerMetadataV1GetAllAttributesMetadataGet
|
|
|
|
const resource = this.getCurrentNodeParameter('resource') as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const attributes = (await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/rest/default/V1/attributeMetadata/${resource}`,
|
|
|
|
)) as CustomerAttributeMetadata[];
|
2021-09-15 01:07:02 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const attribute of attributes) {
|
|
|
|
if (attribute.system === false && attribute.frontend_label !== '') {
|
|
|
|
returnData.push({
|
|
|
|
name: attribute.frontend_label as string,
|
|
|
|
value: attribute.attribute_code as string,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
returnData.sort(sort);
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getSystemAttributes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/attributeMetadatacustomer#operation/customerCustomerMetadataV1GetAllAttributesMetadataGet
|
|
|
|
const resource = this.getCurrentNodeParameter('resource') as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const attributes = (await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/rest/default/V1/attributeMetadata/${resource}`,
|
|
|
|
)) as CustomerAttributeMetadata[];
|
2021-09-15 01:07:02 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const attribute of attributes) {
|
|
|
|
if (attribute.system === true && attribute.frontend_label !== null) {
|
|
|
|
returnData.push({
|
|
|
|
name: attribute.frontend_label as string,
|
|
|
|
value: attribute.attribute_code as string,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
returnData.sort(sort);
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getProductTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/productslinkstypes
|
2022-08-17 08:50:24 -07:00
|
|
|
const types = (await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/rest/default/V1/products/types',
|
2022-08-17 08:50:24 -07:00
|
|
|
)) as IDataObject[];
|
2021-09-15 01:07:02 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const type of types) {
|
|
|
|
returnData.push({
|
|
|
|
name: type.label as string,
|
|
|
|
value: type.name as string,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
returnData.sort(sort);
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getCategories(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/categories#operation/catalogCategoryManagementV1GetTreeGet
|
2022-08-17 08:50:24 -07:00
|
|
|
const { items: categories } = (await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/rest/default/V1/categories/list',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
{
|
|
|
|
search_criteria: {
|
|
|
|
filter_groups: [
|
|
|
|
{
|
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
field: 'is_active',
|
|
|
|
condition_type: 'eq',
|
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-09-15 01:07:02 -07:00
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
)) as { items: IDataObject[] };
|
2021-09-15 01:07:02 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const category of categories) {
|
|
|
|
returnData.push({
|
|
|
|
name: category.name as string,
|
|
|
|
value: category.id as string,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
returnData.sort(sort);
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getAttributeSets(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/productsattribute-setssetslist#operation/catalogAttributeSetRepositoryV1GetListGet
|
2022-08-17 08:50:24 -07:00
|
|
|
const { items: attributeSets } = (await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/rest/default/V1/products/attribute-sets/sets/list',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
{
|
|
|
|
search_criteria: 0,
|
|
|
|
},
|
|
|
|
)) as { items: IDataObject[] };
|
2021-09-15 01:07:02 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const attributeSet of attributeSets) {
|
|
|
|
returnData.push({
|
|
|
|
name: attributeSet.attribute_set_name as string,
|
|
|
|
value: attributeSet.attribute_set_id as string,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
returnData.sort(sort);
|
|
|
|
return returnData;
|
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
async getFilterableCustomerAttributes(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
2022-12-02 12:54:28 -08:00
|
|
|
return getProductAttributes.call(this, (attribute) => attribute.is_filterable);
|
2021-09-15 01:07:02 -07:00
|
|
|
},
|
|
|
|
async getProductAttributes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
return getProductAttributes.call(this);
|
|
|
|
},
|
|
|
|
// async getProductAttributesFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
// return getProductAttributes.call(this, undefined, { name: '*', value: '*', description: 'All properties' });
|
|
|
|
// },
|
2022-08-17 08:50:24 -07:00
|
|
|
async getFilterableProductAttributes(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
2021-09-15 01:07:02 -07:00
|
|
|
return getProductAttributes.call(this, (attribute) => attribute.is_searchable === '1');
|
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
async getSortableProductAttributes(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
2022-12-02 12:54:28 -08:00
|
|
|
return getProductAttributes.call(this, (attribute) => attribute.used_for_sort_by);
|
2021-09-15 01:07:02 -07:00
|
|
|
},
|
|
|
|
async getOrderAttributes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2022-08-17 08:50:24 -07:00
|
|
|
return getOrderFields()
|
|
|
|
.map((field) => ({ name: capitalCase(field), value: field }))
|
|
|
|
.sort(sort);
|
2021-09-15 01:07:02 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-09-15 01:07:02 -07:00
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-09-15 01:07:02 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
try {
|
|
|
|
if (resource === 'customer') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
// https://magento.redoc.ly/2.3.7-admin/tag/customerscustomerId#operation/customerCustomerRepositoryV1SavePut
|
|
|
|
const email = this.getNodeParameter('email', i) as string;
|
|
|
|
const firstname = this.getNodeParameter('firstname', i) as string;
|
|
|
|
const lastname = this.getNodeParameter('lastname', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const { addresses, customAttributes, password, ...rest } = this.getNodeParameter(
|
|
|
|
'additionalFields',
|
|
|
|
i,
|
|
|
|
) as {
|
2021-09-15 01:07:02 -07:00
|
|
|
addresses: {
|
2022-08-17 08:50:24 -07:00
|
|
|
address: [
|
|
|
|
{
|
|
|
|
street: string;
|
|
|
|
},
|
|
|
|
];
|
2021-09-15 01:07:02 -07:00
|
|
|
};
|
|
|
|
customAttributes: {
|
2022-08-17 08:50:24 -07:00
|
|
|
customAttribute: CustomAttribute[];
|
|
|
|
};
|
|
|
|
password: string;
|
2021-09-15 01:07:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const body: NewCustomer = {
|
|
|
|
customer: {
|
|
|
|
email,
|
|
|
|
firstname,
|
|
|
|
lastname,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
body.customer!.addresses = adjustAddresses(addresses?.address || []);
|
|
|
|
|
|
|
|
body.customer!.custom_attributes = customAttributes?.customAttribute || {};
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body.customer!.extension_attributes = [
|
|
|
|
'amazon_id',
|
|
|
|
'is_subscribed',
|
|
|
|
'vertex_customer_code',
|
|
|
|
'vertex_customer_country',
|
2022-12-02 06:25:21 -08:00
|
|
|
].reduce((obj, value: string): any => {
|
|
|
|
if ((rest as IDataObject).hasOwnProperty(value)) {
|
|
|
|
const data = Object.assign(obj, { [value]: (rest as IDataObject)[value] });
|
|
|
|
delete (rest as IDataObject)[value];
|
|
|
|
return data;
|
|
|
|
} else {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
}, {});
|
2021-09-15 01:07:02 -07:00
|
|
|
|
|
|
|
if (password) {
|
|
|
|
body.password = password;
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
Object.assign(body.customer!, rest);
|
2021-09-15 01:07:02 -07:00
|
|
|
|
|
|
|
responseData = await magentoApiRequest.call(this, 'POST', '/rest/V1/customers', body);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'delete') {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/customerscustomerId#operation/customerCustomerRepositoryV1SavePut
|
|
|
|
const customerId = this.getNodeParameter('customerId', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/rest/default/V1/customers/${customerId}`,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'get') {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/customerscustomerId#operation/customerCustomerRepositoryV1GetByIdGet
|
|
|
|
const customerId = this.getNodeParameter('customerId', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/rest/default/V1/customers/${customerId}`,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/customerssearch
|
|
|
|
const filterType = this.getNodeParameter('filterType', i) as string;
|
2022-12-02 12:54:28 -08:00
|
|
|
const sortOption = this.getNodeParameter('options.sort', i, {}) as {
|
2022-08-17 08:50:24 -07:00
|
|
|
sort: [{ direction: string; field: string }];
|
|
|
|
};
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2021-09-15 01:07:02 -07:00
|
|
|
let qs: Search = {};
|
|
|
|
|
|
|
|
if (filterType === 'manual') {
|
|
|
|
const filters = this.getNodeParameter('filters', i) as { conditions: Filter[] };
|
|
|
|
const matchType = this.getNodeParameter('matchType', i) as string;
|
2022-12-02 12:54:28 -08:00
|
|
|
qs = getFilterQuery(Object.assign(filters, { matchType }, sortOption));
|
2021-09-15 01:07:02 -07:00
|
|
|
} else if (filterType === 'json') {
|
|
|
|
const filterJson = this.getNodeParameter('filterJson', i) as string;
|
|
|
|
if (validateJSON(filterJson) !== undefined) {
|
|
|
|
qs = JSON.parse(filterJson);
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeApiError(this.getNode(), {
|
|
|
|
message: 'Filter (JSON) must be a valid json',
|
|
|
|
});
|
2021-09-15 01:07:02 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qs = {
|
|
|
|
search_criteria: {},
|
|
|
|
};
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (Object.keys(sortOption).length !== 0) {
|
2021-09-15 01:07:02 -07:00
|
|
|
qs.search_criteria = {
|
2022-12-02 12:54:28 -08:00
|
|
|
sort_orders: sortOption.sort,
|
2021-09-15 01:07:02 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2021-09-15 01:07:02 -07:00
|
|
|
qs.search_criteria!.page_size = 100;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/rest/default/V1/customers/search',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs as unknown as IDataObject,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', 0);
|
2021-09-15 01:07:02 -07:00
|
|
|
qs.search_criteria!.page_size = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/rest/default/V1/customers/search',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs as unknown as IDataObject,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
responseData = responseData.items;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'update') {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/customerscustomerId#operation/customerCustomerRepositoryV1SavePut
|
|
|
|
const customerId = this.getNodeParameter('customerId', 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;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const { addresses, customAttributes, password, ...rest } = this.getNodeParameter(
|
|
|
|
'updateFields',
|
|
|
|
i,
|
|
|
|
) as {
|
2021-09-15 01:07:02 -07:00
|
|
|
addresses: {
|
2022-08-17 08:50:24 -07:00
|
|
|
address: [
|
|
|
|
{
|
|
|
|
street: string;
|
|
|
|
},
|
|
|
|
];
|
2021-09-15 01:07:02 -07:00
|
|
|
};
|
|
|
|
customAttributes: {
|
2022-08-17 08:50:24 -07:00
|
|
|
customAttribute: CustomAttribute[];
|
|
|
|
};
|
|
|
|
password: string;
|
2021-09-15 01:07:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const body: NewCustomer = {
|
|
|
|
customer: {
|
|
|
|
email,
|
|
|
|
firstname: firstName,
|
|
|
|
lastname: lastName,
|
|
|
|
id: parseInt(customerId, 10),
|
|
|
|
website_id: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
body.customer!.addresses = adjustAddresses(addresses?.address || []);
|
|
|
|
|
|
|
|
body.customer!.custom_attributes = customAttributes?.customAttribute || {};
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body.customer!.extension_attributes = [
|
|
|
|
'amazon_id',
|
|
|
|
'is_subscribed',
|
|
|
|
'vertex_customer_code',
|
|
|
|
'vertex_customer_country',
|
2022-12-02 06:25:21 -08:00
|
|
|
].reduce((obj, value: string): any => {
|
|
|
|
if ((rest as IDataObject).hasOwnProperty(value)) {
|
|
|
|
const data = Object.assign(obj, { [value]: (rest as IDataObject)[value] });
|
|
|
|
delete (rest as IDataObject)[value];
|
|
|
|
return data;
|
|
|
|
} else {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
}, {});
|
2021-09-15 01:07:02 -07:00
|
|
|
|
|
|
|
if (password) {
|
|
|
|
body.password = password;
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
Object.assign(body.customer!, rest);
|
2021-09-15 01:07:02 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PUT',
|
|
|
|
`/rest/V1/customers/${customerId}`,
|
|
|
|
body,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resource === 'invoice') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
///https://magento.redoc.ly/2.3.7-admin/tag/orderorderIdinvoice
|
|
|
|
const orderId = this.getNodeParameter('orderId', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/rest/default/V1/order/${orderId}/invoice`,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resource === 'order') {
|
|
|
|
if (operation === 'cancel') {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/ordersidcancel
|
|
|
|
const orderId = this.getNodeParameter('orderId', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/rest/default/V1/orders/${orderId}/cancel`,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'get') {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/ordersid#operation/salesOrderRepositoryV1GetGet
|
|
|
|
const orderId = this.getNodeParameter('orderId', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/rest/default/V1/orders/${orderId}`,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'ship') {
|
|
|
|
///https://magento.redoc.ly/2.3.7-admin/tag/orderorderIdship#operation/salesShipOrderV1ExecutePost
|
|
|
|
const orderId = this.getNodeParameter('orderId', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/rest/default/V1/order/${orderId}/ship`,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/orders#operation/salesOrderRepositoryV1GetListGet
|
|
|
|
const filterType = this.getNodeParameter('filterType', i) as string;
|
2022-12-02 12:54:28 -08:00
|
|
|
const sortOption = this.getNodeParameter('options.sort', i, {}) as {
|
2022-08-17 08:50:24 -07:00
|
|
|
sort: [{ direction: string; field: string }];
|
|
|
|
};
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2021-09-15 01:07:02 -07:00
|
|
|
let qs: Search = {};
|
|
|
|
|
|
|
|
if (filterType === 'manual') {
|
|
|
|
const filters = this.getNodeParameter('filters', i) as { conditions: Filter[] };
|
|
|
|
const matchType = this.getNodeParameter('matchType', i) as string;
|
2022-12-02 12:54:28 -08:00
|
|
|
qs = getFilterQuery(Object.assign(filters, { matchType }, sortOption));
|
2021-09-15 01:07:02 -07:00
|
|
|
} else if (filterType === 'json') {
|
|
|
|
const filterJson = this.getNodeParameter('filterJson', i) as string;
|
|
|
|
if (validateJSON(filterJson) !== undefined) {
|
|
|
|
qs = JSON.parse(filterJson);
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeApiError(this.getNode(), {
|
|
|
|
message: 'Filter (JSON) must be a valid json',
|
|
|
|
});
|
2021-09-15 01:07:02 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qs = {
|
|
|
|
search_criteria: {},
|
|
|
|
};
|
2022-12-02 12:54:28 -08:00
|
|
|
if (Object.keys(sortOption).length !== 0) {
|
2021-09-15 01:07:02 -07:00
|
|
|
qs.search_criteria = {
|
2022-12-02 12:54:28 -08:00
|
|
|
sort_orders: sortOption.sort,
|
2021-09-15 01:07:02 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2021-09-15 01:07:02 -07:00
|
|
|
qs.search_criteria!.page_size = 100;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/rest/default/V1/orders',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs as unknown as IDataObject,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', 0);
|
2021-09-15 01:07:02 -07:00
|
|
|
qs.search_criteria!.page_size = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/rest/default/V1/orders',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs as unknown as IDataObject,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
responseData = responseData.items;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resource === 'product') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
// https://magento.redoc.ly/2.3.7-admin/tag/products#operation/catalogProductRepositoryV1SavePost
|
|
|
|
const sku = this.getNodeParameter('sku', i) as string;
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
|
|
|
const attributeSetId = this.getNodeParameter('attributeSetId', i) as string;
|
|
|
|
const price = this.getNodeParameter('price', i) as number;
|
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
const {
|
|
|
|
customAttributes,
|
|
|
|
category: _category,
|
|
|
|
...rest
|
|
|
|
} = this.getNodeParameter('additionalFields', i) as {
|
2021-09-15 01:07:02 -07:00
|
|
|
customAttributes: {
|
2022-08-17 08:50:24 -07:00
|
|
|
customAttribute: CustomAttribute[];
|
|
|
|
};
|
|
|
|
category: string;
|
2021-09-15 01:07:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const body: NewProduct = {
|
|
|
|
product: {
|
|
|
|
sku,
|
|
|
|
name,
|
|
|
|
attribute_set_id: parseInt(attributeSetId, 10),
|
2021-12-23 04:30:35 -08:00
|
|
|
price,
|
2021-09-15 01:07:02 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
body.product!.custom_attributes = customAttributes?.customAttribute || {};
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
Object.assign(body.product!, rest);
|
2021-09-15 01:07:02 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/rest/default/V1/products',
|
|
|
|
body,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'delete') {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/productssku#operation/catalogProductRepositoryV1DeleteByIdDelete
|
|
|
|
const sku = this.getNodeParameter('sku', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/rest/default/V1/products/${sku}`,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'get') {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/productssku#operation/catalogProductRepositoryV1GetGet
|
|
|
|
const sku = this.getNodeParameter('sku', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/rest/default/V1/products/${sku}`,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/customerssearch
|
|
|
|
const filterType = this.getNodeParameter('filterType', i) as string;
|
2022-12-02 12:54:28 -08:00
|
|
|
const sortOption = this.getNodeParameter('options.sort', i, {}) as {
|
2022-08-17 08:50:24 -07:00
|
|
|
sort: [{ direction: string; field: string }];
|
|
|
|
};
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2021-09-15 01:07:02 -07:00
|
|
|
let qs: Search = {};
|
|
|
|
|
|
|
|
if (filterType === 'manual') {
|
|
|
|
const filters = this.getNodeParameter('filters', i) as { conditions: Filter[] };
|
|
|
|
const matchType = this.getNodeParameter('matchType', i) as string;
|
2022-12-02 12:54:28 -08:00
|
|
|
qs = getFilterQuery(Object.assign(filters, { matchType }, sortOption));
|
2021-09-15 01:07:02 -07:00
|
|
|
} else if (filterType === 'json') {
|
|
|
|
const filterJson = this.getNodeParameter('filterJson', i) as string;
|
|
|
|
if (validateJSON(filterJson) !== undefined) {
|
|
|
|
qs = JSON.parse(filterJson);
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeApiError(this.getNode(), {
|
|
|
|
message: 'Filter (JSON) must be a valid json',
|
|
|
|
});
|
2021-09-15 01:07:02 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qs = {
|
|
|
|
search_criteria: {},
|
|
|
|
};
|
2022-12-02 12:54:28 -08:00
|
|
|
if (Object.keys(sortOption).length !== 0) {
|
2021-09-15 01:07:02 -07:00
|
|
|
qs.search_criteria = {
|
2022-12-02 12:54:28 -08:00
|
|
|
sort_orders: sortOption.sort,
|
2021-09-15 01:07:02 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2021-09-15 01:07:02 -07:00
|
|
|
qs.search_criteria!.page_size = 100;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/rest/default/V1/products',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs as unknown as IDataObject,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', 0);
|
2021-09-15 01:07:02 -07:00
|
|
|
qs.search_criteria!.page_size = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/rest/default/V1/products',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs as unknown as IDataObject,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
responseData = responseData.items;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'update') {
|
|
|
|
//https://magento.redoc.ly/2.3.7-admin/tag/productssku#operation/catalogProductRepositoryV1SavePut
|
|
|
|
const sku = this.getNodeParameter('sku', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const { customAttributes, ...rest } = this.getNodeParameter('updateFields', i) as {
|
2021-09-15 01:07:02 -07:00
|
|
|
customAttributes: {
|
2022-08-17 08:50:24 -07:00
|
|
|
customAttribute: CustomAttribute[];
|
|
|
|
};
|
2021-09-15 01:07:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!Object.keys(rest).length) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeApiError(this.getNode(), {
|
|
|
|
message: 'At least one parameter has to be updated',
|
|
|
|
});
|
2021-09-15 01:07:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const body: NewProduct = {
|
|
|
|
product: {
|
|
|
|
sku,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
body.product!.custom_attributes = customAttributes?.customAttribute || {};
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
Object.assign(body.product!, rest);
|
2021-09-15 01:07:02 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await magentoApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PUT',
|
|
|
|
`/rest/default/V1/products/${sku}`,
|
|
|
|
body,
|
|
|
|
);
|
2021-09-15 01:07:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-09-15 01:07:02 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-09-15 01:07:02 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2021-09-15 01:07:02 -07:00
|
|
|
}
|
|
|
|
}
|