ToolJet/plugins/packages/woocommerce/lib/index.ts
Kiran Ashok 3b96e99d6a
Woocommerce integration 🚀 (#2866)
* base structure

* tested all customer operations

* all product enpoints tested

* all order api tested

* typo fix

* icon updated

* tested all operation , cleanup

* added create coupon operation

* removing unwanted types

* cleanup

* updating test connection bug

* doc updated

* docs updated

* removing logs :: pr updates

* removing types :: pr updates

* cleanup :: pr changes

* operation structure updated

* restructured whole code for seperating operations

* typo bug updated

* updating pr updates remove host , test error case updated

* typo bug fixed

* updating naming conventions

* plugins operation.json reusable fields

* pr changes :: making all ids common

* fixing customer params

* updating all additional param types

* updating all product params

* including some more product params

* adding all order params

* adding all coupon params

* updating common defenitions

* capitalise labels

* updating all list operation definitions

* clearing some typos

* global defenition

* lock file changes

* lock file changes

* server lock file changes

* typo fix

* typo fix

Co-authored-by: gsmithun4 <gsmithun4@gmail.com>
2022-05-19 11:38:14 +05:30

64 lines
2.3 KiB
TypeScript

import { QueryResult, QueryService, ConnectionTestResult } from '@tooljet-plugins/common';
import { SourceOptions, QueryOptions } from './types';
import { customerOpeations, productOperations, orderOperations, couponOperations } from './operation';
const WooCommerceRestApi = require('@woocommerce/woocommerce-rest-api').default;
export default class Woocommerce implements QueryService {
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions): Promise<QueryResult> {
const WooCommerce = await this.getConnection(sourceOptions);
const { resource, operation } = queryOptions;
let result: any;
switch (resource) {
case 'customer':
result = await customerOpeations(WooCommerce, queryOptions, operation);
break;
case 'product':
result = await productOperations(WooCommerce, queryOptions, operation);
break;
case 'order':
result = await orderOperations(WooCommerce, queryOptions, operation);
break;
case 'coupon':
result = await couponOperations(WooCommerce, queryOptions, operation);
break;
}
return {
status: 'ok',
data: result,
};
}
async getConnection(sourceOptions: any, _options?: object): Promise<any> {
const { host, consumer_key, consumer_secret } = sourceOptions;
const WooCommerce = new WooCommerceRestApi({
url: host, // Your store URL
consumerKey: consumer_key, // Your consumer key
consumerSecret: consumer_secret, // Your consumer secret
version: 'wc/v3', // WooCommerce WP REST API version
});
return WooCommerce;
}
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
const { host, consumer_key, consumer_secret } = sourceOptions;
const WooCommerce = new WooCommerceRestApi({
url: host, // Your store URL
consumerKey: consumer_key, // Your consumer key
consumerSecret: consumer_secret, // Your consumer secret
version: 'wc/v3', // WooCommerce WP REST API version
});
const client = await WooCommerce.get('system_status')
.then((response) => {
return response.data;
})
.catch(() => {
throw new Error('Invalid credentials');
});
if (client?.data?.status == '401') {
throw new Error('Invalid credentials');
}
return {
status: 'ok',
};
}
}