 
 (defn send-past-positions [channel params]
   "send all past positions to websocket channel"
-  (def positions (redis/wcar* (car/lrange (get-position-key params) 0 -1)))
+  (def positions (position/fetch-positions params))
   (doseq [p (reverse positions)] (send-position channel p)))
 
 (defn subscribe-to-redis [channel trip-id]

    [snap-spot.redis :as redis]
    [taoensso.carmine :as car :refer (wcar)]
    [taoensso.timbre :as timbre]
    [clojure.data.json :as json]
    [me.shenfeng.mustache :as mustache]))

(timbre/refer-timbre)

(defn get-position-key [params] (str (:id params) "-positions"))

(defn send-position [channel position]
  "send postion to websocket channel"

    (subscribe-to-redis channel (:id params))
    (send-past-positions channel params)))

(defn position-from-params [params]
  (select-keys [:lat :lon :instant] params))

(defn handle-new-position [trip position]
  "push new position to subscribed channels and persist to redis"
  (redis/wcar* (car/publish (:id trip) position))
  (position/add trip position))

(def update-validations 
  {:id [v/required v/number]
   :secret [v/required v/number]
   :lat [v/required v/number]
   :lon [v/required v/number]
   :instant [v/required v/number]})

(defn update [req] 
  "update position for trip"
  (let [p (:params req)
        trip (trip/fetch (:id p))
        position (position-from-params p)
        errors (helper/validate-all [[p update-validations] 
                                     [trip trip/validations] 
                                     [position position/validations]])]
    (if errors
      (helper/error-response errors) 
      (if (= (:secret p) (:secret trip "")) 
        (do (handle-new-position trip position)
            "Position updated")
        (helper/error-response "invalid secret")))))
