/
cmd_change_totp_telephone.go
55 lines (42 loc) · 1.01 KB
/
cmd_change_totp_telephone.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 account
import (
"context"
"fmt"
"github.com/polyscone/tofu/internal/app"
"github.com/polyscone/tofu/internal/pkg/errsx"
)
type ChangeTOTPTelGuard interface {
CanChangeTOTPTel(userID int) bool
}
func (s *Service) ChangeTOTPTel(ctx context.Context, guard ChangeTOTPTelGuard, userID int, newTel string) error {
var input struct {
userID int
newTel Tel
}
{
if !guard.CanChangeTOTPTel(userID) {
return app.ErrUnauthorised
}
var err error
var errs errsx.Map
input.userID = userID
if input.newTel, err = NewTel(newTel); err != nil {
errs.Set("new phone", err)
}
if errs != nil {
return fmt.Errorf("%w: %w", app.ErrMalformedInput, errs)
}
}
user, err := s.repo.FindUserByID(ctx, input.userID)
if err != nil {
return fmt.Errorf("find user by id: %w", err)
}
if err := user.ChangeTOTPTel(input.newTel); err != nil {
return err
}
if err := s.repo.SaveUser(ctx, user); err != nil {
return fmt.Errorf("save user: %w", err)
}
s.broker.Flush(&user.Events)
return nil
}