@cloudcommerce/app-tiny-erp 0.0.109 → 0.0.111

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.
@@ -1,5 +1,25 @@
1
- @cloudcommerce/app-tiny-erp:build: cache hit, replaying output b927f146cfb47469
2
- @cloudcommerce/app-tiny-erp:build: 
3
- @cloudcommerce/app-tiny-erp:build: > @cloudcommerce/app-tiny-erp@0.0.108 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: 
1
+ @cloudcommerce/app-tiny-erp:build: cache hit, replaying output ed8703116778a07f
2
+ @cloudcommerce/app-tiny-erp:build: 
3
+ @cloudcommerce/app-tiny-erp:build: > @cloudcommerce/app-tiny-erp@0.0.110 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:  22:41 warning 'variation' is already declared in the upper scope on line 22 column 11 no-shadow
12
+ @cloudcommerce/app-tiny-erp:build:  63:16 warning 'produto' is already declared in the upper scope on line 38 column 30 no-shadow
13
+ @cloudcommerce/app-tiny-erp:build:  66:13 warning 'productId' is already declared in the upper scope on line 8 column 15 no-shadow
14
+ @cloudcommerce/app-tiny-erp:build:  77:63 warning 'product' is already declared in the upper scope on line 9 column 7 no-shadow
15
+ @cloudcommerce/app-tiny-erp:build: 
16
+ @cloudcommerce/app-tiny-erp:build: /home/leo/code/ecomplus/cloud-commerce/packages/apps/tiny-erp/lib/integration/parsers/product-from-tiny.js
17
+ @cloudcommerce/app-tiny-erp:build:  27:16 warning 'data' is already declared in the upper scope on line 17 column 14 no-shadow
18
+ @cloudcommerce/app-tiny-erp:build: 
19
+ @cloudcommerce/app-tiny-erp:build: /home/leo/code/ecomplus/cloud-commerce/packages/apps/tiny-erp/lib/integration/parsers/product-to-tiny.js
20
+ @cloudcommerce/app-tiny-erp:build:  13:14 warning Do not nest ternary expressions no-nested-ternary
21
+ @cloudcommerce/app-tiny-erp:build:  60:21 warning Do not nest ternary expressions no-nested-ternary
22
+ @cloudcommerce/app-tiny-erp:build:  117:42 warning 'i' is already declared in the upper scope on line 106 column 45 no-shadow
23
+ @cloudcommerce/app-tiny-erp:build: 
24
+ @cloudcommerce/app-tiny-erp:build: ✖ 9 problems (0 errors, 9 warnings)
25
+ @cloudcommerce/app-tiny-erp:build: 
@@ -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
+
7
8
  // Async integration handlers
8
9
  const integrationHandlers = {
9
- exportation: {
10
- product_ids: exportProduct,
11
- order_ids: exportOrder,
12
- },
13
- importation: {
14
- skus: importProduct,
15
- order_numbers: importOrder,
16
- },
10
+ exportation: {
11
+ product_ids: exportProduct,
12
+ order_ids: exportOrder,
13
+ },
14
+ importation: {
15
+ skus: importProduct,
16
+ order_numbers: importOrder,
17
+ },
17
18
  };
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)
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)
24
27
  && appData.ignore_events.includes(evName)) {
25
- logger.info('>> ', key, ' - Ignored event');
26
- return null;
28
+ logger.info('>> ', key, ' - Ignored event');
29
+ return null;
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');
27
38
  }
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');
39
+ }
40
+ if (process.env.TINY_ERP_TOKEN) {
41
+ let integrationConfig;
42
+ let canCreateNew = false;
43
+ if (evName === 'applications-dataSet') {
44
+ integrationConfig = appData;
45
+ canCreateNew = true;
46
+ } else if (evName === 'orders-anyStatusSet') {
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;
36
57
  }
58
+ } else if (!appData.update_price) {
59
+ return null;
60
+ }
61
+ integrationConfig = {
62
+ _exportation: {
63
+ product_ids: [resourceId],
64
+ },
65
+ };
37
66
  }
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
- };
67
+ if (integrationConfig) {
68
+ const actions = Object.keys(integrationHandlers);
69
+ actions.forEach((action) => {
70
+ for (let i = 1; i <= 3; i++) {
71
+ actions.push(`${('_'.repeat(i))}${action}`);
52
72
  }
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'
73
+ });
74
+ for (let i = 0; i < actions.length; i++) {
75
+ const action = actions[i];
76
+ const actionQueues = integrationConfig[action];
77
+ if (typeof actionQueues === 'object' && actionQueues) {
78
+ // eslint-disable-next-line guard-for-in, no-restricted-syntax
79
+ for (const queue in actionQueues) {
80
+ const ids = actionQueues[queue];
81
+ if (Array.isArray(ids) && ids.length) {
82
+ const isHiddenQueue = action.charAt(0) === '_';
83
+ const handlerName = action.replace(/^_+/, '');
84
+ const handler = integrationHandlers[handlerName][queue.toLowerCase()];
85
+ const nextId = ids[0];
86
+ if (typeof nextId === 'string'
88
87
  && nextId.length
89
88
  && handler) {
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
- }
89
+ const debugFlag = `#${action}/${queue}/${nextId}`;
90
+ logger.info(`> Starting ${debugFlag}`);
91
+ const queueEntry = {
92
+ action,
93
+ queue,
94
+ nextId,
95
+ key,
96
+ app,
97
+ };
98
+ return handler(apiDoc, queueEntry, appData, canCreateNew, isHiddenQueue).then((payload) => {
99
+ return afterQueue(queueEntry, appData, app, payload);
100
+ }).catch((err) => {
101
+ return afterQueue(queueEntry, appData, app, err);
102
+ });
103
+ }
108
104
  }
105
+ }
109
106
  }
107
+ }
110
108
  }
