fixing a number of bugs

This commit is contained in:
Bryce Covert
2018-04-08 14:46:23 -07:00
parent c5344a27eb
commit 9dabb633a7
15 changed files with 462 additions and 81 deletions

View File

@@ -23,29 +23,32 @@
(def queue-url "https://sqs.us-east-1.amazonaws.com/679918342773/integreat-mail-prod")
(defn process-sqs []
(println "Fetching messages from sqs...")
(let [companies (companies/get-all)]
(doseq [message (:messages (sqs/receive-message {:queue-url queue-url
:wait-time-seconds 5
:max-number-of-messages 10
#_#_:attribute-names ["All"]}))]
(let [message-body (json/read-str (:body message)
:key-fn keyword)]
(doseq [r (:Records message-body)]
(println "Processing record " r)
(let [props (Session/getDefaultInstance (Properties.))
message-stream (-> (s3/get-object {:key (-> r :s3 :object :key)
:bucket-name (-> r :s3 :bucket :name)})
:input-stream)
mail (message/read-message (MimeMessage. props message-stream))]
(doseq [pdf-stream (->> (-> mail :body)
(filter :content-type)
(filter #(re-find #"application/pdf" (:content-type %)) ))]
(let [filename (str "/tmp/" (UUID/randomUUID) ".pdf")]
(io/copy (:body pdf-stream) (io/file filename))
(invoices/import (parse/parse-file filename filename) companies)
(io/delete-file filename))))))
(sqs/delete-message (assoc message :queue-url queue-url )))))
(try
(println "Fetching messages from sqs...")
(let [companies (companies/get-all)]
(doseq [message (:messages (sqs/receive-message {:queue-url queue-url
:wait-time-seconds 5
:max-number-of-messages 10
#_#_:attribute-names ["All"]}))]
(let [message-body (json/read-str (:body message)
:key-fn keyword)]
(doseq [r (:Records message-body)]
(println "Processing record " r)
(let [props (Session/getDefaultInstance (Properties.))
message-stream (-> (s3/get-object {:key (-> r :s3 :object :key)
:bucket-name (-> r :s3 :bucket :name)})
:input-stream)
mail (message/read-message (MimeMessage. props message-stream))]
(doseq [pdf-stream (->> (-> mail :body)
(filter :content-type)
(filter #(re-find #"application/pdf" (:content-type %)) ))]
(let [filename (str "/tmp/" (UUID/randomUUID) ".pdf")]
(io/copy (:body pdf-stream) (io/file filename))
(invoices/import (parse/parse-file filename filename) companies)
(io/delete-file filename))))))
(sqs/delete-message (assoc message :queue-url queue-url ))))
(catch Exception e
(println e))))
(defn always-process-sqs []
(while (not (Thread/interrupted))

View File

@@ -34,6 +34,6 @@
(do
(println row)
(assoc row
:company (:name (parse/best-match companies customer-identifier))
:company-id (:id (parse/best-match companies customer-identifier))
:imported false
:potential-duplicate false)))))

View File

@@ -27,7 +27,7 @@
(str/join "," (take (count vendors) (repeat "?")))
") "
"AND "
"scheduled > NOW()"
"sent is NULL"
) ]
vendors))))

View File

@@ -22,7 +22,7 @@
[auto-ap.routes.auth :as auth]
[amazonica.core :refer [defcredential]]))
(defcredential "AKIAIRKDGLBX7J7VJZ6Q" "OtRw2t/xktJBDjP8Jnx1Yf6G+uzBfIkrQEc6nmgo" "us-east-1")
(defcredential "AKIAINHACMVQJ6NYD26A" "FwdL4TbIC/5H/4mwhQy4iSI/eSewyPgfS1EEt6tL" "us-east-1")
(defroutes static-routes
(GET "/" [] (response/resource-response "index.html" {:root "public"}))

View File

@@ -10,16 +10,18 @@
[clj-time.periodic :as p]
[clj-time.local :as l]
[clj-time.jdbc]
[clj-http.client :as http]
[clojure.data.json :as json]
[ring.middleware.json :refer [wrap-json-body]]
)
(:import [org.joda.time DateTime]))
(defn next-sunday []
(let [sunday (->> (p/periodic-seq (time/today) (time/days 1))
(let [sunday (->> (p/periodic-seq (time/plus (time/today) (time/days 1)) (time/days 1))
(filter pred/sunday?)
first)
]
(.toDateTime (time/local-date-time (time/year sunday) (time/month sunday) (time/day sunday)))))
first)]
(time/from-time-zone (time/date-time (time/year sunday) (time/month sunday) (time/day sunday))
(time/time-zone-for-id "America/Los_Angeles"))))
(defn schedule-reminders []
(let [vendors (vendors/find-with-reminders)
@@ -41,30 +43,38 @@
))
(defn send-emails [reminders]
(doseq [{:keys [name email id]} reminders]
(doseq [{:keys [vendor-name email id]} reminders]
(println "Sending email to" email)
(ses/send-email :destination {:to-addresses [email]}
:source "invoices@mail.integreat.aws.brycecovertoperations.com"
:message {:subject "Reminder to send invoices"
:body {:html (str "<h1>Hello " name ",</h1><p>This is a reminder to send this week's invoices to us. You can just reply to this email.</p> <p> - Integreat. </p>")
:text (str "Hello " name ",\r\nThis is a reminder to send this week's invoices to us. You can just reply to this email.\r\n - Integreat.")}})
:body {:html (str "<h1>Hello " vendor-name ",</h1><p>This is a reminder to send this week's invoices to us. You can just reply to this email.</p> <p> - Integreat. </p>")
:text (str "Hello " vendor-name ",\r\nThis is a reminder to send this week's invoices to us. You can just reply to this email.\r\n - Integreat.")}})
(reminders/finish id)))
(defn replace-joda [x]
(into {} (map (fn [[k v]]
[k (if (instance? DateTime v)
(c/to-string v)
(c/to-date v)
v)])
x))
)
x)))
(defroutes routes
(context "/reminders" []
(POST "/send" {:keys [query-params]}
(schedule-reminders)
(-> (reminders/get-ready)
(send-emails))
(POST "/send" {:keys [query-params headers body] :as x}
(let [notification-type (get headers "x-amz-sns-message-type")]
(println "Received notification " notification-type)
(if (= "SubscriptionConfirmation" notification-type)
(do
(println "Responding to confirmation" )
(let [json (json/read-str (slurp body))]
(println json)
(http/get (get json "SubscribeURL"))))
(do
(println "Scheduling")
(schedule-reminders)
(-> (reminders/get-ready)
(send-emails)))))
{:status 200
:body "{}"

View File

@@ -2,6 +2,8 @@
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [re-frame.core :as re-frame]
[cljs-http.client :as http]
[cljs-time.coerce :as c]
[cljs-time.core :as time]
[cljs.core.async :refer [<!]]
[auto-ap.history :as p]
[pushy.core :as pushy]))
@@ -19,6 +21,16 @@
(.setItem js/localStorage name value)
(.removeItem js/localStorage name ))))
(defn dates->date-times [x]
(cond (map? x)
(into {} (map (fn [[k v]]
[k (if (instance? js/Date v)
(time/to-default-time-zone (c/from-date v))
v)])
x))
(list? x)
(map dates->date-times x)))
(re-frame/reg-fx
:http
(fn [{:keys [method uri on-success body headers token]}]
@@ -33,5 +45,6 @@
:url uri})
(<! )
:body
(dates->date-times)
(conj on-success)
(re-frame/dispatch))))))

