From 1a8c7c42f4aadd610ffb9784a301293fcd356c05 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Tue, 8 Nov 2022 17:11:08 -0800 Subject: [PATCH] Updated Clickhouse session logic. --- lib/session.js | 65 +++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/lib/session.js b/lib/session.js index 442de39c..ad9f75b8 100644 --- a/lib/session.js +++ b/lib/session.js @@ -47,38 +47,9 @@ export async function findSession(req) { const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload); const sessionId = uuid(websiteId, hostname, ip, userAgent); - // Find session - let session; - - if (!clickhouse.enabled) { - if (cache.enabled) { - session = await cache.fetchSession(sessionId); - } else { - session = await getSession({ id: sessionId }); - } - - // Create a session if not found - if (!session) { - try { - session = await createSession({ - id: sessionId, - websiteId, - hostname, - browser, - os, - device, - screen, - language, - country, - }); - } catch (e) { - if (!e.message.toLowerCase().includes('unique constraint')) { - throw e; - } - } - } - } else { - session = { + // Clickhouse does not require session lookup + if (clickhouse.enabled) { + return { id: sessionId, websiteId, hostname, @@ -91,5 +62,35 @@ export async function findSession(req) { }; } + // Find session + let session; + + if (cache.enabled) { + session = await cache.fetchSession(sessionId); + } else { + session = await getSession({ id: sessionId }); + } + + // Create a session if not found + if (!session) { + try { + session = await createSession({ + id: sessionId, + websiteId, + hostname, + browser, + os, + device, + screen, + language, + country, + }); + } catch (e) { + if (!e.message.toLowerCase().includes('unique constraint')) { + throw e; + } + } + } + return session; }