cloudcommerce 0.0.108 → 0.0.109
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/CHANGELOG.md +12 -0
- package/package.json +8 -8
- package/packages/api/package.json +1 -1
- package/packages/apps/correios/package.json +1 -1
- package/packages/apps/custom-shipping/package.json +1 -1
- package/packages/apps/discounts/package.json +1 -1
- package/packages/apps/frenet/package.json +1 -1
- package/packages/apps/tiny-erp/lib/event-to-tiny.js +94 -94
- package/packages/apps/tiny-erp/lib/index.js +1 -1
- package/packages/apps/tiny-erp/lib/integration/after-tiny-queue.js +74 -71
- package/packages/apps/tiny-erp/lib/integration/export-order-to-tiny.js +73 -70
- package/packages/apps/tiny-erp/lib/integration/export-product-to-tiny.js +53 -49
- package/packages/apps/tiny-erp/lib/integration/helpers/format-tiny-date.js +3 -3
- package/packages/apps/tiny-erp/lib/integration/import-order-from-tiny.js +75 -76
- package/packages/apps/tiny-erp/lib/integration/import-product-from-tiny.js +140 -137
- package/packages/apps/tiny-erp/lib/integration/parsers/order-from-tiny.js +39 -40
- package/packages/apps/tiny-erp/lib/integration/parsers/order-to-tiny.js +178 -173
- package/packages/apps/tiny-erp/lib/integration/parsers/product-from-tiny.js +171 -173
- package/packages/apps/tiny-erp/lib/integration/parsers/product-to-tiny.js +127 -123
- package/packages/apps/tiny-erp/lib/integration/parsers/status-from-tiny.js +32 -32
- package/packages/apps/tiny-erp/lib/integration/parsers/status-to-tiny.js +37 -37
- package/packages/apps/tiny-erp/lib/integration/post-tiny-erp.js +43 -42
- package/packages/apps/tiny-erp/lib/tiny-erp.js +6 -8
- package/packages/apps/tiny-erp/lib/tiny-webhook.js +73 -76
- package/packages/apps/tiny-erp/package.json +2 -2
- package/packages/cli/package.json +1 -1
- package/packages/config/package.json +1 -1
- package/packages/events/package.json +2 -2
- package/packages/firebase/package.json +2 -2
- package/packages/i18n/package.json +1 -1
- package/packages/modules/lib/firebase/ajv.js +19 -24
- package/packages/modules/lib/firebase/call-app-module.js +116 -110
- package/packages/modules/lib/firebase/checkout.js +151 -152
- package/packages/modules/lib/firebase/functions-checkout/fix-items.js +194 -187
- package/packages/modules/lib/firebase/functions-checkout/get-custumerId.js +25 -26
- package/packages/modules/lib/firebase/functions-checkout/handle-order-transaction.js +121 -119
- package/packages/modules/lib/firebase/functions-checkout/new-order.js +177 -177
- package/packages/modules/lib/firebase/functions-checkout/request-to-module.js +59 -59
- package/packages/modules/lib/firebase/functions-checkout/utils.js +195 -197
- package/packages/modules/lib/firebase/handle-module.js +144 -146
- package/packages/modules/lib/firebase/proxy-apps.js +2 -1
- package/packages/modules/lib/firebase/serve-modules-api.js +52 -53
- package/packages/modules/lib/firebase.js +4 -6
- package/packages/modules/lib/index.js +12 -15
- package/packages/modules/package.json +2 -2
- package/packages/passport/package.json +2 -2
- package/packages/ssr/package.json +5 -6
- package/packages/storefront/dist/client/{LoginOffcanvas.c2a47936.js → LoginOffcanvas.28d2f50c.js} +1 -1
- package/packages/storefront/dist/client/assets/{_...4826ec44.css → _...41f686e3.css} +0 -0
- package/packages/storefront/dist/client/chunks/{LoginForm.c86d05cc.js → LoginForm.c995d3d4.js} +120 -90
- package/packages/storefront/dist/client/chunks/LoginOffcanvas.a1f3fe3b.js +1 -0
- package/packages/storefront/dist/client/chunks/runtime-dom.esm-bundler.62d623e7.js +1 -0
- package/packages/storefront/dist/client/{client.6db2fd63.js → client.6736232b.js} +1 -1
- package/packages/storefront/dist/client/sw.js +1 -1
- package/packages/storefront/dist/server/entry.mjs +11 -7
- package/packages/storefront/package.json +12 -9
- package/packages/storefront/src/lib/components/LoginForm.vue +42 -22
- package/packages/storefront/src/lib/components/TheHeader.vue +1 -1
- package/packages/types/package.json +1 -1
- package/packages/storefront/dist/client/chunks/LoginOffcanvas.794b9840.js +0 -1
- package/packages/storefront/dist/client/chunks/runtime-dom.esm-bundler.4b976197.js +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [0.0.109](https://github.com/ecomplus/cloud-commerce/compare/v0.0.108...v0.0.109) (2022-10-04)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **storefront:** Start handling email link sign in within `LoginForm` ([a1f17bb](https://github.com/ecomplus/cloud-commerce/commit/a1f17bba3cb8a4ec78deaefcb8caac3736d7e4f5))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* **deps:** Update all non-major dependencies ([c5d6f56](https://github.com/ecomplus/cloud-commerce/commit/c5d6f56c37edbadc04dbcefae35408118bc22111))
|
|
16
|
+
|
|
5
17
|
### [0.0.108](https://github.com/ecomplus/cloud-commerce/compare/v0.0.107...v0.0.108) (2022-10-01)
|
|
6
18
|
|
|
7
19
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cloudcommerce",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.109",
|
|
5
5
|
"description": "Open fair-code headless commerce platform: API-first, microservices based, event driven and cloud native",
|
|
6
6
|
"main": "packages/api/lib/index.js",
|
|
7
7
|
"author": "E-Com Club Softwares para E-commerce <ti@e-com.club>",
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"@commitlint/cli": "^17.1.2",
|
|
20
20
|
"@commitlint/config-conventional": "^17.1.0",
|
|
21
21
|
"@commitlint/rules": "^17.0.0",
|
|
22
|
-
"@types/node": "^18.
|
|
23
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
24
|
-
"@typescript-eslint/parser": "^5.
|
|
25
|
-
"esbuild": "^0.15.
|
|
22
|
+
"@types/node": "^18.8.1",
|
|
23
|
+
"@typescript-eslint/eslint-plugin": "^5.39.0",
|
|
24
|
+
"@typescript-eslint/parser": "^5.39.0",
|
|
25
|
+
"esbuild": "^0.15.10",
|
|
26
26
|
"eslint": "^8.24.0",
|
|
27
27
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
28
28
|
"eslint-plugin-import": "^2.26.0",
|
|
@@ -31,9 +31,9 @@
|
|
|
31
31
|
"node-fetch": "^3.2.10",
|
|
32
32
|
"standard-version": "^9.5.0",
|
|
33
33
|
"ts-node": "^10.9.1",
|
|
34
|
-
"turbo": "^1.5.
|
|
35
|
-
"typescript": "^4.8.
|
|
36
|
-
"vite": "^3.1.
|
|
34
|
+
"turbo": "^1.5.5",
|
|
35
|
+
"typescript": "^4.8.4",
|
|
36
|
+
"vite": "^3.1.4",
|
|
37
37
|
"vitest": "^0.23.4",
|
|
38
38
|
"zx": "^7.0.8"
|
|
39
39
|
},
|
|
@@ -4,112 +4,112 @@ import exportOrder from './integration/export-order-to-tiny.js';
|
|
|
4
4
|
import importProduct from './integration/import-product-from-tiny.js';
|
|
5
5
|
import importOrder from './integration/import-order-from-tiny.js';
|
|
6
6
|
import afterQueue from './integration/after-tiny-queue.js';
|
|
7
|
-
|
|
8
7
|
// Async integration handlers
|
|
9
8
|
const integrationHandlers = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
exportation: {
|
|
10
|
+
product_ids: exportProduct,
|
|
11
|
+
order_ids: exportOrder,
|
|
12
|
+
},
|
|
13
|
+
importation: {
|
|
14
|
+
skus: importProduct,
|
|
15
|
+
order_numbers: importOrder,
|
|
16
|
+
},
|
|
18
17
|
};
|
|
19
|
-
const handleApiEvent = async ({
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const appData = { ...app.data, ...app.hidden_data };
|
|
26
|
-
if (Array.isArray(appData.ignore_events)
|
|
18
|
+
const handleApiEvent = async ({ evName, apiEvent, apiDoc, app, }) => {
|
|
19
|
+
const resourceId = apiEvent.resource_id;
|
|
20
|
+
logger.info('>> ', resourceId, ' - Action: ', apiEvent.action);
|
|
21
|
+
const key = `${evName}_${resourceId}`;
|
|
22
|
+
const appData = { ...app.data, ...app.hidden_data };
|
|
23
|
+
if (Array.isArray(appData.ignore_events)
|
|
27
24
|
&& appData.ignore_events.includes(evName)) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
logger.info(`> Webhook ${resourceId} [${evName}]`);
|
|
32
|
-
if (!process.env.TINY_ERP_TOKEN) {
|
|
33
|
-
const tinyToken = appData.tiny_api_token;
|
|
34
|
-
if (typeof tinyToken === 'string' && tinyToken) {
|
|
35
|
-
process.env.TINY_ERP_TOKEN = tinyToken;
|
|
36
|
-
} else {
|
|
37
|
-
logger.warn('Missing Tiny API token');
|
|
25
|
+
logger.info('>> ', key, ' - Ignored event');
|
|
26
|
+
return null;
|
|
38
27
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
canCreateNew = Boolean(appData.new_orders);
|
|
48
|
-
integrationConfig = {
|
|
49
|
-
_exportation: {
|
|
50
|
-
order_ids: [resourceId],
|
|
51
|
-
},
|
|
52
|
-
};
|
|
53
|
-
} else {
|
|
54
|
-
if (evName === 'products-new') {
|
|
55
|
-
if (!appData.new_products) {
|
|
56
|
-
return null;
|
|
28
|
+
logger.info(`> Webhook ${resourceId} [${evName}]`);
|
|
29
|
+
if (!process.env.TINY_ERP_TOKEN) {
|
|
30
|
+
const tinyToken = appData.tiny_api_token;
|
|
31
|
+
if (typeof tinyToken === 'string' && tinyToken) {
|
|
32
|
+
process.env.TINY_ERP_TOKEN = tinyToken;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
logger.warn('Missing Tiny API token');
|
|
57
36
|
}
|
|
58
|
-
} else if (!appData.update_price) {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
integrationConfig = {
|
|
62
|
-
_exportation: {
|
|
63
|
-
product_ids: [resourceId],
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
37
|
}
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
38
|
+
if (process.env.TINY_ERP_TOKEN) {
|
|
39
|
+
let integrationConfig;
|
|
40
|
+
let canCreateNew = false;
|
|
41
|
+
if (evName === 'applications-dataSet') {
|
|
42
|
+
integrationConfig = appData;
|
|
43
|
+
canCreateNew = true;
|
|
44
|
+
}
|
|
45
|
+
else if (evName === 'orders-anyStatusSet') {
|
|
46
|
+
canCreateNew = Boolean(appData.new_orders);
|
|
47
|
+
integrationConfig = {
|
|
48
|
+
_exportation: {
|
|
49
|
+
order_ids: [resourceId],
|
|
50
|
+
},
|
|
51
|
+
};
|
|
72
52
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
53
|
+
else {
|
|
54
|
+
if (evName === 'products-new') {
|
|
55
|
+
if (!appData.new_products) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else if (!appData.update_price) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
integrationConfig = {
|
|
63
|
+
_exportation: {
|
|
64
|
+
product_ids: [resourceId],
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (integrationConfig) {
|
|
69
|
+
const actions = Object.keys(integrationHandlers);
|
|
70
|
+
actions.forEach((action) => {
|
|
71
|
+
for (let i = 1; i <= 3; i++) {
|
|
72
|
+
actions.push(`${('_'.repeat(i))}${action}`);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
for (let i = 0; i < actions.length; i++) {
|
|
76
|
+
const action = actions[i];
|
|
77
|
+
const actionQueues = integrationConfig[action];
|
|
78
|
+
if (typeof actionQueues === 'object' && actionQueues) {
|
|
79
|
+
// eslint-disable-next-line guard-for-in, no-restricted-syntax
|
|
80
|
+
for (const queue in actionQueues) {
|
|
81
|
+
const ids = actionQueues[queue];
|
|
82
|
+
if (Array.isArray(ids) && ids.length) {
|
|
83
|
+
const isHiddenQueue = action.charAt(0) === '_';
|
|
84
|
+
const handlerName = action.replace(/^_+/, '');
|
|
85
|
+
const handler = integrationHandlers[handlerName][queue.toLowerCase()];
|
|
86
|
+
const nextId = ids[0];
|
|
87
|
+
if (typeof nextId === 'string'
|
|
87
88
|
&& nextId.length
|
|
88
89
|
&& handler) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
90
|
+
const debugFlag = `#${action}/${queue}/${nextId}`;
|
|
91
|
+
logger.info(`> Starting ${debugFlag}`);
|
|
92
|
+
const queueEntry = {
|
|
93
|
+
action,
|
|
94
|
+
queue,
|
|
95
|
+
nextId,
|
|
96
|
+
key,
|
|
97
|
+
app,
|
|
98
|
+
};
|
|
99
|
+
return handler(apiDoc, queueEntry, appData, canCreateNew, isHiddenQueue).then((payload) => {
|
|
100
|
+
return afterQueue(queueEntry, appData, app, payload);
|
|
101
|
+
}).catch((err) => {
|
|
102
|
+
return afterQueue(queueEntry, appData, app, err);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
104
108
|
}
|
|
105
|
-
}
|
|
106
109
|
}
|
|
107
|
-
}
|
|
108
110
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
return null;
|
|
111
|
+
// Nothing to do
|
|
112
|
+
return null;
|
|
112
113
|
};
|
|
113
|
-
|
|
114
114
|
export default handleApiEvent;
|
|
115
|
-
|
|
115
|
+
//# sourceMappingURL=event-to-tiny.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from './tiny-erp.js';
|
|
2
|
-
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1,79 +1,82 @@
|
|
|
1
1
|
import logger from 'firebase-functions/lib/logger';
|
|
2
2
|
import updateAppData from '@cloudcommerce/firebase/lib/helpers/update-app-data';
|
|
3
|
-
|
|
4
3
|
export default async (queueEntry, appData, application, payload) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
} else {
|
|
27
|
-
const { config, response } = payload;
|
|
28
|
-
if (response) {
|
|
29
|
-
const { data, status } = response;
|
|
30
|
-
notes = `Error: Status ${status} \n${JSON.stringify(data)}`;
|
|
31
|
-
if (!status || status === 429 || status >= 500) {
|
|
32
|
-
return setTimeout(() => {
|
|
33
|
-
throw payload;
|
|
34
|
-
}, 2000);
|
|
4
|
+
const isError = payload instanceof Error;
|
|
5
|
+
const isImportation = queueEntry.action.endsWith('importation');
|
|
6
|
+
const logs = appData.logs || [];
|
|
7
|
+
const logEntry = {
|
|
8
|
+
resource: /order/i.test(queueEntry.queue) ? 'orders' : 'products',
|
|
9
|
+
[(isImportation ? 'tiny_id' : 'resource_id')]: queueEntry.nextId,
|
|
10
|
+
success: !isError,
|
|
11
|
+
imestamp: new Date().toISOString(),
|
|
12
|
+
};
|
|
13
|
+
let notes;
|
|
14
|
+
if (payload) {
|
|
15
|
+
if (!isError) {
|
|
16
|
+
// payload = response
|
|
17
|
+
const { data, status, config } = payload;
|
|
18
|
+
if (data && data._id) {
|
|
19
|
+
logEntry.resource_id = data._id;
|
|
20
|
+
}
|
|
21
|
+
notes = `Status ${status}`;
|
|
22
|
+
if (config) {
|
|
23
|
+
notes += ` [${config.url}]`;
|
|
24
|
+
}
|
|
35
25
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
else {
|
|
27
|
+
const { config, response } = payload;
|
|
28
|
+
if (response) {
|
|
29
|
+
const { data, status } = response;
|
|
30
|
+
notes = `Error: Status ${status} \n${JSON.stringify(data)}`;
|
|
31
|
+
if (!status || status === 429 || status >= 500) {
|
|
32
|
+
return setTimeout(() => {
|
|
33
|
+
throw payload;
|
|
34
|
+
}, 2000);
|
|
35
|
+
}
|
|
36
|
+
if (config) {
|
|
37
|
+
const { url, method, data } = config;
|
|
38
|
+
notes += `\n\n-- Request -- \n${method} ${url} \n${JSON.stringify(data)}`;
|
|
39
|
+
}
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
}
|
|
42
|
+
else if (payload.isConfigError === true) {
|
|
43
|
+
notes = payload.message;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
notes = payload.stack;
|
|
47
|
+
}
|
|
39
48
|
}
|
|
40
|
-
// @ts-ignore
|
|
41
|
-
} else if (payload.isConfigError === true) {
|
|
42
|
-
notes = payload.message;
|
|
43
|
-
} else {
|
|
44
|
-
notes = payload.stack;
|
|
45
|
-
}
|
|
46
49
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
logEntry.notes = notes.substring(0, 5000);
|
|
50
|
-
}
|
|
51
|
-
if (isError || !isImportation) {
|
|
52
|
-
logs.unshift(logEntry);
|
|
53
|
-
await updateAppData(application, {
|
|
54
|
-
logs: logs.slice(0, 200),
|
|
55
|
-
}, {
|
|
56
|
-
isHiddenData: true,
|
|
57
|
-
canSendPubSub: false,
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
const { action, queue, nextId } = queueEntry;
|
|
61
|
-
let queueList = appData[action][queue];
|
|
62
|
-
if (Array.isArray(queueList)) {
|
|
63
|
-
const idIndex = queueList.indexOf(nextId);
|
|
64
|
-
if (idIndex > -1) {
|
|
65
|
-
queueList.splice(idIndex, 1);
|
|
50
|
+
if (notes) {
|
|
51
|
+
logEntry.notes = notes.substring(0, 5000);
|
|
66
52
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
53
|
+
if (isError || !isImportation) {
|
|
54
|
+
logs.unshift(logEntry);
|
|
55
|
+
await updateAppData(application, {
|
|
56
|
+
logs: logs.slice(0, 200),
|
|
57
|
+
}, {
|
|
58
|
+
isHiddenData: true,
|
|
59
|
+
canSendPubSub: false,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const { action, queue, nextId } = queueEntry;
|
|
63
|
+
let queueList = appData[action][queue];
|
|
64
|
+
if (Array.isArray(queueList)) {
|
|
65
|
+
const idIndex = queueList.indexOf(nextId);
|
|
66
|
+
if (idIndex > -1) {
|
|
67
|
+
queueList.splice(idIndex, 1);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
queueList = [];
|
|
72
|
+
}
|
|
73
|
+
const data = {
|
|
74
|
+
[action]: {
|
|
75
|
+
...appData[action],
|
|
76
|
+
[queue]: queueList,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
logger.info(JSON.stringify(data));
|
|
80
|
+
return updateAppData(application, data);
|
|
78
81
|
};
|
|
79
|
-
|
|
82
|
+
//# sourceMappingURL=after-tiny-queue.js.map
|
|
@@ -3,82 +3,85 @@ import api from '@cloudcommerce/api';
|
|
|
3
3
|
import postTiny from './post-tiny-erp.js';
|
|
4
4
|
import parseStatus from './parsers/status-to-tiny.js';
|
|
5
5
|
import parseOrder from './parsers/order-to-tiny.js';
|
|
6
|
-
|
|
7
6
|
export default async (apiDoc, queueEntry, appData, canCreateNew) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} else {
|
|
13
|
-
try {
|
|
14
|
-
order = (await api.get(`orders/${orderId}`)).data;
|
|
15
|
-
} catch (err) {
|
|
16
|
-
if (err.statusCode === 404) {
|
|
17
|
-
const msg = `O pedido ${orderId} não existe (:${err.statusCode})`;
|
|
18
|
-
const error = new Error(msg);
|
|
19
|
-
error.isConfigError = true;
|
|
20
|
-
return error;
|
|
21
|
-
}
|
|
22
|
-
throw err;
|
|
7
|
+
const orderId = queueEntry.nextId;
|
|
8
|
+
let order;
|
|
9
|
+
if (orderId === apiDoc._id) {
|
|
10
|
+
order = apiDoc;
|
|
23
11
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (status === 404) {
|
|
38
|
-
tinyData = {};
|
|
39
|
-
} else {
|
|
40
|
-
logger.info(`${orderId} search on tiny ends with status ${status}`);
|
|
41
|
-
throw err;
|
|
12
|
+
else {
|
|
13
|
+
try {
|
|
14
|
+
order = (await api.get(`orders/${orderId}`)).data;
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
if (err.statusCode === 404) {
|
|
18
|
+
const msg = `O pedido ${orderId} não existe (:${err.statusCode})`;
|
|
19
|
+
const error = new Error(msg);
|
|
20
|
+
error.isConfigError = true;
|
|
21
|
+
return error;
|
|
22
|
+
}
|
|
23
|
+
throw err;
|
|
24
|
+
}
|
|
42
25
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
26
|
+
if (!order.financial_status) {
|
|
27
|
+
logger.info(`${orderId} skipped with no financial status`);
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
logger.info(`${orderId} searching order ${order.number}`);
|
|
31
|
+
let tinyData;
|
|
32
|
+
try {
|
|
33
|
+
tinyData = await postTiny('/pedidos.pesquisa.php', {
|
|
34
|
+
numeroEcommerce: String(order.number),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
const status = err.response && err.response.status;
|
|
39
|
+
if (status === 404) {
|
|
40
|
+
tinyData = {};
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
logger.info(`${orderId} search on tiny ends with status ${status}`);
|
|
44
|
+
throw err;
|
|
45
|
+
}
|
|
53
46
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
47
|
+
const { pedidos } = tinyData;
|
|
48
|
+
const tinyStatus = parseStatus(order);
|
|
49
|
+
let originalTinyOrder;
|
|
50
|
+
if (Array.isArray(pedidos)) {
|
|
51
|
+
originalTinyOrder = pedidos.find(({ pedido }) => {
|
|
52
|
+
return order.number === Number(pedido.numero_ecommerce);
|
|
53
|
+
});
|
|
54
|
+
if (originalTinyOrder) {
|
|
55
|
+
originalTinyOrder = originalTinyOrder.pedido;
|
|
56
|
+
}
|
|
58
57
|
}
|
|
59
|
-
if (
|
|
58
|
+
if (!originalTinyOrder) {
|
|
59
|
+
if (!canCreateNew) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
if (appData.approved_orders_only
|
|
60
63
|
&& (tinyStatus === 'aberto' || tinyStatus === 'cancelado')) {
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
logger.info(`${orderId} skipped with status "${tinyStatus}"`);
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
const tinyOrder = parseOrder(order, appData);
|
|
68
|
+
logger.info(`${orderId} ${JSON.stringify(tinyOrder)}`);
|
|
69
|
+
return postTiny('/pedido.incluir.php', {
|
|
70
|
+
pedido: {
|
|
71
|
+
pedido: tinyOrder,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
63
74
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const { id, situacao } = originalTinyOrder;
|
|
74
|
-
logger.info(`${orderId} found with tiny status ${situacao} => ${tinyStatus}`);
|
|
75
|
-
if (tinyStatus && tinyStatus !== situacao) {
|
|
76
|
-
return postTiny('/pedido.alterar.situacao', {
|
|
77
|
-
id,
|
|
78
|
-
situacao: tinyStatus,
|
|
79
|
-
});
|
|
75
|
+
if (originalTinyOrder) {
|
|
76
|
+
const { id, situacao } = originalTinyOrder;
|
|
77
|
+
logger.info(`${orderId} found with tiny status ${situacao} => ${tinyStatus}`);
|
|
78
|
+
if (tinyStatus && tinyStatus !== situacao) {
|
|
79
|
+
return postTiny('/pedido.alterar.situacao', {
|
|
80
|
+
id,
|
|
81
|
+
situacao: tinyStatus,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
80
84
|
}
|
|
81
|
-
|
|
82
|
-
return null;
|
|
85
|
+
return null;
|
|
83
86
|
};
|
|
84
|
-
|
|
87
|
+
//# sourceMappingURL=export-order-to-tiny.js.map
|