Skip to content

Commit

Permalink
move email config from .env to settings model (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
dheidemann committed Aug 1, 2024
1 parent b48fe59 commit 23e7d2b
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 101 deletions.
29 changes: 1 addition & 28 deletions .env
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
PUBLIC_URL="http://localhost:8080"
API_KEY="alongsecurestring"

POSTGRES_PASSWORD="password123"
POSTGRES_USER="postgres"
POSTGRES_DB="postgres"

API_KEY=

LOGO_URL="https://mathphys.info/mathphysinfo-logo.png"
HOMEPAGE_URL="https://mathphys.info"
COPYRIGHT="Copyright © 2024, Fachschaft MathPhysInfo. All rights reserved."

PRIMARY_COLOR="#990000"

SMTP_HOST=
SMTP_USER=
SMTP_PASSWORD=
SMTP_PORT=
FROM_ADDRESS=

EMAIL_GREETING="Hey"
EMAIL_SIGNATURE="Dein"
EMAIL_NAME="Pepp - Die Vorkursverwaltung"

EMAIL_CONFIRM_SUBJECT="Bitte bestätige deine E-Mail Adresse"
EMAIL_CONFIRM_INTRO="danke für deine Registrierung als Vorkurstutor/-in!"
EMAIL_CONFIRM_BUTTON_INSTRUCTION="Bitte klicke hier, um deine E-Mail Adresse und die Verfügbarkeiten zu bestätigen:"
EMAIL_CONFIRM_BUTTON_TEXT="Bestätigen"
EMAIL_CONFIRM_OUTRO="Wir melden uns bei dir."

EMAIL_ASSIGNMENTS_SUBJECT="Deine Veranstaltung"
EMAIL_ASSIGNMENTS_EVENT_TITLE="Veranstaltung"
EMAIL_ASSIGNMENTS_KIND_TITLE="Art"
EMAIL_ASSIGNMENTS_DATE_TITLE="Datum"
EMAIL_ASSIGNMENTS_TIME_TITLE="Uhrzeit"
EMAIL_ASSIGNMENTS_ROOM_TITLE="Raum"
EMAIL_ASSIGNMENTS_BUILDING_TITLE="Gebäude"
EMAIL_ASSIGNMENTS_INTRO="aufgrund deiner angegebenen Verfügbarkeiten, wurdest du der folgenden Veranstaltung zugewiesen:"
EMAIL_ASSIGNMENTS_OUTRO="Sollte dir der Termin doch nicht passen, melde dich bitte zeitnah bei uns, indem du auf diese E-Mail reagierst. Wir freuen uns auf einen erfolgreichen Vorkurs mit dir!"
47 changes: 37 additions & 10 deletions server/db/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func seedData(ctx context.Context, db *bun.DB) error {
}

buildings := []*models.Building{{
Name: "Science Building",
Street: "Main St",
Name: "Mathematikon",
Street: "Beispielstr.",
Number: "1",
City: "Example City",
Zip: 12345,
City: "Heidelberg",
Zip: 69115,
Osm: "https://www.openstreetmap.org/#map=6/40.355/124.739",
}}
if err := insertData(ctx, db, (*models.Building)(nil), buildings, "Buildings"); err != nil {
Expand All @@ -42,19 +42,19 @@ func seedData(ctx context.Context, db *bun.DB) error {
}

labels := []*models.Label{
{Name: "Math", Color: "#87cefa", Kind: "TOPIC"},
{Name: "Tutorial", Color: "#00ff80", Kind: "EVENT_TYPE"},
{Name: "Lecture", Color: "#ffbf00", Kind: "EVENT_TYPE"},
{Name: "Mathe", Color: "#87cefa", Kind: "TOPIC"},
{Name: "Tutorium", Color: "#00ff80", Kind: "EVENT_TYPE"},
{Name: "Vorlesung", Color: "#ffbf00", Kind: "EVENT_TYPE"},
}
if err := insertData(ctx, db, (*models.Label)(nil), labels, "Labels"); err != nil {
return err
}

events := []*models.Event{{
Title: "Linear Algebra",
Title: "Lineare Algebra",
Description: "Lorem Ipsum dolor sit amed",
TopicName: "Math",
TypeName: "Tutorial",
TopicName: "Mathe",
TypeName: "Tutorium",
NeedsTutors: true,
From: time.Now().Add(4 * time.Hour),
To: time.Now().Add(5 * time.Hour),
Expand All @@ -63,6 +63,33 @@ func seedData(ctx context.Context, db *bun.DB) error {
return err
}

settings := []*models.Setting{
{Key: "primary-color", Value: "#990000", Type: "COLOR"},
{Key: "logo-url", Value: "https://mathphys.info/mathphysinfo-logo.png", Type: "STRING"},
{Key: "homepage-url", Value: "https://mathphys.info", Type: "STRING"},
{Key: "copyright-notice", Value: "Copyright © 2024, Fachschaft MathPhysInfo. All rights reserved.", Type: "STRING"},
{Key: "email-greeting", Value: "Hey", Type: "STRING"},
{Key: "email-signature", Value: "Dein", Type: "STRING"},
{Key: "email-name", Value: "Pepp - Die Vorkursverwaltung", Type: "STRING"},
{Key: "email-confirm-subject", Value: "Bitte bestätige deine E-Mail Adresse", Type: "STRING"},
{Key: "email-confirm-intro", Value: "danke für deine Registrierung als Vorkurstutor/-in!", Type: "STRING"},
{Key: "email-confirm-button-instruction", Value: "Bitte klicke hier, um deine E-Mail Adresse und die Verfügbarkeiten zu bestätigen:", Type: "STRING"},
{Key: "email-confirm-button-text", Value: "Bestätigen", Type: "STRING"},
{Key: "email-confirm-outro", Value: "Wir melden uns bei dir.", Type: "STRING"},
{Key: "email-assignment-subject", Value: "Deine Veranstaltung", Type: "STRING"},
{Key: "email-assignment-event-title", Value: "Veranstaltung", Type: "STRING"},
{Key: "email-assignment-kind-title", Value: "Art", Type: "STRING"},
{Key: "email-assignment-date-title", Value: "Datum", Type: "STRING"},
{Key: "email-assignment-time-title", Value: "Uhrzeit", Type: "STRING"},
{Key: "email-assignment-room-title", Value: "Raum", Type: "STRING"},
{Key: "email-assignment-building-title", Value: "Gebäude", Type: "STRING"},
{Key: "email-assignment-intro", Value: "aufgrund deiner angegebenen Verfügbarkeiten, wurdest du der folgenden Veranstaltung zugewiesen:", Type: "STRING"},
{Key: "email-assignment-outro", Value: "Sollte dir der Termin doch nicht passen, melde dich bitte zeitnah bei uns, indem du auf diese E-Mail reagierst. Wir freuen uns auf einen erfolgreichen Vorkurs mit dir!", Type: "STRING"},
}
if err := insertData(ctx, db, (*models.Setting)(nil), settings, "Settings"); err != nil {
return err
}

return nil
}

Expand Down
46 changes: 46 additions & 0 deletions server/email/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package email

import "github.com/matcornic/hermes/v2"

type Config struct {
LogoUrl string
HomepageUrl string
CopyrightNotice string
Greeting string
Signature string
Name string

Confirmation Email
Assignment Email
}

func (c *Config) ApplySettings(s map[string]string) {
c.LogoUrl = s["logo-url"]
c.HomepageUrl = s["homepage-url"]
c.CopyrightNotice = s["copyright-notice"]
c.Greeting = s["email-greeting"]
c.Signature = s["email-signature"]
c.Name = s["email-name"]

c.Confirmation.Subject = s["email-confirm-subject"]
c.Confirmation.Intros = []string{s["email-confirm-intro"]}
c.Confirmation.Outros = []string{s["email-confirm-outro"]}
c.Confirmation.Actions = []hermes.Action{{
Instructions: s["email-confirm-button-instruction"],
Button: hermes.Button{
Color: s["primary-color"],
Text: s["email-confirm-button-text"],
},
}}
c.Confirmation.Table = hermes.Table{
Columns: hermes.Columns{
CustomWidth: map[string]string{
s["email-assignment-date-title"]: "20%",
s["email-assignment-kind-title"]: "30%",
},
},
}

c.Assignment.Intros = []string{s["email-assignment-intro"]}
c.Assignment.Outros = []string{s["email-assignment-outro"]}
}
16 changes: 8 additions & 8 deletions server/email/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ type Email struct {
Outros []string
}

func Send(person models.User, email Email) error {
func Send(person models.User, email Email, config *Config) error {
h := hermes.Hermes{
Product: hermes.Product{
Name: os.Getenv("EMAIL_NAME"),
Link: os.Getenv("HOMEPAGE_URL"),
Logo: os.Getenv("LOGO_URL"),
Copyright: os.Getenv("COPYRIGHT"),
Name: config.Name,
Link: config.HomepageUrl,
Logo: config.LogoUrl,
Copyright: config.CopyrightNotice,
},
}

mail := hermes.Email{
Body: hermes.Body{
Name: person.Fn,
Greeting: os.Getenv("EMAIL_GREETING"),
Signature: os.Getenv("EMAIL_SIGNATURE"),
Greeting: config.Greeting,
Signature: config.Signature,
Intros: email.Intros,
Dictionary: email.Dictionary,
Actions: email.Actions,
Expand All @@ -57,7 +57,7 @@ func Send(person models.User, email Email) error {
}

m := gomail.NewMessage()
m.SetHeader("From", fmt.Sprintf("%s <%s>", os.Getenv("EMAIL_NAME"), from))
m.SetHeader("From", fmt.Sprintf("%s <%s>", config.Name, from))
m.SetHeader("To", person.Mail)
m.SetHeader("Subject", email.Subject)
m.SetBody("text/html", body)
Expand Down
22 changes: 21 additions & 1 deletion server/graph/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,29 @@ package graph
// It serves as dependency injection for your app, add any dependencies you require here.

import (
"context"

"github.com/FachschaftMathPhysInfo/pepp/server/email"
"github.com/uptrace/bun"
)

type Resolver struct {
DB *bun.DB
DB *bun.DB
MailConfig *email.Config
Settings map[string]string
}

func (r *Resolver) FetchSettings(ctx context.Context) error {
s, err := r.Query().Settings(ctx, nil, nil)
if err != nil {
return err
}

for _, setting := range s {
r.Settings[setting.Key] = setting.Value
}

r.MailConfig.ApplySettings(r.Settings)

return nil
}
93 changes: 40 additions & 53 deletions server/graph/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,25 @@ func (r *mutationResolver) UpdateStudentAcceptedStatus(ctx context.Context, mail
func (r *mutationResolver) AddTutor(ctx context.Context, tutor models.Tutor) (string, error) {
// database insert happens in the eventsAvailable resolver
eventsAvailable, err := r.Query().Events(ctx, nil, nil, nil, nil, []string{tutor.Mail})
table := hermes.Table{
Columns: hermes.Columns{
CustomWidth: map[string]string{
os.Getenv("EMAIL_ASSIGNMENTS_DATE_TITLE"): "20%",
os.Getenv("EMAIL_ASSIGNMENTS_KIND_TITLE"): "30%",
}}}

for _, event := range eventsAvailable {
e := []hermes.Entry{
{Key: os.Getenv("EMAIL_ASSIGNMENTS_EVENT_TITLE"), Value: event.Title},
{Key: os.Getenv("EMAIL_ASSIGNMENTS_DATE_TITLE"), Value: event.From.Format("02.01")},
{Key: os.Getenv("EMAIL_ASSIGNMENTS_KIND_TITLE"), Value: event.TypeName}}
table.Data = append(table.Data, e)
if err != nil {
return "", err
}

mail := email.Email{
Subject: os.Getenv("EMAIL_CONFIRM_SUBJECT"),
Intros: []string{os.Getenv("EMAIL_CONFIRM_INTRO")},
Outros: []string{os.Getenv("EMAIL_CONFIRM_OUTRO")},
Table: table}
m := r.MailConfig.Confirmation

if err != nil {
return "", err
m.Table.Data = *new([][]hermes.Entry)
for _, event := range eventsAvailable {
e := []hermes.Entry{
{Key: r.Settings["email-assignment-event-title"], Value: event.Title},
{Key: r.Settings["email-assignment-date-title"], Value: event.From.Format("02.01")},
{Key: r.Settings["email-assignment-kind-title"], Value: event.TypeName}}
m.Table.Data = append(m.Table.Data, e)
}

mail.Actions = []hermes.Action{
{
Instructions: os.Getenv("EMAIL_CONFIRM_BUTTON_INSTRUCTION"),
Button: hermes.Button{
Color: os.Getenv("PRIMARY_COLOR"),
Text: os.Getenv("EMAIL_CONFIRM_BUTTON_TEXT"),
Link: fmt.Sprintf("%s/confirm/%s",
os.Getenv("PUBLIC_URL"), strconv.Itoa(int(tutor.SessionID)))}}}
m.Actions[0].Button.Link = fmt.Sprintf("%s/confirm/%s",
os.Getenv("PUBLIC_URL"), strconv.Itoa(int(tutor.SessionID)))

if err := email.Send(tutor.User, mail); err != nil {
if err := email.Send(tutor.User, m, r.MailConfig); err != nil {
return "Failed to send confirmation mail", err
}

Expand Down Expand Up @@ -300,43 +284,46 @@ func (r *mutationResolver) AssignTutorToEvent(ctx context.Context, link models.E
return "Failed to link tutor to event and room", err
}

event, err := r.Query().Events(ctx, []int{int(link.EventID)}, nil, nil, nil, nil)
tutor, err := r.Query().Tutors(ctx, []string{link.TutorMail}, nil)
room, err := r.Query().Rooms(ctx, []string{link.RoomNumber}, int(link.BuildingID))
e, err := r.Query().Events(ctx, []int{int(link.EventID)}, nil, nil, nil, []string{link.TutorMail})
if err != nil {
return "", err
}
event := e[0]

mail := email.Email{
Subject: fmt.Sprintf("%s: %s",
os.Getenv("EMAIL_ASSIGNMENTS_SUBJECT"), event[0].Title),
Intros: []string{os.Getenv("EMAIL_ASSIGNMENTS_INTRO")},
Outros: []string{os.Getenv("EMAIL_ASSIGNMENTS_OUTRO")},
ro, err := r.Query().Rooms(ctx, []string{link.RoomNumber}, int(link.BuildingID))
if err != nil {
return "", err
}
room := ro[0]

m := r.MailConfig.Assignment

m.Subject = fmt.Sprintf("%s: %s",
r.Settings["email-assignment-subject"], event.Title)

roomNumber := room[0].Number
if room[0].Name != "" {
roomNumber := room.Number
if room.Name != "" {
roomNumber = fmt.Sprintf("%s (%s)",
room[0].Name, room[0].Number)
room.Name, room.Number)
}

mail.Dictionary = []hermes.Entry{
{Key: os.Getenv("EMAIL_ASSIGNMENTS_EVENT_TITLE"),
Value: event[0].Title},
{Key: os.Getenv("EMAIL_ASSIGNMENTS_DATE_TITLE"),
Value: event[0].From.Format("02.01.2006")},
{Key: os.Getenv("EMAIL_ASSIGNMENTS_TIME_TITLE"),
m.Dictionary = []hermes.Entry{
{Key: r.Settings["email-assignment-event-title"],
Value: event.Title},
{Key: r.Settings["email-assignment-date-title"],
Value: event.From.Format("02.01.2006")},
{Key: r.Settings["email-assignment-time-title"],
Value: fmt.Sprintf("%s - %s",
event[0].From.Format("15:04"), event[0].To.Format("15:04"))},
{Key: os.Getenv("EMAIL_ASSIGNMENTS_ROOM_TITLE"),
event.From.Format("15:04"), event.To.Format("15:04"))},
{Key: r.Settings["email-assignment-room-title"],
Value: roomNumber},
{Key: os.Getenv("EMAIL_ASSIGNMENTS_BUILDING_TITLE"),
{Key: r.Settings["email-assignment-building-title"],
Value: fmt.Sprintf("%s, %s %s, %s, %s",
room[0].Building.Name,
room[0].Building.Street, room[0].Building.Number,
strconv.Itoa(int(room[0].Building.Zip)), room[0].Building.City)}}
room.Building.Name,
room.Building.Street, room.Building.Number,
strconv.Itoa(int(room.Building.Zip)), room.Building.City)}}

if err := email.Send(tutor[0].User, mail); err != nil {
if err := email.Send(event.TutorsAssigned[0].User, m, r.MailConfig); err != nil {
return "", err
}

Expand Down
8 changes: 7 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ func main() {
log.Fatal(err)
}

resolver = graph.Resolver{DB: db}
resolver = graph.Resolver{
DB: db,
Settings: make(map[string]string),
MailConfig: new(email.Config)}
if err := resolver.FetchSettings(ctx); err != nil {
log.Fatalf("unable to fetch settings: %s", err)
}

// cronjobs for maintenance tasks
c := cron.New()
Expand Down

0 comments on commit 23e7d2b

Please sign in to comment.