feat: add endpoint to download stock logs csv directly from browser
All checks were successful
Build and Deploy / build-and-push (push) Successful in 42s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 42s
This commit is contained in:
19
src/index.ts
19
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('<html><body><h2>No logs found yet.</h2><p>Wait for the first stock change to happen in Tiny ERP!</p></body></html>');
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`[server]: Middleware server is running at http://localhost:${port}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user