       (if next-page
         (into users (get-intercom-users next-page))
         users))))
-
-
-(defn user-id
-  [user]
-  (try
-    (-> user
-        (get "user_id")
-        (Integer/parseInt))
-    (catch Exception e
-      nil)))
-
-
-(defn user-id-str
-  [user]
-  (get user "user_id"))
-
-
-(defn valid-user-ids
-  [users]
-  (->> users
-       (map user-id)
-       (filter identity)))
 
 
 (defn gets

            [korma.db :as kdb]
            [environ.core :as env]
            [alex-and-georges.debug-repl :as dbr]
            [clojure.tools.trace :as tr]
            [polismath.utils :refer :all]
            [polismath.pretty-printers :as pp]

      {}
      (map
        (fn [[ic-key db-key]]
          [ic-key (db-key dbuser)])
        [[:name              :hname]
         [:user_id           :uid]
         [:email             :email]
         [:remote_created_at #(/ (:created %) 1000)]]))))



; Main functions:
; ===============


(defn backup-intercom-users
  "Backup intercom users to json file specified by filename arg."
  [filename]
  (let [icusers (get-intercom-users)]
    (spit filename (ch/generate-string icusers))))


(defn update-intercom-db
  "Update intercom records by grabbing existing records, and from those the corresponding PG DB records
  so tha twe have enough information to flesh out missing information."
  []
  (let [db-spec          (poll/heroku-db-spec (env/env :database-url))
        ; Get intercom users and break down by those that have id and those that don't, but have email
        icusers          (get-intercom-users)
        icusers-by-id    (filter #(get % "user_id") icusers)
        icusers-by-email (filter #(not (get % "user_id")) icusers)
        ; Get users by id, then by email for those without id, then put them all together in one collection
        dbusers-by-id    (get-db-users-by-uid db-spec
                           (map #(Integer/parseInt (get % "user_id")) icusers-by-id))
        dbusers-by-email (get-db-users-by-email db-spec
                           (map #(get % "email") icusers-by-email)) 
        all-users        (into dbusers-by-id dbusers-by-email)]
    ; First some nice summary stats information
    (println "Total number of intercom users:" (count icusers))
    (println "Number of users with valid ids:" (count icusers-by-id))
    (println "Number w/o:                    " (count icusers-by-email))
    ; Now getting to work
    (println "Now updating all user records in intercom")
    (doseq [u all-users]
      (log/info "Running update for user:" (hash-map-subset u [:uid :email :hname :created]))
      (update-icuser-from-dbuser u))
    ; Call it a night
    (println "Done!")))

