From cdd3723c7b2523fbe89e9048e2464c2f56ef8b66 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Fri, 27 Jul 2018 09:08:19 +0100 Subject: [PATCH] Very close to successful transmit, but not yet working. --- .gitignore | 1 + src/clj/youyesyet/routes/rest.clj | 34 ++++++++++++++----- src/cljs/youyesyet/canvasser_app/ajax.cljs | 2 +- .../youyesyet/canvasser_app/handlers.cljs | 2 +- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 69672f7..457f290 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ src/clj/youyesyet/routes/auto_json\.clj resources/sql/youyesyet\.postgres\.sql \.rebel_readline_history +/dumps/ diff --git a/src/clj/youyesyet/routes/rest.clj b/src/clj/youyesyet/routes/rest.clj index 9ce8759..6463827 100644 --- a/src/clj/youyesyet/routes/rest.clj +++ b/src/clj/youyesyet/routes/rest.clj @@ -1,6 +1,6 @@ (ns ^{:doc "Manually maintained routes which handle data transfer to/from the canvasser app." :author "Simon Brooke"} youyesyet.routes.rest - (:require [adl-support.core :refer [massage-params]] + (:require [adl-support.core :refer [massage-params do-or-log-error]] [clojure.core.memoize :as memo] [clojure.java.io :as io] [clojure.tools.logging :as log] @@ -118,14 +118,32 @@ was to the same address, if so use that as the `visit_id`, if not create a new `visit` record." [request] - (let [params (massage-params request)] + (let [params (:params request)] (log/debug "Creating intention with params: " params) - (if-let [address-id (-> params :address_id)] - (db/create-intention! - db/*db* - (assoc - params :visit_id (current-visit-id request)) - (db/create-intention! db/*db* params))))) + (if (-> request :session :user) + (if + (and + (or (:locality params) + (and (:elector-id params) + (:address_id params))) + (:intention params)) + (do-or-log-error + {:status 201 + :body (with-out-str + (print + (hash-map + :id + (db/create-intention! + db/*db* + (assoc + params :visit_id (current-visit-id request)) + (db/create-intention! db/*db* params)))))} + :error-return {:status 500 + :body "Failed to create intention record"}) + {:status 400 + :body "create-intention requires params: `intention` and either `locality` or both `address_id` and `elector_id`."}) + {:status 403 + :body "You must be logged in to do that"}))) (defn create-request-and-visit! diff --git a/src/cljs/youyesyet/canvasser_app/ajax.cljs b/src/cljs/youyesyet/canvasser_app/ajax.cljs index 3db48fc..7684b8a 100644 --- a/src/cljs/youyesyet/canvasser_app/ajax.cljs +++ b/src/cljs/youyesyet/canvasser_app/ajax.cljs @@ -28,7 +28,7 @@ (defn local-uri? [{:keys [uri]}] - (not (re-find #"^\w+?://" uri))) + (not (re-find #"^\w+?://" (str uri)))) (defn default-headers [request] (if (local-uri? request) diff --git a/src/cljs/youyesyet/canvasser_app/handlers.cljs b/src/cljs/youyesyet/canvasser_app/handlers.cljs index a8d0ebf..9f4f8d0 100644 --- a/src/cljs/youyesyet/canvasser_app/handlers.cljs +++ b/src/cljs/youyesyet/canvasser_app/handlers.cljs @@ -482,7 +482,7 @@ (if-let [item (first (:outqueue db))] ;; if there's something in the queue, transmit it... (let [uri (compose-packet item)] - (js/console.log (str "Transmitting item" uri)) + (js/console.log (str "Transmitting item: " uri)) {:http-xhrio {:method :get :uri uri :format (json-request-format)