Skip to content

Commit dad2e2c

Browse files
committed
feat: email new nta
1 parent 3ef0390 commit dad2e2c

11 files changed

Lines changed: 285 additions & 112 deletions

cmd/email.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ published-exams --- announce published exams
2424
published-rooms --- announce published rooms
2525
invigilations --- send email requesting invigilations constraints
2626
published-invigilations --- announce published invigilations
27+
new-nta --- send emails to examers about new nta
2728
nta-with-room-alone --- send emails to students with room alone before planning
2829
nta-planned --- send emails about rooms to all students with nta after planning
2930
cover-pages [all|<teacherid>] --- send emails with externally generated cover pages
@@ -102,6 +103,15 @@ cover-pages [all|<teacherid>] --- send emails with externally generated cove
102103
if err != nil {
103104
log.Fatalf("got error: %v\n", err)
104105
}
106+
case "new-nta":
107+
if len(args) < 2 {
108+
log.Fatal("need mtknr of new nta")
109+
}
110+
mtknr := args[1]
111+
err := plexams.SendMailNewNTA(context.Background(), mtknr, run)
112+
if err != nil {
113+
log.Fatalf("got error: %v\n", err)
114+
}
105115
case "nta-with-room-alone":
106116
err := plexams.SendHandicapsMailsNTARoomAlone(context.Background(), run)
107117
if err != nil {

db/primuss_studentregs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func (db *DB) RemoveStudentReg(ctx context.Context, program string, ancode int,
344344
func (db *DB) AddStudentReg(ctx context.Context, program string, ancode int, mtknr string) error {
345345
collection := db.getCollection(program, StudentRegs)
346346

347-
student, err := db.StudentByMtknr(ctx, mtknr, nil)
347+
student, err := db.StudentByMtknr(ctx, mtknr)
348348
if err != nil {
349349
log.Error().Err(err).Str("program", program).Int("ancode", ancode).Str("mtknr", mtknr).
350350
Msg("error while trying to get student by mtknr")

db/studentregs.go

Lines changed: 84 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package db
22

33
import (
44
"context"
5-
"sort"
65

76
set "github.com/deckarep/golang-set/v2"
87
"github.com/obcode/plexams.go/graph/model"
@@ -37,88 +36,103 @@ func (db *DB) StudentRegsPerStudentPlanned(ctx context.Context) ([]*model.Studen
3736
return studentRegs, nil
3837
}
3938

40-
func (db *DB) StudentByMtknr(ctx context.Context, mtknr string, ntas map[string]*model.NTA) (*model.Student, error) {
41-
collectionNames, err := db.studentRegsCollectionNames(ctx)
39+
func (db *DB) StudentByMtknr(ctx context.Context, mtknr string) (*model.Student, error) {
40+
collection := db.Client.Database(db.databaseName).Collection(collectionStudentRegsPerStudentPlanned)
41+
filter := bson.D{{Key: "mtknr", Value: mtknr}}
4242

43+
var student model.Student
44+
45+
err := collection.FindOne(ctx, filter).Decode(&student)
4346
if err != nil {
44-
log.Error().Err(err).Msg("cannot get student regs collections")
47+
log.Error().Err(err).Str("mtknr", mtknr).Msg("cannot find student by mtknr")
4548
return nil, err
4649
}
4750

48-
var student *model.Student
51+
return &student, nil
52+
}
4953

50-
for _, collectionName := range collectionNames {
51-
log.Debug().Str("collection", collectionName).Str("mtkntr", mtknr).
52-
Msg("searching for student in collection")
54+
// func (db *DB) StudentByMtknr(ctx context.Context, mtknr string, ntas map[string]*model.NTA) (*model.Student, error) {
55+
// collectionNames, err := db.studentRegsCollectionNames(ctx)
5356

54-
collection := db.Client.Database(db.databaseName).Collection(collectionName)
57+
// if err != nil {
58+
// log.Error().Err(err).Msg("cannot get student regs collections")
59+
// return nil, err
60+
// }
5561

56-
cur, err := collection.Find(ctx, bson.D{{Key: "MTKNR", Value: mtknr}})
57-
if err != nil {
58-
log.Error().Err(err).Str("collection", collectionName).Str("mtkntr", mtknr).
59-
Msg("error while searching for student in collection")
60-
}
61-
defer cur.Close(ctx) //nolint:errcheck
62+
// var student *model.Student
6263

63-
var results []*model.StudentReg
64+
// for _, collectionName := range collectionNames {
65+
// log.Debug().Str("collection", collectionName).Str("mtkntr", mtknr).
66+
// Msg("searching for student in collection")
6467

65-
err = cur.All(ctx, &results)
66-
if err != nil {
67-
log.Error().Err(err).Str("collection", collectionName).Str("mtkntr", mtknr).
68-
Msg("error while decoding student from collection")
69-
}
68+
// collection := db.Client.Database(db.databaseName).Collection(collectionName)
7069

71-
if len(results) > 0 {
72-
log.Debug().Interface("regs", results).Str("collection", collectionName).Str("mtkntr", mtknr).
73-
Msg("found regs for student")
74-
75-
var regs []int
76-
77-
if student != nil && (student.Program != results[0].Program ||
78-
student.Group != results[0].Group ||
79-
student.Name != results[0].Name) {
80-
log.Error().Str("collection", collectionName).Str("mtkntr", mtknr).
81-
Msg("found student in more than one programs")
82-
}
83-
84-
if student != nil {
85-
regs = student.Regs
86-
} else {
87-
regs = make([]int, 0, len(results))
88-
}
89-
90-
for _, res := range results {
91-
regs = append(regs, res.AnCode)
92-
}
93-
94-
sort.Ints(regs)
95-
96-
var nta *model.NTA
97-
98-
if ntas == nil {
99-
nta, err = db.Nta(ctx, mtknr)
100-
if err != nil {
101-
log.Error().Err(err).Str("mtknr", mtknr).Msg("error while checking nta")
102-
}
103-
} else {
104-
nta = ntas[mtknr]
105-
}
106-
107-
student = &model.Student{
108-
Mtknr: mtknr,
109-
Program: results[0].Program,
110-
Group: results[0].Group,
111-
Name: results[0].Name,
112-
Regs: regs,
113-
Nta: nta,
114-
}
70+
// cur, err := collection.Find(ctx, bson.D{{Key: "MTKNR", Value: mtknr}})
71+
// if err != nil {
72+
// log.Error().Err(err).Str("collection", collectionName).Str("mtkntr", mtknr).
73+
// Msg("error while searching for student in collection")
74+
// }
75+
// defer cur.Close(ctx) //nolint:errcheck
11576

116-
}
77+
// var results []*model.StudentReg
11778

118-
}
79+
// err = cur.All(ctx, &results)
80+
// if err != nil {
81+
// log.Error().Err(err).Str("collection", collectionName).Str("mtkntr", mtknr).
82+
// Msg("error while decoding student from collection")
83+
// }
11984

120-
return student, nil
121-
}
85+
// if len(results) > 0 {
86+
// log.Debug().Interface("regs", results).Str("collection", collectionName).Str("mtkntr", mtknr).
87+
// Msg("found regs for student")
88+
89+
// var regs []int
90+
91+
// if student != nil && (student.Program != results[0].Program ||
92+
// student.Group != results[0].Group ||
93+
// student.Name != results[0].Name) {
94+
// log.Error().Str("collection", collectionName).Str("mtkntr", mtknr).
95+
// Msg("found student in more than one programs")
96+
// }
97+
98+
// if student != nil {
99+
// regs = student.Regs
100+
// } else {
101+
// regs = make([]int, 0, len(results))
102+
// }
103+
104+
// for _, res := range results {
105+
// regs = append(regs, res.AnCode)
106+
// }
107+
108+
// sort.Ints(regs)
109+
110+
// var nta *model.NTA
111+
112+
// if ntas == nil {
113+
// nta, err = db.Nta(ctx, mtknr)
114+
// if err != nil {
115+
// log.Error().Err(err).Str("mtknr", mtknr).Msg("error while checking nta")
116+
// }
117+
// } else {
118+
// nta = ntas[mtknr]
119+
// }
120+
121+
// student = &model.Student{
122+
// Mtknr: mtknr,
123+
// Program: results[0].Program,
124+
// Group: results[0].Group,
125+
// Name: results[0].Name,
126+
// Regs: regs,
127+
// Nta: nta,
128+
// }
129+
130+
// }
131+
132+
// }
133+
134+
// return student, nil
135+
// }
122136

123137
func (db *DB) StudentsByName(ctx context.Context, regex string) ([]*model.Student, error) {
124138
collectionNames, err := db.studentRegsCollectionNames(ctx)
@@ -152,7 +166,7 @@ func (db *DB) StudentsByName(ctx context.Context, regex string) ([]*model.Studen
152166
students := make([]*model.Student, 0, studentMtknrs.Cardinality())
153167

154168
for _, mtknr := range studentMtknrs.ToSlice() {
155-
student, err := db.StudentByMtknr(ctx, mtknr, nil)
169+
student, err := db.StudentByMtknr(ctx, mtknr)
156170
if err != nil {
157171
log.Error().Err(err).Str("mtknr", mtknr).Msg("error while trying to get student")
158172
} else {

graph/studentregs.resolvers.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plexams/email.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
//go:embed tmpl/handicapEmailPlannedHTML.tmpl
3030
//go:embed tmpl/handicapEmailRoomAlone.tmpl
3131
//go:embed tmpl/handicapEmailRoomAloneHTML.tmpl
32+
//go:embed tmpl/newNTAEmail.tmpl
33+
//go:embed tmpl/newNTAEmailHTML.tmpl
3234
//go:embed tmpl/preparedEmail.tmpl
3335
//go:embed tmpl/preparedEmailHTML.tmpl
3436
//go:embed tmpl/publishedEmailExams.tmpl

plexams/email_nta.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,84 @@ func (p *Plexams) SendHandicapsMailToStudentPlanned(ctx context.Context, to []st
249249
true,
250250
)
251251
}
252+
253+
type NewNTA struct {
254+
Student *model.Student
255+
Exams []*model.PlannedExam
256+
PlanerName string
257+
}
258+
259+
func (p *Plexams) SendMailNewNTA(ctx context.Context, mtknr string, run bool) error {
260+
student, err := p.StudentByMtknr(ctx, mtknr)
261+
if err != nil {
262+
log.Error().Err(err).Str("mtknr", mtknr).Msg("cannot get nta")
263+
return err
264+
}
265+
if student.Nta == nil {
266+
log.Error().Str("mtknr", mtknr).Msg("student is not an nta")
267+
return fmt.Errorf("student is not an nta")
268+
}
269+
examerIDsSet := set.NewSet[int]()
270+
exams := make([]*model.PlannedExam, 0, len(student.Regs))
271+
for _, ancode := range student.Regs {
272+
exam, err := p.PlannedExam(ctx, ancode)
273+
if err != nil {
274+
log.Error().Err(err).Int("ancode", ancode).Msg("cannot get exam")
275+
return err
276+
}
277+
if exam.Constraints != nil && exam.Constraints.NotPlannedByMe {
278+
continue
279+
}
280+
examerIDsSet.Add(exam.ZpaExam.MainExamerID)
281+
exams = append(exams, exam)
282+
}
283+
to := make([]string, 0, examerIDsSet.Cardinality())
284+
for _, examerID := range examerIDsSet.ToSlice() {
285+
examer, err := p.GetTeacher(ctx, examerID)
286+
if err != nil {
287+
log.Error().Err(err).Int("teacherID", examerID).Msg("cannot get examer")
288+
return err
289+
}
290+
to = append(to, examer.Email)
291+
}
292+
log.Debug().Interface("to", to).Msg("sending email to examers about new nta")
293+
294+
newNTA := &NewNTA{
295+
Student: student,
296+
Exams: exams,
297+
PlanerName: p.planer.Name,
298+
}
299+
300+
tmpl, err := template.ParseFS(emailTemplates, "tmpl/newNTAEmail.tmpl")
301+
if err != nil {
302+
return err
303+
}
304+
bufText := new(bytes.Buffer)
305+
err = tmpl.Execute(bufText, newNTA)
306+
if err != nil {
307+
return err
308+
}
309+
tmpl, err = template.ParseFS(emailTemplates, "tmpl/newNTAEmailHTML.tmpl")
310+
if err != nil {
311+
return err
312+
}
313+
bufHTML := new(bytes.Buffer)
314+
err = tmpl.Execute(bufHTML, newNTA)
315+
if err != nil {
316+
return err
317+
}
318+
319+
if !run {
320+
to = []string{p.planer.Email}
321+
}
322+
323+
return p.sendMail(to,
324+
nil,
325+
fmt.Sprintf("[Prüfungsplanung %s] Neuer NTA", p.semester),
326+
bufText.Bytes(),
327+
bufHTML.Bytes(),
328+
nil,
329+
true,
330+
)
331+
332+
}

plexams/nta.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func (p *Plexams) NtasWithRegs(ctx context.Context) ([]*model.Student, error) {
1818
return p.dbClient.NtasWithRegs(ctx)
1919
}
2020

21+
// Deprecated: use StudentByMtknr
2122
func (p *Plexams) Nta(ctx context.Context, mtknr string) (*model.NTAWithRegs, error) {
2223
return p.dbClient.NtaWithRegs(ctx, mtknr)
2324
}

plexams/rooms.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func (p *Plexams) PreAddRoomToExam(ctx context.Context, ancode int, roomName str
316316
}
317317

318318
if mtknr != nil {
319-
student, err := p.StudentByMtknr(ctx, *mtknr, nil)
319+
student, err := p.StudentByMtknr(ctx, *mtknr)
320320
if err != nil {
321321
log.Error().Err(err).Str("mtknr", *mtknr).Msg("cannot get student by mtknr")
322322
return false, err

0 commit comments

Comments
 (0)