@cloudcommerce/app-tiny-erp 2.8.8 → 2.10.0
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/lib/event-to-tiny.js.map +1 -1
- package/lib/integration/after-tiny-queue.js.map +1 -1
- package/lib/integration/export-order-to-tiny.js.map +1 -1
- package/lib/integration/export-product-to-tiny.js.map +1 -1
- package/lib/integration/import-order-from-tiny.js.map +1 -1
- package/lib/integration/import-product-from-tiny.js.map +1 -1
- package/lib/integration/parsers/order-from-tiny.js.map +1 -1
- package/lib/integration/parsers/order-to-tiny.js.map +1 -1
- package/lib/integration/parsers/product-from-tiny.js.map +1 -1
- package/lib/integration/parsers/product-to-tiny.js.map +1 -1
- package/lib/integration/parsers/status-from-tiny.js.map +1 -1
- package/lib/integration/parsers/status-to-tiny.js.map +1 -1
- package/lib/integration/post-tiny-erp.js.map +1 -1
- package/lib/tiny-webhook.js.map +1 -1
- package/package.json +12 -6
- package/.turbo/turbo-build.log +0 -5
- package/CHANGELOG.md +0 -1
- package/src/event-to-tiny.ts +0 -134
- package/src/index.ts +0 -1
- package/src/integration/after-tiny-queue.ts +0 -80
- package/src/integration/export-order-to-tiny.ts +0 -113
- package/src/integration/export-product-to-tiny.ts +0 -60
- package/src/integration/helpers/format-tiny-date.ts +0 -6
- package/src/integration/import-order-from-tiny.ts +0 -109
- package/src/integration/import-product-from-tiny.ts +0 -176
- package/src/integration/parsers/order-from-tiny.ts +0 -61
- package/src/integration/parsers/order-to-tiny.ts +0 -212
- package/src/integration/parsers/product-from-tiny.ts +0 -318
- package/src/integration/parsers/product-to-tiny.ts +0 -158
- package/src/integration/parsers/status-from-tiny.ts +0 -35
- package/src/integration/parsers/status-to-tiny.ts +0 -42
- package/src/integration/post-tiny-erp.ts +0 -52
- package/src/tiny-erp.ts +0 -26
- package/src/tiny-webhook.ts +0 -103
- package/tsconfig.json +0 -3
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import type { Orders } from '@cloudcommerce/types';
|
|
2
|
-
import logger from 'firebase-functions/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
|
-
|
|
70
|
-
if (appData.ready_for_shipping_only) {
|
|
71
|
-
switch (tinyStatus) {
|
|
72
|
-
case 'aberto':
|
|
73
|
-
case 'cancelado':
|
|
74
|
-
case 'aprovado':
|
|
75
|
-
case 'preparando_envio':
|
|
76
|
-
case 'faturado':
|
|
77
|
-
if (!order.fulfillment_status || order.fulfillment_status.current !== 'ready_for_shipping') {
|
|
78
|
-
logger.info(`${orderId} skipped with status "${tinyStatus}"`);
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
break;
|
|
82
|
-
default:
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const tinyOrder = parseOrder(order, appData);
|
|
88
|
-
logger.info(`${orderId} ${JSON.stringify(tinyOrder)}`);
|
|
89
|
-
return postTiny('/pedido.incluir.php', {
|
|
90
|
-
pedido: {
|
|
91
|
-
pedido: tinyOrder,
|
|
92
|
-
},
|
|
93
|
-
}).then((response) => {
|
|
94
|
-
const tinyErpOnNewOrder = global.$tinyErpOnNewOrder;
|
|
95
|
-
if (tinyErpOnNewOrder && typeof tinyErpOnNewOrder === 'function') {
|
|
96
|
-
return tinyErpOnNewOrder({ response, order, postTiny });
|
|
97
|
-
}
|
|
98
|
-
return response;
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (originalTinyOrder) {
|
|
103
|
-
const { id, situacao } = originalTinyOrder;
|
|
104
|
-
logger.info(`${orderId} found with tiny status ${situacao} => ${tinyStatus}`);
|
|
105
|
-
if (tinyStatus && tinyStatus !== situacao) {
|
|
106
|
-
return postTiny('/pedido.alterar.situacao', {
|
|
107
|
-
id,
|
|
108
|
-
situacao: tinyStatus,
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return null;
|
|
113
|
-
};
|
|
@@ -1,60 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { getFirestore } from 'firebase-admin/firestore';
|
|
2
|
-
import logger from 'firebase-functions/logger';
|
|
3
|
-
import api from '@cloudcommerce/api';
|
|
4
|
-
import postTiny from './post-tiny-erp';
|
|
5
|
-
import parseOrder from './parsers/order-from-tiny';
|
|
6
|
-
import parseStatus from './parsers/status-from-tiny';
|
|
7
|
-
|
|
8
|
-
const getLastStatus = (records) => {
|
|
9
|
-
let statusRecord;
|
|
10
|
-
records.forEach((record) => {
|
|
11
|
-
if (
|
|
12
|
-
record
|
|
13
|
-
&& (!statusRecord || !record.date_time || record.date_time >= statusRecord.date_time)
|
|
14
|
-
) {
|
|
15
|
-
statusRecord = record;
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
return statusRecord && statusRecord.status;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export default async (apiDoc, queueEntry) => {
|
|
22
|
-
const getTinyOrder = async (tinyOrderId) => {
|
|
23
|
-
const { pedido } = await postTiny('/pedido.obter.php', {
|
|
24
|
-
id: Number(tinyOrderId),
|
|
25
|
-
});
|
|
26
|
-
const situacao = typeof pedido.situacao === 'string'
|
|
27
|
-
? pedido.situacao.toLowerCase()
|
|
28
|
-
: null;
|
|
29
|
-
const orderNumber = pedido.numero_ecommerce;
|
|
30
|
-
logger.info(`Import order n${orderNumber} ${tinyOrderId} => ${situacao}`);
|
|
31
|
-
|
|
32
|
-
const documentRef = getFirestore().doc(`tinyErpOrders/${tinyOrderId}`);
|
|
33
|
-
const documentSnapshot = await documentRef.get();
|
|
34
|
-
if (
|
|
35
|
-
documentSnapshot.exists
|
|
36
|
-
&& documentSnapshot.get('situacao') === situacao
|
|
37
|
-
) {
|
|
38
|
-
logger.info(`>> Ignoring Tiny order n${orderNumber} ${tinyOrderId} with same status`);
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const { data: { result } } = await api.get('orders', {
|
|
43
|
-
fields: ['_id', 'payments_history', 'fulfillments', 'shipping_lines'] as const,
|
|
44
|
-
limit: 1,
|
|
45
|
-
params: {
|
|
46
|
-
number: orderNumber || undefined,
|
|
47
|
-
'hidden_metafields.value': `${tinyOrderId}_tiny`,
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
if (!result.length) {
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
const order = result[0];
|
|
54
|
-
const partialOrder = await parseOrder(pedido, order.shipping_lines);
|
|
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({ situacao }).catch(logger.error);
|
|
82
|
-
return (firstResult && firstResult.response) || firstResult;
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
const tinyOrderNumber = queueEntry.nextId;
|
|
87
|
-
if (typeof tinyOrderNumber === 'string' && tinyOrderNumber.startsWith('id:')) {
|
|
88
|
-
return getTinyOrder(tinyOrderNumber.substring(3));
|
|
89
|
-
}
|
|
90
|
-
const filter = typeof tinyOrderNumber === 'string' && tinyOrderNumber.startsWith('ecom:')
|
|
91
|
-
? { numeroEcommerce: tinyOrderNumber.substring(5) }
|
|
92
|
-
: { numero: tinyOrderNumber };
|
|
93
|
-
return postTiny('/pedidos.pesquisa.php', filter).then(({ pedidos }) => {
|
|
94
|
-
let prop = 'numero';
|
|
95
|
-
let tinyOrderNumberSearch = tinyOrderNumber;
|
|
96
|
-
if (filter && filter.numeroEcommerce) {
|
|
97
|
-
prop = 'numero_ecommerce';
|
|
98
|
-
tinyOrderNumberSearch = tinyOrderNumber.substring(5);
|
|
99
|
-
}
|
|
100
|
-
const tinyOrder = pedidos.find(({ pedido }) => {
|
|
101
|
-
return Number(tinyOrderNumberSearch) === Number(pedido[prop]);
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
if (tinyOrder) {
|
|
105
|
-
return getTinyOrder(tinyOrder.pedido.id);
|
|
106
|
-
}
|
|
107
|
-
return null;
|
|
108
|
-
});
|
|
109
|
-
};
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import type { Products } from '@cloudcommerce/types';
|
|
2
|
-
import logger from 'firebase-functions/logger';
|
|
3
|
-
import api from '@cloudcommerce/api';
|
|
4
|
-
import updateAppData from '@cloudcommerce/firebase/lib/helpers/update-app-data';
|
|
5
|
-
import postTiny from './post-tiny-erp';
|
|
6
|
-
import parseProduct from './parsers/product-from-tiny';
|
|
7
|
-
|
|
8
|
-
export default async (apiDoc, queueEntry, appData, canCreateNew, isHiddenQueue) => {
|
|
9
|
-
const [queueSku, queueProductId] = String(queueEntry.nextId)
|
|
10
|
-
.split(';:') as [Products['sku'], Products['_id'] | undefined];
|
|
11
|
-
let product: Products | null = null;
|
|
12
|
-
try {
|
|
13
|
-
const resourceFind = (queueProductId || (`sku:${queueSku}` as `${string}:${string}`));
|
|
14
|
-
product = (await api.get(`products/${resourceFind}`)).data;
|
|
15
|
-
} catch (err: any) {
|
|
16
|
-
if (err.statusCode !== 404) {
|
|
17
|
-
throw err;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
let hasVariations: boolean = false;
|
|
21
|
-
let variationId: string | undefined;
|
|
22
|
-
if (product) {
|
|
23
|
-
const { variations } = product;
|
|
24
|
-
hasVariations = Boolean(variations && variations.length);
|
|
25
|
-
const variation = variations?.find(({ sku }) => queueSku === sku);
|
|
26
|
-
if (variation) {
|
|
27
|
-
variationId = variation._id;
|
|
28
|
-
} else {
|
|
29
|
-
logger.info(`SKU not found ${queueSku}`);
|
|
30
|
-
if (!isHiddenQueue && !appData.update_product) {
|
|
31
|
-
const msg = queueSku
|
|
32
|
-
+ ' corresponde a um produto com variações,'
|
|
33
|
-
+ ' especifique o SKU da variação para importar.';
|
|
34
|
-
const err: any = new Error(msg);
|
|
35
|
-
err.isConfigError = true;
|
|
36
|
-
return err;
|
|
37
|
-
}
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const handleTinyStock = ({ produto: produtoSaldo, tipo }, tinyProduct?) => {
|
|
43
|
-
let quantity = Number(produtoSaldo.saldo) || Number(produtoSaldo.estoqueAtual);
|
|
44
|
-
if (produtoSaldo.saldoReservado) {
|
|
45
|
-
quantity -= Number(produtoSaldo.saldoReservado);
|
|
46
|
-
}
|
|
47
|
-
if (product && (!appData.update_product || variationId)) {
|
|
48
|
-
if (!Number.isNaN(quantity)) {
|
|
49
|
-
if (quantity < 0) {
|
|
50
|
-
quantity = 0;
|
|
51
|
-
}
|
|
52
|
-
let endpoint = `products/${product._id}`;
|
|
53
|
-
if (variationId) {
|
|
54
|
-
endpoint += `variations/${variationId}`;
|
|
55
|
-
}
|
|
56
|
-
endpoint += '/quantity';
|
|
57
|
-
logger.info(endpoint, { quantity });
|
|
58
|
-
// @ts-ignore
|
|
59
|
-
return api.put(endpoint, quantity);
|
|
60
|
-
}
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (!product && tinyProduct && tipo === 'produto') {
|
|
65
|
-
return parseProduct(tinyProduct, tipo, true)
|
|
66
|
-
.then((bodyProduct) => {
|
|
67
|
-
return api.post('products', bodyProduct);
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (!tinyProduct) {
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return postTiny('/produto.obter.php', { id: (tinyProduct.id || produtoSaldo.id) })
|
|
76
|
-
.then(({ produto }) => {
|
|
77
|
-
let method;
|
|
78
|
-
let endpoint;
|
|
79
|
-
let productId = product && product._id;
|
|
80
|
-
if (productId) {
|
|
81
|
-
method = 'PATCH';
|
|
82
|
-
endpoint = `products/${productId}`;
|
|
83
|
-
} else if (tipo === 'produto' || appData.import_all_products) {
|
|
84
|
-
method = 'POST';
|
|
85
|
-
endpoint = 'products';
|
|
86
|
-
} else {
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
// @ts-ignore
|
|
90
|
-
return parseProduct(produto, tipo, method === 'POST').then((parsedProduct: Products) => {
|
|
91
|
-
if (!Number.isNaN(quantity)) {
|
|
92
|
-
parsedProduct.quantity = quantity >= 0 ? quantity : 0;
|
|
93
|
-
}
|
|
94
|
-
logger.info(`${method} ${endpoint}`);
|
|
95
|
-
const promise = api({
|
|
96
|
-
method,
|
|
97
|
-
endpoint,
|
|
98
|
-
data: parsedProduct,
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
if (Array.isArray(produto.variacoes) && produto.variacoes.length) {
|
|
102
|
-
if (!queueEntry.app) {
|
|
103
|
-
logger.warn('Variations cannot be queued without `queueEntry.app`');
|
|
104
|
-
return promise;
|
|
105
|
-
}
|
|
106
|
-
promise.then((response) => {
|
|
107
|
-
let skus = appData.__importation && appData.__importation.skus;
|
|
108
|
-
if (!Array.isArray(skus)) {
|
|
109
|
-
skus = [];
|
|
110
|
-
}
|
|
111
|
-
let isQueuedVariations = false;
|
|
112
|
-
produto.variacoes.forEach(({ variacao }) => {
|
|
113
|
-
const { codigo } = variacao;
|
|
114
|
-
let skuAndId = codigo;
|
|
115
|
-
if (!productId) {
|
|
116
|
-
productId = response.data && response.data._id;
|
|
117
|
-
}
|
|
118
|
-
if (productId) {
|
|
119
|
-
skuAndId += `;:${productId}`;
|
|
120
|
-
}
|
|
121
|
-
if (!skus.includes(codigo) && !skus.includes(skuAndId)) {
|
|
122
|
-
isQueuedVariations = true;
|
|
123
|
-
skus.push(skuAndId);
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
return isQueuedVariations
|
|
127
|
-
? updateAppData(queueEntry.app, {
|
|
128
|
-
__importation: {
|
|
129
|
-
...appData.__importation,
|
|
130
|
-
skus,
|
|
131
|
-
},
|
|
132
|
-
})
|
|
133
|
-
: response;
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
return promise;
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
logger.info(JSON.stringify({
|
|
142
|
-
queueSku,
|
|
143
|
-
queueProductId,
|
|
144
|
-
hasVariations,
|
|
145
|
-
variationId,
|
|
146
|
-
}));
|
|
147
|
-
const { tinyStockUpdate } = queueEntry;
|
|
148
|
-
if (tinyStockUpdate && isHiddenQueue && (queueProductId || (product && product._id))) {
|
|
149
|
-
return handleTinyStock(tinyStockUpdate as any, tinyStockUpdate.produto);
|
|
150
|
-
}
|
|
151
|
-
if (tinyStockUpdate.tipo === 'produto' && !queueProductId) {
|
|
152
|
-
return handleTinyStock({ produto: {}, tipo: 'produto' }, tinyStockUpdate.produto);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return postTiny('/produtos.pesquisa.php', { pesquisa: queueSku })
|
|
156
|
-
.then(({ produtos }) => {
|
|
157
|
-
if (Array.isArray(produtos)) {
|
|
158
|
-
let tinyProduct = produtos.find(({ produto }) => queueSku === String(produto.codigo));
|
|
159
|
-
if (tinyProduct) {
|
|
160
|
-
tinyProduct = tinyProduct.produto;
|
|
161
|
-
if (!hasVariations || variationId) {
|
|
162
|
-
if (tinyStockUpdate) {
|
|
163
|
-
return handleTinyStock(tinyStockUpdate as any, tinyProduct);
|
|
164
|
-
}
|
|
165
|
-
return postTiny('/produto.obter.estoque.php', { id: tinyProduct.id })
|
|
166
|
-
.then((tinyStock) => handleTinyStock(tinyStock, tinyProduct));
|
|
167
|
-
}
|
|
168
|
-
return handleTinyStock({ produto: {} } as any, tinyProduct);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
const msg = `SKU ${queueSku} não encontrado no Tiny`;
|
|
172
|
-
const err: any = new Error(msg);
|
|
173
|
-
err.isConfigError = true;
|
|
174
|
-
throw new Error(err);
|
|
175
|
-
});
|
|
176
|
-
};
|
|
@@ -1,61 +0,0 @@
|
|
|
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
|
-
const indexFromInvoice = shippingLine.invoices
|
|
36
|
-
.findIndex((invoice) => invoice.number === number);
|
|
37
|
-
|
|
38
|
-
if (number && !(indexFromInvoice > -1)) {
|
|
39
|
-
shippingLine.invoices.push({
|
|
40
|
-
number,
|
|
41
|
-
serial_number: String(tinyInvoice.nota_fiscal.serie),
|
|
42
|
-
access_key: String(tinyInvoice.nota_fiscal.chave_acesso),
|
|
43
|
-
});
|
|
44
|
-
} else if (number && (indexFromInvoice > -1)) {
|
|
45
|
-
Object.assign(
|
|
46
|
-
shippingLine.invoices[indexFromInvoice],
|
|
47
|
-
{
|
|
48
|
-
access_key: String(tinyInvoice.nota_fiscal.chave_acesso),
|
|
49
|
-
serial_number: String(tinyInvoice.nota_fiscal.serie),
|
|
50
|
-
},
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
partialOrder.shipping_lines = shippingLines;
|
|
54
|
-
resolve(partialOrder);
|
|
55
|
-
})
|
|
56
|
-
.catch(reject);
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
resolve(partialOrder);
|
|
61
|
-
});
|