-
Notifications
You must be signed in to change notification settings - Fork 2
/
witness_code.go
80 lines (65 loc) · 2.4 KB
/
witness_code.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
package page
import (
"context"
"errors"
"time"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/random"
)
var (
testWitnessCode = "1234"
UseTestWitnessCode = false
ErrTooManyWitnessCodeRequests = errors.New("too many witness code requests")
)
type WitnessCodeSender struct {
donorStore DonorStore
notifyClient NotifyClient
randomCode func(int) string
now func() time.Time
}
func NewWitnessCodeSender(donorStore DonorStore, notifyClient NotifyClient) *WitnessCodeSender {
return &WitnessCodeSender{
donorStore: donorStore,
notifyClient: notifyClient,
randomCode: random.Code,
now: time.Now,
}
}
func (s *WitnessCodeSender) SendToCertificateProvider(ctx context.Context, donor *actor.DonorProvidedDetails, localizer Localizer) error {
if !donor.CertificateProviderCodes.CanRequest(s.now()) {
return ErrTooManyWitnessCodeRequests
}
code := s.randomCode(4)
if UseTestWitnessCode {
code = testWitnessCode
}
donor.CertificateProviderCodes = append(donor.CertificateProviderCodes, actor.WitnessCode{Code: code, Created: s.now()})
if err := s.donorStore.Put(ctx, donor); err != nil {
return err
}
return s.notifyClient.SendActorSMS(ctx, donor.CertificateProvider.Mobile, donor.LpaUID, notify.WitnessCodeSMS{
WitnessCode: code,
DonorFullName: localizer.Possessive(donor.Donor.FullName()),
LpaType: localize.LowerFirst(localizer.T(donor.Type.String())),
})
}
func (s *WitnessCodeSender) SendToIndependentWitness(ctx context.Context, donor *actor.DonorProvidedDetails, localizer Localizer) error {
if !donor.IndependentWitnessCodes.CanRequest(s.now()) {
return ErrTooManyWitnessCodeRequests
}
code := s.randomCode(4)
if UseTestWitnessCode {
code = testWitnessCode
}
donor.IndependentWitnessCodes = append(donor.IndependentWitnessCodes, actor.WitnessCode{Code: code, Created: s.now()})
if err := s.donorStore.Put(ctx, donor); err != nil {
return err
}
return s.notifyClient.SendActorSMS(ctx, donor.IndependentWitness.Mobile, donor.LpaUID, notify.WitnessCodeSMS{
WitnessCode: code,
DonorFullName: localizer.Possessive(donor.Donor.FullName()),
LpaType: localize.LowerFirst(localizer.T(donor.Type.String())),
})
}