added reminder scheduling and sending.

This commit is contained in:
Bryce Covert
2018-04-07 08:36:59 -07:00
parent 982d5ade58
commit a2c1273843
5 changed files with 104 additions and 17 deletions

View File

@@ -4,26 +4,74 @@
[auto-ap.db.reminders :as reminders]
[auto-ap.routes.utils :refer [wrap-secure]]
[amazonica.aws.simpleemail :as ses]
))
[clj-time.core :as time]
[clj-time.coerce :as c]
[clj-time.predicates :as pred]
[clj-time.periodic :as p]
[clj-time.local :as l]
[clj-time.jdbc]
)
(:import [org.joda.time DateTime]))
(defn next-sunday []
(let [sunday (->> (p/periodic-seq (time/today) (time/days 1))
(filter pred/sunday?)
first)
]
(.toDateTime (time/local-date-time (time/year sunday) (time/month sunday) (time/day sunday)))))
(defn schedule-reminders []
(let [vendors (vendors/find-with-reminders)
future-reminders (reminders/find-future (map :id vendors))
has-reminder-scheduled? (set (map :vendor-id future-reminders))
vendors-without-scheduled (filter #(not (has-reminder-scheduled? (:id %))) vendors)]
(println "Reminders already scheduled:" future-reminders)
(println "Reminders will happen at" (next-sunday))
(println "Reminders to schedule" vendors-without-scheduled)
(doseq [{:keys [id invoice-reminder-schedule]} vendors-without-scheduled]
(reminders/insert {:vendor-id id
:scheduled (next-sunday)}))))
(defn find-ready-reminders []
(let [vendors (vendors/get-all)
ready-reminders (reminders/get-ready)]
ready-reminders
))
(defn send-emails [reminders]
(doseq [{:keys [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.")}})
(reminders/finish id)))
(defn replace-joda [x]
(into {} (map (fn [[k v]]
[k (if (instance? DateTime v)
(c/to-string v)
v)])
x))
)
(defroutes routes
(context "/reminders" []
(POST "/send" {:keys [query-params]}
(doseq [{:keys [name email invoice-reminder-schedule]} (vendors/get-all)]
(when (= "Weekly" invoice-reminder-schedule)
(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.")}})))
(schedule-reminders)
(-> (reminders/get-ready)
(send-emails))
{:status 200
:body "{}"
:headers {"Content-Type" "application/edn"}})
(wrap-routes (GET "/" {:keys [query-params]}
{:status 200
:body (pr-str (reminders/get-all))
:body (pr-str (map replace-joda (reminders/get-all)))
:headers {"Content-Type" "application/edn"}})
wrap-secure)))