changeset 827:f3adc0f3a20a

Use templating to make somewhat more interesting test notification mails.
author Sascha Wilde <wilde@intevation.de>
date Thu, 27 Sep 2018 23:24:40 +0200
parents 8926c413db21
children 4e093c5560c3
files pkg/controllers/user.go
diffstat 1 files changed, 49 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/user.go	Thu Sep 27 18:52:41 2018 +0200
+++ b/pkg/controllers/user.go	Thu Sep 27 23:24:40 2018 +0200
@@ -1,9 +1,13 @@
 package controllers
 
 import (
+	"bytes"
 	"database/sql"
 	"fmt"
+	"log"
 	"net/http"
+	"text/template"
+	"time"
 
 	"github.com/gorilla/mux"
 
@@ -53,6 +57,29 @@
 WHERE username = $1`
 )
 
+var (
+	testSysadminNotifyMailTmpl = template.Must(
+		template.New("sysadmin").Parse(`Dear {{ .User }},
+
+this is a test email for the Gemma System Errors notification service.  You
+recieved this mail, because a System Administrator triggered the test mail
+sending function at {{ .Timestamp }}.
+
+When a critical system error is detecte an automated mail will be send to
+the address: {{ .Email }} with details on the error condition.`))
+
+	testWWAdminNotifyMailTmpl = template.Must(
+		template.New("waterwayadmin").Parse(`Dear {{ .User }},
+
+this is a test email for the Gemma System Imports notification service.  You
+recieved this mail, because a System Administrator triggered the test mail
+sending function at {{ .Timestamp }}.
+
+When the status of an data import managed by you changes an automated mail will
+be send to the address: {{ .Email }} with details on the new import status
+(inkluding import errors) and details on the concerned import.`))
+)
+
 func deleteUser(
 	_ interface{}, req *http.Request,
 	db *sql.Conn,
@@ -334,12 +361,25 @@
 		return
 	}
 
-	var body string
+	var subject string
 
+	var tmplVars = struct {
+		User      string
+		Timestamp string
+		Email     string
+	}{
+		User:      string(user),
+		Timestamp: time.Now().Format("2006-01-02 15:04:05"),
+		Email:     string(userData.Email),
+	}
+
+	var bodyTmpl *template.Template
 	if userData.Role == "sys_admin" {
-		body = "Test for Sys Admin Notification"
+		subject = "Sysadmin Notification TEST"
+		bodyTmpl = testSysadminNotifyMailTmpl
 	} else if userData.Role == "waterway_admin" {
-		body = "Test for Water Way Admin Notification"
+		subject = "Waterway Admin Notification TEST"
+		bodyTmpl = testWWAdminNotifyMailTmpl
 	} else {
 		err = JSONError{
 			Code:    http.StatusBadRequest,
@@ -347,7 +387,12 @@
 		}
 		return
 	}
-	err = misc.SendMail(string(userData.Email), "Test Email", body)
+	var buf bytes.Buffer
+	if err := bodyTmpl.Execute(&buf, &tmplVars); err != nil {
+		log.Printf("error: %v\n", err)
+	}
+
+	err = misc.SendMail(string(userData.Email), subject, buf.String())
 	if err != nil {
 		return
 	}