@cloudcommerce/app-tiny-erp 2.9.0 → 2.10.1
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,212 +0,0 @@
|
|
|
1
|
-
import type { Orders } from '@cloudcommerce/types';
|
|
2
|
-
import ecomUtils from '@ecomplus/utils';
|
|
3
|
-
import formatDate from '../helpers/format-tiny-date';
|
|
4
|
-
import parseStatus from './status-to-tiny';
|
|
5
|
-
|
|
6
|
-
export default (order: Orders, appData) => {
|
|
7
|
-
const orderRef = String(order.number) || order._id;
|
|
8
|
-
const tinyOrder: Record<string, any> = {
|
|
9
|
-
numero_pedido_ecommerce: orderRef,
|
|
10
|
-
data_pedido: formatDate(new Date(order.opened_at || order.created_at)),
|
|
11
|
-
ecommerce: 'E-Com Plus',
|
|
12
|
-
situacao: parseStatus(order),
|
|
13
|
-
itens: [],
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const buyer = order.buyers && order.buyers[0];
|
|
17
|
-
const shippingLine = order.shipping_lines && order.shipping_lines[0];
|
|
18
|
-
const transaction = order.transactions && order.transactions[0];
|
|
19
|
-
const shippingAddress = shippingLine && shippingLine.to;
|
|
20
|
-
const billingAddress = transaction && transaction.billing_address;
|
|
21
|
-
|
|
22
|
-
const parseAddress = (address, tinyObject) => {
|
|
23
|
-
[
|
|
24
|
-
['street', 'endereco', 50],
|
|
25
|
-
['number', 'numero', 10],
|
|
26
|
-
['complement', 'complemento', 50],
|
|
27
|
-
['borough', 'bairro', 30],
|
|
28
|
-
['zip', 'cep', 10],
|
|
29
|
-
['city', 'cidade', 30],
|
|
30
|
-
['province_code', 'uf', 30],
|
|
31
|
-
].forEach(([addressField, tinyField, maxLength]) => {
|
|
32
|
-
if (address[addressField]) {
|
|
33
|
-
tinyObject[tinyField] = String(address[addressField])
|
|
34
|
-
.substring(0, maxLength as number).replace('&', 'e');
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
if (buyer) {
|
|
40
|
-
const tinyCustomer: Record<string, any> = {
|
|
41
|
-
codigo: buyer._id,
|
|
42
|
-
nome: (buyer.corporate_name
|
|
43
|
-
|| ecomUtils.fullName(buyer)).substring(0, 30).replace('&', 'e')
|
|
44
|
-
|| `Comprador de #${orderRef}`,
|
|
45
|
-
tipo_pessoa: buyer.registry_type === 'j' ? 'J' : 'F',
|
|
46
|
-
};
|
|
47
|
-
if (buyer.display_name) {
|
|
48
|
-
tinyCustomer.nome_fantasia = buyer.display_name
|
|
49
|
-
.substring(0, 30).replace('&', 'e');
|
|
50
|
-
}
|
|
51
|
-
if (buyer.doc_number && buyer.doc_number.length <= 18) {
|
|
52
|
-
tinyCustomer.cpf_cnpj = buyer.doc_number;
|
|
53
|
-
}
|
|
54
|
-
if (
|
|
55
|
-
buyer.inscription_number && buyer.inscription_number.length <= 18
|
|
56
|
-
&& buyer.inscription_type !== 'Municipal'
|
|
57
|
-
) {
|
|
58
|
-
tinyCustomer.ie = buyer.inscription_number;
|
|
59
|
-
}
|
|
60
|
-
if (buyer.main_email && buyer.main_email.length <= 50) {
|
|
61
|
-
tinyCustomer.email = buyer.main_email;
|
|
62
|
-
}
|
|
63
|
-
if (shippingAddress) {
|
|
64
|
-
parseAddress(billingAddress || shippingAddress, tinyCustomer);
|
|
65
|
-
}
|
|
66
|
-
const phone = buyer.phones && buyer.phones[0];
|
|
67
|
-
if (phone) {
|
|
68
|
-
tinyCustomer.fone = phone.country_code ? `+${phone.country_code} ` : '';
|
|
69
|
-
tinyCustomer.fone += phone.number;
|
|
70
|
-
}
|
|
71
|
-
tinyOrder.cliente = tinyCustomer;
|
|
72
|
-
} else {
|
|
73
|
-
tinyOrder.cliente = {
|
|
74
|
-
nome: `Comprador de #${orderRef}`,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (shippingAddress) {
|
|
79
|
-
tinyOrder.endereco_entrega = {};
|
|
80
|
-
parseAddress(shippingAddress, tinyOrder.endereco_entrega);
|
|
81
|
-
if (shippingAddress.name) {
|
|
82
|
-
tinyOrder.endereco_entrega.nome_destinatario = shippingAddress.name.substring(0, 60);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (order.items) {
|
|
87
|
-
order.items.forEach((item) => {
|
|
88
|
-
if (item.quantity) {
|
|
89
|
-
const itemRef = (item.sku || item._id || Math.random().toString()).substring(0, 60);
|
|
90
|
-
tinyOrder.itens.push({
|
|
91
|
-
item: {
|
|
92
|
-
codigo: itemRef,
|
|
93
|
-
descricao: item.name ? item.name.substring(0, 120) : itemRef,
|
|
94
|
-
unidade: 'UN',
|
|
95
|
-
quantidade: item.quantity,
|
|
96
|
-
valor_unitario: ecomUtils.price(item),
|
|
97
|
-
},
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (order.payment_method_label) {
|
|
104
|
-
tinyOrder.meio_pagamento = order.payment_method_label;
|
|
105
|
-
}
|
|
106
|
-
if (transaction) {
|
|
107
|
-
switch (transaction.payment_method.code) {
|
|
108
|
-
case 'credit_card':
|
|
109
|
-
tinyOrder.forma_pagamento = 'credito';
|
|
110
|
-
break;
|
|
111
|
-
case 'banking_billet':
|
|
112
|
-
tinyOrder.forma_pagamento = 'boleto';
|
|
113
|
-
break;
|
|
114
|
-
case 'account_deposit':
|
|
115
|
-
tinyOrder.forma_pagamento = 'deposito';
|
|
116
|
-
break;
|
|
117
|
-
case 'online_debit':
|
|
118
|
-
case 'debit_card':
|
|
119
|
-
case 'balance_on_intermediary':
|
|
120
|
-
tinyOrder.forma_pagamento = 'debito';
|
|
121
|
-
break;
|
|
122
|
-
default:
|
|
123
|
-
tinyOrder.forma_pagamento = 'multiplas';
|
|
124
|
-
}
|
|
125
|
-
if (!tinyOrder.meio_pagamento && transaction.payment_method.name) {
|
|
126
|
-
tinyOrder.meio_pagamento = transaction.payment_method.name.substring(0, 100);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const tinyErpOrderParser = global.$tinyErpOrderParser;
|
|
131
|
-
if (tinyErpOrderParser && typeof tinyErpOrderParser === 'function') {
|
|
132
|
-
tinyErpOrderParser({ tinyOrder, order });
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (order.shipping_method_label) {
|
|
136
|
-
tinyOrder.forma_frete = order.shipping_method_label;
|
|
137
|
-
}
|
|
138
|
-
if (shippingLine) {
|
|
139
|
-
tinyOrder.forma_envio = 'X';
|
|
140
|
-
if (shippingLine.app) {
|
|
141
|
-
const { carrier } = shippingLine.app;
|
|
142
|
-
if (carrier) {
|
|
143
|
-
if (/correios/i.test(carrier)) {
|
|
144
|
-
tinyOrder.forma_envio = 'C';
|
|
145
|
-
} else if (/b2w/i.test(carrier)) {
|
|
146
|
-
tinyOrder.forma_envio = 'B';
|
|
147
|
-
} else if (/mercado envios/i.test(carrier)) {
|
|
148
|
-
tinyOrder.forma_envio = 'M';
|
|
149
|
-
} else {
|
|
150
|
-
tinyOrder.forma_envio = 'T';
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
if (
|
|
154
|
-
(!tinyOrder.forma_envio || tinyOrder.forma_envio === 'X' || tinyOrder.forma_envio === 'T')
|
|
155
|
-
&& shippingLine.app.service_name && /(pac|sedex)/i.test(shippingLine.app.service_name)
|
|
156
|
-
) {
|
|
157
|
-
tinyOrder.forma_envio = 'C';
|
|
158
|
-
}
|
|
159
|
-
if (!tinyOrder.forma_frete && shippingLine.app.label) {
|
|
160
|
-
tinyOrder.forma_frete = shippingLine.app.label;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
} else {
|
|
164
|
-
tinyOrder.forma_envio = 'S';
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const { amount } = order;
|
|
168
|
-
if (amount) {
|
|
169
|
-
if (typeof amount.freight === 'number') {
|
|
170
|
-
tinyOrder.valor_frete = amount.freight;
|
|
171
|
-
if (amount.tax) {
|
|
172
|
-
tinyOrder.valor_frete += amount.tax;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
if (amount.discount) {
|
|
176
|
-
tinyOrder.valor_desconto = amount.discount;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (order.notes) {
|
|
181
|
-
tinyOrder.obs = order.notes.substring(0, 100);
|
|
182
|
-
}
|
|
183
|
-
if (order.extra_discount && order.extra_discount.discount_coupon) {
|
|
184
|
-
tinyOrder.obs = `${(tinyOrder.obs || '')} - ${order.extra_discount.discount_coupon}`
|
|
185
|
-
.substring(0, 100);
|
|
186
|
-
}
|
|
187
|
-
if (order.staff_notes) {
|
|
188
|
-
tinyOrder.obs_internas = order.staff_notes.substring(0, 100);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (appData.tiny_order_data && typeof appData.tiny_order_data === 'object') {
|
|
192
|
-
Object.keys(appData.tiny_order_data).forEach((field) => {
|
|
193
|
-
let value = appData.tiny_order_data[field];
|
|
194
|
-
switch (value) {
|
|
195
|
-
case undefined:
|
|
196
|
-
case '':
|
|
197
|
-
case null:
|
|
198
|
-
break;
|
|
199
|
-
default:
|
|
200
|
-
if (typeof value === 'string') {
|
|
201
|
-
value = value.trim();
|
|
202
|
-
if (value) {
|
|
203
|
-
tinyOrder[field] = value;
|
|
204
|
-
}
|
|
205
|
-
} else {
|
|
206
|
-
tinyOrder[field] = value;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
return tinyOrder;
|
|
212
|
-
};
|
|
@@ -1,318 +0,0 @@
|
|
|
1
|
-
import type { Products, ProductSet } from '@cloudcommerce/types';
|
|
2
|
-
import logger from 'firebase-functions/logger';
|
|
3
|
-
import axios from 'axios';
|
|
4
|
-
import FormData from 'form-data';
|
|
5
|
-
import ecomUtils from '@ecomplus/utils';
|
|
6
|
-
import getEnv from '@cloudcommerce/firebase/lib/env';
|
|
7
|
-
|
|
8
|
-
const removeAccents = (str: string) => str.replace(/áàãâÁÀÃÂ/g, 'a')
|
|
9
|
-
.replace(/éêÉÊ/g, 'e')
|
|
10
|
-
.replace(/óõôÓÕÔ/g, 'o')
|
|
11
|
-
.replace(/íÍ/g, 'e')
|
|
12
|
-
.replace(/úÚ/g, 'u')
|
|
13
|
-
.replace(/çÇ/g, 'c');
|
|
14
|
-
|
|
15
|
-
const tryImageUpload = (
|
|
16
|
-
originImgUrl: string,
|
|
17
|
-
product: Products,
|
|
18
|
-
index?: number,
|
|
19
|
-
) => new Promise((resolve) => {
|
|
20
|
-
const {
|
|
21
|
-
storeId,
|
|
22
|
-
apiAuth: {
|
|
23
|
-
authenticationId,
|
|
24
|
-
apiKey,
|
|
25
|
-
},
|
|
26
|
-
} = getEnv();
|
|
27
|
-
axios.get(originImgUrl, {
|
|
28
|
-
responseType: 'arraybuffer',
|
|
29
|
-
}).then(({ data }) => {
|
|
30
|
-
const form = new FormData();
|
|
31
|
-
form.append('file', Buffer.from(data), originImgUrl.replace(/.*\/([^/]+)$/, '$1'));
|
|
32
|
-
|
|
33
|
-
return axios.post(`https://apx-storage.e-com.plus/${storeId}/api/v1/upload.json`, form, {
|
|
34
|
-
headers: {
|
|
35
|
-
...form.getHeaders(),
|
|
36
|
-
'X-Store-ID': storeId,
|
|
37
|
-
'X-My-ID': authenticationId,
|
|
38
|
-
'X-API-Key': apiKey,
|
|
39
|
-
},
|
|
40
|
-
}).then(({ data: { picture }, status }) => {
|
|
41
|
-
if (picture) {
|
|
42
|
-
Object.keys(picture).forEach((imgSize) => {
|
|
43
|
-
if (picture[imgSize]) {
|
|
44
|
-
if (!picture[imgSize].url) {
|
|
45
|
-
delete picture[imgSize];
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (picture[imgSize].size !== undefined) {
|
|
49
|
-
delete picture[imgSize].size;
|
|
50
|
-
}
|
|
51
|
-
picture[imgSize].alt = `${product.name} (${imgSize})`;
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
if (Object.keys(picture).length) {
|
|
55
|
-
return resolve({
|
|
56
|
-
_id: ecomUtils.randomObjectId(),
|
|
57
|
-
...picture,
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
const err: any = new Error('Unexpected Storage API response');
|
|
62
|
-
err.response = { data, status };
|
|
63
|
-
throw err;
|
|
64
|
-
});
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
.catch((err) => {
|
|
68
|
-
logger.error(err);
|
|
69
|
-
resolve({
|
|
70
|
-
_id: ecomUtils.randomObjectId(),
|
|
71
|
-
normal: {
|
|
72
|
-
url: originImgUrl,
|
|
73
|
-
alt: product.name,
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
}).then((picture) => {
|
|
78
|
-
if (product && product.pictures) {
|
|
79
|
-
if (index === 0 || index) {
|
|
80
|
-
// @ts-ignore
|
|
81
|
-
product.pictures[index] = picture;
|
|
82
|
-
} else {
|
|
83
|
-
// @ts-ignore
|
|
84
|
-
product.pictures.push(picture);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return picture;
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
export default (
|
|
91
|
-
tinyProduct,
|
|
92
|
-
tipo?:string,
|
|
93
|
-
isNew = true,
|
|
94
|
-
): Promise<ProductSet> => new Promise((resolve) => {
|
|
95
|
-
const sku = tinyProduct.codigo || String(tinyProduct.id);
|
|
96
|
-
const name = (tinyProduct.nome || sku).trim();
|
|
97
|
-
const isProduct = tipo === 'produto';
|
|
98
|
-
const getCorrectPrice = (price) => {
|
|
99
|
-
return Number(price) > 0 ? Number(price) : null;
|
|
100
|
-
};
|
|
101
|
-
const product: ProductSet = {
|
|
102
|
-
available: tinyProduct.situacao === 'A',
|
|
103
|
-
sku,
|
|
104
|
-
name,
|
|
105
|
-
cost_price: !isProduct ? Number(tinyProduct.preco_custo) : Number(tinyProduct.precoCusto),
|
|
106
|
-
price: !isProduct ? Number(tinyProduct.preco_promocional || tinyProduct.preco)
|
|
107
|
-
: Number(getCorrectPrice(tinyProduct.precoPromocional) || tinyProduct.preco),
|
|
108
|
-
base_price: Number(tinyProduct.preco),
|
|
109
|
-
body_html: tinyProduct.descricao_complementar || tinyProduct.descricaoComplementar,
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
if (isNew) {
|
|
113
|
-
if (tinyProduct.seo) {
|
|
114
|
-
if (tinyProduct.seo.slug && tinyProduct.seo.slug.length) {
|
|
115
|
-
product.slug = tinyProduct.seo.slug;
|
|
116
|
-
}
|
|
117
|
-
if (tinyProduct.seo.title && tinyProduct.seo.title.length) {
|
|
118
|
-
product.meta_title = tinyProduct.seo.title.slice(0, 254);
|
|
119
|
-
}
|
|
120
|
-
if (tinyProduct.seo.description && tinyProduct.seo.description.length) {
|
|
121
|
-
product.meta_description = tinyProduct.seo.description.slice(0, 999);
|
|
122
|
-
}
|
|
123
|
-
if (tinyProduct.seo.keywords && tinyProduct.seo.keywords.length) {
|
|
124
|
-
product.keywords = tinyProduct.seo.keywords.split(',');
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
product.slug = removeAccents(name.toLowerCase())
|
|
129
|
-
.replace(/\s+/g, '-')
|
|
130
|
-
.replace(/[^a-z0-9-_./]/g, '');
|
|
131
|
-
if (!/[a-z0-9]/.test(product.slug.charAt(0))) {
|
|
132
|
-
product.slug = `p-${product.slug}`;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (tinyProduct.garantia) {
|
|
136
|
-
product.warranty = tinyProduct.garantia;
|
|
137
|
-
}
|
|
138
|
-
if (tinyProduct.unidade_por_caixa || tinyProduct.unidadePorCaixa) {
|
|
139
|
-
product.min_quantity = !isProduct ? Number(tinyProduct.unidade_por_caixa)
|
|
140
|
-
: Number(tinyProduct.unidadePorCaixa);
|
|
141
|
-
}
|
|
142
|
-
if (tinyProduct.ncm) {
|
|
143
|
-
product.mpn = [tinyProduct.ncm];
|
|
144
|
-
}
|
|
145
|
-
const validateGtin = (gtin) => {
|
|
146
|
-
return typeof gtin === 'string' && /^([0-9]{8}|[0-9]{12,14})$/.test(gtin);
|
|
147
|
-
};
|
|
148
|
-
if (validateGtin(tinyProduct.gtin)) {
|
|
149
|
-
product.gtin = [tinyProduct.gtin];
|
|
150
|
-
if (validateGtin(tinyProduct.gtin_embalagem || tinyProduct.gtinEmbalagem)) {
|
|
151
|
-
product.gtin.push(tinyProduct.gtin_embalagem || tinyProduct.gtinEmbalagem);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const weight = !isProduct ? (tinyProduct.peso_bruto || tinyProduct.peso_liquido)
|
|
156
|
-
: (tinyProduct.pesoBruto || tinyProduct.pesoLiquido);
|
|
157
|
-
|
|
158
|
-
if (weight > 0) {
|
|
159
|
-
product.weight = {
|
|
160
|
-
unit: 'kg',
|
|
161
|
-
value: parseFloat(weight),
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
[
|
|
165
|
-
['largura', 'width'],
|
|
166
|
-
['altura', 'height'],
|
|
167
|
-
['comprimento', 'length'],
|
|
168
|
-
].forEach(([lado, side]) => {
|
|
169
|
-
const dimension = tinyProduct[`${lado}_embalagem`] || tinyProduct[`${lado}Embalagem`];
|
|
170
|
-
if (dimension > 0) {
|
|
171
|
-
if (!product.dimensions) {
|
|
172
|
-
product.dimensions = {};
|
|
173
|
-
}
|
|
174
|
-
product.dimensions[side] = {
|
|
175
|
-
unit: 'cm',
|
|
176
|
-
value: parseFloat(dimension),
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
if (isNew) {
|
|
182
|
-
if (Array.isArray(tinyProduct.variacoes) && tinyProduct.variacoes.length) {
|
|
183
|
-
product.variations = [];
|
|
184
|
-
tinyProduct.variacoes.forEach(({ variacaoObj }) => {
|
|
185
|
-
const variacao = !isProduct ? variacaoObj.variacao : variacaoObj;
|
|
186
|
-
const {
|
|
187
|
-
codigo,
|
|
188
|
-
preco,
|
|
189
|
-
grade,
|
|
190
|
-
estoqueAtual,
|
|
191
|
-
anexos,
|
|
192
|
-
} = variacao;
|
|
193
|
-
const gridIdFormat = (text) => {
|
|
194
|
-
return removeAccents(text.toLowerCase())
|
|
195
|
-
.replace(/\s+/g, '_')
|
|
196
|
-
.replace(/[^a-z0-9_]/g, '')
|
|
197
|
-
.substring(0, 30)
|
|
198
|
-
.padStart(2, 'i');
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
const specifications = {};
|
|
202
|
-
const specTexts: string[] = [];
|
|
203
|
-
|
|
204
|
-
if (grade && typeof grade === 'object') {
|
|
205
|
-
Object.keys(grade).forEach((tipoGrade) => {
|
|
206
|
-
if (grade[tipoGrade]) {
|
|
207
|
-
const gridId = gridIdFormat(tipoGrade);
|
|
208
|
-
const spec = {
|
|
209
|
-
text: grade[tipoGrade],
|
|
210
|
-
};
|
|
211
|
-
specTexts.push(spec.text);
|
|
212
|
-
if (gridId !== 'colors') {
|
|
213
|
-
Object.assign(
|
|
214
|
-
spec,
|
|
215
|
-
{
|
|
216
|
-
value: removeAccents(spec.text.toLowerCase()).substring(0, 100),
|
|
217
|
-
},
|
|
218
|
-
);
|
|
219
|
-
}
|
|
220
|
-
specifications[gridId] = [spec];
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
} else if (Array.isArray(grade)) {
|
|
224
|
-
grade.forEach((gd) => {
|
|
225
|
-
const gridId = gridIdFormat(gd.chave);
|
|
226
|
-
const spec = {
|
|
227
|
-
text: gd.valor,
|
|
228
|
-
};
|
|
229
|
-
specTexts.push(spec.text);
|
|
230
|
-
if (gridId !== 'colors') {
|
|
231
|
-
Object.assign(
|
|
232
|
-
spec,
|
|
233
|
-
{
|
|
234
|
-
value: removeAccents(spec.text.toLowerCase()).substring(0, 100),
|
|
235
|
-
},
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
specifications[gridId] = [spec];
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
let pictureId;
|
|
242
|
-
if (Array.isArray(anexos) && anexos.length
|
|
243
|
-
&& Array.isArray(tinyProduct.anexos) && tinyProduct.anexos.length) {
|
|
244
|
-
pictureId = tinyProduct.anexos.length;
|
|
245
|
-
anexos.forEach((anexo) => {
|
|
246
|
-
tinyProduct.anexos.push(anexo);
|
|
247
|
-
});
|
|
248
|
-
} else if (Array.isArray(tinyProduct.anexos) && tinyProduct.anexos.length) {
|
|
249
|
-
pictureId = 0;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
if (specTexts.length) {
|
|
253
|
-
product.variations?.push({
|
|
254
|
-
_id: ecomUtils.randomObjectId(),
|
|
255
|
-
name: `${name} / ${specTexts.join(' / ')}`.substring(0, 100),
|
|
256
|
-
sku: codigo,
|
|
257
|
-
specifications,
|
|
258
|
-
price: parseFloat(preco || 0),
|
|
259
|
-
quantity: estoqueAtual || 0,
|
|
260
|
-
picture_id: pictureId,
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
if (Array.isArray(tinyProduct.imagens_externas)) {
|
|
267
|
-
product.pictures = [];
|
|
268
|
-
tinyProduct.imagens_externas.forEach((imagemExterna) => {
|
|
269
|
-
if (imagemExterna.imagem_externa) {
|
|
270
|
-
const { url } = imagemExterna.imagem_externa;
|
|
271
|
-
if (url) {
|
|
272
|
-
product.pictures?.push({
|
|
273
|
-
normal: { url },
|
|
274
|
-
_id: ecomUtils.randomObjectId(),
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
if (tinyProduct.anexos) {
|
|
282
|
-
if (!product.pictures) {
|
|
283
|
-
product.pictures = [];
|
|
284
|
-
}
|
|
285
|
-
const promises: Promise<any>[] = [];
|
|
286
|
-
tinyProduct.anexos.forEach((anexo) => {
|
|
287
|
-
let url;
|
|
288
|
-
if (anexo && anexo.anexo) {
|
|
289
|
-
url = anexo.anexo;
|
|
290
|
-
} else if (anexo.url) {
|
|
291
|
-
url = anexo.url;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
if (typeof url === 'string' && url.startsWith('http')) {
|
|
295
|
-
promises.push(tryImageUpload(anexo, product as Products));
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
Promise.all(promises).then((images) => {
|
|
299
|
-
if (Array.isArray(product.variations) && product.variations.length) {
|
|
300
|
-
product.variations.forEach((variation) => {
|
|
301
|
-
if (variation.picture_id) {
|
|
302
|
-
const variationImage = images[variation.picture_id];
|
|
303
|
-
if (variationImage._id) {
|
|
304
|
-
variation.picture_id = variationImage._id;
|
|
305
|
-
} else {
|
|
306
|
-
delete variation.picture_id;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
return resolve(product);
|
|
312
|
-
});
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
resolve(product);
|
|
318
|
-
});
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
import type { Products } from '@cloudcommerce/types';
|
|
2
|
-
import config from '@cloudcommerce/firebase/lib/config';
|
|
3
|
-
import ecomUtils from '@ecomplus/utils';
|
|
4
|
-
|
|
5
|
-
export default async (product: Products, originalTinyProduct, appData) => {
|
|
6
|
-
const { metafields } = config.get();
|
|
7
|
-
const hasVariations = product.variations && product.variations.length;
|
|
8
|
-
let unidade: string = originalTinyProduct?.unidade;
|
|
9
|
-
if (!unidade) {
|
|
10
|
-
if (
|
|
11
|
-
product.measurement
|
|
12
|
-
&& product.measurement.unit !== 'oz'
|
|
13
|
-
&& product.measurement.unit !== 'ct'
|
|
14
|
-
) {
|
|
15
|
-
unidade = product.measurement.unit.substring(0, 3).toUpperCase();
|
|
16
|
-
} else {
|
|
17
|
-
unidade = 'UN';
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
const tinyProduct: Record<string, any> = {
|
|
21
|
-
sequencia: 1,
|
|
22
|
-
origem: 0,
|
|
23
|
-
situacao: product.available && product.visible ? 'A' : 'I',
|
|
24
|
-
tipo: 'P',
|
|
25
|
-
classe_produto: hasVariations ? 'V' : 'S',
|
|
26
|
-
codigo: product.sku,
|
|
27
|
-
nome: ecomUtils.name(product, 'pt_br').substring(0, 120),
|
|
28
|
-
unidade,
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
if (ecomUtils.onPromotion(product)) {
|
|
32
|
-
tinyProduct.preco = product.base_price;
|
|
33
|
-
tinyProduct.preco_promocional = ecomUtils.price(product);
|
|
34
|
-
} else {
|
|
35
|
-
tinyProduct.preco = product.price;
|
|
36
|
-
}
|
|
37
|
-
if (product.cost_price) {
|
|
38
|
-
tinyProduct.preco_custo = product.cost_price;
|
|
39
|
-
}
|
|
40
|
-
if (product.min_quantity) {
|
|
41
|
-
tinyProduct.unidade_por_caixa = product.min_quantity < 1000
|
|
42
|
-
? String(product.min_quantity) : '999';
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (product.short_description) {
|
|
46
|
-
tinyProduct.descricao_complementar = product.short_description;
|
|
47
|
-
}
|
|
48
|
-
if (product.warranty) {
|
|
49
|
-
tinyProduct.garantia = product.warranty.substring(0, 20);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (product.mpn && product.mpn.length) {
|
|
53
|
-
[tinyProduct.ncm] = product.mpn;
|
|
54
|
-
}
|
|
55
|
-
if (product.gtin && product.gtin.length) {
|
|
56
|
-
[tinyProduct.gtin] = product.gtin;
|
|
57
|
-
if (product.gtin[1]) {
|
|
58
|
-
// eslint-disable-next-line prefer-destructuring
|
|
59
|
-
tinyProduct.gtin_embalagem = product.gtin[1];
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (product.weight && product.weight.value) {
|
|
64
|
-
tinyProduct.peso_bruto = product.weight.value;
|
|
65
|
-
tinyProduct.peso_liquido = product.weight.value;
|
|
66
|
-
|
|
67
|
-
if (metafields.tinyErpProductWeigthUnit) {
|
|
68
|
-
product.weight.unit = metafields.tinyErpProductWeigthUnit;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (product.weight.unit === 'mg') {
|
|
72
|
-
tinyProduct.peso_bruto /= 1000000;
|
|
73
|
-
tinyProduct.peso_liquido /= 1000000;
|
|
74
|
-
} else if (product.weight.unit === 'g') {
|
|
75
|
-
tinyProduct.peso_bruto /= 1000;
|
|
76
|
-
tinyProduct.peso_liquido /= 1000;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
const { dimensions } = product;
|
|
80
|
-
if (dimensions) {
|
|
81
|
-
Object.keys(dimensions).forEach((side) => {
|
|
82
|
-
if (dimensions[side] && dimensions[side].value) {
|
|
83
|
-
let field: string;
|
|
84
|
-
if (side === 'width') {
|
|
85
|
-
field = 'largura';
|
|
86
|
-
} else {
|
|
87
|
-
field = side === 'height' ? 'altura' : 'comprimento';
|
|
88
|
-
}
|
|
89
|
-
field += '_embalagem';
|
|
90
|
-
tinyProduct[field] = dimensions[side].value;
|
|
91
|
-
if (dimensions[side].unit === 'mm') {
|
|
92
|
-
tinyProduct[field] *= 0.1;
|
|
93
|
-
} else if (dimensions[side].unit === 'm') {
|
|
94
|
-
tinyProduct[field] *= 100;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (product.brands && product.brands.length) {
|
|
101
|
-
tinyProduct.marca = product.brands[0].name;
|
|
102
|
-
}
|
|
103
|
-
if (product.category_tree) {
|
|
104
|
-
tinyProduct.categoria = product.category_tree.replace(/\s?>\s?/g, ' >> ');
|
|
105
|
-
} else if (product.categories && product.categories.length) {
|
|
106
|
-
tinyProduct.categoria = product.categories.map(({ name }) => name).join(' >> ');
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (product.pictures && product.pictures.length) {
|
|
110
|
-
tinyProduct.anexos = [];
|
|
111
|
-
product.pictures.forEach(({ zoom, big, normal }) => {
|
|
112
|
-
const img = (zoom || big || normal) as { url: string };
|
|
113
|
-
if (img) {
|
|
114
|
-
tinyProduct.anexos.push({
|
|
115
|
-
anexo: img.url,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (originalTinyProduct) {
|
|
122
|
-
tinyProduct.id = originalTinyProduct.id;
|
|
123
|
-
if (!appData.update_price) {
|
|
124
|
-
['preco', 'preco_promocional', 'preco_custo'].forEach((field) => {
|
|
125
|
-
if (typeof originalTinyProduct[field] === 'number') {
|
|
126
|
-
tinyProduct[field] = originalTinyProduct[field];
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
} else {
|
|
131
|
-
tinyProduct.estoque_atual = product.quantity || 0;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (hasVariations) {
|
|
135
|
-
tinyProduct.variacoes = [];
|
|
136
|
-
product.variations?.forEach((variation, i) => {
|
|
137
|
-
const tinyVariation: Record<string, any> = {
|
|
138
|
-
codigo: variation.sku || `${product.sku}-${(i + 1)}`,
|
|
139
|
-
grade: {},
|
|
140
|
-
};
|
|
141
|
-
if (!originalTinyProduct) {
|
|
142
|
-
tinyVariation.estoque_atual = variation.quantity || 0;
|
|
143
|
-
}
|
|
144
|
-
Object.keys(variation.specifications).forEach((gridId) => {
|
|
145
|
-
const gridOptions = variation.specifications[gridId];
|
|
146
|
-
if (gridOptions && gridOptions.length) {
|
|
147
|
-
gridOptions.forEach(({ text }, ii) => {
|
|
148
|
-
tinyVariation.grade[ii === 0 ? gridId : `${gridId}_${(ii + 1)}`] = text;
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
tinyProduct.variacoes.push({
|
|
153
|
-
variacao: tinyVariation,
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
return tinyProduct;
|
|
158
|
-
};
|