This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.ts
45 lines (40 loc) · 1.48 KB
/
run.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import type { NextApiRequest, NextApiResponse } from 'next';
import { timingSafeEqual } from 'node:crypto';
import createHttpError from 'http-errors';
import { handleInstallation } from '../../../lib/api/runner/core';
import db, {
installationsTable, meetingsTable,
} from '../../../lib/api/db';
import env from '../../../lib/api/env';
import { apiRoute } from '../../../lib/api/apiRoute';
export type RunResponse = {
status: string
};
export default apiRoute(async (
req: NextApiRequest,
res: NextApiResponse<RunResponse>,
) => {
// When removing, also remove security exemption
if (env.SCHEDULER_API_KEY !== 'UNSAFE_ONLY_USE_LOCALLY_NO_AUTH') {
const providedKey = Array.isArray(req.headers['x-api-key'])
? req.headers['x-api-key'].join('')
: req.headers['x-api-key'];
if (!providedKey || !timingSafeEqual(
Buffer.from(env.SCHEDULER_API_KEY),
Buffer.from(providedKey ?? ''),
)) {
throw createHttpError.Unauthorized('Missing or incorrect API key');
}
}
const installations = await db.scan(installationsTable);
const meetings = await db.scan(meetingsTable);
await Promise.all(installations.map(async (installation) => {
try {
await handleInstallation(installation, meetings.filter((m) => m.installationId === installation.id));
} catch (err) {
console.error(`Error handling installation: ${installation.id}`);
console.error(err);
}
}));
res.status(200).json({ status: 'Complete' });
}, 'insecure_no_auth');