2022-08-29 05:20:54 +02:00
|
|
|
import { ok, methodNotAllowed, badRequest, parseToken } from 'next-basics';
|
2020-10-10 20:04:07 +02:00
|
|
|
import { useAuth } from 'lib/middleware';
|
2022-07-12 23:14:36 +02:00
|
|
|
import { getRealtimeData } from 'queries';
|
2022-05-06 04:04:28 +02:00
|
|
|
import { SHARE_TOKEN_HEADER } from 'lib/constants';
|
2022-08-29 05:20:54 +02:00
|
|
|
import { secret } from 'lib/crypto';
|
2022-11-15 22:21:14 +01:00
|
|
|
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
|
|
|
import { NextApiResponse } from 'next';
|
|
|
|
import { RealtimeUpdate } from 'interface/api/models';
|
|
|
|
|
|
|
|
export interface InitUpdateRequestQuery {
|
|
|
|
start_at: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async (
|
|
|
|
req: NextApiRequestQueryBody<InitUpdateRequestQuery>,
|
|
|
|
res: NextApiResponse<RealtimeUpdate>,
|
|
|
|
) => {
|
2020-10-10 20:04:07 +02:00
|
|
|
await useAuth(req, res);
|
|
|
|
|
|
|
|
if (req.method === 'GET') {
|
|
|
|
const { start_at } = req.query;
|
|
|
|
|
2022-05-06 04:04:28 +02:00
|
|
|
const token = req.headers[SHARE_TOKEN_HEADER];
|
2020-10-10 20:04:07 +02:00
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
return badRequest(res);
|
|
|
|
}
|
|
|
|
|
2022-08-29 05:20:54 +02:00
|
|
|
const { websites } = parseToken(token, secret());
|
2020-10-10 20:04:07 +02:00
|
|
|
|
|
|
|
const data = await getRealtimeData(websites, new Date(+start_at));
|
|
|
|
|
|
|
|
return ok(res, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|