-
Notifications
You must be signed in to change notification settings - Fork 16
/
organizations.go
254 lines (216 loc) · 8.05 KB
/
organizations.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
248
249
250
251
252
253
254
package data
import (
"bytes"
"context"
"database/sql"
"errors"
"fmt"
"image"
"net/url"
"regexp"
"strings"
"time"
// Don't remove the `image/jpeg` and `image/png` packages import unless
// the `image` package is no longer necessary.
// It registers the `Decoders` to handle the image decoding - `image.Decode`.
// See https://pkg.go.dev/image#pkg-overview
_ "image/jpeg"
_ "image/png"
"github.com/stellar/stellar-disbursement-platform-backend/db"
)
const (
DefaultSMSRegistrationMessageTemplate = "You have a payment waiting for you from the {{.OrganizationName}}. Click {{.RegistrationLink}} to register."
DefaultOTPMessageTemplate = "{{.OTP}} is your {{.OrganizationName}} phone verification code."
)
type Organization struct {
ID string `json:"id" db:"id"`
Name string `json:"name" db:"name"`
TimezoneUTCOffset string `json:"timezone_utc_offset" db:"timezone_utc_offset"`
// SMSResendInterval is the time period that SDP will wait to resend the invitation SMS to the receivers that aren't registered.
// If it's nil means resending the invitation SMS is deactivated.
SMSResendInterval *int64 `json:"sms_resend_interval" db:"sms_resend_interval"`
// PaymentCancellationPeriodDays is the number of days for a ready payment to be automatically cancelled.
PaymentCancellationPeriodDays *int64 `json:"payment_cancellation_period_days" db:"payment_cancellation_period_days"`
SMSRegistrationMessageTemplate string `json:"sms_registration_message_template" db:"sms_registration_message_template"`
// OTPMessageTemplate is the message template to send the OTP code to the receivers validates their identity when registering their wallets.
// The message may have the template values {{.OTP}} and {{.OrganizationName}}, it will be parsed and the values injected when executing the template.
// When the {{.OTP}} is not found in the message, it's added at the beginning of the message.
// Example:
// {{.OTP}} OTPMessageTemplate
OTPMessageTemplate string `json:"otp_message_template" db:"otp_message_template"`
PrivacyPolicyLink *string `json:"privacy_policy_link" db:"privacy_policy_link"`
Logo []byte `db:"logo"`
IsApprovalRequired bool `json:"is_approval_required" db:"is_approval_required"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
type OrganizationUpdate struct {
Name string `json:",omitempty"`
Logo []byte `json:",omitempty"`
TimezoneUTCOffset string `json:",omitempty"`
IsApprovalRequired *bool `json:",omitempty"`
SMSResendInterval *int64 `json:",omitempty"`
PaymentCancellationPeriodDays *int64 `json:",omitempty"`
// Using pointers to accept empty strings
SMSRegistrationMessageTemplate *string `json:",omitempty"`
OTPMessageTemplate *string `json:",omitempty"`
PrivacyPolicyLink *string `json:",omitempty"`
}
type LogoType string
const (
PNGLogoType LogoType = "png"
JPEGLogoType LogoType = "jpeg"
// tzRegexExpression validates the TimezoneUTCOffset value. It expects the following format:
// plus or minus symbol + two numbers + colon symbol + two numbers
// Example:
// +02:00 or -03:00
// Any other value will be invalid.
tzRegexExpression string = `^(\+|-)\d{2}:\d{2}$`
)
var tzRegex *regexp.Regexp
func init() {
tzRegex = regexp.MustCompile(tzRegexExpression)
}
func (lt LogoType) ToHTTPContentType() string {
return fmt.Sprintf("image/%s", lt)
}
func (ou *OrganizationUpdate) validate() error {
if ou.areAllFieldsEmpty() {
return fmt.Errorf("name, timezone UTC offset, approval workflow flag, SMS Resend Interval, SMS invite template, OTP message template, privacy policy link or logo is required")
}
if len(ou.Logo) > 0 {
_, format, err := image.Decode(bytes.NewBuffer(ou.Logo))
if err != nil {
return fmt.Errorf("error decoding image bytes: %w", err)
}
if !strings.Contains(fmt.Sprintf("%s %s", PNGLogoType, JPEGLogoType), format) {
return fmt.Errorf("invalid image type provided. Expect %s or %s", PNGLogoType, JPEGLogoType)
}
}
if ou.TimezoneUTCOffset != "" && !tzRegex.MatchString(ou.TimezoneUTCOffset) {
return fmt.Errorf("invalid timezone UTC offset format. Example: +02:00 or -03:00")
}
if ou.PrivacyPolicyLink != nil && *ou.PrivacyPolicyLink != "" {
_, err := url.ParseRequestURI(*ou.PrivacyPolicyLink)
if err != nil {
return fmt.Errorf("invalid privacy policy link: %w", err)
}
}
return nil
}
func (ou *OrganizationUpdate) areAllFieldsEmpty() bool {
return (ou.Name == "" &&
len(ou.Logo) == 0 &&
ou.TimezoneUTCOffset == "" &&
ou.IsApprovalRequired == nil &&
ou.SMSRegistrationMessageTemplate == nil &&
ou.OTPMessageTemplate == nil &&
ou.SMSResendInterval == nil &&
ou.PaymentCancellationPeriodDays == nil &&
ou.PrivacyPolicyLink == nil)
}
type OrganizationModel struct {
dbConnectionPool db.DBConnectionPool
}
func (om *OrganizationModel) Get(ctx context.Context) (*Organization, error) {
var organization Organization
query := `
SELECT
*
FROM
organizations o
LIMIT 1
`
err := om.dbConnectionPool.GetContext(ctx, &organization, query)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrRecordNotFound
}
return nil, fmt.Errorf("error querying organization table: %w", err)
}
return &organization, nil
}
func (om *OrganizationModel) Update(ctx context.Context, ou *OrganizationUpdate) error {
if err := ou.validate(); err != nil {
return fmt.Errorf("invalid organization update: %w", err)
}
query := `
WITH org_cte AS (
SELECT id FROM organizations LIMIT 1
)
UPDATE
organizations
SET
%s
FROM org_cte
WHERE organizations.id = org_cte.id
`
args := []interface{}{}
fields := []string{}
if ou.Name != "" {
fields = append(fields, "name = ?")
args = append(args, ou.Name)
}
if len(ou.Logo) > 0 {
fields = append(fields, "logo = ?")
args = append(args, ou.Logo)
}
if ou.TimezoneUTCOffset != "" {
fields = append(fields, "timezone_utc_offset = ?")
args = append(args, ou.TimezoneUTCOffset)
}
if ou.IsApprovalRequired != nil {
fields = append(fields, "is_approval_required = ?")
args = append(args, *ou.IsApprovalRequired)
}
if ou.SMSRegistrationMessageTemplate != nil {
if *ou.SMSRegistrationMessageTemplate != "" {
fields = append(fields, "sms_registration_message_template = ?")
args = append(args, *ou.SMSRegistrationMessageTemplate)
} else {
// When empty value is passed by parameter we set the DEFAULT value for the column.
fields = append(fields, "sms_registration_message_template = DEFAULT")
}
}
if ou.OTPMessageTemplate != nil {
if *ou.OTPMessageTemplate != "" {
fields = append(fields, "otp_message_template = ?")
args = append(args, *ou.OTPMessageTemplate)
} else {
// When empty value is passed by parameter we set the DEFAULT value for the column.
fields = append(fields, "otp_message_template = DEFAULT")
}
}
if ou.PrivacyPolicyLink != nil {
if *ou.PrivacyPolicyLink != "" {
link, _ := url.ParseRequestURI(*ou.PrivacyPolicyLink)
fields = append(fields, "privacy_policy_link = ?")
args = append(args, link.String())
} else {
fields = append(fields, "privacy_policy_link = NULL")
}
}
if ou.SMSResendInterval != nil {
if *ou.SMSResendInterval > 0 {
fields = append(fields, "sms_resend_interval = ?")
args = append(args, *ou.SMSResendInterval)
} else {
// When 0 (zero) is passed by parameter we set it as NULL.
fields = append(fields, "sms_resend_interval = NULL")
}
}
if ou.PaymentCancellationPeriodDays != nil {
if *ou.PaymentCancellationPeriodDays > 0 {
fields = append(fields, "payment_cancellation_period_days = ?")
args = append(args, *ou.PaymentCancellationPeriodDays)
} else {
fields = append(fields, "payment_cancellation_period_days = NULL")
}
}
query = om.dbConnectionPool.Rebind(fmt.Sprintf(query, strings.Join(fields, ", ")))
_, err := om.dbConnectionPool.ExecContext(ctx, query, args...)
if err != nil {
return fmt.Errorf("error updating organization: %w", err)
}
return nil
}