 ;; Requests for checking the status of a computation
 
 (defn get-datadump-url
-  [filename]
-  (full-url (str "datadump/results?filename=" filename)))
+  [filename zinvite]
+  (full-url (str "datadump/results?filename=" filename "&zinvite=" zinvite)))
 
 (defn complete-response
-  [filename]
+  [filename zinvite]
   {:status  201
    :headers {"Content-Type" "text/plain"
-             "Location"     (get-datadump-url filename)}
+             "Location"     (get-datadump-url filename zinvite)}
    :body    "Export is complete. Download at the Location url specified in the header"})
 
 (defn get-datadump-status-handler
   [{:keys [params] :as request}]
-  (let [filename (:filename params)]
-    (case (-> filename get-export-status (get "status"))
-      ("pending" "started" "processing") (check-back-response filename 200)
-      "complete"                         (complete-response filename)
+  (let [{:keys [filename zinvite]} params]
+    (case (-> (get-export-status filename zinvite) (get "status"))
+      ("pending" "started" "processing") (check-back-response filename zinvite 200)
+      "complete"                         (complete-response filename zinvite)
       ;; In case we don't match, 404
       respond-404)))
 

        (response/file-response (full-path filename)))
      (do
        (handle-timedout-datadump! aws-cred datadump filename params)
        (check-back-response filename)))))


;; Route everything together, build handlers, etc

;; Here, we're setting up basic mongo read and write helpers.

(defn update-export-status
  [filename zinvite document]
  (mc/insert (db/mongo-db (:env/env :mongolab-uri))
             (db/mongo-collection-name "exports")
             {:filename filename :zinvite zinvite}
             (db/format-for-mongo identity (assoc document
                                                  :filename filename
                                                  :zinvite zinvite
                                                  :lastupdate (System/currentTimeMillis)))
             {:upsert true}))

(defn get-export-status
  [filename zinvite]
  (mc/find-one (db/mongo-db (:env/env :mongolab-uri))
               (db/mongo-collection-name "exports")
               {:filename filename :zinvite zinvite}))

(defn notify-mongo
  [filename status params]
  (update-export-status filename (:zinvite params) {:status status :params params}))


;; We use AWS to store conversation exports whcih took a long time to compute.
