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}`); });