feat: sync Tiny orders directly to Graphs
All checks were successful
Build and Deploy / build-and-push (push) Successful in 59s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 59s
This commit is contained in:
50
src/index.ts
50
src/index.ts
@@ -40,6 +40,56 @@ 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 trigger the Graphs backfill script without terminal access
|
||||
app.get('/api/trigger-graphs-backfill', (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 scriptPath = path.join(__dirname, 'scripts', 'backfill-graphs.js');
|
||||
const env = { ...process.env };
|
||||
|
||||
const optionalEnvMap: Record<string, string> = {
|
||||
start: 'GRAPHS_BACKFILL_START_DATE',
|
||||
end: 'GRAPHS_BACKFILL_END_DATE',
|
||||
dryRun: 'GRAPHS_BACKFILL_DRY_RUN',
|
||||
maxOrders: 'GRAPHS_BACKFILL_MAX_ORDERS',
|
||||
maxDays: 'GRAPHS_BACKFILL_MAX_DAYS',
|
||||
contactFallback: 'GRAPHS_BACKFILL_CONTACT_FALLBACK'
|
||||
};
|
||||
|
||||
for (const [queryKey, envKey] of Object.entries(optionalEnvMap)) {
|
||||
const value = req.query[queryKey];
|
||||
if (typeof value === 'string' && value.trim() !== '') {
|
||||
env[envKey] = value.trim();
|
||||
}
|
||||
}
|
||||
|
||||
const child = spawn('node', [scriptPath], {
|
||||
detached: true,
|
||||
stdio: 'inherit',
|
||||
env
|
||||
});
|
||||
|
||||
child.unref();
|
||||
|
||||
console.log('[server]: Graphs backfill script manually triggered via HTTP endpoint.');
|
||||
res.status(200).json({
|
||||
status: 'STARTED',
|
||||
message: 'Graphs backfill script is running in the background. Check Portainer logs for progress.',
|
||||
options: {
|
||||
start: env.GRAPHS_BACKFILL_START_DATE || null,
|
||||
end: env.GRAPHS_BACKFILL_END_DATE || null,
|
||||
dryRun: env.GRAPHS_BACKFILL_DRY_RUN || 'false',
|
||||
maxOrders: env.GRAPHS_BACKFILL_MAX_ORDERS || null,
|
||||
maxDays: env.GRAPHS_BACKFILL_MAX_DAYS || null,
|
||||
contactFallback: env.GRAPHS_BACKFILL_CONTACT_FALLBACK || 'true'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user