2020-07-19 10:57:01 +02:00
|
|
|
import { savePageView, saveEvent } from 'lib/db';
|
2020-07-28 08:52:14 +02:00
|
|
|
import { useCors, useSession } from 'lib/middleware';
|
2020-07-23 05:45:09 +02:00
|
|
|
import { createToken } from 'lib/crypto';
|
2020-08-08 02:19:42 +02:00
|
|
|
import { ok, badRequest } from 'lib/response';
|
2020-07-17 10:03:38 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2020-07-26 01:31:07 +02:00
|
|
|
await useCors(req, res);
|
2020-07-28 08:52:14 +02:00
|
|
|
await useSession(req, res);
|
2020-07-18 19:36:46 +02:00
|
|
|
|
2020-07-28 08:52:14 +02:00
|
|
|
const { session } = req;
|
2020-07-24 04:56:55 +02:00
|
|
|
const token = await createToken(session);
|
2020-07-23 05:45:09 +02:00
|
|
|
const { website_id, session_id } = session;
|
2020-07-20 10:54:21 +02:00
|
|
|
const { type, payload } = req.body;
|
2020-07-17 10:03:38 +02:00
|
|
|
|
2020-07-20 10:54:21 +02:00
|
|
|
if (type === 'pageview') {
|
|
|
|
const { url, referrer } = payload;
|
2020-07-24 04:56:55 +02:00
|
|
|
|
|
|
|
await savePageView(website_id, session_id, url, referrer);
|
2020-07-20 10:54:21 +02:00
|
|
|
} else if (type === 'event') {
|
|
|
|
const { url, event_type, event_value } = payload;
|
2020-07-17 10:03:38 +02:00
|
|
|
|
2020-07-24 04:56:55 +02:00
|
|
|
await saveEvent(website_id, session_id, url, event_type, event_value);
|
2020-08-08 02:19:42 +02:00
|
|
|
} else {
|
|
|
|
return badRequest(res);
|
2020-07-24 04:56:55 +02:00
|
|
|
}
|
2020-07-23 05:45:09 +02:00
|
|
|
|
2020-08-08 02:19:42 +02:00
|
|
|
return ok(res, { session: token });
|
2020-07-17 10:03:38 +02:00
|
|
|
};
|