@cloudcommerce/app-tiny-erp 0.0.59 → 0.0.60

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.
Files changed (54) hide show
  1. package/.turbo/turbo-build.log +32 -6
  2. package/lib/event-to-tiny.js +111 -0
  3. package/lib/event-to-tiny.js.map +1 -0
  4. package/lib/index.js +2 -0
  5. package/lib/index.js.map +1 -0
  6. package/lib/integration/after-tiny-queue.js +79 -0
  7. package/lib/integration/after-tiny-queue.js.map +1 -0
  8. package/lib/integration/export-order-to-tiny.js +81 -0
  9. package/lib/integration/export-order-to-tiny.js.map +1 -0
  10. package/lib/integration/export-product-to-tiny.js +58 -0
  11. package/lib/integration/export-product-to-tiny.js.map +1 -0
  12. package/lib/integration/helpers/format-tiny-date.js +7 -0
  13. package/lib/integration/helpers/format-tiny-date.js.map +1 -0
  14. package/lib/integration/import-order-from-tiny.js +94 -0
  15. package/lib/integration/import-order-from-tiny.js.map +1 -0
  16. package/lib/integration/import-product-from-tiny.js +169 -0
  17. package/lib/integration/import-product-from-tiny.js.map +1 -0
  18. package/lib/integration/parsers/order-from-tiny.js +46 -0
  19. package/lib/integration/parsers/order-from-tiny.js.map +1 -0
  20. package/lib/integration/parsers/order-to-tiny.js +193 -0
  21. package/lib/integration/parsers/order-to-tiny.js.map +1 -0
  22. package/lib/integration/parsers/product-from-tiny.js +199 -0
  23. package/lib/integration/parsers/product-from-tiny.js.map +1 -0
  24. package/lib/integration/parsers/product-to-tiny.js +129 -0
  25. package/lib/integration/parsers/product-to-tiny.js.map +1 -0
  26. package/lib/integration/parsers/status-from-tiny.js +34 -0
  27. package/lib/integration/parsers/status-from-tiny.js.map +1 -0
  28. package/lib/integration/parsers/status-to-tiny.js +39 -0
  29. package/lib/integration/parsers/status-to-tiny.js.map +1 -0
  30. package/lib/integration/post-tiny-erp.js +47 -0
  31. package/lib/integration/post-tiny-erp.js.map +1 -0
  32. package/lib/tiny-erp.js +17 -0
  33. package/lib/tiny-erp.js.map +1 -0
  34. package/lib/tiny-webhook.js +135 -0
  35. package/lib/tiny-webhook.js.map +1 -0
  36. package/package.json +13 -6
  37. package/src/event-to-tiny.ts +129 -0
  38. package/src/index.ts +1 -0
  39. package/src/integration/after-tiny-queue.ts +80 -0
  40. package/src/integration/export-order-to-tiny.ts +86 -0
  41. package/src/integration/export-product-to-tiny.ts +60 -0
  42. package/src/integration/helpers/format-tiny-date.ts +6 -0
  43. package/src/integration/import-order-from-tiny.ts +104 -0
  44. package/src/integration/import-product-from-tiny.ts +174 -0
  45. package/src/integration/parsers/order-from-tiny.ts +49 -0
  46. package/src/integration/parsers/order-to-tiny.ts +205 -0
  47. package/src/integration/parsers/product-from-tiny.ts +215 -0
  48. package/src/integration/parsers/product-to-tiny.ts +138 -0
  49. package/src/integration/parsers/status-from-tiny.ts +35 -0
  50. package/src/integration/parsers/status-to-tiny.ts +42 -0
  51. package/src/integration/post-tiny-erp.ts +52 -0
  52. package/src/tiny-erp.ts +23 -0
  53. package/src/tiny-webhook.ts +143 -0
  54. package/src/firebase.ts +0 -0
