2020-10-10 20:04:07 +02:00
|
|
|
import { useAuth } from 'lib/middleware';
|
|
|
|
import { ok, methodNotAllowed, badRequest } from 'lib/response';
|
|
|
|
import { getRealtimeData } from 'lib/queries';
|
|
|
|
import { parseToken } from 'lib/crypto';
|
2020-10-11 11:29:55 +02:00
|
|
|
import { TOKEN_HEADER } from 'lib/constants';
|
2020-10-10 20:04:07 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
await useAuth(req, res);
|
|
|
|
|
|
|
|
if (req.method === 'GET') {
|
|
|
|
const { start_at } = req.query;
|
|
|
|
|
2020-10-11 11:29:55 +02:00
|
|
|
const token = req.headers[TOKEN_HEADER];
|
2020-10-10 20:04:07 +02:00
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
return badRequest(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { websites } = await parseToken(token);
|
|
|
|
|
|
|
|
const data = await getRealtimeData(websites, new Date(+start_at));
|
|
|
|
|
|
|
|
return ok(res, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|