cloudcommerce 0.0.97 → 0.0.98
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 +7 -0
- package/package.json +1 -1
- 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 +1 -1
- package/packages/cli/package.json +1 -1
- package/packages/config/package.json +1 -1
- package/packages/events/package.json +1 -1
- package/packages/firebase/lib/config.js +27 -25
- package/packages/firebase/lib/const.js +3 -2
- package/packages/firebase/lib/env.js +2 -1
- package/packages/firebase/lib/handlers/check-store-events.js +146 -145
- package/packages/firebase/lib/helpers/pubsub.js +20 -18
- package/packages/firebase/lib/helpers/update-app-data.js +38 -38
- package/packages/firebase/lib/index.js +9 -7
- package/packages/firebase/lib/init.js +2 -1
- package/packages/firebase/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 +110 -109
- 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 +1 -1
- package/packages/passport/package.json +1 -1
- package/packages/ssr/package.json +1 -1
- package/packages/storefront/dist/client/LoginOffcanvas.daf3f717.js +1 -0
- package/packages/storefront/dist/client/admin/config.json +1 -0
- package/packages/storefront/dist/client/assets/_...522e6bf2.css +4 -0
- package/packages/storefront/dist/client/assets/_...a48b75c7.css +1 -0
- package/packages/storefront/dist/client/assets/cms-preview.css +274 -0
- package/packages/storefront/dist/client/assets/cms.css +114 -0
- package/packages/storefront/dist/client/assets/cvv.png +0 -0
- package/packages/storefront/dist/client/assets/icons/bootstrap-icons/font/storefront-icons.woff2 +0 -0
- package/packages/storefront/dist/client/assets/icons/feather-icons/font/storefront-icons.woff2 +0 -0
- package/packages/storefront/dist/client/assets/icons/font-awesome/font/storefront-icons.woff2 +0 -0
- package/packages/storefront/dist/client/assets/icons/line-awesome/font/storefront-icons.woff2 +0 -0
- package/packages/storefront/dist/client/assets/icons/tabler-icons/font/storefront-icons.woff2 +0 -0
- package/packages/storefront/dist/client/assets/img-placeholder.png +0 -0
- package/packages/storefront/dist/client/assets/payments.png +0 -0
- package/packages/storefront/dist/client/assets/ssl-safe.png +0 -0
- package/packages/storefront/dist/client/chunks/LoginForm.d9251274.js +1066 -0
- package/packages/storefront/dist/client/chunks/LoginOffcanvas.07fe6492.js +1 -0
- package/packages/storefront/dist/client/chunks/index.esm.84a649c7.js +331 -0
- package/packages/storefront/dist/client/chunks/preload-helper.f15ab524.js +1 -0
- package/packages/storefront/dist/client/chunks/runtime-dom.esm-bundler.738639ee.js +1 -0
- package/packages/storefront/dist/client/chunks/workbox-window.prod.es5.4b654ae6.js +2 -0
- package/packages/storefront/dist/client/client.b552d86a.js +1 -0
- package/packages/storefront/dist/client/hoisted.054c36ac.js +1 -0
- package/packages/storefront/dist/client/img/icon.png +0 -0
- package/packages/storefront/dist/client/img/large-icon.png +0 -0
- package/packages/storefront/dist/client/img/uploads/banner1.png +0 -0
- package/packages/storefront/dist/client/img/uploads/banner2.png +0 -0
- package/packages/storefront/dist/client/img/uploads/banner2.webp +0 -0
- package/packages/storefront/dist/client/img/uploads/favicon.png +0 -0
- package/packages/storefront/dist/client/img/uploads/headless.png +0 -0
- package/packages/storefront/dist/client/img/uploads/headphone.png +0 -0
- package/packages/storefront/dist/client/img/uploads/headphone.webp +0 -0
- package/packages/storefront/dist/client/img/uploads/icon.png +0 -0
- package/packages/storefront/dist/client/img/uploads/large-icon.png +0 -0
- package/packages/storefront/dist/client/img/uploads/logo.png +0 -0
- package/packages/storefront/dist/client/img/uploads/logo.webp +0 -0
- package/packages/storefront/dist/client/img/uploads/og-image.png +0 -0
- package/packages/storefront/dist/client/img/uploads/passion.png +0 -0
- package/packages/storefront/dist/client/img/uploads/passion.webp +0 -0
- package/packages/storefront/dist/client/img/uploads/pwa-reliable.png +0 -0
- package/packages/storefront/dist/client/img/uploads/rect8589.png +0 -0
- package/packages/storefront/dist/client/img/uploads/rect859.png +0 -0
- package/packages/storefront/dist/client/img/uploads/rect89.png +0 -0
- package/packages/storefront/dist/client/img/uploads/rect89.webp +0 -0
- package/packages/storefront/dist/client/img/uploads/ssl-safe.png +0 -0
- package/packages/storefront/dist/client/manifest.webmanifest +1 -0
- package/packages/storefront/dist/client/page.3aa82516.js +1 -0
- package/packages/storefront/dist/client/robots.txt +8 -0
- package/packages/storefront/dist/client/sw.js +1 -0
- package/packages/storefront/dist/client/workbox-6f0d1f78.js +1 -0
- package/packages/storefront/dist/client/~partytown/partytown-atomics.js +2 -0
- package/packages/storefront/dist/client/~partytown/partytown-media.js +2 -0
- package/packages/storefront/dist/client/~partytown/partytown-sw.js +2 -0
- package/packages/storefront/dist/client/~partytown/partytown.js +2 -0
- package/packages/storefront/dist/server/entry.mjs +2852 -0
- package/packages/storefront/dist/server/manifest.webmanifest +1 -0
- package/packages/storefront/dist/server/registerSW.js +1 -0
- package/packages/storefront/package.json +1 -1
- package/packages/storefront/src/lib/layouts/PagesHeader.astro +2 -1
- package/packages/types/package.json +1 -1
|
@@ -1,58 +1,62 @@
|
|
|
1
1
|
import api from '@cloudcommerce/api';
|
|
2
2
|
import postTiny from './post-tiny-erp.js';
|
|
3
3
|
import parseProduct from './parsers/product-to-tiny.js';
|
|
4
|
-
|
|
5
4
|
export default async (apiDoc, queueEntry, appData, canCreateNew) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
const productId = queueEntry.nextId;
|
|
6
|
+
let product;
|
|
7
|
+
if (productId === apiDoc._id) {
|
|
8
|
+
product = apiDoc;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
try {
|
|
12
|
+
product = (await api.get(`products/${productId}`)).data;
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
if (err.statusCode === 404) {
|
|
16
|
+
const msg = `O produto ${productId} não existe (:${err.statusCode})`;
|
|
17
|
+
const error = new Error(msg);
|
|
18
|
+
error.isConfigError = true;
|
|
19
|
+
return error;
|
|
20
|
+
}
|
|
21
|
+
throw err;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
let tinyData;
|
|
11
25
|
try {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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;
|
|
26
|
+
tinyData = await postTiny('/produtos.pesquisa.php', {
|
|
27
|
+
pesquisa: product.sku,
|
|
28
|
+
});
|
|
21
29
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (err.response && err.response.status === 404) {
|
|
30
|
-
tinyData = {};
|
|
31
|
-
} else {
|
|
32
|
-
throw err;
|
|
30
|
+
catch (err) {
|
|
31
|
+
if (err.response && err.response.status === 404) {
|
|
32
|
+
tinyData = {};
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
throw err;
|
|
36
|
+
}
|
|
33
37
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
+
}
|
|
47
|
+
else if (!canCreateNew) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
45
50
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
: null;
|
|
51
|
+
const tinyProduct = parseProduct(product, originalTinyProduct, appData);
|
|
52
|
+
return tinyProduct
|
|
53
|
+
? postTiny(originalTinyProduct ? '/produto.alterar.php' : '/produto.incluir.php', {
|
|
54
|
+
produto: {
|
|
55
|
+
produtos: [{
|
|
56
|
+
produto: tinyProduct,
|
|
57
|
+
}],
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
: null;
|
|
57
61
|
};
|
|
58
|
-
|
|
62
|
+
//# sourceMappingURL=export-product-to-tiny.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export default (d) => {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
/* eslint-disable prefer-template */
|
|
3
|
+
return d.getDate().toString().padStart(2, '0') + '/'
|
|
4
4
|
+ (d.getMonth() + 1).toString().padStart(2, '0') + '/'
|
|
5
5
|
+ d.getFullYear();
|
|
6
6
|
};
|
|
7
|
-
|
|
7
|
+
//# sourceMappingURL=format-tiny-date.js.map
|
|
@@ -5,88 +5,87 @@ import api from '@cloudcommerce/api';
|
|
|
5
5
|
import postTiny from './post-tiny-erp.js';
|
|
6
6
|
import parseOrder from './parsers/order-from-tiny.js';
|
|
7
7
|
import parseStatus from './parsers/status-from-tiny.js';
|
|
8
|
-
|
|
9
8
|
const getLastStatus = (records) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
let statusRecord;
|
|
10
|
+
records.forEach((record) => {
|
|
11
|
+
if (record
|
|
13
12
|
&& (!statusRecord || !record.date_time || record.date_time >= statusRecord.date_time)) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
statusRecord = record;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
return statusRecord && statusRecord.status;
|
|
18
17
|
};
|
|
19
|
-
|
|
20
18
|
export default async (apiDoc, queueEntry) => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
19
|
+
const getTinyOrder = async (tinyOrderId) => {
|
|
20
|
+
const { pedido } = await postTiny('/pedido.obter.php', {
|
|
21
|
+
id: Number(tinyOrderId),
|
|
22
|
+
});
|
|
23
|
+
const situacao = typeof pedido.situacao === 'string'
|
|
24
|
+
? pedido.situacao.toLowerCase()
|
|
25
|
+
: null;
|
|
26
|
+
const orderNumber = pedido.numero_ecommerce;
|
|
27
|
+
logger.info(`Import order n${orderNumber} ${tinyOrderId} => ${situacao}`);
|
|
28
|
+
const documentRef = getFirestore().doc(`tinyErpOrders/${tinyOrderId}`);
|
|
29
|
+
const documentSnapshot = await documentRef.get();
|
|
30
|
+
if (documentSnapshot.exists
|
|
33
31
|
&& documentSnapshot.get('situacao') === situacao) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
32
|
+
logger.info(`>> Ignoring Tiny order n${orderNumber} ${tinyOrderId} with same status`);
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
let listEndpoint = 'orders?limit=1&fields=_id,payments_history,fulfillments,shipping_lines';
|
|
36
|
+
if (orderNumber) {
|
|
37
|
+
listEndpoint += `&number=${orderNumber}`;
|
|
38
|
+
}
|
|
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
|
|
63
62
|
// @ts-ignore
|
|
64
63
|
&& (!order[subresource] || getLastStatus(order[subresource]) !== newStatus)) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
return postTiny('/pedidos.pesquisa.php', {
|
|
81
|
-
numero: tinyOrderNumber,
|
|
82
|
-
}).then(({ pedidos }) => {
|
|
83
|
-
const tinyOrder = pedidos.find(({ pedido }) => {
|
|
84
|
-
return Number(tinyOrderNumber) === Number(pedido.numero);
|
|
85
|
-
});
|
|
86
|
-
if (tinyOrder) {
|
|
87
|
-
return getTinyOrder(tinyOrder.pedido.id);
|
|
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({ situacao }).catch(logger.error);
|
|
72
|
+
return (firstResult && firstResult.response) || firstResult;
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
const tinyOrderNumber = queueEntry.nextId;
|
|
76
|
+
if (typeof tinyOrderNumber === 'string' && tinyOrderNumber.startsWith('id:')) {
|
|
77
|
+
return getTinyOrder(tinyOrderNumber.substring(3));
|
|
88
78
|
}
|
|
89
|
-
return
|
|
90
|
-
|
|
79
|
+
return postTiny('/pedidos.pesquisa.php', {
|
|
80
|
+
numero: tinyOrderNumber,
|
|
81
|
+
}).then(({ pedidos }) => {
|
|
82
|
+
const tinyOrder = pedidos.find(({ pedido }) => {
|
|
83
|
+
return Number(tinyOrderNumber) === Number(pedido.numero);
|
|
84
|
+
});
|
|
85
|
+
if (tinyOrder) {
|
|
86
|
+
return getTinyOrder(tinyOrder.pedido.id);
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
});
|
|
91
90
|
};
|
|
92
|
-
|
|
91
|
+
//# sourceMappingURL=import-order-from-tiny.js.map
|
|
@@ -3,156 +3,159 @@ import api from '@cloudcommerce/api';
|
|
|
3
3
|
import updateAppData from '@cloudcommerce/firebase/lib/helpers/update-app-data';
|
|
4
4
|
import postTiny from './post-tiny-erp.js';
|
|
5
5
|
import parseProduct from './parsers/product-from-tiny.js';
|
|
6
|
-
|
|
7
6
|
export default async (apiDoc, queueEntry, appData, canCreateNew, isHiddenQueue) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} catch (err) {
|
|
13
|
-
if (err.statusCode !== 404) {
|
|
14
|
-
throw err;
|
|
7
|
+
const [sku, productId] = String(queueEntry.nextId).split(';:');
|
|
8
|
+
let product = null;
|
|
9
|
+
try {
|
|
10
|
+
product = (await api.get(`products/${(productId || `sku:${sku}`)}`)).data;
|
|
15
11
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const { variations } = product;
|
|
21
|
-
hasVariations = Boolean(variations && variations.length);
|
|
22
|
-
const variation = variations?.find((variation) => sku === variation.sku);
|
|
23
|
-
if (variation) {
|
|
24
|
-
variationId = variation._id;
|
|
25
|
-
} else {
|
|
26
|
-
logger.info(`SKU not found ${sku}`);
|
|
27
|
-
if (!isHiddenQueue && !appData.update_product) {
|
|
28
|
-
const msg = `${sku
|
|
29
|
-
} corresponde a um produto com variações,`
|
|
30
|
-
+ ' especifique o SKU da variação para importar.';
|
|
31
|
-
const err = new Error(msg);
|
|
32
|
-
err.isConfigError = true;
|
|
33
|
-
return err;
|
|
34
|
-
}
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
const handleTinyStock = ({ produto, tipo }, tinyProduct) => {
|
|
39
|
-
let quantity = Number(produto.saldo) || Number(produto.estoqueAtual);
|
|
40
|
-
if (produto.saldoReservado) {
|
|
41
|
-
quantity -= Number(produto.saldoReservado);
|
|
12
|
+
catch (err) {
|
|
13
|
+
if (err.statusCode !== 404) {
|
|
14
|
+
throw err;
|
|
15
|
+
}
|
|
42
16
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
17
|
+
let hasVariations = false;
|
|
18
|
+
let variationId;
|
|
19
|
+
if (product) {
|
|
20
|
+
const { variations } = product;
|
|
21
|
+
hasVariations = Boolean(variations && variations.length);
|
|
22
|
+
const variation = variations?.find((variation) => sku === variation.sku);
|
|
23
|
+
if (variation) {
|
|
24
|
+
variationId = variation._id;
|
|
47
25
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
26
|
+
else {
|
|
27
|
+
logger.info(`SKU not found ${sku}`);
|
|
28
|
+
if (!isHiddenQueue && !appData.update_product) {
|
|
29
|
+
const msg = sku
|
|
30
|
+
+ ' corresponde a um produto com variações,'
|
|
31
|
+
+ ' especifique o SKU da variação para importar.';
|
|
32
|
+
const err = new Error(msg);
|
|
33
|
+
err.isConfigError = true;
|
|
34
|
+
return err;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
51
37
|
}
|
|
52
|
-
endpoint += '/quantity';
|
|
53
|
-
logger.info(endpoint, { quantity });
|
|
54
|
-
// @ts-ignore
|
|
55
|
-
return api.put(endpoint, quantity);
|
|
56
|
-
}
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
if (!tinyProduct) {
|
|
60
|
-
return null;
|
|
61
38
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
let productId = product && product._id;
|
|
67
|
-
if (productId) {
|
|
68
|
-
method = 'PATCH';
|
|
69
|
-
endpoint = `products/${productId}`;
|
|
70
|
-
} else if (tipo === 'produto' || !tipo) {
|
|
71
|
-
method = 'POST';
|
|
72
|
-
endpoint = 'products';
|
|
73
|
-
} else {
|
|
74
|
-
return null;
|
|
39
|
+
const handleTinyStock = ({ produto, tipo }, tinyProduct) => {
|
|
40
|
+
let quantity = Number(produto.saldo) || Number(produto.estoqueAtual);
|
|
41
|
+
if (produto.saldoReservado) {
|
|
42
|
+
quantity -= Number(produto.saldoReservado);
|
|
75
43
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
endpoint,
|
|
85
|
-
data: product,
|
|
86
|
-
});
|
|
87
|
-
if (Array.isArray(produto.variacoes) && produto.variacoes.length) {
|
|
88
|
-
if (!queueEntry.app) {
|
|
89
|
-
logger.warn('Variations cannot be queued without `queueEntry.app`');
|
|
90
|
-
return promise;
|
|
91
|
-
}
|
|
92
|
-
promise.then((response) => {
|
|
93
|
-
let skus = appData.__importation && appData.__importation.skus;
|
|
94
|
-
if (!Array.isArray(skus)) {
|
|
95
|
-
skus = [];
|
|
96
|
-
}
|
|
97
|
-
let isQueuedVariations = false;
|
|
98
|
-
produto.variacoes.forEach(({ variacao }) => {
|
|
99
|
-
const { codigo } = variacao;
|
|
100
|
-
let skuAndId = codigo;
|
|
101
|
-
if (!productId) {
|
|
102
|
-
productId = response.data && response.data._id;
|
|
44
|
+
if (product && (!appData.update_product || variationId)) {
|
|
45
|
+
if (!Number.isNaN(quantity)) {
|
|
46
|
+
if (quantity < 0) {
|
|
47
|
+
quantity = 0;
|
|
48
|
+
}
|
|
49
|
+
let endpoint = `products/${product._id}`;
|
|
50
|
+
if (variationId) {
|
|
51
|
+
endpoint += `variations/${variationId}`;
|
|
103
52
|
}
|
|
104
|
-
|
|
105
|
-
|
|
53
|
+
endpoint += '/quantity';
|
|
54
|
+
logger.info(endpoint, { quantity });
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
return api.put(endpoint, quantity);
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
if (!tinyProduct) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return postTiny('/produto.obter.php', { id: tinyProduct.id })
|
|
64
|
+
.then(({ produto }) => {
|
|
65
|
+
let method;
|
|
66
|
+
let endpoint;
|
|
67
|
+
let productId = product && product._id;
|
|
68
|
+
if (productId) {
|
|
69
|
+
method = 'PATCH';
|
|
70
|
+
endpoint = `products/${productId}`;
|
|
71
|
+
}
|
|
72
|
+
else if (tipo === 'produto' || !tipo) {
|
|
73
|
+
method = 'POST';
|
|
74
|
+
endpoint = 'products';
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
// @ts-ignore
|
|
80
|
+
return parseProduct(produto, method === 'POST').then((product) => {
|
|
81
|
+
if (!Number.isNaN(quantity)) {
|
|
82
|
+
product.quantity = quantity >= 0 ? quantity : 0;
|
|
106
83
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
84
|
+
logger.info(`${method} ${endpoint}`);
|
|
85
|
+
const promise = api({
|
|
86
|
+
method,
|
|
87
|
+
endpoint,
|
|
88
|
+
data: product,
|
|
89
|
+
});
|
|
90
|
+
if (Array.isArray(produto.variacoes) && produto.variacoes.length) {
|
|
91
|
+
if (!queueEntry.app) {
|
|
92
|
+
logger.warn('Variations cannot be queued without `queueEntry.app`');
|
|
93
|
+
return promise;
|
|
94
|
+
}
|
|
95
|
+
promise.then((response) => {
|
|
96
|
+
let skus = appData.__importation && appData.__importation.skus;
|
|
97
|
+
if (!Array.isArray(skus)) {
|
|
98
|
+
skus = [];
|
|
99
|
+
}
|
|
100
|
+
let isQueuedVariations = false;
|
|
101
|
+
produto.variacoes.forEach(({ variacao }) => {
|
|
102
|
+
const { codigo } = variacao;
|
|
103
|
+
let skuAndId = codigo;
|
|
104
|
+
if (!productId) {
|
|
105
|
+
productId = response.data && response.data._id;
|
|
106
|
+
}
|
|
107
|
+
if (productId) {
|
|
108
|
+
skuAndId += `;:${productId}`;
|
|
109
|
+
}
|
|
110
|
+
if (!skus.includes(codigo) && !skus.includes(skuAndId)) {
|
|
111
|
+
isQueuedVariations = true;
|
|
112
|
+
skus.push(skuAndId);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
return isQueuedVariations
|
|
116
|
+
? updateAppData(queueEntry.app, {
|
|
117
|
+
__importation: {
|
|
118
|
+
...appData.__importation,
|
|
119
|
+
skus,
|
|
120
|
+
},
|
|
121
|
+
})
|
|
122
|
+
: response;
|
|
123
|
+
});
|
|
110
124
|
}
|
|
111
|
-
|
|
112
|
-
return isQueuedVariations
|
|
113
|
-
? updateAppData(queueEntry.app, {
|
|
114
|
-
__importation: {
|
|
115
|
-
...appData.__importation,
|
|
116
|
-
skus,
|
|
117
|
-
},
|
|
118
|
-
})
|
|
119
|
-
: response;
|
|
125
|
+
return promise;
|
|
120
126
|
});
|
|
121
|
-
}
|
|
122
|
-
return promise;
|
|
123
127
|
});
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
128
|
+
};
|
|
129
|
+
logger.info(JSON.stringify({
|
|
130
|
+
sku,
|
|
131
|
+
productId,
|
|
132
|
+
hasVariations,
|
|
133
|
+
variationId,
|
|
134
|
+
}));
|
|
135
|
+
const { tinyStockUpdate } = queueEntry;
|
|
136
|
+
if (tinyStockUpdate && isHiddenQueue && productId) {
|
|
137
|
+
return handleTinyStock(tinyStockUpdate);
|
|
138
|
+
}
|
|
139
|
+
return postTiny('/produtos.pesquisa.php', { pesquisa: sku })
|
|
140
|
+
.then(({ produtos }) => {
|
|
141
|
+
if (Array.isArray(produtos)) {
|
|
142
|
+
let tinyProduct = produtos.find(({ produto }) => sku === String(produto.codigo));
|
|
143
|
+
if (tinyProduct) {
|
|
144
|
+
tinyProduct = tinyProduct.produto;
|
|
145
|
+
if (!hasVariations || variationId) {
|
|
146
|
+
if (tinyStockUpdate) {
|
|
147
|
+
return handleTinyStock(tinyStockUpdate, tinyProduct);
|
|
148
|
+
}
|
|
149
|
+
return postTiny('/produto.obter.estoque.php', { id: tinyProduct.id })
|
|
150
|
+
.then((tinyStock) => handleTinyStock(tinyStock, tinyProduct));
|
|
151
|
+
}
|
|
152
|
+
return handleTinyStock({ produto: {} }, tinyProduct);
|
|
145
153
|
}
|
|
146
|
-
return postTiny('/produto.obter.estoque.php', { id: tinyProduct.id })
|
|
147
|
-
.then((tinyStock) => handleTinyStock(tinyStock, tinyProduct));
|
|
148
|
-
}
|
|
149
|
-
return handleTinyStock({ produto: {} }, tinyProduct);
|
|
150
154
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
throw new Error(err);
|
|
155
|
+
const msg = `SKU ${sku} não encontrado no Tiny`;
|
|
156
|
+
const err = new Error(msg);
|
|
157
|
+
err.isConfigError = true;
|
|
158
|
+
throw new Error(err);
|
|
156
159
|
});
|
|
157
160
|
};
|
|
158
|
-
|
|
161
|
+
//# sourceMappingURL=import-product-from-tiny.js.map
|