86 lines
2.9 KiB
JavaScript
86 lines
2.9 KiB
JavaScript
const formatOrderRow = (row) => ({
|
|
Nome_Cliente: row.cliente_nome,
|
|
Data_Pedido: row.data_pedido,
|
|
Valor_Pedido: parseFloat(row.valor_pedido),
|
|
ID_Produto: row.produto_id,
|
|
Descricao_Produto: row.produto_descricao,
|
|
Quantidade: row.quantidade,
|
|
Valor_Unitario: parseFloat(row.valor_unitario),
|
|
Recebido_Em: row.created_at,
|
|
ID_Pedido: row.pedido_id,
|
|
Fone_Cliente: row.cliente_fone,
|
|
cliente_nome_fantasia: row.cliente_nome_fantasia || '',
|
|
id_vendedor: row.id_vendedor || '',
|
|
nome_vendedor: row.nome_vendedor || '',
|
|
marketplace: row.marketplace || '',
|
|
canal_venda: row.canal_venda || '',
|
|
numero_ecommerce: row.numero_ecommerce || ''
|
|
});
|
|
|
|
const normalizeOrderDate = (dateValue) => {
|
|
if (!dateValue) return null;
|
|
|
|
const value = String(dateValue).trim();
|
|
const match = value.match(/^(\d{1,4})[-/](\d{1,2})[-/](\d{1,4})/);
|
|
if (!match) return null;
|
|
|
|
const [, first, second, third] = match;
|
|
const year = first.length === 4 ? Number(first) : Number(third);
|
|
const month = Number(second);
|
|
const day = first.length === 4 ? Number(third) : Number(first);
|
|
const date = new Date(Date.UTC(year, month - 1, day));
|
|
|
|
if (
|
|
date.getUTCFullYear() !== year ||
|
|
date.getUTCMonth() !== month - 1 ||
|
|
date.getUTCDate() !== day
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
return date.toISOString().slice(0, 10);
|
|
};
|
|
|
|
const pickFirstValue = (item, fieldNames) => {
|
|
for (const fieldName of fieldNames) {
|
|
const value = item[fieldName];
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
return '';
|
|
};
|
|
|
|
const normalizeOrderPayload = (item) => {
|
|
const fallbackId = `${item.Nome_Cliente}_${item.Data_Pedido}_${item.Valor_Pedido}`;
|
|
const orderId = item.id || item.ID_Pedido || (item.json && item.json.body && item.json.body.id) || fallbackId;
|
|
const fone = item.Fone_Cliente || item.fone || item.celular || '';
|
|
const orderDate = item.Data_Pedido || '';
|
|
|
|
return [
|
|
item.Nome_Cliente || 'Unknown',
|
|
orderDate,
|
|
normalizeOrderDate(orderDate),
|
|
parseFloat(item.Valor_Pedido) || 0,
|
|
item.ID_Produto || '',
|
|
item.Descricao_Produto || '',
|
|
parseInt(item.Quantidade, 10) || 0,
|
|
parseFloat(item.Valor_Unitario) || 0,
|
|
String(orderId),
|
|
String(fone),
|
|
String(pickFirstValue(item, ['nome_fantasia', 'Nome_Fantasia', 'Cliente_Nome_Fantasia', 'cliente_nome_fantasia'])),
|
|
String(pickFirstValue(item, ['id_vendedor', 'ID_Vendedor'])),
|
|
String(pickFirstValue(item, ['nome_vendedor', 'Nome_Vendedor'])),
|
|
String(pickFirstValue(item, ['marketplace', 'Marketplace', 'nome_ecommerce', 'Nome_Ecommerce'])),
|
|
String(pickFirstValue(item, ['canal_venda', 'Canal_Venda'])),
|
|
String(pickFirstValue(item, ['numero_ecommerce', 'Numero_Ecommerce']))
|
|
];
|
|
};
|
|
|
|
module.exports = {
|
|
formatOrderRow,
|
|
normalizeOrderDate,
|
|
normalizeOrderPayload
|
|
};
|