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

@@ -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 "{}"