View File

@@ -4,10 +4,13 @@
[reagent.core :as reagent]
[auto-ap.subs :as subs]
[auto-ap.events.admin.reminders :as events]
[auto-ap.views.utils :refer [login-url dispatch-value-change dispatch-event]]
[auto-ap.views.utils :refer [login-url dispatch-value-change dispatch-event date-time->str date->str]]
[cljs.reader :as edn]
[auto-ap.routes :as routes]
[bidi.bidi :as bidi]))
(defn reminders-table []
(let [reminders (or @(re-frame/subscribe [::subs/reminders]) [])]
[:table {:class "table", :style {:width "100%"}}
@@ -20,9 +23,9 @@
^{:key id}
[:tr
[:td vendor-name]
[:td (str scheduled)]
[:td (date->str scheduled)]
[:td (when sent
[:span [:span.icon [:i.fa.fa-check]] "Sent " (str sent)]) ]])]]))
[:span [:span.icon [:i.fa.fa-check]] "Sent " (date-time->str sent)]) ]])]]))
(defn admin-reminders-page []

View File

@@ -1,5 +1,6 @@
(ns auto-ap.views.utils
(:require [re-frame.core :as re-frame]))
(:require [re-frame.core :as re-frame]
[cljs-time.format :as format]))
(defn active-when= [active-page candidate]
(when (= active-page candidate) " active"))
@@ -18,3 +19,12 @@
(fn [e]
(.preventDefault e)
(re-frame/dispatch event)))
(def pretty-long (format/formatter "MM/dd/yyyy HH:mm:ss"))
(def pretty (format/formatter "MM/dd/yyyy"))
(defn date->str [d]
(format/unparse pretty d))
(defn date-time->str [d]
(format/unparse pretty-long d))