165 lines
3.0 KiB
Go
165 lines
3.0 KiB
Go
package jobhandler
|
|
|
|
import (
|
|
"bytes"
|
|
"github.com/jordan-wright/email"
|
|
"html/template"
|
|
"log"
|
|
"menuserver/dbconnection"
|
|
"net/smtp"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
from = os.Getenv("EMAIL_FROM")
|
|
login = os.Getenv("EMAIL_LOGIN")
|
|
recipients = strings.Split(os.Getenv("EMAIL_RECIPIENTS"), ",")
|
|
// recipients = []string{"Martin <martind2000@gmail.com>"}
|
|
hostname = os.Getenv("EMAIL_HOSTNAME")
|
|
)
|
|
|
|
func DoJob(r *dbconnection.SQLiteRepository) {
|
|
|
|
var data PugData
|
|
|
|
var store []MenuEntry
|
|
|
|
var menuItems []dbconnection.Menu
|
|
|
|
srcURL := os.Getenv("SITE_URL")
|
|
var types = [7]int{0, 0, 0, 0, 0, 0, 0}
|
|
limit := 2
|
|
|
|
log.Printf("Doing Job....")
|
|
|
|
now := time.Now()
|
|
|
|
daystart := (now.Unix() / 86400) * 86400
|
|
|
|
data.Ts = now.Format("Monday, January 2, 2006")
|
|
|
|
items, err := r.GetRandom(now.Unix())
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, item := range items {
|
|
if types[item.Meat] < limit && len(store) < 5 {
|
|
entry := MenuEntry{
|
|
Url: srcURL + "/view/" + item.Short,
|
|
Meat: item.Meat,
|
|
Name: item.Name,
|
|
}
|
|
store = append(store, entry)
|
|
types[item.Meat] = types[item.Meat] + 1
|
|
|
|
menuItems = append(menuItems, item)
|
|
}
|
|
}
|
|
|
|
data.Menu = store
|
|
|
|
soupItem, err := r.GetRandomSoup(now.Unix())
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
soupEntry := MenuEntry{
|
|
Url: srcURL + "/view/" + soupItem.Short,
|
|
Meat: soupItem.Meat,
|
|
Name: soupItem.Name,
|
|
}
|
|
|
|
data.Soup = soupEntry
|
|
|
|
menuItems = append(menuItems, soupItem)
|
|
|
|
outputMsg := generateHTML(data)
|
|
|
|
subject := "Suggestions 🍽️ - " + data.Ts
|
|
|
|
sendSMTP(outputMsg, subject)
|
|
|
|
updateTimestamps(r, menuItems, daystart)
|
|
}
|
|
|
|
func generateHTML(data PugData) string {
|
|
|
|
var buf bytes.Buffer
|
|
t := template.New("mail")
|
|
|
|
t, err := t.Parse(`<html lang="en">
|
|
<head><meta charset="utf-8"/><title>Suggestions</title></head>
|
|
<body>
|
|
<h1>Suggestions for {{ .Ts }}</h1>
|
|
<h2>Mains</h2>
|
|
<ol>
|
|
{{ range .Menu }}
|
|
<li><a href="{{.Url}}">{{ .Name }}</a></li>
|
|
{{ end }}
|
|
</ol>
|
|
<H2>Soup</H2>
|
|
<ul>
|
|
<li><a href="{{.Soup.Url}}">{{.Soup.Name}}</a></li>
|
|
</ul>
|
|
<small>V3.00 -- 😊 -- GO!</small>
|
|
</body>
|
|
</html>`)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
err = t.Execute(&buf, data)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// log.Printf("%+v\n")
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
func getEmojis() []string {
|
|
return []string{"🍽️", "🍴", "🍳", "👨🏻🍳", "👩🏻🍳", "🥑", "🥕", "🫑", "🍔"}
|
|
}
|
|
|
|
func updateTimestamps(r *dbconnection.SQLiteRepository, menuItems []dbconnection.Menu, timestamp int64) {
|
|
|
|
err := r.UpdateTimeStamps(menuItems, timestamp)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
}
|
|
|
|
func sendSMTP(inmsg string, subject string) {
|
|
|
|
log.Println("Sending Email")
|
|
|
|
e := email.NewEmail()
|
|
|
|
auth := smtp.PlainAuth("", login, os.Getenv("EMAIL_PASSWORD"), hostname)
|
|
|
|
e.From = from
|
|
e.To = []string{from}
|
|
e.Bcc = recipients
|
|
e.Subject = subject
|
|
e.Text = []byte(inmsg)
|
|
e.HTML = []byte(inmsg)
|
|
|
|
err := e.Send(hostname+":587", auth)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
} else {
|
|
log.Println("Mail sent!")
|
|
}
|
|
}
|