Mercurial > gemma
changeset 5336:2ec8a34ae683 extented-report
Rephrased the email message of the report and write the reponsible admin and her/his email address into it.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 01 Jun 2021 14:40:56 +0200 |
parents | dcd5692a2889 |
children | 492fc5870330 |
files | pkg/imports/report.go |
diffstat | 1 files changed, 48 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/imports/report.go Tue Jun 01 02:46:14 2021 +0200 +++ b/pkg/imports/report.go Tue Jun 01 14:40:56 2021 +0200 @@ -45,19 +45,26 @@ type reportJobCreator struct{} -const selectReportUsersSQL = ` +const ( + selectReportUsersSQL = ` SELECT username, email_address FROM users.list_users WHERE report_reciever ORDER BY country, username` + selectCurrentUserSQL = ` +SELECT current_user, email_address +FROM users.list_users +WHERE username = current_user` +) + var reportMailTmpl = template.Must(template.New("report-mail"). - Parse(`Dear {{ .User }} + Parse(`Dear {{ .Receiver }} this is an automatically generated report from the Gemma system. -You received this mail because you are marked a report receiver in -the system. Please contact the system adminstrator of the system -if your recieved this mail against you consent. +You received this mail because you are listed as a report receiver in +the system. If you received this mail without consent please +contact {{ .Admin }} under {{ .AdminEmail }}. Find attached {{ .Attachment }} which contains the {{ .Report }} from {{ .When }}. @@ -168,6 +175,7 @@ } defer tx.Rollback() + // Fetch receivers var users []misc.EmailReceiver if err := func() error { @@ -194,6 +202,15 @@ return nil, nil } + // Fetch admin who is responsible for the report. + var admin misc.EmailReceiver + if err := tx.QueryRowContext( + ctx, selectCurrentUserSQL).Scan(&admin.Name, &admin.Address); err != nil { + log.Printf("error: Cannot find sender: %v\n") + return nil, fmt.Errorf("cannot find sender: %v", err) + } + + // Generate the actual report. if err := action.Execute(ctx, tx, template); err != nil { log.Printf("error: %v\n", err) return nil, fmt.Errorf("Generating report failed: %v", err) @@ -211,10 +228,28 @@ attached := r.Name + "-" + now + ".xlsx" - attachments := []misc.EmailAttachment{{ - Name: attached, - Content: buf.Bytes(), - }} + body := func(u misc.EmailReceiver) (string, error) { + fill := struct { + Receiver string + Attachment string + Report string + When string + Admin string + AdminEmail string + }{ + Receiver: u.Name, + Attachment: attached, + Report: r.Name, + When: now, + Admin: admin.Name, + AdminEmail: admin.Address, + } + var sb strings.Builder + if err := reportMailTmpl.Execute(&sb, &fill); err != nil { + return "", err + } + return sb.String(), nil + } errorHandler := func(r misc.EmailReceiver, err error) error { // We do not terminate the sending of the emails if @@ -223,30 +258,14 @@ return nil } - body := func(u misc.EmailReceiver) (string, error) { - fill := struct { - User string - Attachment string - Report string - When string - }{ - User: u.Name, - Attachment: attached, - Report: r.Name, - When: now, - } - var sb strings.Builder - if err := reportMailTmpl.Execute(&sb, &fill); err != nil { - return "", err - } - return sb.String(), nil - } - if err := misc.SendMailToAll( users, "Report "+r.Name+" from "+now, body, - attachments, + []misc.EmailAttachment{{ + Name: attached, + Content: buf.Bytes(), + }}, errorHandler, ); err != nil { return nil, err