/
alert.go
247 lines (219 loc) · 7.28 KB
/
alert.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package pgmodels
import (
"bytes"
"time"
"github.com/APTrust/registry/common"
"github.com/APTrust/registry/constants"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"github.com/stretchr/stew/slice"
)
const (
ErrAlertInstitutionID = "InstitutionID is required."
ErrAlertType = "Alert type is missing or invalid."
ErrAlertContent = "Alert content cannot be empty."
)
type Alert struct {
BaseModel
InstitutionID int64 `json:"institution_id"`
Type string `json:"type"`
Subject string `json:"subject"`
Content string `json:"content"`
DeletionRequestID int64 `json:"deletion_request_id"`
CreatedAt time.Time `json:"created_at"`
DeletionRequest *DeletionRequest `json:"-" pg:"rel:has-one"`
PremisEvents []*PremisEvent `json:"premis_events" pg:"many2many:alerts_premis_events"`
Users []*User `json:"users" pg:"many2many:alerts_users"`
WorkItems []*WorkItem `json:"work_items" pg:"many2many:alerts_work_items"`
}
type AlertsPremisEvents struct {
AlertID int64
PremisEventID int64
}
type AlertsUsers struct {
AlertID int64
UserID int64
SentAt time.Time
ReadAt time.Time
}
type AlertsWorkItems struct {
AlertID int64
WorkItemID int64
}
// init does some setup work so go-pg can recognize many-to-many
// relations. Go automatically calls this function once when package
// is imported.
func init() {
orm.RegisterTable((*AlertsPremisEvents)(nil))
orm.RegisterTable((*AlertsUsers)(nil))
orm.RegisterTable((*AlertsWorkItems)(nil))
}
// AlertByID returns the alert with the specified id.
// Returns pg.ErrNoRows if there is no match.
func AlertByID(id int64) (*Alert, error) {
query := NewQuery().Where(`"alert"."id"`, "=", id).Relations("DeletionRequest", "PremisEvents", "Users", "WorkItems")
return AlertGet(query)
}
// AlertGet returns the first alert matching the query.
func AlertGet(query *Query) (*Alert, error) {
var alert Alert
err := query.Select(&alert)
return &alert, err
}
// AlertSelect returns all alerts matching the query.
func AlertSelect(query *Query) ([]*Alert, error) {
var alerts []*Alert
err := query.Select(&alerts)
return alerts, err
}
// Save saves this alert to the database. This will peform an insert
// if Alert.ID is zero. Otherwise, it updates. It also saves all of
// the many-to-many relations (PremisEvents, Users, and WorkItems), though
// note that on update it does not delete any of these relations. We don't
// have a use case for that yet, since alerts are generally created and never
// updated.
func (alert *Alert) Save() error {
err := alert.Validate()
if err != nil {
return err
}
registryContext := common.Context()
db := registryContext.DB
return db.RunInTransaction(db.Context(), func(tx *pg.Tx) error {
var err error
if alert.ID == 0 {
alert.CreatedAt = time.Now().UTC()
_, err = tx.Model(alert).Insert()
} else {
_, err = tx.Model(alert).WherePK().Update()
}
if err != nil {
registryContext.Log.Error().Msgf("Transaction failed. Model: %v. Error: %v", alert, err)
}
return alert.saveRelations(tx)
})
}
// This is run inside the Save transaction.
func (alert *Alert) saveRelations(tx *pg.Tx) error {
err := alert.saveEvents(tx)
if err != nil {
return err
}
err = alert.saveWorkItems(tx)
if err != nil {
return err
}
err = alert.saveUsers(tx)
return err
}
func (alert *Alert) saveEvents(tx *pg.Tx) error {
sql := "insert into alerts_premis_events (alert_id, premis_event_id) values (?, ?) on conflict do nothing"
for _, event := range alert.PremisEvents {
_, err := tx.Exec(sql, alert.ID, event.ID)
if err != nil {
return err
}
}
return nil
}
func (alert *Alert) saveUsers(tx *pg.Tx) error {
sql := "insert into alerts_users (alert_id, user_id, sent_at, read_at) values (?, ?, ?, ?) on conflict do nothing"
for _, user := range alert.Users {
_, err := tx.Exec(sql, alert.ID, user.ID, nil, nil)
if err != nil {
return err
}
}
return nil
}
func (alert *Alert) saveWorkItems(tx *pg.Tx) error {
sql := "insert into alerts_work_items (alert_id, work_item_id) values (?, ?) on conflict do nothing"
for _, item := range alert.WorkItems {
_, err := tx.Exec(sql, alert.ID, item.ID)
if err != nil {
return err
}
}
return nil
}
// Validate validates the model. This is called automatically on insert
// and update.
func (alert *Alert) Validate() *common.ValidationError {
errors := make(map[string]string)
if alert.InstitutionID < 1 {
errors["InstitutionID"] = ErrAlertInstitutionID
}
if !slice.Contains(constants.AlertTypes, alert.Type) {
errors["Type"] = ErrAlertType
}
if common.IsEmptyString(alert.Content) {
errors["Content"] = ErrAlertContent
}
if len(errors) > 0 {
return &common.ValidationError{Errors: errors}
}
return nil
}
// MarkAsSent marks an alert as sent.
func (alert *Alert) MarkAsSent(userID int64) error {
now := time.Now().UTC()
sql := "update alerts_users set sent_at = ? where alert_id = ? and user_id = ?"
_, err := common.Context().DB.Exec(sql, now, alert.ID, userID)
return err
}
// MarkAsRead marks an alert as read if its current ReadAt date is null.
func (alert *Alert) MarkAsRead(userID int64) error {
now := time.Now().UTC()
sql := "update alerts_users set read_at = ? where alert_id = ? and user_id = ? and read_at is null"
_, err := common.Context().DB.Exec(sql, now, alert.ID, userID)
return err
}
// MarkAsUnread marks an alert as unread.
func (alert *Alert) MarkAsUnread(userID int64) error {
sql := "update alerts_users set read_at = null where alert_id = ? and user_id = ?"
_, err := common.Context().DB.Exec(sql, alert.ID, userID)
return err
}
// CreateAlert adds customized text to the alert and saves it in the
// database. Param templateName is the name of the text template used
// to construct the alert message. Param alertData is the custom data
// to put into the template.
//
// This returns the alert with a non-zero ID (since it saves it) and
// an error if there's a problem with the template or the save.
func CreateAlert(alert *Alert, templateName string, alertData map[string]interface{}) (*Alert, error) {
// Create the alert text from the template...
tmpl := common.TextTemplates[templateName]
var buf bytes.Buffer
err := tmpl.Execute(&buf, alertData)
if err != nil {
return nil, err
}
// Set the alert text & save it.
alert.Content = buf.String()
err = alert.Save()
if err != nil {
return nil, err
}
// Send the alert & mark as sent
for _, recipient := range alert.Users {
err := common.Context().SESClient.Send(recipient.Email, alert.Subject, alert.Content)
if err == nil {
err = alert.MarkAsSent(recipient.ID)
if err != nil {
common.Context().Log.Error().Msgf("Could not mark alert %d to user %s as sent, even though it was: %v", alert.ID, recipient.Email, err)
}
} else {
common.Context().Log.Error().Msgf("Saved but could not send alert %d to user %s: %v", alert.ID, recipient.Email, err)
}
}
// Show the alert text in dev and test consoles,
// so we don't have to look it up in the DB.
// For dev/test, we need to see the review and
// confirmation URLS in this alert so we can
// review and test them.
common.ConsoleDebug("***********************")
common.ConsoleDebug(alert.Content)
common.ConsoleDebug("***********************")
return alert, err
}