@@ -1,6 +1,32 @@
1
- @cloudcommerce/app-tiny-erp:build: cache hit, replaying output 34636894520b5793
2
- @cloudcommerce/app-tiny-erp:build: 
3
- @cloudcommerce/app-tiny-erp:build: > @cloudcommerce/app-tiny-erp@0.0.0 build /home/leo/code/ecomplus/cloud-commerce/packages/apps/tiny-erp
4
- @cloudcommerce/app-tiny-erp:build: > echo '@ecomplus/tiny-erp'
5
- @cloudcommerce/app-tiny-erp:build: 
6
- @cloudcommerce/app-tiny-erp:build: @ecomplus/tiny-erp
1
+ @cloudcommerce/app-tiny-erp:build: cache hit, replaying output f168c19322174ba6
2
+ @cloudcommerce/app-tiny-erp:build: 
3
+ @cloudcommerce/app-tiny-erp:build: > @cloudcommerce/app-tiny-erp@0.0.59 build /home/leo/code/ecomplus/cloud-commerce/packages/apps/tiny-erp
4
+ @cloudcommerce/app-tiny-erp:build: > sh ../../../scripts/build-lib.sh
5
+ @cloudcommerce/app-tiny-erp:build: 
6
+ @cloudcommerce/app-tiny-erp:build: 
7
+ @cloudcommerce/app-tiny-erp:build: /home/leo/code/ecomplus/cloud-commerce/packages/apps/tiny-erp/lib/integration/after-tiny-queue.js
8
+ @cloudcommerce/app-tiny-erp:build:  37:32 warning 'data' is already declared in the upper scope on line 29 column 17 no-shadow
9
+ @cloudcommerce/app-tiny-erp:build: 
10
+ @cloudcommerce/app-tiny-erp:build: /home/leo/code/ecomplus/cloud-commerce/packages/apps/tiny-erp/lib/integration/import-product-from-tiny.js
11
+ @cloudcommerce/app-tiny-erp:build:  38:41 warning 'variation' is already declared in the upper scope on line 38 column 11 no-shadow
12
+ @cloudcommerce/app-tiny-erp:build:  79:16 warning 'produto' is already declared in the upper scope on line 54 column 30 no-shadow
13
+ @cloudcommerce/app-tiny-erp:build:  82:13 warning 'productId' is already declared in the upper scope on line 10 column 15 no-shadow
14
+ @cloudcommerce/app-tiny-erp:build:  93:63 warning 'product' is already declared in the upper scope on line 25 column 7 no-shadow
15
+ @cloudcommerce/app-tiny-erp:build:  95:13 warning Assignment to property of function parameter 'product' no-param-reassign
16
+ @cloudcommerce/app-tiny-erp:build: 
17
+ @cloudcommerce/app-tiny-erp:build: /home/leo/code/ecomplus/cloud-commerce/packages/apps/tiny-erp/lib/integration/parsers/order-to-tiny.js
18
+ @cloudcommerce/app-tiny-erp:build:  30:9 warning Assignment to property of function parameter 'tinyObject' no-param-reassign
19
+ @cloudcommerce/app-tiny-erp:build: 
20
+ @cloudcommerce/app-tiny-erp:build: /home/leo/code/ecomplus/cloud-commerce/packages/apps/tiny-erp/lib/integration/parsers/product-from-tiny.js
21
+ @cloudcommerce/app-tiny-erp:build:  27:16 warning 'data' is already declared in the upper scope on line 17 column 14 no-shadow
22
+ @cloudcommerce/app-tiny-erp:build:  32:22 warning Assignment to property of function parameter 'data' no-param-reassign
23
+ @cloudcommerce/app-tiny-erp:build:  36:22 warning Assignment to property of function parameter 'data' no-param-reassign
24
+ @cloudcommerce/app-tiny-erp:build:  38:13 warning Assignment to property of function parameter 'data' no-param-reassign
25
+ @cloudcommerce/app-tiny-erp:build: 
26
+ @cloudcommerce/app-tiny-erp:build: /home/leo/code/ecomplus/cloud-commerce/packages/apps/tiny-erp/lib/integration/parsers/product-to-tiny.js
27
+ @cloudcommerce/app-tiny-erp:build:  13:14 warning Do not nest ternary expressions no-nested-ternary
28
+ @cloudcommerce/app-tiny-erp:build:  60:21 warning Do not nest ternary expressions no-nested-ternary
29
+ @cloudcommerce/app-tiny-erp:build:  117:42 warning 'i' is already declared in the upper scope on line 106 column 45 no-shadow
30
+ @cloudcommerce/app-tiny-erp:build: 
31
+ @cloudcommerce/app-tiny-erp:build: ✖ 14 problems (0 errors, 14 warnings)
32
+ @cloudcommerce/app-tiny-erp:build: 
@@ -0,0 +1,111 @@
1
+ import logger from 'firebase-functions/lib/logger';
2
+ import exportProduct from './integration/export-product-to-tiny.js';
3
+ import exportOrder from './integration/export-order-to-tiny.js';
4
+ import importProduct from './integration/import-product-from-tiny.js';
5
+ import importOrder from './integration/import-order-from-tiny.js';
6
+ import afterQueue from './integration/after-tiny-queue.js';
7
+
8
+ // Async integration handlers
9
+ const integrationHandlers = {
10
+ exportation: {
11
+ product_ids: exportProduct,
12
+ order_ids: exportOrder,
13
+ },
14
+ importation: {
15
+ skus: importProduct,
16
+ order_numbers: importOrder,
17
+ },
18
+ };
19
+ const handleApiEvent = async ({
20
+ evName, apiEvent, apiDoc, app,
21
+ }) => {
22
+ const resourceId = apiEvent.resource_id;
23
+ logger.info('>> ', resourceId, ' - Action: ', apiEvent.action);
24
+ const key = `${evName}_${resourceId}`;
25
+ const appData = { ...app.data, ...app.hidden_data };
26
+ if (Array.isArray(appData.ignore_events)
27
+ && appData.ignore_events.includes(evName)) {
28
+ logger.info('>> ', key, ' - Ignored event');
29
+ return null;
30
+ }
31
+ logger.info(`> Webhook ${resourceId} [${evName}]`);
32
+ const tinyToken = appData.tiny_api_token;
33
+ if (typeof tinyToken === 'string' && tinyToken) {
34
+ process.env.TINY_ERP_TOKEN = tinyToken;
35
+ let integrationConfig;
36
+ let canCreateNew = false;
37
+ if (evName === 'applications-dataSet') {
38
+ integrationConfig = appData;
39
+ canCreateNew = true;
40
+ } else if (evName === 'orders-anyStatusSet') {
41
+ canCreateNew = Boolean(appData.new_orders);
42
+ integrationConfig = {
43
+ _exportation: {
44
+ order_ids: [resourceId],
45
+ },
46
+ };
47
+ } else {
48
+ if (evName === 'products-new') {
49
+ if (!appData.new_products) {
50
+ return null;
51
+ }
52
+ } else if (!appData.update_price) {
53
+ return null;
54
+ }
55
+ integrationConfig = {
56
+ _exportation: {
57
+ product_ids: [resourceId],
58
+ },
59
+ };
60
+ }
61
+ if (integrationConfig) {
62
+ const actions = Object.keys(integrationHandlers);
63
+ actions.forEach((action) => {
64
+ for (let i = 1; i <= 3; i++) {
65
+ actions.push(`${('_'.repeat(i))}${action}`);
66
+ }
67
+ });
68
+ for (let i = 0; i < actions.length; i++) {
69
+ const action = actions[i];
70
+ const actionQueues = integrationConfig[action];
71
+ if (typeof actionQueues === 'object' && actionQueues) {
72
+ // eslint-disable-next-line guard-for-in, no-restricted-syntax
73
+ for (const queue in actionQueues) {
74
+ const ids = actionQueues[queue];
75
+ if (Array.isArray(ids) && ids.length) {
76
+ const isHiddenQueue = action.charAt(0) === '_';
77
+ const mustUpdateAppQueue = evName === 'applications-dataSet';
78
+ const handlerName = action.replace(/^_+/, '');
79
+ const handler = integrationHandlers[handlerName][queue.toLowerCase()];
80
+ const nextId = ids[0];
81
+ if (typeof nextId === 'string'
82
+ && nextId.length
83
+ && handler) {
84
+ const debugFlag = `#${action}/${queue}/${nextId}`;
85
+ logger.info(`> Starting ${debugFlag}`);
86
+ const queueEntry = {
87
+ action,
88
+ queue,
89
+ nextId,
90
+ key,
91
+ mustUpdateAppQueue,
92
+ app,
93
+ };
94
+ return handler(apiDoc, queueEntry, appData, canCreateNew, isHiddenQueue).then((payload) => {
95
+ return afterQueue(queueEntry, appData, app, payload);
96
+ }).catch((err) => {
97
+ return afterQueue(queueEntry, appData, app, err);
98
+ });
99
+ }
100
+ }
101
+ }
102
+ }
103
+ }
104
+ }
105
+ }
106
+ // Nothing to do
107
+ return null;
108
+ };
109
+
110
+ export default handleApiEvent;
111
+ // # sourceMappingURL=event-to-tiny.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-to-tiny.js","sourceRoot":"","sources":["../src/event-to-tiny.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,+BAA+B,CAAC;AACnD,OAAO,aAAa,MAAM,sCAAsC,CAAC;AACjE,OAAO,WAAW,MAAM,oCAAoC,CAAC;AAC7D,OAAO,aAAa,MAAM,wCAAwC,CAAC;AACnE,OAAO,WAAW,MAAM,sCAAsC,CAAC;AAC/D,OAAO,UAAU,MAAM,gCAAgC,CAAC;AAExD,6BAA6B;AAC7B,MAAM,mBAAmB,GAAG;IAC1B,WAAW,EAAE;QACX,WAAW,EAAE,aAAa;QAC1B,SAAS,EAAE,WAAW;KACvB;IACD,WAAW,EAAE;QACX,IAAI,EAAE,aAAa;QACnB,aAAa,EAAE,WAAW;KAC3B;CACF,CAAC;AAEF,MAAM,cAAc,GAAoB,KAAK,EAAE,EAC7C,MAAM,EACN,QAAQ,EACR,MAAM,EACN,GAAG,GACJ,EAAE,EAAE;IACH,MAAM,UAAU,GAAG,QAAQ,CAAC,WAAW,CAAC;IACxC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC/D,MAAM,GAAG,GAAG,GAAG,MAAM,IAAI,UAAU,EAAE,CAAC;IACtC,MAAM,OAAO,GAAG,EAAE,GAAG,GAAG,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC;IACpD,IACE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC;WACjC,OAAO,CAAC,aAAa,C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vC,MAAM,UAAU,GAAG;oCACjB,MAAM;oCACN,KAAK;oCACL,MAAM;oCACN,GAAG;oCACH,kBAAkB;oCAClB,GAAG;iCACJ,CAAC;gCACF,OAAO,OAAO,CACZ,MAAM,EACN,UAAU,EACV,OAAO,EACP,YAAY,EACZ,aAAa,CACd,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;oCACjB,OAAO,UAAU,CAAC,UAAU,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;gCACvD,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;oCACf,OAAO,UAAU,CAAC,UAAU,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;gCACnD,CAAC,CAAC,CAAC;6BACJ;yBACF;qBACF;iBACF;aACF;SACF;KACF;IACD,gBAAgB;IAChB,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF,eAAe,cAAc,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './tiny-erp';
2
+ // # sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
@@ -0,0 +1,79 @@
1
+ import logger from 'firebase-functions/lib/logger';
2
+ import updateAppData from '@cloudcommerce/firebase/lib/helpers/update-app-data';
3
+
4
+ export default async (queueEntry, appData, application, payload) => {
5
+ const isError = payload instanceof Error;
6
+ const isImportation = queueEntry.action.endsWith('importation');
7
+ const logs = appData.logs || [];
8
+ const logEntry = {
9
+ resource: /order/i.test(queueEntry.queue) ? 'orders' : 'products',
10
+ [(isImportation ? 'tiny_id' : 'resource_id')]: queueEntry.nextId,
11
+ success: !isError,
12
+ imestamp: new Date().toISOString(),
13
+ };
14
+ let notes;
15
+ if (payload) {
16
+ if (!isError) {
17
+ // payload = response
18
+ const { data, status, config } = payload;
19
+ if (data && data._id) {
20
+ logEntry.resource_id = data._id;
21
+ }
22
+ notes = `Status ${status}`;
23
+ if (config) {
24
+ notes += ` [${config.url}]`;
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);
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
+ } else if (payload.isConfigError === true) {
42
+ notes = payload.message;
43
+ } else {
44
+ notes = payload.stack;
45
+ }
46
+ }
47
+ }
48
+ if (notes) {
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);
66
+ }
67
+ } else {
68
+ queueList = [];
69
+ }
70
+ const data = {
71
+ [action]: {
72
+ ...appData[action],
73
+ [queue]: queueList,
74
+ },
75
+ };
76
+ logger.info(JSON.stringify(data));
77
+ return updateAppData(application, data);
78
+ };
79
+ // # sourceMappingURL=after-tiny-queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"after-tiny-queue.js","sourceRoot":"","sources":["../../src/integration/after-tiny-queue.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,+BAA+B,CAAC;AACnD,OAAO,aAAa,MAAM,qDAAqD,CAAC;AAEhF,eAAe,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,WAAW,EAAE,OAAO,EA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AE;YAChB,SAAS,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;SAC9B;KACF;SAAM;QACL,SAAS,GAAG,EAAE,CAAC;KAChB;IACD,MAAM,IAAI,GAAG;QACX,CAAC,MAAM,CAAC,EAAE;YACR,GAAG,OAAO,CAAC,MAAM,CAAC;YAClB,CAAC,KAAK,CAAC,EAAE,SAAS;SACnB;KACF,CAAC;IACF,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;IAClC,OAAO,aAAa,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;AAC1C,CAAC,CAAC"}
@@ -0,0 +1,81 @@
1
+ import logger from 'firebase-functions/lib/logger';
2
+ import api from '@cloudcommerce/api';
3
+ import postTiny from './post-tiny-erp.js';
4
+ import parseStatus from './parsers/status-to-tiny.js';
5
+ import parseOrder from './parsers/order-to-tiny.js';
6
+
7
+ export default async (apiDoc, queueEntry, appData, canCreateNew) => {
8
+ const orderId = queueEntry.nextId;
9
+ let order;
10
+ if (orderId === apiDoc._id) {
11
+ order = apiDoc;
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;
23
+ }
24
+ }
25
+ if (!order.financial_status) {
26
+ logger.info(`${orderId} skipped with no financial status`);
27
+ return null;
28
+ }
29
+ logger.info(`${orderId} searching order ${order.number}`);
30
+ let tinyData;
31
+ try {
32
+ tinyData = await postTiny('/pedidos.pesquisa.php', {
33
+ numeroEcommerce: String(order.number),
34
+ });
35
+ } catch (err) {
36
+ const status = err.response && err.response.status;
37
+ if (status === 404) {
38
+ tinyData = {};
39
+ } else {
40
+ logger.info(`${orderId} search on tiny ends with status ${status}`);
41
+ throw err;
42
+ }
43
+ }
44
+ const { pedidos } = tinyData;
45
+ const tinyStatus = parseStatus(order);
46
+ let originalTinyOrder;
47
+ if (Array.isArray(pedidos)) {
48
+ originalTinyOrder = pedidos.find(({ pedido }) => {
49
+ return order.number === Number(pedido.numero_ecommerce);
50
+ });
51
+ if (originalTinyOrder) {
52
+ originalTinyOrder = originalTinyOrder.pedido;
53
+ }
54
+ }
55
+ if (!originalTinyOrder) {
56
+ if (!canCreateNew) {
57
+ return null;
58
+ }
59
+ if (appData.approved_orders_only
60
+ && (tinyStatus === 'aberto' || tinyStatus === 'cancelado')) {
61
+ logger.info(`${orderId} skipped with status "${tinyStatus}"`);
62
+ return null;
63
+ }
64
+ const tinyOrder = parseOrder(order, appData);
65
+ logger.info(`${orderId} ${JSON.stringify(tinyOrder)}`);
66
+ return postTiny('/pedido.incluir.php', {
67
+ pedido: {
68
+ pedido: tinyOrder,
69
+ },
70
+ });
71
+ }
72
+ logger.info(`${orderId} found with tiny status ${tinyStatus}`);
73
+ if (tinyStatus) {
74
+ return postTiny('/pedido.alterar.situacao', {
75
+ id: originalTinyOrder.id,
76
+ situacao: tinyStatus,
77
+ });
78
+ }
79
+ return null;
80
+ };
81
+ // # sourceMappingURL=export-order-to-tiny.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"export-order-to-tiny.js","sourceRoot":"","sources":["../../src/integration/export-order-to-tiny.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,+BAA+B,CAAC;AACnD,OAAO,GAAG,MAAM,oBAAoB,CAAC;AACrC,OAAO,QAAQ,MAAM,iBAAiB,CAAC;AACvC,OAAO,WAAW,MAAM,0BAA0B,CAAC;AACnD,OAAO,UAAU,MAAM,yBAAyB,CAAC;AAEjD,eAAe,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,YAAY,EAAE,EAAE;IACjE,MAAM,OAAO,GAAG,UAAU,CAAC,MAAM,CAAC;IAClC,IAAI,KAAa,CAAC;IAClB,IAAI,OAAO,KAAK,MAAM,C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AACd,CAAC,CAAC"}
@@ -0,0 +1,58 @@
1
+ import api from '@cloudcommerce/api';
2
+ import postTiny from './post-tiny-erp.js';
3
+ import parseProduct from './parsers/product-to-tiny.js';
4
+
5
+ export default async (apiDoc, queueEntry, appData, canCreateNew) => {
6
+ const productId = queueEntry.nextId;
7
+ let product;
8
+ if (productId === apiDoc._id) {
9
+ product = apiDoc;
10
+ } else {
11
+ try {
12
+ product = (await api.get(`products/${productId}`)).data;
13
+ } catch (err) {
14
+ if (err.statusCode === 404) {
15
+ const msg = `O produto ${productId} não existe (:${err.statusCode})`;
16
+ const error = new Error(msg);
17
+ error.isConfigError = true;
18
+ return error;
19
+ }
20
+ throw err;
21
+ }
22
+ }
23
+ let tinyData;
24
+ try {
25
+ tinyData = await postTiny('/produtos.pesquisa.php', {
26
+ pesquisa: product.sku,
27
+ });
28
+ } catch (err) {
29
+ if (err.response && err.response.status === 404) {
30
+ tinyData = {};
31
+ } else {
32
+ throw err;
33
+ }
34
+ }
35
+ const { produtos } = tinyData;
36
+ let originalTinyProduct;
37
+ if (Array.isArray(produtos)) {
38
+ originalTinyProduct = produtos.find(({ produto }) => {
39
+ return product.sku === String(produto.codigo);
40
+ });
41
+ if (originalTinyProduct) {
42
+ originalTinyProduct = originalTinyProduct.produto;
43
+ } else if (!canCreateNew) {
44
+ return null;
45
+ }
46
+ }
47
+ const tinyProduct = parseProduct(product, originalTinyProduct, appData);
48
+ return tinyProduct
49
+ ? postTiny(originalTinyProduct ? '/produto.alterar.php' : '/produto.incluir.php', {
50
+ produto: {
51
+ produtos: [{
52
+ produto: tinyProduct,
53
+ }],
54
+ },
55
+ })
56
+ : null;
57
+ };
58
+ // # sourceMappingURL=export-product-to-tiny.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"export-product-to-tiny.js","sourceRoot":"","sources":["../../src/integration/export-product-to-tiny.ts"],"names":[],"mappings":"AACA,OAAO,GAAG,MAAM,oBAAoB,CAAC;AACrC,OAAO,QAAQ,MAAM,iB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AC,IAAI,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YAClD,OAAO,OAAO,CAAC,GAAG,KAAK,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAChD,CAAC,CAAC,CAAC;QACH,IAAI,mBAAmB,EAAE;YACvB,mBAAmB,GAAG,mBAAmB,CAAC,OAAO,CAAC;SACnD;aAAM,IAAI,CAAC,YAAY,EAAE;YACxB,OAAO,IAAI,CAAC;SACb;KACF;IACD,MAAM,WAAW,GAAG,YAAY,CAAC,OAAO,EAAE,mBAAmB,EAAE,OAAO,CAAC,CAAC;IACxE,OAAO,WAAW;QAChB,CAAC,CAAC,QAAQ,CAAC,mBAAmB,CAAC,CAAC,CAAC,sBAAsB,CAAC,CAAC,CAAC,sBAAsB,EAAE;YAChF,OAAO,EAAE;gBACP,QAAQ,EAAE,CAAC;wBACT,OAAO,EAAE,WAAW;qBACrB,CAAC;aACH;SACF,CAAC;QACF,CAAC,CAAC,IAAI,CAAC;AACX,CAAC,CAAC"}
@@ -0,0 +1,7 @@
1
+ export default (d) => {
2
+ /* eslint-disable prefer-template */
3
+ return d.getDate().toString().padStart(2, '0') + '/'
4
+ + (d.getMonth() + 1).toString().padStart(2, '0') + '/'
5
+ + d.getFullYear();
6
+ };
7
+ // # sourceMappingURL=format-tiny-date.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format-tiny-date.js","sourceRoot":"","sources":["../../../src/integration/helpers/format-tiny-date.ts"],"names":[],"mappings":"AAAA,eAAe,CAAC,CAAO,EAAE,EAAE;IACzB,oCAAoC;IACpC,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,GAAG;UAChD,CAAC,CAAC,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,GAAG;UACpD,CAAC,CAAC,WAAW,EAAE,CAAC;AACtB,CAAC,CAAC"}
@@ -0,0 +1,94 @@
1
+ import { firestore } from 'firebase-admin';
2
+ import logger from 'firebase-functions/lib/logger';
3
+ import api from '@cloudcommerce/api';
4
+ import postTiny from './post-tiny-erp.js';
5
+ import parseOrder from './parsers/order-from-tiny.js';
6
+ import parseStatus from './parsers/status-from-tiny.js';
7
+
8
+ const getLastStatus = (records) => {
9
+ let statusRecord;
10
+ records.forEach((record) => {
11
+ if (record
12
+ && (!statusRecord || !record.date_time || record.date_time >= statusRecord.date_time)) {
13
+ statusRecord = record;
14
+ }
15
+ });
16
+ return statusRecord && statusRecord.status;
17
+ };
18
+
19
+ export default async (apiDoc, queueEntry) => {
20
+ const getTinyOrder = async (tinyOrderId) => {
21
+ const { pedido } = await postTiny('/pedido.obter.php', {
22
+ id: Number(tinyOrderId),
23
+ });
24
+ const situacao = typeof pedido.situacao === 'string'
25
+ ? pedido.situacao.toLowerCase()
26
+ : null;
27
+ const orderNumber = pedido.numero_ecommerce;
28
+ logger.info(`Import order n${orderNumber} ${tinyOrderId} => ${situacao}`);
29
+ const documentRef = firestore().doc(`tinyErpOrders/${tinyOrderId}`);
30
+ const documentSnapshot = await documentRef.get();
31
+ if (documentSnapshot.exists
32
+ && documentSnapshot.get('situacao') === situacao) {
33
+ logger.info(`>> Ignoring Tiny order n${orderNumber} ${tinyOrderId} with same status`);
34
+ return null;
35
+ }
36
+ let listEndpoint = 'orders?limit=1&fields=_id,payments_history,fulfillments,shipping_lines';
37
+ if (orderNumber) {
38
+ listEndpoint += `&number=${orderNumber}`;
39
+ } else {
40
+ listEndpoint += `&hidden_metafields.value=${tinyOrderId}_tiny`;
41
+ }
42
+ const { data: { result } } = await api.get(listEndpoint);
43
+ if (!result.length) {
44
+ return null;
45
+ }
46
+ const order = result[0];
47
+ const partialOrder = await parseOrder(pedido, order.shipping_lines);
48
+ const promises = [];
49
+ if (partialOrder && Object.keys(partialOrder).length) {
50
+ promises.push(api.patch(`orders/${order._id}`, partialOrder));
51
+ }
52
+ const { fulfillmentStatus, financialStatus } = parseStatus(situacao);
53
+ const data = {
54
+ date_time: new Date().toISOString(),
55
+ flags: ['from-tiny'],
56
+ };
57
+ [
58
+ [financialStatus, 'payments_history'],
59
+ [fulfillmentStatus, 'fulfillments'],
60
+ ].forEach(([newStatus, subresource]) => {
61
+ if (newStatus
62
+ // @ts-ignore
63
+ && (!order[subresource] || getLastStatus(order[subresource]) !== newStatus)) {
64
+ data.status = newStatus;
65
+ promises.push(api.post(`orders/${order._id}/${subresource}`, data));
66
+ logger.info(`${order._id} updated to ${newStatus} from Tiny ${tinyOrderId}`);
67
+ }
68
+ });
69
+ return Promise.all(promises)
70
+ .then(([firstResult]) => {
71
+ documentRef.set({
72
+ situacao,
73
+ updatedAt: firestore.Timestamp.fromDate(new Date()),
74
+ }).catch(logger.error);
75
+ return (firstResult && firstResult.response) || firstResult;
76
+ });
77
+ };
78
+ const tinyOrderNumber = queueEntry.nextId;
79
+ if (typeof tinyOrderNumber === 'string' && tinyOrderNumber.startsWith('id:')) {
80
+ return getTinyOrder(tinyOrderNumber.substring(3));
81
+ }
82
+ return postTiny('/pedidos.pesquisa.php', {
83
+ numero: tinyOrderNumber,
84
+ }).then(({ pedidos }) => {
85
+ const tinyOrder = pedidos.find(({ pedido }) => {
86
+ return Number(tinyOrderNumber) === Number(pedido.numero);
87
+ });
88
+ if (tinyOrder) {
89
+ return getTinyOrder(tinyOrder.pedido.id);
90
+ }
91
+ return null;
92
+ });
93
+ };
94
+ // # sourceMappingURL=import-order-from-tiny.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"import-order-from-tiny.js","sourceRoot":"","sources":["../../src/integration/import-order-from-tiny.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,MAAM,MAAM,+BAA+B,CAAC;AACnD,OAAO,GAAG,MAAM,oBAAoB,CAAC;AACrC,OAAO,QAAQ,MAAM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