Skip to content

Commit

Permalink
Creating an empty user if there is no data (user is not a student) an…
Browse files Browse the repository at this point in the history
…d making a check to see if the user should be updated with new data
  • Loading branch information
benrencher committed Oct 16, 2024
1 parent 9c483ed commit 8abce06
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
23 changes: 16 additions & 7 deletions src/clj/y_video_back/apis/persons.clj
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,22 @@
(def walk-result (walk/keywordize-keys json)) ;; remember to include walk library
(def data_array (walk-result :data))
(def data (first data_array))
{
:full-name (data :preferred_name)
:byu-id (data :byu_id)
:email (data :student_email_address)
:account-type 3
:person-id personid
}
(if-not (= nil data)
{
:full-name (data :preferred_name)
:byu-id (data :byu_id)
:email (data :student_email_address)
:account-type 3
:person-id personid
}
{
:full-name "unknown"
:byu-id "unknown"
:email "unknown"
:account-type 5
:person-id "unknown"
}
)
)

(defn get-user-data-new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@
:body {:message "collection not found"}}
(let [username (:username body)]
(if (nil? (users/READ-BY-USERNAME username))
(uc/get-session-id username))
(uc/create-potentially-empty-user username))
;; {:status 404
;; :body {:message (str "Unknown user:" username)}
;; }
;; I think this is called if the user doesn't already exist, but we can't make users this way anymore
;; either need to deactivate this, or create an entirely new way to make users, but it means people have to put the user's byuid instead of netid, or include netid, byuid, and personid
;; all of that will require front end changes as well, and it could be sort of extensive depending on how many fields we want to use to define the user
(if (user-collections-assoc/EXISTS-COLL-USER? id username)
(user-collections-assoc/DELETE-BY-IDS [id username]))
(let [result (utils/get-id (user-collections-assoc/CREATE (into (dissoc body :username) {:collection-id id :username username})))]
Expand All @@ -96,7 +102,9 @@
(user-collections-assoc/CREATE {:collection-id id :username username
:account-role (:account-role body)})
(if (empty? (users/READ-BY-USERNAME username))
(uc/get-session-id username))))
(uc/create-potentially-empty-user username)
;; (do) this means skip, and this will need to go higher up so the other code isn't executed if this user doesn't exist
)))
{:status 200
:body {:message (str (count (:usernames body)) " users added to collection")}})))

Expand Down
29 changes: 28 additions & 1 deletion src/clj/y_video_back/user_creator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,42 @@
(let [user-res (users/READ-BY-USERNAME [username])]
(if-not (= 0 (count user-res))
(do
(if (< (inst-ms (:last-person-api (first user-res)))
(if (or
;; If account hasn't been updated within the last hour,
;; or account-name, email, person-id, or account-type is unknown or invalid value (5),
;; update the user data by calling update-user function
(< (inst-ms (:last-person-api (first user-res)))
(- (System/currentTimeMillis) (* 3600000 (-> env :user-data-refresh-after))))
(= (:account-name (first user-res)) "unknown")
(= (:email (first user-res)) "unknown")
(= (:byu-person-id (first user-res)) "unknown")
(= (:account-type (first user-res)) 5)
)
(update-user username (:id (first user-res)) byuid personid))
(cc/check-courses-with-api username)
(get-auth-token (:id (first user-res))))
(let [user-create-res (create-user username byuid personid)]
(cc/check-courses-with-api username)
(get-auth-token (:id user-create-res))))))

(defn create-potentially-empty-user
"Checks if the user has data via student data api. If not, creates a user devoid of all meaningful
data except for username (netid). Intended to be used when associated new users with a collection
before that user created an account by logging in"
[username]
(def student-data (persons-api/get-student-summary username "unknown"))
(let [create-res (users/CREATE {:username username
:email (:email student-data)
:last-login "na"
:account-type (:account-type student-data)
:account-name (:full-name student-data)
:last-person-api (java.sql.Timestamp. (System/currentTimeMillis))
:byu-person-id (:person-id student-data)})]
(cc/check-courses-with-api username true)
create-res
)
)

(defn user-id-to-session-id
"Generates session id for user with given id. If user does not exist, returns nil."
[user-id]
Expand Down

0 comments on commit 8abce06

Please sign in to comment.