diff --git a/internal/page/witness_code.go b/internal/page/witness_code.go index d57a4d126f..27365c2b6a 100644 --- a/internal/page/witness_code.go +++ b/internal/page/witness_code.go @@ -45,16 +45,15 @@ func (s *WitnessCodeSender) SendToCertificateProvider(ctx context.Context, donor donor.CertificateProviderCodes = append(donor.CertificateProviderCodes, actor.WitnessCode{Code: code, Created: s.now()}) - err := s.notifyClient.SendActorSMS(ctx, donor.CertificateProvider.Mobile, donor.LpaUID, notify.WitnessCodeSMS{ + 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())), }) - if err != nil { - return err - } - - return s.donorStore.Put(ctx, donor) } func (s *WitnessCodeSender) SendToIndependentWitness(ctx context.Context, donor *actor.DonorProvidedDetails, localizer Localizer) error { @@ -69,14 +68,13 @@ func (s *WitnessCodeSender) SendToIndependentWitness(ctx context.Context, donor donor.IndependentWitnessCodes = append(donor.IndependentWitnessCodes, actor.WitnessCode{Code: code, Created: s.now()}) - err := s.notifyClient.SendActorSMS(ctx, donor.IndependentWitness.Mobile, donor.LpaUID, notify.WitnessCodeSMS{ + 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())), }) - if err != nil { - return err - } - - return s.donorStore.Put(ctx, donor) } diff --git a/internal/page/witness_code_test.go b/internal/page/witness_code_test.go index a06864fa9c..413df43dc7 100644 --- a/internal/page/witness_code_test.go +++ b/internal/page/witness_code_test.go @@ -98,6 +98,11 @@ func TestWitnessCodeSenderSendToCertificateProviderWhenNotifyClientErrors(t *tes SendActorSMS(mock.Anything, mock.Anything, mock.Anything, mock.Anything). Return(expectedError) + donorStore := newMockDonorStore(t) + donorStore.EXPECT(). + Put(mock.Anything, mock.Anything). + Return(nil) + localizer := newMockLocalizer(t) localizer.EXPECT(). T("property-and-affairs"). @@ -107,6 +112,7 @@ func TestWitnessCodeSenderSendToCertificateProviderWhenNotifyClientErrors(t *tes Return("Joe Jones’") sender := &WitnessCodeSender{ + donorStore: donorStore, notifyClient: notifyClient, randomCode: func(int) string { return "1234" }, now: time.Now, @@ -121,35 +127,21 @@ func TestWitnessCodeSenderSendToCertificateProviderWhenNotifyClientErrors(t *tes } func TestWitnessCodeSenderSendToCertificateProviderWhenDonorStoreErrors(t *testing.T) { - notifyClient := newMockNotifyClient(t) - notifyClient.EXPECT(). - SendActorSMS(mock.Anything, mock.Anything, mock.Anything, mock.Anything). - Return(nil) - donorStore := newMockDonorStore(t) donorStore.EXPECT(). Put(mock.Anything, mock.Anything). Return(expectedError) - localizer := newMockLocalizer(t) - localizer.EXPECT(). - T("property-and-affairs"). - Return("property and affairs") - localizer.EXPECT(). - Possessive("Joe Jones"). - Return("Joe Jones’") - sender := &WitnessCodeSender{ - donorStore: donorStore, - notifyClient: notifyClient, - randomCode: func(int) string { return "1234" }, - now: time.Now, + donorStore: donorStore, + randomCode: func(int) string { return "1234" }, + now: time.Now, } err := sender.SendToCertificateProvider(context.Background(), &actor.DonorProvidedDetails{ CertificateProvider: actor.CertificateProvider{Mobile: "0777"}, Donor: actor.Donor{FirstNames: "Joe", LastName: "Jones"}, Type: actor.LpaTypePropertyAndAffairs, - }, localizer) + }, nil) assert.Equal(t, expectedError, err) } @@ -220,6 +212,11 @@ func TestWitnessCodeSenderSendToIndependentWitnessWhenNotifyClientErrors(t *test SendActorSMS(mock.Anything, mock.Anything, mock.Anything, mock.Anything). Return(expectedError) + donorStore := newMockDonorStore(t) + donorStore.EXPECT(). + Put(mock.Anything, mock.Anything). + Return(nil) + localizer := newMockLocalizer(t) localizer.EXPECT(). T("property-and-affairs"). @@ -229,6 +226,7 @@ func TestWitnessCodeSenderSendToIndependentWitnessWhenNotifyClientErrors(t *test Return("Joe Jones’") sender := &WitnessCodeSender{ + donorStore: donorStore, notifyClient: notifyClient, randomCode: func(int) string { return "1234" }, now: time.Now, @@ -243,35 +241,21 @@ func TestWitnessCodeSenderSendToIndependentWitnessWhenNotifyClientErrors(t *test } func TestWitnessCodeSenderSendToIndependentWitnessWhenDonorStoreErrors(t *testing.T) { - notifyClient := newMockNotifyClient(t) - notifyClient.EXPECT(). - SendActorSMS(mock.Anything, mock.Anything, mock.Anything, mock.Anything). - Return(nil) - donorStore := newMockDonorStore(t) donorStore.EXPECT(). Put(mock.Anything, mock.Anything). Return(expectedError) - localizer := newMockLocalizer(t) - localizer.EXPECT(). - T("property-and-affairs"). - Return("property and affairs") - localizer.EXPECT(). - Possessive("Joe Jones"). - Return("Joe Jones’") - sender := &WitnessCodeSender{ - donorStore: donorStore, - notifyClient: notifyClient, - randomCode: func(int) string { return "1234" }, - now: time.Now, + donorStore: donorStore, + randomCode: func(int) string { return "1234" }, + now: time.Now, } err := sender.SendToIndependentWitness(context.Background(), &actor.DonorProvidedDetails{ IndependentWitness: actor.IndependentWitness{Mobile: "0777"}, Donor: actor.Donor{FirstNames: "Joe", LastName: "Jones"}, Type: actor.LpaTypePropertyAndAffairs, - }, localizer) + }, nil) assert.Equal(t, expectedError, err) }