comparison pkg/imports/report.go @ 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 74bae79de83e
comparison
equal deleted inserted replaced
5335:dcd5692a2889 5336:2ec8a34ae683
43 43
44 const ReportJobKind JobKind = "report" 44 const ReportJobKind JobKind = "report"
45 45
46 type reportJobCreator struct{} 46 type reportJobCreator struct{}
47 47
48 const selectReportUsersSQL = ` 48 const (
49 selectReportUsersSQL = `
49 SELECT username, email_address 50 SELECT username, email_address
50 FROM users.list_users 51 FROM users.list_users
51 WHERE report_reciever 52 WHERE report_reciever
52 ORDER BY country, username` 53 ORDER BY country, username`
53 54
55 selectCurrentUserSQL = `
56 SELECT current_user, email_address
57 FROM users.list_users
58 WHERE username = current_user`
59 )
60
54 var reportMailTmpl = template.Must(template.New("report-mail"). 61 var reportMailTmpl = template.Must(template.New("report-mail").
55 Parse(`Dear {{ .User }} 62 Parse(`Dear {{ .Receiver }}
56 63
57 this is an automatically generated report from the Gemma system. 64 this is an automatically generated report from the Gemma system.
58 You received this mail because you are marked a report receiver in 65 You received this mail because you are listed as a report receiver in
59 the system. Please contact the system adminstrator of the system 66 the system. If you received this mail without consent please
60 if your recieved this mail against you consent. 67 contact {{ .Admin }} under {{ .AdminEmail }}.
61 68
62 Find attached {{ .Attachment }} which contains the {{ .Report }} from {{ .When }}. 69 Find attached {{ .Attachment }} which contains the {{ .Report }} from {{ .When }}.
63 70
64 Kind Regards`)) 71 Kind Regards`))
65 72
166 if err != nil { 173 if err != nil {
167 return nil, err 174 return nil, err
168 } 175 }
169 defer tx.Rollback() 176 defer tx.Rollback()
170 177
178 // Fetch receivers
171 var users []misc.EmailReceiver 179 var users []misc.EmailReceiver
172 180
173 if err := func() error { 181 if err := func() error {
174 rows, err := tx.QueryContext(ctx, selectReportUsersSQL) 182 rows, err := tx.QueryContext(ctx, selectReportUsersSQL)
175 if err != nil { 183 if err != nil {
192 if len(users) == 0 { 200 if len(users) == 0 {
193 feedback.Warn("No users found to send reports to.") 201 feedback.Warn("No users found to send reports to.")
194 return nil, nil 202 return nil, nil
195 } 203 }
196 204
205 // Fetch admin who is responsible for the report.
206 var admin misc.EmailReceiver
207 if err := tx.QueryRowContext(
208 ctx, selectCurrentUserSQL).Scan(&admin.Name, &admin.Address); err != nil {
209 log.Printf("error: Cannot find sender: %v\n")
210 return nil, fmt.Errorf("cannot find sender: %v", err)
211 }
212
213 // Generate the actual report.
197 if err := action.Execute(ctx, tx, template); err != nil { 214 if err := action.Execute(ctx, tx, template); err != nil {
198 log.Printf("error: %v\n", err) 215 log.Printf("error: %v\n", err)
199 return nil, fmt.Errorf("Generating report failed: %v", err) 216 return nil, fmt.Errorf("Generating report failed: %v", err)
200 } 217 }
201 218
209 226
210 now := start.UTC().Format("2006-01-02") 227 now := start.UTC().Format("2006-01-02")
211 228
212 attached := r.Name + "-" + now + ".xlsx" 229 attached := r.Name + "-" + now + ".xlsx"
213 230
214 attachments := []misc.EmailAttachment{{ 231 body := func(u misc.EmailReceiver) (string, error) {
215 Name: attached, 232 fill := struct {
216 Content: buf.Bytes(), 233 Receiver string
217 }} 234 Attachment string
235 Report string
236 When string
237 Admin string
238 AdminEmail string
239 }{
240 Receiver: u.Name,
241 Attachment: attached,
242 Report: r.Name,
243 When: now,
244 Admin: admin.Name,
245 AdminEmail: admin.Address,
246 }
247 var sb strings.Builder
248 if err := reportMailTmpl.Execute(&sb, &fill); err != nil {
249 return "", err
250 }
251 return sb.String(), nil
252 }
218 253
219 errorHandler := func(r misc.EmailReceiver, err error) error { 254 errorHandler := func(r misc.EmailReceiver, err error) error {
220 // We do not terminate the sending of the emails if 255 // We do not terminate the sending of the emails if
221 // sending failed. We only log it. 256 // sending failed. We only log it.
222 feedback.Warn("Sending report to %s failed: %v", r.Name, err) 257 feedback.Warn("Sending report to %s failed: %v", r.Name, err)
223 return nil 258 return nil
224 } 259 }
225 260
226 body := func(u misc.EmailReceiver) (string, error) {
227 fill := struct {
228 User string
229 Attachment string
230 Report string
231 When string
232 }{
233 User: u.Name,
234 Attachment: attached,
235 Report: r.Name,
236 When: now,
237 }
238 var sb strings.Builder
239 if err := reportMailTmpl.Execute(&sb, &fill); err != nil {
240 return "", err
241 }
242 return sb.String(), nil
243 }
244
245 if err := misc.SendMailToAll( 261 if err := misc.SendMailToAll(
246 users, 262 users,
247 "Report "+r.Name+" from "+now, 263 "Report "+r.Name+" from "+now,
248 body, 264 body,
249 attachments, 265 []misc.EmailAttachment{{
266 Name: attached,
267 Content: buf.Bytes(),
268 }},
250 errorHandler, 269 errorHandler,
251 ); err != nil { 270 ); err != nil {
252 return nil, err 271 return nil, err
253 } 272 }
254 273