@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
@@ -0,0 +1,80 @@
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
+
15
+ let notes;
16
+ if (payload) {
17
+ if (!isError) {
18
+ // payload = response
19
+ const { data, status, config } = payload;
20
+ if (data && data._id) {
21
+ logEntry.resource_id = data._id;
22
+ }
23
+ notes = `Status ${status}`;
24
+ if (config) {
25
+ notes += ` [${config.url}]`;
26
+ }
27
+ } else {
28
+ const { config, response } = payload as any;
29
+ if (response) {
30
+ const { data, status } = response;
31
+ notes = `Error: Status ${status} \n${JSON.stringify(data)}`;
32
+ if (!status || status === 429 || status >= 500) {
33
+ return setTimeout(() => {
34
+ throw payload;
35
+ }, 2000);
36
+ }
37
+ if (config) {
38
+ const { url, method, data } = config;
39
+ notes += `\n\n-- Request -- \n${method} ${url} \n${JSON.stringify(data)}`;
40
+ }
41
+ // @ts-ignore
42
+ } else if (payload.isConfigError === true) {
43
+ notes = payload.message;
44
+ } else {
45
+ notes = payload.stack;
46
+ }
47
+ }
48
+ }
49
+ if (notes) {
50
+ logEntry.notes = notes.substring(0, 5000);
51
+ }
52
+
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
+ } else {
70
+ queueList = [];
71
+ }
72
+ const data = {
73
+ [action]: {
74
+ ...appData[action],
75
+ [queue]: queueList,
76
+ },
77
+ };
78
+ logger.info(JSON.stringify(data));
79
+ return updateAppData(application, data);
80
+ };
@@ -0,0 +1,86 @@
1
+ import type { Orders } from '@cloudcommerce/types';
2
+ import logger from 'firebase-functions/lib/logger';
3
+ import api from '@cloudcommerce/api';
4
+ import postTiny from './post-tiny-erp';
5
+ import parseStatus from './parsers/status-to-tiny';
6
+ import parseOrder from './parsers/order-to-tiny';
7
+
8
+ export default async (apiDoc, queueEntry, appData, canCreateNew) => {
9
+ const orderId = queueEntry.nextId;
10
+ let order: Orders;
11
+ if (orderId === apiDoc._id) {
12
+ order = apiDoc;
13
+ } else {
14
+ try {
15
+ order = (await api.get(`orders/${orderId}`)).data;
16
+ } catch (err: any) {
17
+ if (err.statusCode === 404) {
18
+ const msg = `O pedido ${orderId} não existe (:${err.statusCode})`;
19
+ const error: any = 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
+
32
+ let tinyData: { pedidos?: any };
33
+ try {
34
+ tinyData = await postTiny('/pedidos.pesquisa.php', {
35
+ numeroEcommerce: String(order.number),
36
+ });
37
+ } catch (err: any) {
38
+ const status = err.response && err.response.status;
39
+ if (status === 404) {
40
+ tinyData = {};
41
+ } else {
42
+ logger.info(`${orderId} search on tiny ends with status ${status}`);
43
+ throw err;
44
+ }
45
+ }
46
+
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
+ }
57
+ }
58
+ if (!originalTinyOrder) {
59
+ if (!canCreateNew) {
60
+ return null;
61
+ }
62
+ if (
63
+ appData.approved_orders_only
64
+ && (tinyStatus === 'aberto' || tinyStatus === 'cancelado')
65
+ ) {
66
+ logger.info(`${orderId} skipped with status "${tinyStatus}"`);
67
+ return null;
68
+ }
69
+ const tinyOrder = parseOrder(order, appData);
70
+ logger.info(`${orderId} ${JSON.stringify(tinyOrder)}`);
71
+ return postTiny('/pedido.incluir.php', {
72
+ pedido: {
73
+ pedido: tinyOrder,
74
+ },
75
+ });
76
+ }
77
+
78
+ logger.info(`${orderId} found with tiny status ${tinyStatus}`);
79
+ if (tinyStatus) {
80
+ return postTiny('/pedido.alterar.situacao', {
81
+ id: originalTinyOrder.id,
82
+ situacao: tinyStatus,
83
+ });
84
+ }
85
+ return null;
86
+ };
@@ -0,0 +1,60 @@
1
+ import type { Products } from '@cloudcommerce/types';
2
+ import api from '@cloudcommerce/api';
3
+ import postTiny from './post-tiny-erp';
4
+ import parseProduct from './parsers/product-to-tiny';
5
+
6
+ export default async (apiDoc, queueEntry, appData, canCreateNew) => {
7
+ const productId = queueEntry.nextId;
8
+ let product: Products;
9
+ if (productId === apiDoc._id) {
10
+ product = apiDoc;
11
+ } else {
12
+ try {
13
+ product = (await api.get(`products/${productId}`)).data;
14
+ } catch (err: any) {
15
+ if (err.statusCode === 404) {
16
+ const msg = `O produto ${productId} não existe (:${err.statusCode})`;
17
+ const error: any = new Error(msg);
18
+ error.isConfigError = true;
19
+ return error;
20
+ }
21
+ throw err;
22
+ }
23
+ }
24
+
25
+ let tinyData: { produtos?: any };
26
+ try {
27
+ tinyData = await postTiny('/produtos.pesquisa.php', {
28
+ pesquisa: product.sku,
29
+ });
30
+ } catch (err: any) {
31
+ if (err.response && err.response.status === 404) {
32
+ tinyData = {};
33
+ } else {
34
+ throw err;
35
+ }
36
+ }
37
+
38
+ const { produtos } = tinyData;
39
+ let originalTinyProduct;
40
+ if (Array.isArray(produtos)) {
41
+ originalTinyProduct = produtos.find(({ produto }) => {
42
+ return product.sku === String(produto.codigo);
43
+ });
44
+ if (originalTinyProduct) {
45
+ originalTinyProduct = originalTinyProduct.produto;
46
+ } else if (!canCreateNew) {
47
+ return null;
48
+ }
49
+ }
50
+ const tinyProduct = parseProduct(product, originalTinyProduct, appData);
51
+ return tinyProduct
52
+ ? postTiny(originalTinyProduct ? '/produto.alterar.php' : '/produto.incluir.php', {
53
+ produto: {
54
+ produtos: [{
55
+ produto: tinyProduct,
56
+ }],
57
+ },
58
+ })
59
+ : null;
60
+ };
@@ -0,0 +1,6 @@
1
+ export default (d: Date) => {
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
+ };
@@ -0,0 +1,104 @@
1
+ import type { Orders } from '@cloudcommerce/types';
2
+ import { firestore } from 'firebase-admin';
3
+ import logger from 'firebase-functions/lib/logger';
4
+ import api from '@cloudcommerce/api';
5
+ import postTiny from './post-tiny-erp';
6
+ import parseOrder from './parsers/order-from-tiny';
7
+ import parseStatus from './parsers/status-from-tiny';
8
+
9
+ const getLastStatus = (records) => {
10
+ let statusRecord;
11
+ records.forEach((record) => {
12
+ if (
13
+ record
14
+ && (!statusRecord || !record.date_time || record.date_time >= statusRecord.date_time)
15
+ ) {
16
+ statusRecord = record;
17
+ }
18
+ });
19
+ return statusRecord && statusRecord.status;
20
+ };
21
+
22
+ export default async (apiDoc, queueEntry) => {
23
+ const getTinyOrder = async (tinyOrderId) => {
24
+ const { pedido } = await postTiny('/pedido.obter.php', {
25
+ id: Number(tinyOrderId),
26
+ });
27
+ const situacao = typeof pedido.situacao === 'string'
28
+ ? pedido.situacao.toLowerCase()
29
+ : null;
30
+ const orderNumber = pedido.numero_ecommerce;
31
+ logger.info(`Import order n${orderNumber} ${tinyOrderId} => ${situacao}`);
32
+
33
+ const documentRef = firestore().doc(`tinyErpOrders/${tinyOrderId}`);
34
+ const documentSnapshot = await documentRef.get();
35
+ if (
36
+ documentSnapshot.exists
37
+ && documentSnapshot.get('situacao') === situacao
38
+ ) {
39
+ logger.info(`>> Ignoring Tiny order n${orderNumber} ${tinyOrderId} with same status`);
40
+ return null;
41
+ }
42
+
43
+ let listEndpoint = 'orders?limit=1&fields=_id,payments_history,fulfillments,shipping_lines';
44
+ if (orderNumber) {
45
+ listEndpoint += `&number=${orderNumber}`;
46
+ } else {
47
+ listEndpoint += `&hidden_metafields.value=${tinyOrderId}_tiny`;
48
+ }
49
+ const { data: { result } } = await api.get(listEndpoint as 'orders');
50
+ if (!result.length) {
51
+ return null;
52
+ }
53
+ const order = result[0];
54
+ const partialOrder = await parseOrder(pedido, order.shipping_lines) as Orders;
55
+ const promises: Promise<any>[] = [];
56
+ if (partialOrder && Object.keys(partialOrder).length) {
57
+ promises.push(api.patch(`orders/${order._id}`, partialOrder));
58
+ }
59
+ const { fulfillmentStatus, financialStatus } = parseStatus(situacao);
60
+ const data: Record<string, any> = {
61
+ date_time: new Date().toISOString(),
62
+ flags: ['from-tiny'],
63
+ };
64
+ [
65
+ [financialStatus, 'payments_history'],
66
+ [fulfillmentStatus, 'fulfillments'],
67
+ ].forEach(([newStatus, subresource]) => {
68
+ if (
69
+ newStatus
70
+ // @ts-ignore
71
+ && (!order[subresource] || getLastStatus(order[subresource]) !== newStatus)
72
+ ) {
73
+ data.status = newStatus;
74
+ promises.push(api.post(`orders/${order._id}/${subresource}`, data as any));
75
+ logger.info(`${order._id} updated to ${newStatus} from Tiny ${tinyOrderId}`);
76
+ }
77
+ });
78
+
79
+ return Promise.all(promises)
80
+ .then(([firstResult]) => {
81
+ documentRef.set({
82
+ situacao,
83
+ updatedAt: firestore.Timestamp.fromDate(new Date()),
84
+ }).catch(logger.error);
85
+ return (firstResult && firstResult.response) || firstResult;
86
+ });
87
+ };
88
+
89
+ const tinyOrderNumber = queueEntry.nextId;
90
+ if (typeof tinyOrderNumber === 'string' && tinyOrderNumber.startsWith('id:')) {
91
+ return getTinyOrder(tinyOrderNumber.substring(3));
92
+ }
93
+ return postTiny('/pedidos.pesquisa.php', {
94
+ numero: tinyOrderNumber,
95
+ }).then(({ pedidos }) => {
96
+ const tinyOrder = pedidos.find(({ pedido }) => {
97
+ return Number(tinyOrderNumber) === Number(pedido.numero);
98
+ });
99
+ if (tinyOrder) {
100
+ return getTinyOrder(tinyOrder.pedido.id);
101
+ }
102
+ return null;
103
+ });
104
+ };
@@ -0,0 +1,174 @@
1
+ import type { Products } from '@cloudcommerce/types';
2
+ // eslint-disable-next-line import/no-unresolved
3
+ import { getFirestore } from 'firebase-admin/firestore';
4
+ import logger from 'firebase-functions/lib/logger';
5
+ import api from '@cloudcommerce/api';
6
+ import updateAppData from '@cloudcommerce/firebase/lib/helpers/update-app-data';
7
+ import postTiny from './post-tiny-erp';
8
+ import parseProduct from './parsers/product-from-tiny';
9
+
10
+ export default async (apiDoc, queueEntry, appData, canCreateNew, isHiddenQueue) => {
11
+ const [sku, productId] = String(queueEntry.nextId).split(';:');
12
+ let { tinyStockUpdate } = queueEntry;
13
+ if (!tinyStockUpdate) {
14
+ const querySnapshot = await getFirestore().collection('tinyErpStockUpdates')
15
+ .where('ref', '==', `${sku}`).get();
16
+ let lastUpdateTime;
17
+ querySnapshot.forEach((documentSnapshot) => {
18
+ const updateTime = documentSnapshot.updateTime.toDate().getTime();
19
+ if (!lastUpdateTime || updateTime > lastUpdateTime) {
20
+ lastUpdateTime = updateTime;
21
+ tinyStockUpdate = documentSnapshot.data();
22
+ }
23
+ documentSnapshot.ref.delete().catch(logger.error);
24
+ });
25
+ }
26
+
27
+ let product: Products | null = null;
28
+ try {
29
+ product = (await api.get(`products/${(productId || `sku:${sku}`)}`)).data;
30
+ } catch (err: any) {
31
+ if (err.statusCode !== 404) {
32
+ throw err;
33
+ }
34
+ }
35
+ let hasVariations: boolean = false;
36
+ let variationId: string | undefined;
37
+ if (product) {
38
+ const { variations } = product;
39
+ hasVariations = Boolean(variations && variations.length);
40
+ const variation = variations?.find((variation) => sku === variation.sku);
41
+ if (variation) {
42
+ variationId = variation._id;
43
+ } else {
44
+ logger.info(`SKU not found ${sku}`);
45
+ if (!isHiddenQueue && !appData.update_product) {
46
+ const msg = sku
47
+ + ' corresponde a um produto com variações,'
48
+ + ' especifique o SKU da variação para importar.';
49
+ const err: any = new Error(msg);
50
+ err.isConfigError = true;
51
+ return err;
52
+ }
53
+ return null;
54
+ }
55
+ }
56
+
57
+ const handleTinyStock = ({ produto, tipo }, tinyProduct?) => {
58
+ let quantity = Number(produto.saldo) || Number(produto.estoqueAtual);
59
+ if (produto.saldoReservado) {
60
+ quantity -= Number(produto.saldoReservado);
61
+ }
62
+ if (product && (!appData.update_product || variationId)) {
63
+ if (!Number.isNaN(quantity)) {
64
+ if (quantity < 0) {
65
+ quantity = 0;
66
+ }
67
+ let endpoint = `products/${product._id}`;
68
+ if (variationId) {
69
+ endpoint += `variations/${variationId}`;
70
+ }
71
+ endpoint += '/quantity';
72
+ logger.info(endpoint, { quantity });
73
+ // @ts-ignore
74
+ return api.put(endpoint, quantity);
75
+ }
76
+ return null;
77
+ }
78
+ if (!tinyProduct) {
79
+ return null;
80
+ }
81
+
82
+ return postTiny('/produto.obter.php', { id: tinyProduct.id })
83
+ .then(({ produto }) => {
84
+ let method;
85
+ let endpoint;
86
+ let productId = product && product._id;
87
+ if (productId) {
88
+ method = 'PATCH';
89
+ endpoint = `products/${productId}`;
90
+ } else if (tipo === 'produto' || !tipo) {
91
+ method = 'POST';
92
+ endpoint = 'products';
93
+ } else {
94
+ return null;
95
+ }
96
+ // @ts-ignore
97
+ return parseProduct(produto, method === 'POST').then((product: Products) => {
98
+ if (!Number.isNaN(quantity)) {
99
+ product.quantity = quantity >= 0 ? quantity : 0;
100
+ }
101
+ logger.info(`${method} ${endpoint}`);
102
+ const promise = api({
103
+ method,
104
+ endpoint,
105
+ data: product,
106
+ });
107
+
108
+ if (Array.isArray(produto.variacoes) && produto.variacoes.length) {
109
+ promise.then((response) => {
110
+ let skus = appData.__importation && appData.__importation.skus;
111
+ if (!Array.isArray(skus)) {
112
+ skus = [];
113
+ }
114
+ let isQueuedVariations = false;
115
+ produto.variacoes.forEach(({ variacao }) => {
116
+ const { codigo } = variacao;
117
+ let skuAndId = codigo;
118
+ if (!productId) {
119
+ productId = response.data && response.data._id;
120
+ }
121
+ if (productId) {
122
+ skuAndId += `;:${productId}`;
123
+ }
124
+ if (!skus.includes(codigo) && !skus.includes(skuAndId)) {
125
+ isQueuedVariations = true;
126
+ skus.push(skuAndId);
127
+ }
128
+ });
129
+ return isQueuedVariations
130
+ ? updateAppData(queueEntry.app, {
131
+ __importation: {
132
+ ...appData.__importation,
133
+ skus,
134
+ },
135
+ })
136
+ : response;
137
+ });
138
+ }
139
+ return promise;
140
+ });
141
+ });
142
+ };
143
+
144
+ logger.info(JSON.stringify({
145
+ sku,
146
+ productId,
147
+ hasVariations,
148
+ variationId,
149
+ }));
150
+ if (tinyStockUpdate && isHiddenQueue && productId) {
151
+ return handleTinyStock(tinyStockUpdate as any);
152
+ }
153
+ return postTiny('/produtos.pesquisa.php', { pesquisa: sku })
154
+ .then(({ produtos }) => {
155
+ if (Array.isArray(produtos)) {
156
+ let tinyProduct = produtos.find(({ produto }) => sku === String(produto.codigo));
157
+ if (tinyProduct) {
158
+ tinyProduct = tinyProduct.produto;
159
+ if (!hasVariations || variationId) {
160
+ if (tinyStockUpdate) {
161
+ return handleTinyStock(tinyStockUpdate as any, tinyProduct);
162
+ }
163
+ return postTiny('/produto.obter.estoque.php', { id: tinyProduct.id })
164
+ .then((tinyStock) => handleTinyStock(tinyStock, tinyProduct));
165
+ }
166
+ return handleTinyStock({ produto: {} } as any, tinyProduct);
167
+ }
168
+ }
169
+ const msg = `SKU ${sku} não encontrado no Tiny`;
170
+ const err: any = new Error(msg);
171
+ err.isConfigError = true;
172
+ throw new Error(err);
173
+ });
174
+ };
@@ -0,0 +1,49 @@
1
+ import postTiny from '../post-tiny-erp';
2
+
3
+ export default (tinyOrder, shippingLines) => new Promise((resolve, reject) => {
4
+ const partialOrder: Record<string, any> = {};
5
+ if (tinyOrder.obs_interna) {
6
+ partialOrder.staff_notes = tinyOrder.obs_interna;
7
+ }
8
+
9
+ if (shippingLines && shippingLines.length) {
10
+ const shippingLine = shippingLines[0];
11
+ if (
12
+ (tinyOrder.codigo_rastreamento || tinyOrder.url_rastreamento)
13
+ && (!shippingLine.tracking_codes || !shippingLine.tracking_codes.length)
14
+ ) {
15
+ let link;
16
+ if (tinyOrder.url_rastreamento) {
17
+ link = tinyOrder.url_rastreamento;
18
+ }
19
+ const tracking = {
20
+ code: String(tinyOrder.codigo_rastreamento)
21
+ || link.replace(/^https?:\/\/[^/]+/, '').replace(/^[^?]+\?/, '').substring(0, 70),
22
+ link,
23
+ };
24
+ shippingLine.tracking_codes = [tracking];
25
+ partialOrder.shipping_lines = shippingLines;
26
+ }
27
+
28
+ if (tinyOrder.id_nota_fiscal > 0) {
29
+ if (!shippingLine.invoices) {
30
+ shippingLine.invoices = [];
31
+ }
32
+ postTiny('/nota.fiscal.obter.php', { id: tinyOrder.id_nota_fiscal })
33
+ .then((tinyInvoice) => {
34
+ const number = String(tinyInvoice.nota_fiscal.numero);
35
+ if (number && !shippingLine.invoices.find((invoice) => invoice.number === number)) {
36
+ shippingLine.invoices.push({
37
+ number,
38
+ serial_number: String(tinyInvoice.nota_fiscal.serie),
39
+ });
40
+ }
41
+ partialOrder.shipping_lines = shippingLines;
42
+ resolve(partialOrder);
43
+ })
44
+ .catch(reject);
45
+ return;
46
+ }
47
+ }
48
+ resolve(partialOrder);
49
+ });