Refactored session and collect process.

This commit is contained in:
Mike Cao 2020-07-20 01:54:21 -07:00
parent e58aa9e90f
commit 132bbcbe0d
7 changed files with 192 additions and 252 deletions

View file

@ -13,37 +13,39 @@ export async function runQuery(query) {
});
}
export async function getWebsite(website_id) {
export async function getWebsite(website_uuid) {
return runQuery(
prisma.website.findOne({
where: {
website_uuid: website_id,
website_uuid,
},
}),
);
}
export async function createSession(website_id, session_id, data) {
await runQuery(
export async function createSession(website_id, data) {
return runQuery(
prisma.session.create({
data: {
session_uuid: session_id,
website: {
connect: {
website_uuid: website_id,
website_id,
},
},
...data,
},
select: {
session_id: true,
},
}),
);
}
export async function getSession(session_id) {
export async function getSession(session_uuid) {
return runQuery(
prisma.session.findOne({
where: {
session_uuid: session_id,
session_uuid,
},
}),
);
@ -55,12 +57,12 @@ export async function savePageView(website_id, session_id, url, referrer) {
data: {
website: {
connect: {
website_uuid: website_id,
website_id,
},
},
session: {
connect: {
session_uuid: session_id,
session_id,
},
},
url,
@ -76,12 +78,12 @@ export async function saveEvent(website_id, session_id, url, event_type, event_v
data: {
website: {
connect: {
website_uuid: website_id,
website_id,
},
},
session: {
connect: {
session_uuid: session_id,
session_id,
},
},
url,

49
lib/session.js Normal file
View file

@ -0,0 +1,49 @@
import { getWebsite, getSession, createSession } from 'lib/db';
import { getCountry, getDevice, getIpAddress, hash, isValidSession } from 'lib/utils';
export default async function checkSession(req) {
const { payload } = req.body;
const { session } = payload;
if (isValidSession(session)) {
return session;
}
const ip = getIpAddress(req);
const { userAgent, browser, os } = getDevice(req);
const country = await getCountry(req, ip);
const { website: website_uuid, hostname, screen, language } = payload;
if (website_uuid) {
const website = await getWebsite(website_uuid);
if (website) {
const { website_id } = website;
const session_uuid = hash(`${website_id}${hostname}${ip}${userAgent}${os}`);
let session = await getSession(session_uuid);
if (!session) {
session = await createSession(website_id, {
session_uuid,
hostname,
browser,
os,
screen,
language,
country,
});
}
const { session_id } = session;
return [
website_id,
website_uuid,
session_id,
session_uuid,
hash(website_id, website_uuid, session_id, session_uuid),
].join(':');
}
}
}

View file

@ -12,8 +12,8 @@ export function md5(s) {
return crypto.createHash('md5').update(s).digest('hex');
}
export function hash(s) {
return uuid(s, md5(process.env.HASH_SALT));
export function hash(...args) {
return uuid(args.join(''), md5(process.env.HASH_SALT));
}
export function validHash(s) {
@ -60,61 +60,19 @@ export async function getCountry(req, ip) {
return result.country.iso_code;
}
export async function parseSessionRequest(req) {
if (req.method === 'POST') {
const ip = getIpAddress(req);
const { website_id, hostname, screen, language } = req.body;
const { userAgent, browser, os } = getDevice(req);
const country = await getCountry(req, ip);
const session_id = hash(`${website_id}${hostname}${ip}${userAgent}${os}`);
return {
website_id,
session_id,
hostname,
browser,
os,
screen,
language,
country,
};
}
return {};
export function parseSession(session) {
const [website_id, website_uuid, session_id, session_uuid, sig] = (session || '').split(':');
return {
website_id: parseInt(website_id),
website_uuid,
session_id: parseInt(session_id),
session_uuid,
sig,
};
}
export function parseCollectRequest(req) {
if (req.method === 'POST') {
const { type, payload } = req.body;
export function isValidSession(session) {
const { website_id, website_uuid, session_id, session_uuid, sig } = parseSession(session);
if (payload.session) {
const {
url,
referrer,
event_type,
event_value,
session: { website_id, session_id, time, hash: validationHash },
} = payload;
if (
validHash(website_id) &&
validHash(session_id) &&
validHash(validationHash) &&
hash(`${website_id}${session_id}${time}`) === validationHash
) {
return {
success: 1,
type,
website_id,
session_id,
url,
referrer,
event_type,
event_value,
};
}
}
}
return { success: 0 };
return hash(website_id, website_uuid, session_id, session_uuid) === sig;
}