-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
363 lines (317 loc) · 8.89 KB
/
email.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package plexams
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"html/template"
"net/smtp"
"net/textproto"
"time"
// TODO: Ersetzen durch github.com/wneessen/go-mail
"github.com/jordan-wright/email"
"github.com/logrusorgru/aurora"
"github.com/obcode/plexams.go/graph/model"
"github.com/rs/zerolog/log"
"github.com/theckman/yacspin"
)
type HandicapsEmail struct {
MainExamer string
Exams []*HandicapExam
PlanerName string
}
type HandicapExam struct {
AnCode int
Module string
TypeExamFull string
HandicapStudents []*HandicapStudent
}
type HandicapStudent struct {
Name string
Compensation string
}
func (p *Plexams) SendTestMail() error {
e := &email.Email{
To: []string{p.planer.Email},
From: fmt.Sprintf("%s <%s>", p.planer.Name, p.planer.Email),
Subject: "Awesome Subject",
Text: []byte("Text Body is, of course, supported!"),
HTML: []byte("<h1>Fancy HTML is supported, too!</h1>"),
Headers: textproto.MIMEHeader{},
}
return e.SendWithStartTLS(fmt.Sprintf("%s:%d", p.email.server, p.email.port),
smtp.PlainAuth("", p.email.username, p.email.password, p.email.server),
&tls.Config{
InsecureSkipVerify: true,
ServerName: p.email.server,
})
}
func (p *Plexams) SendGeneratedExamMails(ctx context.Context, run bool) error {
generatedExams, err := p.GeneratedExams(ctx)
if err != nil {
log.Error().Err(err).Msg("cannot get generated exams")
return err
}
notFromZPA := false
teachers, err := p.GetTeachers(ctx, ¬FromZPA)
if err != nil {
log.Error().Err(err).Msg("cannot get generated exams")
return err
}
teachersMap := make(map[int]*model.Teacher)
for _, teacher := range teachers {
teachersMap[teacher.ID] = teacher
}
for _, exam := range generatedExams {
cfg := yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[69],
Suffix: aurora.Sprintf(aurora.Cyan(" sending email about exam %d. %s (%s)"),
aurora.Yellow(exam.ZpaExam.AnCode),
aurora.Magenta(exam.ZpaExam.Module),
aurora.Magenta(exam.ZpaExam.MainExamer),
),
SuffixAutoColon: true,
StopCharacter: "✓",
StopColors: []string{"fgGreen"},
StopFailMessage: "not planned by me",
StopFailCharacter: "✗",
StopFailColors: []string{"fgRed"},
}
spinner, err := yacspin.New(cfg)
if err != nil {
log.Debug().Err(err).Msg("cannot create spinner")
}
err = spinner.Start()
if err != nil {
log.Debug().Err(err).Msg("cannot start spinner")
}
if exam.Constraints != nil && exam.Constraints.NotPlannedByMe {
err = spinner.StopFail()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
continue
}
teacher, ok := teachersMap[exam.ZpaExam.MainExamerID]
if !ok {
log.Debug().Int("ancode", exam.Ancode).Str("module", exam.ZpaExam.Module).Str("teacher", exam.ZpaExam.MainExamer).
Msg("no info about teacher in zpa")
continue
}
var to string
if run {
to = teacher.Email
} else {
to = "galority@gmail.com"
}
hasStudentRegs := false
for _, primussExam := range exam.PrimussExams {
hasStudentRegs = hasStudentRegs || len(primussExam.StudentRegs) > 0
}
err = p.SendGeneratedExamMailToTeacher(ctx, to, &GeneratedExamMailData{
Exam: exam,
Teacher: teacher,
PlanerName: p.planer.Name,
HasStudentRegs: hasStudentRegs,
})
if err != nil {
log.Error().Err(err).Msg("cannot send email")
return err
}
err = spinner.Stop()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}
return nil
}
type GeneratedExamMailData struct {
Exam *model.GeneratedExam
Teacher *model.Teacher
PlanerName string
HasStudentRegs bool
}
func (p *Plexams) SendGeneratedExamMailToTeacher(ctx context.Context, to string, generatedExamMailData *GeneratedExamMailData) error {
log.Debug().Interface("to", to).Msg("sending email")
tmpl, err := template.ParseFiles("tmpl/generatedExamEmail.tmpl")
if err != nil {
return err
}
bufText := new(bytes.Buffer)
err = tmpl.Execute(bufText, generatedExamMailData)
if err != nil {
return err
}
tmpl, err = template.ParseFiles("tmpl/generatedExamEmailHTML.tmpl")
if err != nil {
return err
}
bufHTML := new(bytes.Buffer)
err = tmpl.Execute(bufHTML, generatedExamMailData)
if err != nil {
return err
}
subject := fmt.Sprintf("[Prüfungsplanung %s] Vorliegende Planungsdaten für Ihre Prüfung %s",
p.semester, generatedExamMailData.Exam.ZpaExam.Module)
if !generatedExamMailData.HasStudentRegs {
subject = fmt.Sprintf("[Prüfungsplanung %s] Keine Anmeldungen für Ihre Prüfung %s",
p.semester, generatedExamMailData.Exam.ZpaExam.Module)
}
return p.sendMail([]string{to},
subject,
bufText.Bytes(),
bufHTML.Bytes(),
)
}
// Deprecated: rm me
func (p *Plexams) SendHandicapsMails(ctx context.Context, run bool) error {
ntasByTeacher, err := p.NtasWithRegsByTeacher(ctx)
if err != nil {
return err
}
for _, nta := range ntasByTeacher {
exams := make([]*HandicapExam, 0, len(nta.Exams))
for _, exam := range nta.Exams {
handicapStudents := make([]*HandicapStudent, 0, len(exam.Ntas))
for _, ntaForExam := range exam.Ntas {
handicapStudents = append(handicapStudents, &HandicapStudent{
Name: ntaForExam.Nta.Name,
Compensation: ntaForExam.Nta.Compensation,
})
}
exams = append(exams, &HandicapExam{
AnCode: exam.Exam.AnCode,
Module: exam.Exam.Module,
TypeExamFull: exam.Exam.ExamTypeFull,
HandicapStudents: handicapStudents,
})
}
var to []string
if run {
to = []string{nta.Teacher.Email}
} else {
to = []string{"galority@gmail.com"}
}
err = p.SendHandicapsMailToMainExamer(ctx, to, &HandicapsEmail{
MainExamer: nta.Teacher.Fullname,
Exams: exams,
PlanerName: p.planer.Name,
})
if err != nil {
return err
}
}
return nil
}
// Deprecated: rm me
func (p *Plexams) SendHandicapsMailToMainExamer(ctx context.Context, to []string, handicapsEmail *HandicapsEmail) error {
log.Debug().Interface("to", to).Msg("sending email")
tmpl, err := template.ParseFiles("tmpl/handicapEmail.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/handicapEmailHTML.tmpl")
if err != nil {
return err
}
bufHTML := new(bytes.Buffer)
err = tmpl.Execute(bufHTML, handicapsEmail)
if err != nil {
return err
}
return p.sendMail(to,
fmt.Sprintf("[Prüfungsplanung %s] Nachteilausgleich(e) für Ihre Prüfung(en)", p.semester),
bufText.Bytes(),
bufHTML.Bytes(),
)
}
type NTAEmail struct {
NTA *model.Student
Exams []*model.PlannedExam
PlanerName string
}
func (p *Plexams) SendHandicapsMailsNTARoomAlone(ctx context.Context, run bool) error {
ntas, err := p.NtasWithRegs(ctx)
if err != nil {
log.Error().Err(err).Msg("cannot get ntas")
return err
}
for _, nta := range ntas {
if !nta.Nta.NeedsRoomAlone {
continue
}
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)
}
}
err = p.SendHandicapsMailToStudent(ctx, []string{p.planer.Email}, &NTAEmail{
NTA: nta,
Exams: exams,
PlanerName: p.planer.Name,
})
if err != nil {
return err
}
}
return nil
}
func (p *Plexams) SendHandicapsMailToStudent(ctx context.Context, to []string, handicapsEmail *NTAEmail) error {
log.Debug().Interface("to", to).Msg("sending email")
tmpl, err := template.ParseFiles("tmpl/handicapEmailRoomAlone.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/handicapEmailRoomAloneHTML.tmpl")
if err != nil {
return err
}
bufHTML := new(bytes.Buffer)
err = tmpl.Execute(bufHTML, handicapsEmail)
if err != nil {
return err
}
return p.sendMail(to,
fmt.Sprintf("[Prüfungsplanung %s] Eigener Raum für Ihre Prüfung(en)", p.semester),
bufText.Bytes(),
bufHTML.Bytes(),
)
}
func (p *Plexams) sendMail(to []string, subject string, text []byte, html []byte) error {
e := &email.Email{
To: to,
Bcc: []string{p.planer.Email},
From: fmt.Sprintf("%s <%s>", p.planer.Name, p.planer.Email),
Subject: subject,
Text: text,
HTML: html,
Headers: textproto.MIMEHeader{},
}
err := e.SendWithStartTLS(fmt.Sprintf("%s:%d", p.email.server, p.email.port),
smtp.PlainAuth("", p.email.username, p.email.password, p.email.server),
&tls.Config{
InsecureSkipVerify: true,
ServerName: p.email.server,
})
if err != nil {
return err
}
return p.Log(context.Background(), fmt.Sprintf("send email to %s", to), string(text))
}