From cf02cc4bc1124b505044f42f810dcdfa31f79208 Mon Sep 17 00:00:00 2001 From: Ian McEwen Date: Fri, 13 Dec 2024 09:40:18 -0700 Subject: [PATCH] Use an upsert instead of a simple insert for login tracking --- src/apps/persistence/users.clj | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/apps/persistence/users.clj b/src/apps/persistence/users.clj index 5e534da1..cbf077e7 100644 --- a/src/apps/persistence/users.clj +++ b/src/apps/persistence/users.clj @@ -54,9 +54,25 @@ :session_id (uuidify session-id) :login_time (long->timestamp login-time)})))) +(defn- get-login-record + "Gets an existing login record that matches all fields" + [user-id ip-address session-id login-time] + (first + (select :logins + (where {:user_id user-id + :ip_address ip-address + :session_id (uuidify session-id) + :login_time (long->timestamp login-time)})))) + +(defn- upsert-login-record + "Records a user login, if at least one field differs from an existing entry." + [user-id ip-address session-id login-time] + (or (get-login-record user-id ip-address session-id login-time) + (insert-login-record user-id ip-address session-id login-time))) + (defn record-login "Records when a user logs into the DE. Returns the recorded login time." [username ip-address session-id login-time] - (-> (insert-login-record (get-user-id username) ip-address session-id login-time) + (-> (upsert-login-record (get-user-id username) ip-address session-id login-time) (:login_time) (.getTime)))