Skip to content

Commit

Permalink
feat: email for all NTAs
Browse files Browse the repository at this point in the history
  • Loading branch information
obcode committed Jan 10, 2025
1 parent 8e0f3f8 commit be1c5f7
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 4 deletions.
9 changes: 7 additions & 2 deletions cmd/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var (
Use: "email [subcommand]",
Short: "send email",
Long: `Send emails.
nta --- send emails to teachers about nta,
nta-with-room-alone --- send emails to students with room alone,
nta-with-room-alone --- send emails to students with room alone before planning,
nta-planned --- send emails about rooms to all students with nta after planning,
primuss-data [all|<ancode>] --- send emails to teachers about primuss data and nta.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -27,6 +27,11 @@ primuss-data [all|<ancode>] --- send emails to teachers about primuss data and n
if err != nil {
log.Fatalf("got error: %v\n", err)
}
case "nta-planned":
err := plexams.SendHandicapsMailsNTAPlanned(context.Background(), run)
if err != nil {
log.Fatalf("got error: %v\n", err)
}
case "primuss-data":
if len(args) < 2 {
log.Fatal("need program and primuss-ancode")
Expand Down
134 changes: 132 additions & 2 deletions plexams/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"

// TODO: Ersetzen durch github.com/wneessen/go-mail

set "github.com/deckarep/golang-set/v2"
"github.com/jordan-wright/email"
"github.com/logrusorgru/aurora"
"github.com/obcode/plexams.go/graph/model"
Expand Down Expand Up @@ -349,7 +351,7 @@ func (p *Plexams) SendHandicapsMailsNTARoomAlone(ctx context.Context, run bool)
}
}

err = p.SendHandicapsMailToStudent(ctx, to, cc, &NTAEmail{
err = p.SendHandicapsMailToStudentRoomAlone(ctx, to, cc, &NTAEmail{
NTA: nta,
Exams: exams,
PlanerName: p.planer.Name,
Expand All @@ -362,7 +364,7 @@ func (p *Plexams) SendHandicapsMailsNTARoomAlone(ctx context.Context, run bool)
return nil
}

func (p *Plexams) SendHandicapsMailToStudent(ctx context.Context, to []string, cc []string, handicapsEmail *NTAEmail) error {
func (p *Plexams) SendHandicapsMailToStudentRoomAlone(ctx context.Context, to []string, cc []string, handicapsEmail *NTAEmail) error {
log.Debug().Interface("to", to).Msg("sending email")

tmpl, err := template.ParseFiles("tmpl/handicapEmailRoomAlone.tmpl")
Expand Down Expand Up @@ -393,6 +395,134 @@ func (p *Plexams) SendHandicapsMailToStudent(ctx context.Context, to []string, c
)
}

type NTAEmailExamAndRoom struct {
Exam *model.PlannedExam
Room *model.PlannedRoom
Invigilator *model.Teacher
}

type NTAEmailWithRooms struct {
NTA *model.Student
ExamsWithRoom []NTAEmailExamAndRoom
PlanerName string
}

func (p *Plexams) SendHandicapsMailsNTAPlanned(ctx context.Context, run bool) error {
ntas, err := p.NtasWithRegs(ctx)
if err != nil {
log.Error().Err(err).Msg("cannot get ntas")
return err
}

atLeastOneEmailMissing := false
for _, nta := range ntas {
if nta.Nta.Email == nil || *nta.Nta.Email == "" {
log.Error().Str("mtknr", nta.Mtknr).Str("name", nta.Name).Msg("no email set")
atLeastOneEmailMissing = true
}
}
if atLeastOneEmailMissing {
return fmt.Errorf("at least one email missing")
}

for _, nta := range ntas {
exams := make([]*model.PlannedExam, 0, len(nta.Regs))
for _, ancode := range nta.Regs {
exam, err := p.PlannedExam(ctx, ancode)
if err != nil {
log.Error().Err(err).Int("ancode", ancode).Msg("cannot get exam")
return err
}
if exam.Constraints == nil || !exam.Constraints.NotPlannedByMe {
exams = append(exams, exam)
}
}

var to, cc []string

examsWithRoom := make([]NTAEmailExamAndRoom, 0, len(exams))
to = []string{*nta.Nta.Email}
ccSet := set.NewSet[string]()
for _, exam := range exams {
teacher, err := p.GetTeacher(ctx, exam.ZpaExam.MainExamerID)
if err != nil {
log.Error().Err(err).Int("ancode", exam.Ancode).Msg("cannot get teacher")
return err
}
ccSet.Add(teacher.Email)
// invigilators cc
room, err := p.PlannedRoomForStudent(ctx, exam.Ancode, nta.Mtknr)
if room == nil || err != nil {
log.Error().Int("ancode", exam.Ancode).Str("mtknr", nta.Mtknr).Msg("no room")
continue
}
invigilator, err := p.GetInvigilatorInSlot(ctx, room.RoomName, exam.PlanEntry.DayNumber, exam.PlanEntry.SlotNumber)
if err != nil || invigilator == nil {
log.Error().Err(err).Int("ancode", exam.Ancode).Str("room", room.RoomName).
Int("slot", exam.PlanEntry.SlotNumber).Int("day", exam.PlanEntry.DayNumber).
Msg("cannot get invigilator")
continue
}
log.Debug().Str("mtknr", nta.Mtknr).Str("name", nta.Name).Str("room", room.RoomName).Str("invigilator", invigilator.Fullname).
Msg("found info")
ccSet.Add(invigilator.Email)
examsWithRoom = append(examsWithRoom, NTAEmailExamAndRoom{
Exam: exam,
Room: room,
Invigilator: invigilator,
})
}
cc = ccSet.ToSlice()

if !run {
to = []string{p.planer.Email}
log.Debug().Interface("cc", cc).Msg("not sending cc")
cc = []string{}
}

err = p.SendHandicapsMailToStudentPlanned(ctx, to, cc, &NTAEmailWithRooms{
NTA: nta,
ExamsWithRoom: examsWithRoom,
PlanerName: p.planer.Name,
})
if err != nil {
return err
}
}
return nil
}

func (p *Plexams) SendHandicapsMailToStudentPlanned(ctx context.Context, to []string, cc []string, handicapsEmail *NTAEmailWithRooms) error {
log.Debug().Interface("to", to).Msg("sending email")

tmpl, err := template.ParseFiles("tmpl/handicapEmailPlanned.tmpl")
if err != nil {
return err
}
bufText := new(bytes.Buffer)
err = tmpl.Execute(bufText, handicapsEmail)
if err != nil {
return err
}

tmpl, err = template.ParseFiles("tmpl/handicapEmailPlannedHTML.tmpl")
if err != nil {
return err
}
bufHTML := new(bytes.Buffer)
err = tmpl.Execute(bufHTML, handicapsEmail)
if err != nil {
return err
}

return p.sendMail(to,
cc,
fmt.Sprintf("[Prüfungsplanung %s] Räume für Ihre Prüfung(en)", p.semester),
bufText.Bytes(),
bufHTML.Bytes(),
)
}

func (p *Plexams) sendMail(to []string, cc []string, subject string, text []byte, html []byte) error {
e := &email.Email{
To: to,
Expand Down
18 changes: 18 additions & 0 deletions tmpl/handicapEmailPlanned.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Hallo {{ .NTA.Name }},

Sie haben gemäß den mir vorliegenden Informationen Anspruch auf Nachteilsausgleich
bei Ihren Prüfungen.

Für folgende Prüfungen, für die Sie sich angemeldet haben, habe ich Sie im jeweils angegebenen Raum eingeplant:

{{ range .ExamsWithRoom -}}
- {{ .Room.RoomName }} --- {{ .Exam.ZpaExam.MainExamer }}: {{ .Exam.ZpaExam.Module }}
{{ end }}

Mit freundlichen Grüßen
{{ .PlanerName }}

<pre>
--
Diese E-Mail wurde generiert und gesendet von https://github.com/obcode/plexams.go
</pre>
22 changes: 22 additions & 0 deletions tmpl/handicapEmailPlannedHTML.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<p>Hallo {{ .NTA.Name }},</p>

<p>Sie haben gemäß den mir vorliegenden Informationen Anspruch auf Nachteilsausgleich
bei Ihren Prüfungen.</p>

<p>Für folgende Prüfungen habe ich Sie im jeweils angegebenen Raum eingeplant:</p>

<ul>
{{ range .ExamsWithRoom -}}
<li>
{{ .Room.RoomName }} --- {{ .Exam.ZpaExam.MainExamer }}: {{ .Exam.ZpaExam.Module }}
</li>
{{ end }}
</ul>

<p>Mit freundlichen Grüßen<br />
{{ .PlanerName }}</p>

<pre>
--
Diese E-Mail wurde generiert und gesendet von https://github.com/obcode/plexams.go
</pre>

0 comments on commit be1c5f7

Please sign in to comment.