Skip to content

Commit

Permalink
Use an upsert instead of a simple insert for login tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmcorvidae committed Dec 13, 2024
1 parent 6983233 commit cf02cc4
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 cf02cc4

Please sign in to comment.