Mercurial > gemma
annotate pkg/imports/email.go @ 5244:8f9cd1df5028 new-fwa
Fixed a copy-by-value problem.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 12 May 2020 19:11:02 +0200 |
parents | 92812bf2f008 |
children | 5f47eeea988d |
rev | line source |
---|---|
1657
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package imports |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 "context" |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "database/sql" |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "log" |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 "strings" |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 "text/template" |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 "gemma.intevation.de/gemma/pkg/auth" |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 "gemma.intevation.de/gemma/pkg/config" |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 "gemma.intevation.de/gemma/pkg/misc" |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 ) |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 const ( |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 selectEmailSQL = `SELECT email_address FROM users.list_users WHERE username = $1` |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 |
2895
92812bf2f008
Improve notification email contents
Bernhard Reiter <bernhard@intevation.de>
parents:
2797
diff
changeset
|
31 importNotificationMailSubject = `Gemma: import notification mail` |
1657
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 ) |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 var ( |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 importNotificationMailTmpl = template.Must( |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 template.New("notification").Parse(`Dear {{ .User }}, |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 a {{ .Description }} import on server {{ .Server }} triggered |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 this email notification. |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 |
2895
92812bf2f008
Improve notification email contents
Bernhard Reiter <bernhard@intevation.de>
parents:
2797
diff
changeset
|
41 {{ if eq .State "accepted" }}The imported data were successfully integrated into the database. {{ end -}} |
92812bf2f008
Improve notification email contents
Bernhard Reiter <bernhard@intevation.de>
parents:
2797
diff
changeset
|
42 {{ if eq .State "unchanged" }}The import has not changed any data in the database. {{ end -}} |
92812bf2f008
Improve notification email contents
Bernhard Reiter <bernhard@intevation.de>
parents:
2797
diff
changeset
|
43 {{ if eq .State "failed" }}The import failed for some reasons. {{ end -}} |
1657
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 {{ if eq .State "pending" }}The imported data could be integrated into the database |
2895
92812bf2f008
Improve notification email contents
Bernhard Reiter <bernhard@intevation.de>
parents:
2797
diff
changeset
|
45 but your final decision is needed. {{ end -}} |
1657
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 Please follow this link to have a closer look at the details: |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 |
2797
3c83d1cc0348
email.go: update link to new format
Thomas Junk <thomas.junk@intevation.de>
parents:
2346
diff
changeset
|
49 {{ .Server }}/#/imports/overview/{{ .ID }} |
1657
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 Best regards |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 Your service team`)) |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 ) |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 func sendNotificationMail(user, description, state string, id int64) { |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 config.WaitReady() |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 ctx := context.Background() |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 var email string |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 if err := auth.RunAs(ctx, user, |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 func(conn *sql.Conn) error { |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 return conn.QueryRowContext(ctx, selectEmailSQL, user).Scan(&email) |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 }, |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 ); err != nil { |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 log.Printf("error: %v\n", err) |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 return |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 } |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 data := struct { |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 User string |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 Description string |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 Server string |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 State string |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 ID int64 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 }{ |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 User: user, |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 Description: description, |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 Server: config.ExternalURL(), |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 State: state, |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 ID: id, |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 } |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 var body strings.Builder |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 if err := importNotificationMailTmpl.Execute(&body, &data); err != nil { |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 log.Printf("error: %v\n", err) |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 return |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 } |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 if err := misc.SendMail(email, importNotificationMailSubject, body.String()); err != nil { |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 log.Printf("error: %v\n", err) |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 } |
c354631e0018
Import queue: Moved email notification stuff to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 } |