can send emails on a whim

This commit is contained in:
Bryce Covert
2018-04-13 20:28:31 -07:00
parent c8e959bd40
commit 431c2883e2
9 changed files with 103 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
(ns auto-ap.routes.reminders
(:require [amazonica.aws.simpleemail :as ses]
(:require
[auto-ap.db.reminders :as reminders]
[auto-ap.db.vendors :as vendors]
[auto-ap.routes.utils :refer [wrap-secure]]
@@ -30,24 +30,18 @@
(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)}))))
(doseq [{:keys [id primary-email invoice-reminder-schedule]} vendors-without-scheduled]
(reminders/insert (assoc (reminders/template)
:vendor-id id
:email primary-email
: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 [vendor-name email id]} reminders]
(println "Sending email to" email)
(ses/send-email :destination {:to-addresses [email]}
:source (:invoice-email env)
:message {:subject "Reminder to send invoices"
: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]]
@@ -71,7 +65,7 @@
(println "Scheduling")
(schedule-reminders)
(-> (reminders/get-ready)
(send-emails)))))
(reminders/send-emails)))))
{:status 200
:body "{}"

View File

@@ -2,8 +2,9 @@
(:require [auto-ap.db.vendors :as vendors]
[auto-ap.entities.vendors :as entity]
[auto-ap.routes.utils :refer [wrap-secure wrap-spec]]
[compojure.core :refer [GET POST PUT context defroutes
wrap-routes]]))
[auto-ap.db.reminders :as reminders]
[clj-time.core :as time]
[compojure.core :refer [GET POST PUT context defroutes wrap-routes]]))
(defroutes routes
(wrap-routes
@@ -12,17 +13,32 @@
{:status 200
:body (pr-str (vendors/get-all))
:headers {"Content-Type" "application/edn"}})
(wrap-spec
(wrap-routes
(PUT "/:id" {:keys [edn-params] {:keys [id]} :route-params :as r}
{:status 200
:body (pr-str (vendors/upsert id edn-params))
:headers {"Content-Type" "application/edn"}})
::entity/vendor)
(wrap-spec
#(wrap-spec % ::entity/vendor))
(POST "/:id/remind" {:keys [edn-params] {:keys [id :<< as-int]} :route-params :as r}
(let [id (if (int? id)
id
(Integer/parseInt id))
vendor (vendors/get-by-id id)]
(reminders/insert (assoc
(reminders/template)
:email (:primary-email vendor)
:vendor-id id
:scheduled (time/now)))
(-> (reminders/get-ready)
(reminders/send-emails))
{:status 200
:body "{}"
:headers {"Content-Type" "application/edn"}}))
(wrap-routes
(POST "/" {:keys [edn-params] :as r}
{:status 200
:body (pr-str (vendors/insert edn-params))
:headers {"Content-Type" "application/edn"}})
::entity/vendor))
#(wrap-spec % ::entity/vendor)))
wrap-secure))