/
cmsnotifications.go
58 lines (53 loc) · 1.67 KB
/
cmsnotifications.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) 2019 The Bitum developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"time"
"github.com/bitum-project/politeia/politeiawww/user"
)
// Seconds Minutes Hours Days Months DayOfWeek
const emailSchedule = "0 0 12 5 * *" // Check at 12:00 PM on 5th day every month
func (p *politeiawww) checkInvoiceNotifications() {
log.Infof("Starting cron for invoice email checking")
// Launch invoice notification cron job
err := p.cron.AddFunc(emailSchedule, func() {
log.Infof("Running invoice email notification cron")
currentMonth := time.Now().Month()
currentYear := time.Now().Year()
// Check all CMS users
err := p.db.AllUsers(func(user *user.User) {
log.Tracef("Checking user: %v", user.Username)
if user.Admin {
return
}
invoiceFound := false
userInvoices, err := p.cmsDB.InvoicesByUserID(user.ID.String())
if err != nil {
log.Errorf("Error retrieving user invoices email: %v %v", err,
user.Email)
}
for _, inv := range userInvoices {
// Check to see if invoices match last month + current year
if inv.Month == uint(currentMonth-1) && inv.Year == uint(currentYear) {
invoiceFound = true
}
}
log.Tracef("Checked user: %v sending email? %v", user.Username,
!invoiceFound)
if !invoiceFound {
err = p.emailInvoiceNotifications(user.Email, user.Username)
if err != nil {
log.Errorf("Error sending email: %v %v", err, user.Email)
}
}
})
if err != nil {
log.Errorf("Error querying for AllUsers: %v", err)
}
})
if err != nil {
log.Errorf("Error running invoice notification cron: %v", err)
}
p.cron.Start()
}