Skip to content

Commit

Permalink
Re-calculate average rating after new feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
madis committed Dec 4, 2023
1 parent 384c024 commit 1344560
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 10 additions & 0 deletions server/src/ethlance/server/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,16 @@
:join [:Job [:= :Job.job/id :JobStory.job/id]]
:where [:= :job-story/id job-story-id]})))))

(defn get-arbiter-id-by-job-story-id [conn job-story-id]
(safe-go
(:id (<? (db/get conn {:select [[:JobArbiter.user/id :id]]
:from [:JobStory]
:join [:Job [:= :Job.job/id :JobStory.job/id]
:JobArbiter [:= :Job.job/id :JobArbiter.job/id]]
:where [:and
[:= :job-story/id job-story-id]
[:= :job-arbiter/status "accepted"]]})))))

(defn update-job-story-status [conn job-story-id status]
(safe-go
(<? (db/get conn {:update :JobStory
Expand Down
19 changes: 18 additions & 1 deletion server/src/ethlance/server/graphql/resolvers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,21 @@
:message/text text
:direct-message/recipient to})))) ; FIXME: this should be job-story-message,

(defn re-calculate-user-average-rating [user-id job-story-id]
(db/with-async-resolver-tx conn
(let [employer (<? (ethlance-db/get-employer-id-by-job-story-id conn job-story-id))
candidate (<? (ethlance-db/get-candidate-id-by-job-story-id conn job-story-id))
arbiter (<? (ethlance-db/get-arbiter-id-by-job-story-id conn job-story-id))
mean (:mean (<? (db/get conn {:select [[(sql/call :avg :feedback/rating) :mean]]
:from [:JobStoryFeedbackMessage]
:where [:and
[:ilike user-id :user/id]
[:= :job-story/id job-story-id]]})))]
(cond
(= employer user-id) (ethlance-db/update-row! conn :Employer {:user/id user-id :employer/rating mean})
(= candidate user-id) (ethlance-db/update-row! conn :Candidate {:user/id user-id :candidate/rating mean})
(= arbiter user-id) (ethlance-db/update-row! conn :Arbiter {:user/id user-id :arbiter/rating mean})))))

(defn leave-feedback-mutation [_ {:keys [:job-story/id :text :rating :to] :as params} {:keys [current-user timestamp]}]
; Change JobStory status to "ended-by-feedback" when employer or candidate sends feedback
(db/with-async-resolver-tx conn
Expand All @@ -1025,7 +1040,7 @@
{:select [[:JobStory.job-story/status :status]]
:from [:JobStory]
:where [:= :job-story/id job-story-id]})))
arbiter-feedback-before-ending? (and (not= previous-status "ended-by-feedback")
arbiter-feedback-before-ending? (and (not= previous-status "finished")
(not feedback-from-participants?))]

(when feedback-from-participants?
Expand All @@ -1039,6 +1054,8 @@
:message/text text
:feedback/rating rating
:user/id to}))
(re-calculate-user-average-rating to job-story-id)

true
)))

Expand Down

0 comments on commit 1344560

Please sign in to comment.