Skip to content

Commit

Permalink
Merge pull request #278 from ianmcorvidae/dedup-logins
Browse files Browse the repository at this point in the history
Use an upsert instead of a simple insert for login tracking
  • Loading branch information
ianmcorvidae authored Dec 13, 2024
2 parents 6983233 + cf02cc4 commit 7c58157
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/apps/persistence/users.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

0 comments on commit 7c58157

Please sign in to comment.