63 lines
1.9 KiB
JavaScript
63 lines
1.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
|
|
});
|
|
|
|
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 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)
|
|
];
|
|
};
|
|
|
|
module.exports = {
|
|
formatOrderRow,
|
|
normalizeOrderDate,
|
|
normalizeOrderPayload
|
|
};
|