@chift/chift-nodejs 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintcache +1 -0
- package/.eslintignore +1 -0
- package/.eslintrc.json +25 -0
- package/.github/workflows/ci.yml +73 -0
- package/.husky/pre-commit +4 -0
- package/.prettierignore +1 -0
- package/.prettierrc.json +7 -0
- package/CHANGELOG.md +4 -0
- package/LICENSE +201 -0
- package/README.md +48 -0
- package/coverage/clover.xml +1645 -0
- package/coverage/coverage-final.json +19 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +146 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov-report/src/helpers/index.html +131 -0
- package/coverage/lcov-report/src/helpers/openapi.ts.html +151 -0
- package/coverage/lcov-report/src/helpers/settings.ts.html +94 -0
- package/coverage/lcov-report/src/index.html +116 -0
- package/coverage/lcov-report/src/index.ts.html +88 -0
- package/coverage/lcov-report/src/modules/accounting.ts.html +1156 -0
- package/coverage/lcov-report/src/modules/api.ts.html +190 -0
- package/coverage/lcov-report/src/modules/consumer.ts.html +616 -0
- package/coverage/lcov-report/src/modules/consumers.ts.html +331 -0
- package/coverage/lcov-report/src/modules/custom.ts.html +193 -0
- package/coverage/lcov-report/src/modules/datastores.ts.html +142 -0
- package/coverage/lcov-report/src/modules/ecommerce.ts.html +331 -0
- package/coverage/lcov-report/src/modules/flow.ts.html +589 -0
- package/coverage/lcov-report/src/modules/index.html +326 -0
- package/coverage/lcov-report/src/modules/integrations.ts.html +151 -0
- package/coverage/lcov-report/src/modules/internalApi.ts.html +586 -0
- package/coverage/lcov-report/src/modules/invoicing.ts.html +391 -0
- package/coverage/lcov-report/src/modules/pos.ts.html +421 -0
- package/coverage/lcov-report/src/modules/sync.ts.html +316 -0
- package/coverage/lcov-report/src/modules/syncs.ts.html +169 -0
- package/coverage/lcov-report/src/modules/webhooks.ts.html +343 -0
- package/coverage/lcov.info +1976 -0
- package/dist/src/helpers/openapi.d.ts +3 -0
- package/dist/src/helpers/openapi.js +18 -0
- package/dist/src/helpers/settings.d.ts +4 -0
- package/dist/src/helpers/settings.js +5 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +17 -0
- package/dist/src/modules/accounting.d.ts +48 -0
- package/dist/src/modules/accounting.js +255 -0
- package/dist/src/modules/api.d.ts +39333 -0
- package/dist/src/modules/api.js +36 -0
- package/dist/src/modules/consumer.d.ts +2588 -0
- package/dist/src/modules/consumer.js +118 -0
- package/dist/src/modules/consumers.d.ts +13034 -0
- package/dist/src/modules/consumers.js +51 -0
- package/dist/src/modules/custom.d.ts +8 -0
- package/dist/src/modules/custom.js +36 -0
- package/dist/src/modules/datastores.d.ts +18 -0
- package/dist/src/modules/datastores.js +23 -0
- package/dist/src/modules/ecommerce.d.ts +16 -0
- package/dist/src/modules/ecommerce.js +69 -0
- package/dist/src/modules/flow.d.ts +15 -0
- package/dist/src/modules/flow.js +156 -0
- package/dist/src/modules/integrations.d.ts +14 -0
- package/dist/src/modules/integrations.js +23 -0
- package/dist/src/modules/internalApi.d.ts +22 -0
- package/dist/src/modules/internalApi.js +160 -0
- package/dist/src/modules/invoicing.d.ts +18 -0
- package/dist/src/modules/invoicing.js +90 -0
- package/dist/src/modules/pos.d.ts +20 -0
- package/dist/src/modules/pos.js +80 -0
- package/dist/src/modules/sync.d.ts +10494 -0
- package/dist/src/modules/sync.js +75 -0
- package/dist/src/modules/syncs.d.ts +26200 -0
- package/dist/src/modules/syncs.js +29 -0
- package/dist/src/modules/webhooks.d.ts +55 -0
- package/dist/src/modules/webhooks.js +53 -0
- package/dist/src/types/api.d.ts +28 -0
- package/dist/src/types/api.js +2 -0
- package/dist/src/types/consumers.d.ts +8 -0
- package/dist/src/types/consumers.js +2 -0
- package/dist/src/types/public-api/mappings.d.ts +21 -0
- package/dist/src/types/public-api/mappings.js +2 -0
- package/dist/src/types/sync.d.ts +20 -0
- package/dist/src/types/sync.js +2 -0
- package/dist/test/modules/accounting.test.d.ts +1 -0
- package/dist/test/modules/accounting.test.js +453 -0
- package/dist/test/modules/consumer.test.d.ts +1 -0
- package/dist/test/modules/consumer.test.js +89 -0
- package/dist/test/modules/consumers.test.d.ts +1 -0
- package/dist/test/modules/consumers.test.js +109 -0
- package/dist/test/modules/ecommerce.test.d.ts +1 -0
- package/dist/test/modules/ecommerce.test.js +193 -0
- package/dist/test/modules/flow.test.d.ts +1 -0
- package/dist/test/modules/flow.test.js +69 -0
- package/dist/test/modules/integrations.test.d.ts +1 -0
- package/dist/test/modules/integrations.test.js +54 -0
- package/dist/test/modules/invoicing.test.d.ts +1 -0
- package/dist/test/modules/invoicing.test.js +108 -0
- package/dist/test/modules/pos.test.d.ts +1 -0
- package/dist/test/modules/pos.test.js +164 -0
- package/dist/test/modules/sync.test.d.ts +1 -0
- package/dist/test/modules/sync.test.js +81 -0
- package/dist/test/modules/syncs.test.d.ts +1 -0
- package/dist/test/modules/syncs.test.js +53 -0
- package/dist/test/modules/webhooks.test.d.ts +1 -0
- package/dist/test/modules/webhooks.test.js +120 -0
- package/jest.config.ts +195 -0
- package/package.json +47 -0
- package/src/helpers/openapi.ts +22 -0
- package/src/helpers/settings.ts +3 -0
- package/src/index.ts +1 -0
- package/src/modules/accounting.ts +357 -0
- package/src/modules/api.ts +35 -0
- package/src/modules/consumer.ts +177 -0
- package/src/modules/consumers.ts +82 -0
- package/src/modules/custom.ts +36 -0
- package/src/modules/datastores.ts +19 -0
- package/src/modules/ecommerce.ts +82 -0
- package/src/modules/flow.ts +168 -0
- package/src/modules/integrations.ts +22 -0
- package/src/modules/internalApi.ts +162 -0
- package/src/modules/invoicing.ts +106 -0
- package/src/modules/pos.ts +112 -0
- package/src/modules/sync.ts +77 -0
- package/src/modules/syncs.ts +28 -0
- package/src/modules/webhooks.ts +86 -0
- package/src/types/api.ts +35 -0
- package/src/types/consumers.ts +9 -0
- package/src/types/public-api/mappings.ts +21 -0
- package/src/types/public-api/schema.d.ts +9436 -0
- package/src/types/sync.ts +23 -0
- package/test/data/accounting_invoice.pdf +0 -0
- package/test/modules/accounting.test.ts +483 -0
- package/test/modules/consumer.test.ts +68 -0
- package/test/modules/consumers.test.ts +83 -0
- package/test/modules/ecommerce.test.ts +177 -0
- package/test/modules/integrations.test.ts +22 -0
- package/test/modules/invoicing.test.ts +88 -0
- package/test/modules/pos.test.ts +145 -0
- package/test/modules/sync.test.ts +60 -0
- package/test/modules/syncs.test.ts +23 -0
- package/test/modules/webhooks.test.ts +92 -0
- package/test/set_envs.sh +14 -0
- package/tsconfig.json +107 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { components } from '../types/public-api/schema';
|
|
2
|
+
import { InternalAPI } from './internalApi';
|
|
3
|
+
|
|
4
|
+
const DataStores = (internalApi: InternalAPI) => {
|
|
5
|
+
const _internalApi: InternalAPI = internalApi;
|
|
6
|
+
|
|
7
|
+
const getDataStores = async () => {
|
|
8
|
+
const { data }: { data: components['schemas']['DataStoreItem'][] } = await _internalApi.get(
|
|
9
|
+
'/datastores'
|
|
10
|
+
);
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
getDataStores,
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { DataStores };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { operations, components } from '../types/public-api/schema';
|
|
2
|
+
import { RequestData } from '../types/api';
|
|
3
|
+
|
|
4
|
+
type getOrdersParams = Omit<
|
|
5
|
+
operations['ecommerce_get_orders']['parameters']['query'],
|
|
6
|
+
'page' | 'size'
|
|
7
|
+
>;
|
|
8
|
+
|
|
9
|
+
const ecommerceFactory = {
|
|
10
|
+
getCustomers(): RequestData<components['schemas']['CommerceCustomerItem'][]> {
|
|
11
|
+
return {
|
|
12
|
+
method: 'get',
|
|
13
|
+
url: '/consumers/{consumer_id}/commerce/customers',
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
getProducts(): RequestData<components['schemas']['app__routers__commerce__ProductItem'][]> {
|
|
17
|
+
return {
|
|
18
|
+
method: 'get',
|
|
19
|
+
url: '/consumers/{consumer_id}/commerce/products',
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
getCustomer(customerId: string): RequestData<components['schemas']['CommerceCustomerItem']> {
|
|
23
|
+
return {
|
|
24
|
+
method: 'get',
|
|
25
|
+
url: `/consumers/{consumer_id}/commerce/customers/${customerId}`,
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
getProduct(
|
|
29
|
+
productId: string
|
|
30
|
+
): RequestData<components['schemas']['app__routers__commerce__ProductItem']> {
|
|
31
|
+
return {
|
|
32
|
+
method: 'get',
|
|
33
|
+
url: `/consumers/{consumer_id}/commerce/products/${productId}`,
|
|
34
|
+
};
|
|
35
|
+
},
|
|
36
|
+
getProductVariantById(variantId: string): RequestData<components['schemas']['VariantItem']> {
|
|
37
|
+
return {
|
|
38
|
+
method: 'get',
|
|
39
|
+
url: `/consumers/{consumer_id}/commerce/variants/${variantId}`,
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
updateAvailableQuantity(
|
|
43
|
+
variantId: string,
|
|
44
|
+
inventoryDetails: components['schemas']['InventoryDetailsUpdate']
|
|
45
|
+
): RequestData<components['schemas']['InventoryDetailsItem']> {
|
|
46
|
+
return {
|
|
47
|
+
method: 'post',
|
|
48
|
+
url: `/consumers/{consumer_id}/commerce/variants/set_quantity/${variantId}`,
|
|
49
|
+
body: inventoryDetails,
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
getLocations(): RequestData<components['schemas']['CommerceLocationItem'][]> {
|
|
53
|
+
return {
|
|
54
|
+
method: 'get',
|
|
55
|
+
url: '/consumers/{consumer_id}/commerce/locations',
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
getOrders(params: getOrdersParams): RequestData<components['schemas']['OrderItemOut'][]> {
|
|
59
|
+
return {
|
|
60
|
+
method: 'get',
|
|
61
|
+
url: '/consumers/{consumer_id}/commerce/orders',
|
|
62
|
+
params: params,
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
createOrder(
|
|
66
|
+
order: components['schemas']['OrderItemIn']
|
|
67
|
+
): RequestData<components['schemas']['OrderItemOut']> {
|
|
68
|
+
return {
|
|
69
|
+
method: 'post',
|
|
70
|
+
url: `/consumers/{consumer_id}/commerce/orders`,
|
|
71
|
+
body: order,
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
getOrder(orderId: string): RequestData<components['schemas']['OrderItemOut']> {
|
|
75
|
+
return {
|
|
76
|
+
method: 'get',
|
|
77
|
+
url: `/consumers/{consumer_id}/commerce/orders/${orderId}`,
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export { ecommerceFactory };
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { components } from '../types/public-api/schema';
|
|
2
|
+
import { InternalAPI } from './internalApi';
|
|
3
|
+
import { Consumers } from './consumers';
|
|
4
|
+
import { Consumer } from './consumer';
|
|
5
|
+
import { SimpleResponseModel } from '../types/sync';
|
|
6
|
+
|
|
7
|
+
const Flow = (
|
|
8
|
+
internalApi: any,
|
|
9
|
+
body: components['schemas']['ReadFlowItem'],
|
|
10
|
+
syncid: string,
|
|
11
|
+
consumers: string[],
|
|
12
|
+
process?: (consumer: any, context: any) => any
|
|
13
|
+
) => {
|
|
14
|
+
const _internalApi: InternalAPI = internalApi;
|
|
15
|
+
const data: components['schemas']['ReadFlowItem'] = body;
|
|
16
|
+
const _syncid = syncid;
|
|
17
|
+
const _consumers = consumers;
|
|
18
|
+
const _process = process;
|
|
19
|
+
|
|
20
|
+
const sendEvent = async (payload: components['schemas']['PostSyncFlowEvent']) => {
|
|
21
|
+
const { data: response }: { data: components['schemas']['TriggerResponse'] } =
|
|
22
|
+
await _internalApi.post(`/syncs/${_syncid}/flows/${data.id}/event`, payload);
|
|
23
|
+
return response;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const getExecution = async (executionId: string) => {
|
|
27
|
+
const { data: response }: { data: components['schemas']['ChainExecutionItem'] } =
|
|
28
|
+
await _internalApi.get(`/syncs/${_syncid}/flows/${data.id}/executions/${executionId}`);
|
|
29
|
+
return response;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Internal use: Function used to execute a flow while testing locally or by calling the sendEvent function
|
|
34
|
+
*/
|
|
35
|
+
const execute = async ({ testData = {}, context = {} }: any) => {
|
|
36
|
+
// first create the process in Chift (it will check if it's already created or not and execute it)
|
|
37
|
+
await _internalApi.post(`/syncs/${_syncid}/flows`, {
|
|
38
|
+
name: data.name,
|
|
39
|
+
description: data.description,
|
|
40
|
+
execution: data.execution,
|
|
41
|
+
trigger: data.trigger,
|
|
42
|
+
config: data.config,
|
|
43
|
+
});
|
|
44
|
+
// execute locally or remotely by sending an event to execute the flow
|
|
45
|
+
if (context.local) {
|
|
46
|
+
// when you execute locally you need to have a process defined
|
|
47
|
+
const logs = context.logs || false;
|
|
48
|
+
if (_process) executeLocal(_process, logs);
|
|
49
|
+
else throw Error('Process is not defined it cannot be executed');
|
|
50
|
+
} else {
|
|
51
|
+
await sendEvent(testData);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Internal use: Function used to execute a flow locally based on the configured consumers.
|
|
57
|
+
*/
|
|
58
|
+
const localExecution = async (process: (consumer: any, context: any) => any) => {
|
|
59
|
+
_internalApi.debug = true;
|
|
60
|
+
for (let i = 0; i < _consumers.length; i++) {
|
|
61
|
+
// we do not care about the customer
|
|
62
|
+
let consumer;
|
|
63
|
+
try {
|
|
64
|
+
consumer = await Consumers(_internalApi).getConsumerById(_consumers[i]);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
console.log(
|
|
67
|
+
`Cannot run for consumer ${_consumers[i]} as it cannot be found; probably part of an external account ? Use the platform to debug.`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
if (consumer) {
|
|
71
|
+
const syncData = await consumer.getSyncData(_syncid);
|
|
72
|
+
const flow = syncData.enabled_flows?.find((flow) => flow.id === data.id);
|
|
73
|
+
if (flow && syncData.status === 'active') {
|
|
74
|
+
const context = {
|
|
75
|
+
...syncData,
|
|
76
|
+
flow_id: flow.id,
|
|
77
|
+
flow_name: flow.name,
|
|
78
|
+
flow_values: flow.values,
|
|
79
|
+
flow_config: flow.config,
|
|
80
|
+
};
|
|
81
|
+
delete context['enabled_flows'];
|
|
82
|
+
await process(consumer, context);
|
|
83
|
+
} else {
|
|
84
|
+
console.log(
|
|
85
|
+
`Cannot run for consumer ${_consumers[i]} as the flow is not activated or not correctly configured`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
_internalApi.debug = false;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Internal use: Function used to execute a flow locally for debugging purposes. It will create the executions server-side on Chift to be able to follow-up the execution through Chift's UI.
|
|
95
|
+
* It will call localExecution to execute the flow for the configured consumers
|
|
96
|
+
*/
|
|
97
|
+
const executeLocal = async (
|
|
98
|
+
process: (consumer: typeof Consumer, context: any) => any,
|
|
99
|
+
log = false
|
|
100
|
+
) => {
|
|
101
|
+
if (log) {
|
|
102
|
+
// create executions on the platform to add the logs to the server
|
|
103
|
+
const { data: response }: { data: SimpleResponseModel } = await _internalApi.post(
|
|
104
|
+
`/syncs/${_syncid}/flows/${data.id}/local`,
|
|
105
|
+
{ type: 'START' }
|
|
106
|
+
);
|
|
107
|
+
if (response.data) {
|
|
108
|
+
const { executionid, chainexecutionid } = response.data;
|
|
109
|
+
_internalApi.setRelatedChainExecutionId(chainexecutionid);
|
|
110
|
+
try {
|
|
111
|
+
await localExecution(process);
|
|
112
|
+
await _internalApi.post(`/syncs/${_syncid}/flows/${data.id}/local`, {
|
|
113
|
+
type: 'END',
|
|
114
|
+
executionid: executionid,
|
|
115
|
+
chainexecutionid: chainexecutionid,
|
|
116
|
+
});
|
|
117
|
+
} catch (err: any) {
|
|
118
|
+
if (err.error) {
|
|
119
|
+
// it's an error from the one api
|
|
120
|
+
const error_message = `Error when executing request with url ${err.url} for consumer ${err.consumerName} (${err.consumerId})`;
|
|
121
|
+
console.log(`[ERROR]: ${error_message}: ${JSON.stringify(err.error)}`);
|
|
122
|
+
await _internalApi.post(`/syncs/${_syncid}/flows/${data.id}/local`, {
|
|
123
|
+
type: 'END',
|
|
124
|
+
executionid: executionid,
|
|
125
|
+
chainexecutionid: chainexecutionid,
|
|
126
|
+
error: true,
|
|
127
|
+
error_message: error_message,
|
|
128
|
+
technical_error_message: JSON.stringify(err.error),
|
|
129
|
+
});
|
|
130
|
+
} else {
|
|
131
|
+
console.log('[ERROR]: ' + err.toString());
|
|
132
|
+
await _internalApi.post(`/syncs/${_syncid}/flows/${data.id}/local`, {
|
|
133
|
+
type: 'END',
|
|
134
|
+
executionid: executionid,
|
|
135
|
+
chainexecutionid: chainexecutionid,
|
|
136
|
+
error: true,
|
|
137
|
+
technical_error_message: err.toString(),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
_internalApi.setRelatedChainExecutionId('');
|
|
142
|
+
} else {
|
|
143
|
+
throw Error('Execution could not be executed');
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
try {
|
|
147
|
+
await localExecution(process);
|
|
148
|
+
} catch (err: any) {
|
|
149
|
+
if (err.error) {
|
|
150
|
+
const error_message = `Error when executing request with url ${err.url} for consumer ${err.consumerName} (${err.consumerId})`;
|
|
151
|
+
console.log(`[ERROR] ${error_message}: ${JSON.stringify(err.error)}`);
|
|
152
|
+
} else {
|
|
153
|
+
console.log('[ERROR]: ' + err.toString());
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
execute,
|
|
161
|
+
getExecution,
|
|
162
|
+
executeLocal,
|
|
163
|
+
flowId: data.id,
|
|
164
|
+
name: data.name,
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
export { Flow };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { operations } from '../types/public-api/schema';
|
|
2
|
+
import { InternalAPI } from './internalApi';
|
|
3
|
+
import { chiftOperations } from '../types/public-api/mappings';
|
|
4
|
+
|
|
5
|
+
const Integrations = (internalApi: InternalAPI) => {
|
|
6
|
+
const _internalApi: InternalAPI = internalApi;
|
|
7
|
+
|
|
8
|
+
const getIntegrations = async () => {
|
|
9
|
+
const {
|
|
10
|
+
data,
|
|
11
|
+
}: {
|
|
12
|
+
data: operations[chiftOperations['getIntegrations']]['responses'][200]['content']['application/json'];
|
|
13
|
+
} = await _internalApi.get('/integrations');
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
getIntegrations,
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { Integrations };
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import axios, { AxiosInstance } from 'axios';
|
|
2
|
+
import { AuthType, TokenType, RequestData } from '../types/api';
|
|
3
|
+
import Settings from '../helpers/settings';
|
|
4
|
+
|
|
5
|
+
class InternalAPI {
|
|
6
|
+
instance: AxiosInstance;
|
|
7
|
+
auth: AuthType;
|
|
8
|
+
token?: TokenType;
|
|
9
|
+
debug = false;
|
|
10
|
+
relatedChainExecutionId?: string;
|
|
11
|
+
get;
|
|
12
|
+
post;
|
|
13
|
+
patch;
|
|
14
|
+
delete;
|
|
15
|
+
|
|
16
|
+
constructor(auth: AuthType) {
|
|
17
|
+
// add interceptor
|
|
18
|
+
this.auth = auth;
|
|
19
|
+
this.instance = axios.create({
|
|
20
|
+
baseURL: this.auth.baseUrl || Settings.BASE_URL,
|
|
21
|
+
});
|
|
22
|
+
this.get = this.instance.get;
|
|
23
|
+
this.post = this.instance.post;
|
|
24
|
+
this.patch = this.instance.patch;
|
|
25
|
+
this.delete = this.instance.delete;
|
|
26
|
+
|
|
27
|
+
this.instance.interceptors.request.use(
|
|
28
|
+
(config) => {
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
if (this.relatedChainExecutionId) {
|
|
31
|
+
config.headers['X-Chift-RelatedChainExecutionId'] =
|
|
32
|
+
this.relatedChainExecutionId;
|
|
33
|
+
}
|
|
34
|
+
if (this.token) {
|
|
35
|
+
if (this.token?.expires_on < new Date().getTime()) {
|
|
36
|
+
return this.getToken().then(() => {
|
|
37
|
+
config.headers['Authorization'] =
|
|
38
|
+
'Bearer ' + this.token?.access_token;
|
|
39
|
+
return resolve(config);
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
config.headers['Authorization'] = 'Bearer ' + this.token?.access_token;
|
|
43
|
+
return resolve(config);
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
return this.getToken().then(() => {
|
|
47
|
+
config.headers['Authorization'] = 'Bearer ' + this.token?.access_token;
|
|
48
|
+
return resolve(config);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
function (error) {
|
|
54
|
+
// Do something with request error
|
|
55
|
+
return Promise.reject(error);
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
getToken = async () => {
|
|
61
|
+
try {
|
|
62
|
+
const tokenData: AuthType = {
|
|
63
|
+
clientId: this.auth.clientId,
|
|
64
|
+
clientSecret: this.auth.clientSecret,
|
|
65
|
+
accountId: this.auth.accountId,
|
|
66
|
+
};
|
|
67
|
+
if (this.auth.envId) {
|
|
68
|
+
tokenData['envId'] = this.auth.envId;
|
|
69
|
+
}
|
|
70
|
+
const res = await axios.post(
|
|
71
|
+
`${this.auth.baseUrl || Settings.BASE_URL}/token`,
|
|
72
|
+
tokenData
|
|
73
|
+
);
|
|
74
|
+
this.token = res.data;
|
|
75
|
+
} catch (err: any) {
|
|
76
|
+
if (axios.isAxiosError(err)) {
|
|
77
|
+
if (err.response) {
|
|
78
|
+
if (err.response.status === 401) {
|
|
79
|
+
throw new Error('The provided credentials are not correct');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
getPaginationParams = (currPage: number) => {
|
|
87
|
+
return {
|
|
88
|
+
page: currPage,
|
|
89
|
+
size: 100,
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
public setRelatedChainExecutionId(chainExecutionId: string) {
|
|
94
|
+
this.relatedChainExecutionId = chainExecutionId;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public async makeRequest<TResponse>(requestData: RequestData<TResponse>) {
|
|
98
|
+
try {
|
|
99
|
+
if (this.debug) {
|
|
100
|
+
console.log(
|
|
101
|
+
`[DEBUG]: Executing operation ${requestData?.property} with url ${requestData.url} for consumer: ${requestData.consumerName}`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
let continuePagination = true;
|
|
105
|
+
let items: any[] = [];
|
|
106
|
+
let currentPage = 0;
|
|
107
|
+
while (continuePagination) {
|
|
108
|
+
currentPage++;
|
|
109
|
+
let params = requestData.params || {};
|
|
110
|
+
if (requestData.method === 'get') {
|
|
111
|
+
params = {
|
|
112
|
+
...params,
|
|
113
|
+
...this.getPaginationParams(currentPage),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const res = await this.instance({
|
|
117
|
+
url: requestData.url,
|
|
118
|
+
method: requestData.method,
|
|
119
|
+
params: params,
|
|
120
|
+
data: requestData.body !== undefined ? requestData.body : undefined,
|
|
121
|
+
});
|
|
122
|
+
const { data } = res;
|
|
123
|
+
if (data) {
|
|
124
|
+
if (requestData.method === 'get' && 'total' in data && 'items' in data) {
|
|
125
|
+
if (currentPage * 100 > data.total) {
|
|
126
|
+
continuePagination = false;
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
if (this.debug) {
|
|
130
|
+
console.log(`[DEBUG]: Data received: ${JSON.stringify(data)}`);
|
|
131
|
+
}
|
|
132
|
+
return data;
|
|
133
|
+
}
|
|
134
|
+
items = items.concat(data.items);
|
|
135
|
+
} else {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (this.debug) {
|
|
140
|
+
console.log(`[DEBUG]: Data received: ${JSON.stringify(items)}`);
|
|
141
|
+
}
|
|
142
|
+
return items;
|
|
143
|
+
} catch (e: any) {
|
|
144
|
+
if (e.response) {
|
|
145
|
+
if (e.response.data) {
|
|
146
|
+
const error = { ...e.response.data, status_code: e.response.status };
|
|
147
|
+
const fullerror = {
|
|
148
|
+
error,
|
|
149
|
+
consumerId: requestData.consumerId,
|
|
150
|
+
consumerName: requestData.consumerName,
|
|
151
|
+
url: requestData.url,
|
|
152
|
+
};
|
|
153
|
+
//return error as TResponse;
|
|
154
|
+
throw fullerror;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
throw e;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export { InternalAPI };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { components, operations } from '../types/public-api/schema';
|
|
2
|
+
import { RequestData } from '../types/api';
|
|
3
|
+
|
|
4
|
+
const invoicingFactory = {
|
|
5
|
+
getInvoices(
|
|
6
|
+
params: operations['invoicing_get_invoices']['parameters']['query']
|
|
7
|
+
): RequestData<components['schemas']['InvoiceItemOut'][]> {
|
|
8
|
+
return {
|
|
9
|
+
method: 'get',
|
|
10
|
+
url: `/consumers/{consumer_id}/invoicing/invoices`,
|
|
11
|
+
params: params,
|
|
12
|
+
};
|
|
13
|
+
},
|
|
14
|
+
getInvoiceById(
|
|
15
|
+
invoiceId: string,
|
|
16
|
+
params: operations['invoicing_get_invoice']['parameters']['query']
|
|
17
|
+
): RequestData<components['schemas']['InvoiceItemOut']> {
|
|
18
|
+
return {
|
|
19
|
+
method: 'get',
|
|
20
|
+
url: `/consumers/{consumer_id}/invoicing/invoices/${invoiceId}`,
|
|
21
|
+
params: params,
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
createInvoice(
|
|
25
|
+
invoice: components['schemas']['InvoiceItem']
|
|
26
|
+
): RequestData<components['schemas']['InvoiceItemOut']> {
|
|
27
|
+
return {
|
|
28
|
+
method: 'create',
|
|
29
|
+
url: `/consumers/{consumer_id}/invoicing/invoices`,
|
|
30
|
+
body: invoice,
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
getProducts(): RequestData<components['schemas']['ProductItemOut'][]> {
|
|
34
|
+
return {
|
|
35
|
+
method: 'get',
|
|
36
|
+
url: `/consumers/{consumer_id}/invoicing/products`,
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
getProductById(productId: string): RequestData<components['schemas']['ProductItemOut']> {
|
|
40
|
+
return {
|
|
41
|
+
method: 'get',
|
|
42
|
+
url: `/consumers/{consumer_id}/invoicing/products/${productId}`,
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
createProduct(
|
|
46
|
+
product: components['schemas']['models__invoicing__ProductItem']
|
|
47
|
+
): RequestData<components['schemas']['ProductItemOut']> {
|
|
48
|
+
return {
|
|
49
|
+
method: 'create',
|
|
50
|
+
url: `/consumers/{consumer_id}/invoicing/products`,
|
|
51
|
+
body: product,
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
getTaxes(): RequestData<components['schemas']['models__invoicing__VatCode'][]> {
|
|
55
|
+
return {
|
|
56
|
+
method: 'get',
|
|
57
|
+
url: `/consumers/{consumer_id}/invoicing/taxes`,
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
getTaxById(taxId: string): RequestData<components['schemas']['models__invoicing__VatCode']> {
|
|
61
|
+
return {
|
|
62
|
+
method: 'get',
|
|
63
|
+
url: `/consumers/{consumer_id}/invoicing/taxes/${taxId}`,
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
getOpportunities(): RequestData<components['schemas']['OpportunityItem'][]> {
|
|
67
|
+
return {
|
|
68
|
+
method: 'get',
|
|
69
|
+
url: `/consumers/{consumer_id}/invoicing/opportunities`,
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
getOpportunitiesById(
|
|
73
|
+
opportunityId: string
|
|
74
|
+
): RequestData<components['schemas']['OpportunityItem']> {
|
|
75
|
+
return {
|
|
76
|
+
method: 'get',
|
|
77
|
+
url: `/consumers/{consumer_id}/invoicing/opportunities/${opportunityId}`,
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
getContacts(
|
|
81
|
+
params?: operations['invoicing_get_contacts']['parameters']['query']
|
|
82
|
+
): RequestData<components['schemas']['ContactItemOut'][]> {
|
|
83
|
+
return {
|
|
84
|
+
method: 'get',
|
|
85
|
+
url: `/consumers/{consumer_id}/invoicing/contacts`,
|
|
86
|
+
params: params,
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
getContactById(contactId: string): RequestData<components['schemas']['ContactItemOut']> {
|
|
90
|
+
return {
|
|
91
|
+
method: 'get',
|
|
92
|
+
url: `/consumers/{consumer_id}/invoicing/contacts/${contactId}`,
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
createContact(
|
|
96
|
+
contact: components['schemas']['ContactItemIn']
|
|
97
|
+
): RequestData<components['schemas']['ContactItemOut']> {
|
|
98
|
+
return {
|
|
99
|
+
method: 'create',
|
|
100
|
+
url: `/consumers/{consumer_id}/invoicing/contacts`,
|
|
101
|
+
body: contact,
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export { invoicingFactory };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { operations, components } from '../types/public-api/schema';
|
|
2
|
+
import { RequestData } from '../types/api';
|
|
3
|
+
|
|
4
|
+
type getPaymentsParams = Omit<
|
|
5
|
+
operations['pos_get_payments']['parameters']['query'],
|
|
6
|
+
'page' | 'size'
|
|
7
|
+
>;
|
|
8
|
+
|
|
9
|
+
type getPaymentMethodsParams = Omit<
|
|
10
|
+
operations['pos_get_payments_methods']['parameters']['query'],
|
|
11
|
+
'page' | 'size'
|
|
12
|
+
>;
|
|
13
|
+
|
|
14
|
+
type getCustomersParams = Omit<
|
|
15
|
+
operations['pos_get_customers']['parameters']['query'],
|
|
16
|
+
'page' | 'size'
|
|
17
|
+
>;
|
|
18
|
+
|
|
19
|
+
type getOrdersParams = Omit<operations['pos_get_orders']['parameters']['query'], 'page' | 'size'>;
|
|
20
|
+
|
|
21
|
+
const posFactory = {
|
|
22
|
+
getLocations(): RequestData<components['schemas']['POSLocationItem'][]> {
|
|
23
|
+
return {
|
|
24
|
+
method: 'get',
|
|
25
|
+
url: '/consumers/{consumer_id}/pos/locations',
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
getOrders(params: getOrdersParams): RequestData<components['schemas']['OrderItem'][]> {
|
|
29
|
+
return {
|
|
30
|
+
params,
|
|
31
|
+
method: 'get',
|
|
32
|
+
url: '/consumers/{consumer_id}/pos/orders',
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
getCustomers(
|
|
36
|
+
params: getCustomersParams
|
|
37
|
+
): RequestData<components['schemas']['POSCustomerItem'][]> {
|
|
38
|
+
return {
|
|
39
|
+
params,
|
|
40
|
+
method: 'get',
|
|
41
|
+
url: '/consumers/{consumer_id}/pos/customers',
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
getOrder(orderId: string): RequestData<components['schemas']['OrderItem']> {
|
|
45
|
+
return {
|
|
46
|
+
method: 'get',
|
|
47
|
+
url: `/consumers/{consumer_id}/pos/orders/${orderId}`,
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
getCustomer(customerId: string): RequestData<components['schemas']['POSCustomerItem']> {
|
|
51
|
+
return {
|
|
52
|
+
method: 'get',
|
|
53
|
+
url: `/consumers/{consumer_id}/pos/customers/${customerId}`,
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
createCustomer(
|
|
57
|
+
customer: components['schemas']['POSCreateCustomerItem']
|
|
58
|
+
): RequestData<components['schemas']['POSCustomerItem']> {
|
|
59
|
+
return {
|
|
60
|
+
method: 'create',
|
|
61
|
+
url: `/consumers/{consumer_id}/pos/customers`,
|
|
62
|
+
body: customer,
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
getPaymentMethods(
|
|
66
|
+
params: getPaymentMethodsParams
|
|
67
|
+
): RequestData<components['schemas']['PaymentMethods'][]> {
|
|
68
|
+
return {
|
|
69
|
+
params,
|
|
70
|
+
method: 'get',
|
|
71
|
+
url: `/consumers/{consumer_id}/pos/payment-methods`,
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
getSales(
|
|
75
|
+
params: operations['pos_get_sales']['parameters']['query']
|
|
76
|
+
): RequestData<components['schemas']['SalesItem']> {
|
|
77
|
+
return {
|
|
78
|
+
params,
|
|
79
|
+
method: 'get',
|
|
80
|
+
url: `/consumers/{consumer_id}/pos/sales`,
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
getClosure(
|
|
84
|
+
date: string,
|
|
85
|
+
params: operations['pos_get_closure']['parameters']['query']
|
|
86
|
+
): RequestData<components['schemas']['ClosureItem']> {
|
|
87
|
+
return {
|
|
88
|
+
params,
|
|
89
|
+
method: 'get',
|
|
90
|
+
url: `/consumers/{consumer_id}/pos/closures/${date}`,
|
|
91
|
+
};
|
|
92
|
+
},
|
|
93
|
+
getPayments(params: getPaymentsParams): RequestData<components['schemas']['PaymentItem'][]> {
|
|
94
|
+
return {
|
|
95
|
+
params,
|
|
96
|
+
method: 'get',
|
|
97
|
+
url: `/consumers/{consumer_id}/pos/payments`,
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
updateOrder(
|
|
101
|
+
orderId: string,
|
|
102
|
+
order: components['schemas']['UpdateOrderItem']
|
|
103
|
+
): RequestData<components['schemas']['OrderItem']> {
|
|
104
|
+
return {
|
|
105
|
+
method: 'patch',
|
|
106
|
+
url: `/consumers/{consumer_id}/pos/orders/${orderId}`,
|
|
107
|
+
body: order,
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export { posFactory };
|