 
 (defn create-team
   "Creates a team based on a token"
-  [conn token]
-  (let [team-data (get-team-data token)
-        team-id (d/tempid :db.part/user)
-        team-tx {:db/id team-id
+  [conn token team-data]
+  (let [team-tx {:db/id (d/tempid :db.part/user)
                  :team/domain (:domain team-data)
                  :team/token token
                  :team/name (:name team-data)

                 :team/questions [{:question/text "What have you accomplished this week?"
                                   :question/order 1}
                                  {:question/text "What you commit to do next week?"
                                   :question/order 2}]}
        ]
    @(d/transact conn [team-tx])))

(defn team-endpoint [config]
  (context "/team" []
   (GET "/" req
     (str ring.middleware.anti-forgery/*anti-forgery-token*))
   (POST "/" req
     (if-let [token (get (:form-params req) "token")]
       (let [db (:db req)
             conn (:conn req)
             team (ffirst (d/q '[:find ?e :in $ ?t :where [?e :team/token ?t]] db token))]
         (if team
           (pr-str (d/touch (d/entity db team)))
           (create-team conn token)))
       {:status 400}))))

(ns org.teamweek.webapp.endpoint.team
  (:require [compojure.core :refer :all]
            [ring.util.response :refer :all]
            [ring.middleware.flash :refer [flash-response]]
            [clj-http.client :as http]
            [cheshire.core :refer [parse-string]]
            [datomic.api :as d]))
