2020-08-12 07:24:41 +02:00
|
|
|
import { getRankings } from 'lib/queries';
|
2020-08-08 02:19:42 +02:00
|
|
|
import { ok, badRequest } from 'lib/response';
|
2020-08-01 04:05:14 +02:00
|
|
|
|
2020-08-07 04:37:19 +02:00
|
|
|
const sessionColumns = ['browser', 'os', 'device', 'country'];
|
2020-08-01 04:05:14 +02:00
|
|
|
const pageviewColumns = ['url', 'referrer'];
|
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
const { id, type, start_at, end_at } = req.query;
|
|
|
|
|
|
|
|
if (!sessionColumns.includes(type) && !pageviewColumns.includes(type)) {
|
2020-08-08 02:19:42 +02:00
|
|
|
return badRequest(res);
|
2020-08-01 04:05:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const table = sessionColumns.includes(type) ? 'session' : 'pageview';
|
|
|
|
|
|
|
|
const rankings = await getRankings(+id, new Date(+start_at), new Date(+end_at), type, table);
|
|
|
|
|
2020-08-08 02:19:42 +02:00
|
|
|
return ok(res, rankings);
|
2020-08-01 04:05:14 +02:00
|
|
|
};
|