| 1 | import type { IncomingMessage, ServerResponse } from 'node:http';
|
| 2 | import { HostedRepository } from '../packages/persistence/src/hosted.js';
|
| 3 | import { assertProductionConfiguration } from './production-config.js';
|
| 4 |
|
| 5 | assertProductionConfiguration('cron');
|
| 6 |
|
| 7 | export default async function handler(request: IncomingMessage, response: ServerResponse) {
|
| 8 | const secret = process.env.CRON_SECRET;
|
| 9 | if (!secret) {
|
| 10 | response.statusCode = 503;
|
| 11 | response.end(JSON.stringify({ error: 'cron_not_configured' }));
|
| 12 | return;
|
| 13 | }
|
| 14 | if (request.headers.authorization !== `Bearer ${secret}`) {
|
| 15 | response.statusCode = 401;
|
| 16 | response.end(JSON.stringify({ error: 'unauthorized' }));
|
| 17 | return;
|
| 18 | }
|
| 19 | const databaseUrl = process.env.DATABASE_URL;
|
| 20 | if (!databaseUrl) {
|
| 21 | response.statusCode = 503;
|
| 22 | response.end(JSON.stringify({ error: 'database_not_configured' }));
|
| 23 | return;
|
| 24 | }
|
| 25 | const result = await new HostedRepository(databaseUrl).processDueFeedback();
|
| 26 | response.statusCode = 200;
|
| 27 | response.setHeader('content-type', 'application/json');
|
| 28 | response.end(JSON.stringify({ ok: true, ...result }));
|
| 29 | }
|