 
 (ko/defentity participants
   (ko/entity-fields :pid :uid :zid :created)
-  (ko/belongs-to user (:fk :uid))
+  (ko/belongs-to users (:fk :uid))
   (ko/belongs-to conversations (:fk :zid)))
 
-(defn get-db-users-with-stats-by-uid
-  [uids]
-  (kdb/with-db (poll/heroku-db-spec (env/env :database-url))
-    (ko/select
-      users
-      (ko/fields :uid
-                 :created
-                 :email
-                 :owned_convs.avg_n_ptpts
-                 :owned_convs.n_owned_convs
-                 :ptpt_summary.n_ptptd_convs)
-      ; Join summary stats of owned conversations
-      (ko/join :left
-        [(ko/subselect
-           conversations
-           (ko/fields :owner)
-           ; Join participant count summaries per conv
-           (ko/join
-             [(ko/subselect
-                participants
-                (ko/fields :zid)
-                (ko/aggregate (count (ko/raw "DISTINCT pid")) :n_ptpts :zid))
-              ; as conv_summary
-              :conv_summary]
-             (= :conv_summary.zid :zid))
-           ; Average participant counts, and count number of conversations
-           (ko/aggregate (avg :conv_summary.n_ptpts) :avg_n_ptpts)
-           (ko/aggregate (count (ko/raw "DISTINCT conversations.zid")) :n_owned_convs)
-           (ko/group :owner))
-         ; as owned_convs
-         :owned_convs]
-        (= :owned_convs.owner :uid))
-      ; Join summary stats on participation
-      (ko/join
-        :left
-        [(ko/subselect
-           participants
-           (ko/fields :uid)
-           (ko/aggregate (count (ko/raw "DISTINCT zid")) :n_ptptd_convs :uid))
-         :ptpt_summary]
-        (= :ptpt_summary.uid :uid))
-      (ko/where (in :uid uids)))))
-
+
+(def get-db-users
+  (->
+    (ko/select* users)
+    (ko/fields :uid :hname :username :email :is_owner :created :plan)))
+
+
+(def get-db-users-with-stats
+  (->
+    get-db-users
+    (ko/fields :owned_convs.avg_n_ptpts
+               :owned_convs.n_owned_convs
+               :ptpt_summary.n_ptptd_convs)
+    ; Join summary stats of owned conversations
+    (ko/join :left
+      [(ko/subselect
+         conversations
+         (ko/fields :owner)
+         ; Join participant count summaries per conv
+         (ko/join
+           [(ko/subselect
+              participants
+              (ko/fields :zid)
+              (ko/aggregate (count (ko/raw "DISTINCT pid")) :n_ptpts :zid))
+            ; as conv_summary
+            :conv_summary]
+           (= :conv_summary.zid :zid))
+         ; Average participant counts, and count number of conversations
+         (ko/aggregate (avg :conv_summary.n_ptpts) :avg_n_ptpts)
+         (ko/aggregate (count (ko/raw "DISTINCT conversations.zid")) :n_owned_convs)
+         (ko/group :owner))
+       ; as owned_convs
+       :owned_convs]
+      (= :owned_convs.owner :uid))
+    ; Join summary stats on participation
+    (ko/join
+      :left
+      [(ko/subselect
+         participants
+         (ko/fields :uid)
+         (ko/aggregate (count (ko/raw "DISTINCT zid")) :n_ptptd_convs :uid))
+       :ptpt_summary]
+      (= :ptpt_summary.uid :uid))))
+
+
+(defn get-db-users-by-uid
+  [db-spec uids]
+  (kdb/with-db db-spec
+    (->
+      get-db-users-with-stats
+      (ko/where (in :uid uids))
+      (ko/select))))
+
+
+(defn get-db-users-by-email
+  [db-spec emails]
+  (kdb/with-db db-spec
+    (->
+      get-db-users-with-stats
+      (ko/where (in :email emails))
+      (ko/select))))
 
 (defn update-icuser-from-dbuser
   [dbuser]

  (mapv #(get m % not-found) ks))


(defn get-db-users-by-uid
  [db-spec uids]
  (kdb/with-db db-spec
    (ko/select
      "users"
      (ko/fields :uid :hname :username :email :is_owner :created :plan)
      (ko/where {:uid [in uids]}))))


(defn get-db-users-by-email
  [db-spec emails]
  (kdb/with-db db-spec
    (ko/select
      "users"
      (ko/fields :uid :hname :username :email :is_owner :created :plan)
      (ko/where {:email [in emails]}))))


(defn update-icuser
  [params]
  (->>

  (ko/pk :zid)
  (ko/entity-fields :zid :owner)
  (ko/has-many votes)
  (ko/belongs-to users (:fk :owner)))

(ko/defentity votes
  (ko/entity-fields :zid :pid :tid :vote :created)
