Revert "remove postgres collect"

This reverts commit 86bd9a7793.
This commit is contained in:
Brian Cao 2022-10-06 13:13:44 -07:00
parent 0e10470c02
commit 6bb61d7a53

View File

@ -1,13 +1,14 @@
import { parseToken } from 'next-basics';
import { validate } from 'uuid';
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import redis, { DELETED } from 'lib/redis'; import redis, { DELETED } from 'lib/redis';
import { getClientInfo, getJsonBody } from 'lib/request'; import { getClientInfo, getJsonBody } from 'lib/request';
import { parseToken } from 'next-basics'; import { createSession, getSessionByUuid, getWebsiteByUuid } from 'queries';
import { getWebsiteByUuid } from 'queries';
import { validate } from 'uuid';
export async function getSession(req) { export async function getSession(req) {
const { payload } = getJsonBody(req); const { payload } = getJsonBody(req);
const hasRedis = process.env.REDIS_URL; const hasRedis = process.env.REDIS_URL;
const hasClickhouse = process.env.CLICKHOUSE_URL;
if (!payload) { if (!payload) {
throw new Error('Invalid request'); throw new Error('Invalid request');
@ -52,17 +53,49 @@ export async function getSession(req) {
let sessionId = null; let sessionId = null;
let session = null; let session = null;
session = { if (!hasClickhouse) {
session_id: sessionId, // Check if session exists
session_uuid, if (hasRedis) {
hostname, sessionId = Number(await redis.client.get(`session:${session_uuid}`));
browser, }
os,
screen, // Check database if does not exists in Redis
language, if (!sessionId) {
country, session = await getSessionByUuid(session_uuid);
device, sessionId = session ? session.session_id : null;
}; }
if (!sessionId) {
try {
session = await createSession(websiteId, {
session_uuid,
hostname,
browser,
os,
screen,
language,
country,
device,
});
} catch (e) {
if (!e.message.toLowerCase().includes('unique constraint')) {
throw e;
}
}
}
} else {
session = {
session_id: sessionId,
session_uuid,
hostname,
browser,
os,
screen,
language,
country,
device,
};
}
return { return {
website_id: websiteId, website_id: websiteId,