From d89e2c11b155b906e3f7a7af94b3ce758a7c2b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Mon, 11 May 2026 11:56:10 -0300 Subject: [PATCH] feat: add endpoint to download stock logs csv directly from browser --- src/index.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index bdef486..0291848 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,7 +31,7 @@ app.get('/api/trigger-backfill', (req: Request, res: Response) => { // Spawn as a detached background process so it doesn't block the web server const child = spawn('node', [scriptPath], { detached: true, - stdio: 'ignore' + stdio: 'inherit' // Inherit allows the logs to show up in Portainer's log viewer! }); child.unref(); @@ -40,6 +40,23 @@ app.get('/api/trigger-backfill', (req: Request, res: Response) => { res.status(200).json({ status: 'STARTED', message: 'Backfill script is now running quietly in the background.' }); }); +// Hidden endpoint to download the stock CSV log directly from the browser +app.get('/api/stock-logs/download', (req: Request, res: Response) => { + const expectedToken = process.env.TINY_WEBHOOK_SECRET; + if (expectedToken && req.query.token !== expectedToken) { + res.status(401).json({ error: 'Unauthorized' }); + return; + } + + const csvFile = path.join(process.cwd(), 'stock_log.csv'); + const fs = require('fs'); + if (fs.existsSync(csvFile)) { + res.download(csvFile, 'estoque_log.csv'); + } else { + res.status(404).send('

No logs found yet.

Wait for the first stock change to happen in Tiny ERP!

'); + } +}); + app.listen(port, () => { console.log(`[server]: Middleware server is running at http://localhost:${port}`); });