-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_emails.go
55 lines (43 loc) · 1.09 KB
/
update_emails.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
package system
import (
"context"
"fmt"
"github.com/polyscone/tofu/app"
"github.com/polyscone/tofu/errsx"
)
type UpdateEmailsGuard interface {
CanUpdateEmails() bool
}
func (s *Service) UpdateEmails(ctx context.Context, guard UpdateEmailsGuard, systemEmail, securityEmail string) error {
var input struct {
systemEmail Email
securityEmail Email
}
{
if !guard.CanUpdateEmails() {
return app.ErrForbidden
}
var err error
var errs errsx.Map
if input.systemEmail, err = NewEmail(systemEmail); err != nil {
errs.Set("system email", err)
}
if input.securityEmail, err = NewEmail(securityEmail); err != nil {
errs.Set("security email", err)
}
if errs != nil {
return fmt.Errorf("%w: %w", app.ErrMalformedInput, errs)
}
}
config, err := s.repo.FindConfig(ctx)
if err != nil {
return fmt.Errorf("find config: %w", err)
}
config.ChangeSystemEmail(input.systemEmail)
config.ChangeSecurityEmail(input.securityEmail)
if err := s.repo.SaveConfig(ctx, config); err != nil {
return fmt.Errorf("save config: %w", err)
}
s.broker.Flush(&config.Events)
return nil
}