feat: log stock changes to local csv file for analysis
All checks were successful
Build and Deploy / build-and-push (push) Successful in 47s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 47s
This commit is contained in:
@@ -179,6 +179,7 @@ export const handleTinyStockUpdate = async (req: Request, res: Response): Promis
|
||||
|
||||
const dados = payload.dados || {};
|
||||
const idProduto = String(dados.idProduto || dados.id || "");
|
||||
const nomeProduto = String(dados.nome || dados.descricao || "Unknown");
|
||||
const novoSaldo = Number(dados.saldo || 0);
|
||||
|
||||
let deltaEstoque = 0;
|
||||
@@ -199,6 +200,21 @@ export const handleTinyStockUpdate = async (req: Request, res: Response): Promis
|
||||
fs.writeFileSync(memoryFile, JSON.stringify(memory, null, 2));
|
||||
|
||||
console.log(`[Stock Tracker] Product ${idProduto}: Old=${estoqueAntigo ?? 'None'} -> New=${novoSaldo}. Delta=${deltaEstoque > 0 ? '+' : ''}${deltaEstoque}`);
|
||||
|
||||
// --- CSV LOGGING FOR ANALYSIS ---
|
||||
if (deltaEstoque !== 0 || estoqueAntigo === undefined) {
|
||||
const csvFile = path.join(process.cwd(), 'stock_log.csv');
|
||||
const timestamp = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
||||
const oldStockStr = estoqueAntigo !== undefined ? estoqueAntigo : 'NEW';
|
||||
const csvLine = `"${timestamp}","${idProduto}","${nomeProduto}","${oldStockStr}","${novoSaldo}","${deltaEstoque > 0 ? '+' : ''}${deltaEstoque}"\n`;
|
||||
|
||||
// Create file with headers if it doesn't exist
|
||||
if (!fs.existsSync(csvFile)) {
|
||||
fs.writeFileSync(csvFile, '"Data","ID_Produto","Nome_Produto","Estoque_Antigo","Estoque_Novo","Quantidade_Adicionada"\n');
|
||||
}
|
||||
|
||||
fs.appendFileSync(csvFile, csvLine);
|
||||
}
|
||||
}
|
||||
|
||||
// Inject the delta into the payload so n8n can easily read it
|
||||
|
||||
Reference in New Issue
Block a user