111
- // Nothing to do
112
- return null;
109
+ }
110
+ // Nothing to do
111
+ return null;
113
112
  };
113
+
114
114
  export default handleApiEvent;
115
- //# sourceMappingURL=event-to-tiny.js.map
115
+ // # sourceMappingURL=event-to-tiny.js.map
package/lib/index.js CHANGED
@@ -1,2 +1,2 @@
1
1
  export * from './tiny-erp.js';
2
- //# sourceMappingURL=index.js.map
2
+ // # sourceMappingURL=index.js.map
@@ -1,82 +1,79 @@
1
1
  import logger from 'firebase-functions/lib/logger';
2
2
  import updateAppData from '@cloudcommerce/firebase/lib/helpers/update-app-data';
3
+
3
4
  export default async (queueEntry, appData, application, payload) => {
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
- }
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);
25
35
  }
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
- }
36
+ if (config) {
37
+ const { url, method, data } = config;
38
+ notes += `\n\n-- Request -- \n${method} ${url} \n${JSON.stringify(data)}`;
48
39
  }
40
+ // @ts-ignore
41
+ } else if (payload.isConfigError === true) {
42
+ notes = payload.message;
43
+ } else {
44
+ notes = payload.stack;
45
+ }
49
46
  }
50
- if (notes) {
51
- logEntry.notes = notes.substring(0, 5000);
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);
52
66
  }
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);
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);
81
78
  };
82
- //# sourceMappingURL=after-tiny-queue.js.map
79
+ // # sourceMappingURL=after-tiny-queue.js.map
@@ -3,85 +3,82 @@ 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
+
6
7
  export default async (apiDoc, queueEntry, appData, canCreateNew) => {
7
- const orderId = queueEntry.nextId;
8
- let order;
9
- if (orderId === apiDoc._id) {
10
- order = apiDoc;
11
- }
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
- }
25
- }
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;
8
+ const orderId = queueEntry.nextId;
9
+ let order;
10
+ if (orderId === apiDoc._id) {
11
+ order = apiDoc;
12
+ } else {
32
13
  try {
33
- tinyData = await postTiny('/pedidos.pesquisa.php', {
34
- numeroEcommerce: String(order.number),
35
- });
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;
36
42
  }
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
- }
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;
46
53
  }
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
- }
54
+ }
55
+ if (!originalTinyOrder) {
56
+ if (!canCreateNew) {
57
+ return null;
57
58
  }
58
- if (!originalTinyOrder) {
59
- if (!canCreateNew) {
60
- return null;
61
- }
62
- if (appData.approved_orders_only
59
+ if (appData.approved_orders_only
63
60
  && (tinyStatus === 'aberto' || tinyStatus === 'cancelado')) {
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
- });
61
+ logger.info(`${orderId} skipped with status "${tinyStatus}"`);
62
+ return null;
74
63
  }
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
- }
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
+ if (originalTinyOrder) {
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
+ });
84
80
  }
85
- return null;
81
+ }
82
+ return null;
86
83
  };
87
- //# sourceMappingURL=export-order-to-tiny.js.map
84
+ // # sourceMappingURL=export-order-to-tiny.js.map