-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathuser_preference.go
More file actions
102 lines (89 loc) · 2.73 KB
/
Copy pathuser_preference.go
File metadata and controls
102 lines (89 loc) · 2.73 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
package models
import (
"fmt"
"gorm.io/gorm"
)
const TableNameUserPreferences = "user_preferences"
// Valid user preference labels
const (
UserPreferenceLightwellNotificationEnabled = "lightwell-notification-enabled"
UserPreferenceLightwellNotificationMinimum = "lightwell-notification-minimum"
)
// ValidUserPreferenceLabels is the set of accepted preference labels
var ValidUserPreferenceLabels = map[string]bool{
UserPreferenceLightwellNotificationEnabled: true,
UserPreferenceLightwellNotificationMinimum: true,
}
// ValidUserPreferenceValues maps labels to their accepted values.
// Labels with an empty set accept any non-empty value.
var ValidUserPreferenceValues = map[string]map[string]bool{
UserPreferenceLightwellNotificationEnabled: {
"true": true,
"false": true,
},
UserPreferenceLightwellNotificationMinimum: {
"critical": true,
"high": true,
"medium": true,
"low": true,
},
}
// IsValidUserPreferenceLabel reports whether label is an accepted preference label
func IsValidUserPreferenceLabel(label string) bool {
return ValidUserPreferenceLabels[label]
}
// ValidateUserPreferenceValue checks that value is valid for the given label
func ValidateUserPreferenceValue(label, value string) error {
allowed, ok := ValidUserPreferenceValues[label]
if !ok {
return fmt.Errorf("invalid preference label: %s", label)
}
if len(allowed) == 0 {
return nil
}
if !allowed[value] {
return fmt.Errorf("invalid value %q for preference label %s", value, label)
}
return nil
}
// UserPreference stores a single user preference as a label/value pair
type UserPreference struct {
Base
OrgID string `gorm:"not null"`
UserID string `gorm:"not null"`
Label string `gorm:"not null"`
Value string `gorm:"not null"`
}
func (up *UserPreference) TableName() string {
return TableNameUserPreferences
}
func (up *UserPreference) BeforeCreate(tx *gorm.DB) error {
if err := up.Base.BeforeCreate(tx); err != nil {
return err
}
return up.validate()
}
func (up *UserPreference) BeforeUpdate(tx *gorm.DB) error {
return up.validate()
}
func (up *UserPreference) validate() error {
if up.OrgID == "" {
return Error{Message: "Org ID cannot be blank.", Validation: true}
}
if up.UserID == "" {
return Error{Message: "User ID cannot be blank.", Validation: true}
}
if up.Label == "" {
return Error{Message: "Label cannot be blank.", Validation: true}
}
if !IsValidUserPreferenceLabel(up.Label) {
return Error{Message: fmt.Sprintf("Invalid preference label: %s", up.Label), Validation: true}
}
if up.Value == "" {
return Error{Message: "Value cannot be blank.", Validation: true}
}
if err := ValidateUserPreferenceValue(up.Label, up.Value); err != nil {
return Error{Message: err.Error(), Validation: true}
}
return nil
}