comparison pkg/imports/email.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
children d966f03ea819
comparison
equal deleted inserted replaced
1656:6dfad1ad59c8 1657:c354631e0018
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2018 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13
14 package imports
15
16 import (
17 "context"
18 "database/sql"
19 "log"
20 "strings"
21 "text/template"
22
23 "gemma.intevation.de/gemma/pkg/auth"
24 "gemma.intevation.de/gemma/pkg/config"
25 "gemma.intevation.de/gemma/pkg/misc"
26 )
27
28 const (
29 selectEmailSQL = `SELECT email_address FROM users.list_users WHERE username = $1`
30
31 importNotificationMailSubject = `import notification mail`
32 )
33
34 var (
35 importNotificationMailTmpl = template.Must(
36 template.New("notification").Parse(`Dear {{ .User }},
37
38 a {{ .Description }} import on server {{ .Server }} triggered
39 this email notification.
40
41 {{ if eq .State "accepted" }}The imported data were successfully integrated into the database.{{ end -}}
42 {{ if eq .State "failed" }}The import failed for some reasons.{{ end -}}
43 {{ if eq .State "pending" }}The imported data could be integrated into the database
44 but your final decision is needed.{{ end }}
45
46 Please follow this link to have a closer look at the details:
47
48 {{ .Server }}/#/?{{ if eq .State "pending" }}review{{ else }}importlog{{ end }}={{ .ID }}
49
50 Best regards
51 Your service team`))
52 )
53
54 func sendNotificationMail(user, description, state string, id int64) {
55 config.WaitReady()
56
57 ctx := context.Background()
58 var email string
59 if err := auth.RunAs(ctx, user,
60 func(conn *sql.Conn) error {
61 return conn.QueryRowContext(ctx, selectEmailSQL, user).Scan(&email)
62 },
63 ); err != nil {
64 log.Printf("error: %v\n", err)
65 return
66 }
67
68 data := struct {
69 User string
70 Description string
71 Server string
72 State string
73 ID int64
74 }{
75 User: user,
76 Description: description,
77 Server: config.ExternalURL(),
78 State: state,
79 ID: id,
80 }
81
82 var body strings.Builder
83 if err := importNotificationMailTmpl.Execute(&body, &data); err != nil {
84 log.Printf("error: %v\n", err)
85 return
86 }
87
88 if err := misc.SendMail(email, importNotificationMailSubject, body.String()); err != nil {
89 log.Printf("error: %v\n", err)
90 }
91 }