comparison pkg/imports/queue.go @ 1657:c354631e0018

Import queue: Moved email notification stuff to separate file.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 21 Dec 2018 10:33:49 +0100
parents 6dfad1ad59c8
children 4a2fad8f57de
comparison
equal deleted inserted replaced
1656:6dfad1ad59c8 1657:c354631e0018
16 import ( 16 import (
17 "context" 17 "context"
18 "database/sql" 18 "database/sql"
19 "encoding/json" 19 "encoding/json"
20 "fmt" 20 "fmt"
21 "html/template"
22 "log" 21 "log"
23 "runtime/debug" 22 "runtime/debug"
24 "strings" 23 "strings"
25 "sync" 24 "sync"
26 "time" 25 "time"
27 26
28 "github.com/jackc/pgx/pgtype" 27 "github.com/jackc/pgx/pgtype"
29 28
30 "gemma.intevation.de/gemma/pkg/auth" 29 "gemma.intevation.de/gemma/pkg/auth"
31 "gemma.intevation.de/gemma/pkg/config" 30 "gemma.intevation.de/gemma/pkg/config"
32 "gemma.intevation.de/gemma/pkg/misc"
33 ) 31 )
34 32
35 type ( 33 type (
36 // Feedback is passed to the Do method of a Job to log 34 // Feedback is passed to the Do method of a Job to log
37 // informations, warnings or errors. 35 // informations, warnings or errors.
527 go sendNotificationMail(idj.user, jc.Description(), state, idj.id) 525 go sendNotificationMail(idj.user, jc.Description(), state, idj.id)
528 } 526 }
529 }(jc, idj) 527 }(jc, idj)
530 } 528 }
531 } 529 }
532
533 const (
534 selectEmailSQL = `SELECT email_address FROM users.list_users WHERE username = $1`
535
536 importNotificationMailSubject = `import notification mail`
537 )
538
539 var (
540 importNotificationMailTmpl = template.Must(
541 template.New("notification").Parse(`Dear {{ .User }},
542
543 a {{ .Description }} import on server {{ .Server }} triggered
544 this email notification.
545
546 {{ if eq .State "accepted" }}The imported data were successfully integrated into the database.{{ end -}}
547 {{ if eq .State "failed" }}The import failed for some reasons.{{ end -}}
548 {{ if eq .State "pending" }}The imported data could be integrated into the database
549 but your final decision is needed.{{ end }}
550
551 Please follow this link to have a closer look at the details:
552
553 {{ .Server }}/#/?{{ if eq .State "pending" }}review{{ else }}importlog{{ end }}={{ .ID }}
554
555 Best regards
556 Your service team`))
557 )
558
559 func sendNotificationMail(user, description, state string, id int64) {
560 config.WaitReady()
561
562 ctx := context.Background()
563 var email string
564 if err := auth.RunAs(ctx, user,
565 func(conn *sql.Conn) error {
566 return conn.QueryRowContext(ctx, selectEmailSQL, user).Scan(&email)
567 },
568 ); err != nil {
569 log.Printf("error: %v\n", err)
570 return
571 }
572
573 data := struct {
574 User string
575 Description string
576 Server string
577 State string
578 ID int64
579 }{
580 User: user,
581 Description: description,
582 Server: config.ExternalURL(),
583 State: state,
584 ID: id,
585 }
586
587 var body strings.Builder
588 if err := importNotificationMailTmpl.Execute(&body, &data); err != nil {
589 log.Printf("error: %v\n", err)
590 return
591 }
592
593 if err := misc.SendMail(email, importNotificationMailSubject, body.String()); err != nil {
594 log.Printf("error: %v\n", err)
595 }
596 }