Skip to content

Commit

Permalink
Implement send emails question
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-be committed Nov 2, 2023
1 parent e6e416f commit cad9e33
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
10 changes: 8 additions & 2 deletions cmd/secret_santa/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package main

import (
"flag"
"fmt"
"github.com/joho/godotenv"
"log"
"secret-santa/internal/config"
"secret-santa/internal/email"
"secret-santa/internal/processor"

"github.com/joho/godotenv"
)

var yFlag = flag.Bool("y", false, "Send mails directly")

func main() {
flag.Parse()

err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
Expand All @@ -24,6 +30,6 @@ func main() {
return
}

processor := processor.Processor{Config: conf}
processor := processor.Processor{Config: conf, AskBeforeSending: !*yFlag}
processor.Process()
}
13 changes: 12 additions & 1 deletion internal/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"
"math/rand"
"secret-santa/internal/config"
"strings"
)

type Processor struct {
Config *config.Config
Config *config.Config
AskBeforeSending bool
}

var userMapping UserMap = make(map[config.User]config.User)
Expand All @@ -22,6 +24,15 @@ func (processor Processor) Process() {
}
}
fmt.Printf("Succed after %v tries\n", i)
if processor.AskBeforeSending {
fmt.Printf("Summary:\n\n%v\n", userMapping.String())
fmt.Println("Send mails? (y/n)")
var input string
fmt.Scanln(&input)
if strings.ToLower(input) != "y" {
return
}
}
fmt.Println("Sending mails...")
userMapping.sendMails(processor.Config.EmailConfig, processor.Config.SummaryEmail)
}
Expand Down
30 changes: 28 additions & 2 deletions internal/processor/user_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,36 @@ import (

type UserMap map[config.User]config.User

func (userMap UserMap) getLongetsName() int {
longestName := 0
for k := range userMap {
if len(k.Name) > longestName {
longestName = len(k.Name)
}
}
return longestName
}

func AddSpaces(s string, totalLen int) string {
for i := len(s); i < totalLen; i++ {
s += " "
}
return s
}

func (userMap UserMap) String() string {
maxLen := userMap.getLongetsName()*2 + 5
userMappingListString := ""
for k, v := range userMap {
userMappingListString += fmt.Sprintf(" %v (%v has drawn %v)\n", AddSpaces(fmt.Sprintf(" %v => %v", k.Name, v.Name), maxLen), k.Name, v.Name)
}
return userMappingListString
}

func (userMap UserMap) StringHTML() string {
userMappingListString := ""
for k, v := range userMap {
userMappingListString += fmt.Sprintf("<p><b>%v => %v</b> <i>(%v hat %v gezogen)</i></p>\n", k.Name, v.Name, k.Name, v.Name)
userMappingListString += fmt.Sprintf("<p><b>%v => %v</b> <i>(%v has drawn %v)</i></p>\n", k.Name, v.Name, k.Name, v.Name)
}
return userMappingListString
}
Expand Down Expand Up @@ -47,7 +73,7 @@ func (userMap UserMap) sendSummaryMail(conf config.EmailConfig, summaryMail stri
Receiver: summaryMail,
Content: email.Content{
Subject: conf.Subject + " | Summary",
Body: userMap.String(),
Body: userMap.StringHTML(),
},
}
err := mail.SendMail()
Expand Down

0 comments on commit cad9e33

Please sign in to comment.