Skip to content

Instantly share code, notes, and snippets.

@dotemacs
Created January 25, 2023 12:09
Show Gist options
  • Save dotemacs/a24c84c139123d52a005990e12e78c20 to your computer and use it in GitHub Desktop.
Save dotemacs/a24c84c139123d52a005990e12e78c20 to your computer and use it in GitHub Desktop.
(defn update-user
([^js db user]
(update-user db user nil))
([^js db user prev-username]
(let [batch (.batch db)]
(update-user db batch user prev-username)
(.commit batch)))
([^js db batch-or-transaction user prev-username]
(let [user-id (:id user)
username (:username user)]
(.update batch-or-transaction
(user-profile-doc-ref db user-id)
(clj->js (dissoc user :id)))
(when (and prev-username
(not= (user-profile/normalize-username prev-username)
(user-profile/normalize-username username)))
(.delete batch-or-transaction
(username-doc-ref db (user-profile/normalize-username prev-username))))
(when (not= (user-profile/normalize-username prev-username)
(user-profile/normalize-username username))
(.set batch
(username-doc-ref db (user-profile/normalize-username username))
(clj->js {:uid user-id}))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment