-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnotification.go
More file actions
175 lines (147 loc) · 4.48 KB
/
notification.go
File metadata and controls
175 lines (147 loc) · 4.48 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"context"
"time"
"github.com/go-pg/pg/v10"
_ "github.com/go-pg/pg/v10/orm"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
cs "github.com/webtor-io/common-services"
"github.com/webtor-io/web-ui/models/vault"
"github.com/webtor-io/web-ui/services/api"
"github.com/webtor-io/web-ui/services/claims"
"github.com/webtor-io/web-ui/services/common"
"github.com/webtor-io/web-ui/services/notification"
vService "github.com/webtor-io/web-ui/services/vault"
)
func makeNotificationCMD() cli.Command {
notificationCMD := cli.Command{
Name: "notification",
Aliases: []string{"n"},
Usage: "Notification management commands",
}
configureNotification(¬ificationCMD)
return notificationCMD
}
func configureNotification(c *cli.Command) {
sendCmd := cli.Command{
Name: "send",
Aliases: []string{"s"},
Usage: "Sends notifications about expiring resources",
Action: sendExpiringNotifications,
}
configureNotificationSend(&sendCmd)
c.Subcommands = []cli.Command{sendCmd}
}
func configureNotificationSend(c *cli.Command) {
c.Flags = cs.RegisterPGFlags(c.Flags)
c.Flags = common.RegisterFlags(c.Flags)
c.Flags = api.RegisterFlags(c.Flags)
c.Flags = claims.RegisterClientFlags(c.Flags)
c.Flags = vService.RegisterApiFlags(c.Flags)
c.Flags = vService.RegisterFlags(c.Flags)
}
func sendExpiringNotifications(c *cli.Context) error {
ctx := context.Background()
// Setting DB
pg := cs.NewPG(c)
defer pg.Close()
// Setting Migrations
m := cs.NewPGMigration(pg)
err := m.Run()
if err != nil {
return errors.Wrap(err, "failed to run migrations")
}
db := pg.Get()
if db == nil {
return errors.New("db is nil")
}
// Setting Notification
ns := notification.New(c, db)
expirePeriod := c.Duration(vService.VaultResourceExpirePeriodFlag)
for _, days := range []int{7, 3, 1} {
err := sendExpiringNotificationsByDays(ctx, db, expirePeriod, ns, days)
if err != nil {
log.WithError(err).WithField("days", days).Error("failed to send expiring notifications")
}
}
return nil
}
func sendExpiringNotificationsByDays(ctx context.Context, db *pg.DB, expirePeriod time.Duration, ns *notification.Service, days int) error {
log.WithField("days", days).Info("processing expiring notifications")
startTime := time.Now().Add(time.Duration(days-1) * 24 * time.Hour)
endTime := time.Now().Add(time.Duration(days) * 24 * time.Hour)
// 1. Get all resources expiring in the range [days-1, days] from now
// Logic: expire_at + expirePeriod
var resources []vault.Resource
err := db.Model(&resources).
Context(ctx).
WhereGroup(func(q *pg.Query) (*pg.Query, error) {
q = q.WhereOr("expired_at IS NOT NULL AND expired_at + ? * interval '1 microsecond' BETWEEN ? AND ?",
expirePeriod.Microseconds(), startTime, endTime)
return q, nil
}).
Select()
if err != nil {
return errors.Wrap(err, "failed to get resources for notification")
}
if len(resources) == 0 {
return nil
}
// 2. Get unique users
resourceIDs := make([]string, len(resources))
for i, r := range resources {
resourceIDs[i] = r.ResourceID
}
var pledges []vault.Pledge
err = db.Model(&pledges).
Context(ctx).
Relation("User").
Where("resource_id IN (?)", pg.In(resourceIDs)).
Select()
if err != nil {
return errors.Wrap(err, "failed to get pledges for notification")
}
userEmails := make(map[string]string)
for _, p := range pledges {
if p.User == nil || p.User.Email == "" {
continue
}
userID := p.UserID.String()
userEmails[userID] = p.User.Email
}
for userID, email := range userEmails {
var userPledges []vault.Pledge
err = db.Model(&userPledges).
Context(ctx).
Relation("Resource").
Where("pledge.user_id = ?", userID).
WhereGroup(func(q *pg.Query) (*pg.Query, error) {
q = q.WhereOr("Resource.expired_at IS NOT NULL AND Resource.expired_at + ? * interval '1 microsecond' BETWEEN ? AND ?",
expirePeriod.Microseconds(), startTime, endTime)
return q, nil
}).
Select()
if err != nil {
log.WithError(err).WithField("user_id", userID).Error("failed to get user pledges for notification")
continue
}
if len(userPledges) == 0 {
continue
}
var expiring []vault.Resource
for _, up := range userPledges {
if up.Resource != nil {
expiring = append(expiring, *up.Resource)
}
}
if len(expiring) > 0 {
err = ns.SendExpiring(email, days, expiring)
if err != nil {
log.WithError(err).WithField("email", email).Error("failed to send expiring notification")
}
}
}
return nil
}