Skip to content

Commit

Permalink
MLPAB-2022 Copy correspondent in resolving service and trace notify s…
Browse files Browse the repository at this point in the history
…ends (#1421)

* Add tracing spans for notify sends

* Copy correspondent data in resolving service
  • Loading branch information
hawx authored Aug 13, 2024
1 parent e85c900 commit 138fc3e
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 79 deletions.
31 changes: 25 additions & 6 deletions internal/lpastore/lpadata/lpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,31 @@ type Lpa struct {
// witnessed by their CertificateProvider)
SignedAt time.Time
CertificateProviderNotRelatedConfirmedAt time.Time
Submitted bool
Paid bool
IsOrganisationDonor bool
Drafted bool
CannotRegister bool
Correspondent Correspondent

// Submitted is set if SubmittedAt is non-zero for online applications, or set
// to true for paper applications.
Submitted bool

// Paid is set if the PayForLpa task has been completed for online
// applications, or set to true for paper applications as to be in the
// lpa-store the application payment must be complete.
Paid bool

// IsOrganisationDonor is set to true when the Lpa is being made by a
// supporter working for an organisation.
IsOrganisationDonor bool

// Drafted is set if the CheckYourLpa task has been completed for online
// applications, or set to true for paper applications.
Drafted bool

// CannotRegister is set to true if the status in the lpa-store is
// cannot-register.
CannotRegister bool

// Correspondent is set using the data set by the donor for online
// applications, but is not set for paper applications.
Correspondent Correspondent
}

func (l *Lpa) CorrespondentEmail() string {
Expand Down
5 changes: 5 additions & 0 deletions internal/lpastore/resolving_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (s *ResolvingService) merge(lpa *lpadata.Lpa, donor *donordata.Provided) *l
lpa.Paid = donor.Tasks.PayForLpa.IsCompleted()
_, lpa.IsOrganisationDonor = donor.SK.Organisation()
lpa.Donor.Channel = lpadata.ChannelOnline
lpa.Correspondent = lpadata.Correspondent{
FirstNames: donor.Correspondent.FirstNames,
LastName: donor.Correspondent.LastName,
Email: donor.Correspondent.Email,
}

// copy the relationship as it isn't stored in the lpastore.
lpa.CertificateProvider.Relationship = donor.CertificateProvider.Relationship
Expand Down
4 changes: 3 additions & 1 deletion internal/lpastore/resolving_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestResolvingServiceGet(t *testing.T) {
Status: identity.StatusConfirmed,
RetrievedAt: time.Now(),
},
Correspondent: donordata.Correspondent{Email: "x"},
},
resolved: &lpadata.Lpa{
LpaID: "1",
Expand All @@ -58,7 +59,8 @@ func TestResolvingServiceGet(t *testing.T) {
FirstNames: "Paul",
Relationship: lpadata.Personally,
},
Donor: lpadata.Donor{Channel: lpadata.ChannelOnline},
Donor: lpadata.Donor{Channel: lpadata.ChannelOnline},
Correspondent: lpadata.Correspondent{Email: "x"},
},
},
"online with no lpastore record": {
Expand Down
32 changes: 31 additions & 1 deletion internal/notify/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore/lpadata"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

var (
Expand Down Expand Up @@ -136,6 +139,14 @@ type emailWrapper struct {
}

func (c *Client) SendEmail(ctx context.Context, to string, email Email) error {
tracer := otel.GetTracerProvider().Tracer("mlpab")
ctx, span := tracer.Start(ctx, "Email",
trace.WithSpanKind(trace.SpanKindInternal))
span.SetAttributes(
attribute.KeyValue{Key: "template_id", Value: attribute.StringValue(email.emailID(c.isProduction))},
attribute.KeyValue{Key: "to", Value: attribute.StringValue(to)})
defer span.End()

req, err := c.newRequest(ctx, http.MethodPost, "/v2/notifications/email", emailWrapper{
EmailAddress: to,
TemplateID: email.emailID(c.isProduction),
Expand All @@ -145,16 +156,25 @@ func (c *Client) SendEmail(ctx context.Context, to string, email Email) error {
return err
}

_, err = c.do(req)
resp, err := c.do(req)
if err != nil {
c.logger.ErrorContext(ctx, "email send failed", slog.String("to", to))
return err
}
span.SetAttributes(attribute.KeyValue{Key: "notify_id", Value: attribute.StringValue(resp.ID)})

return nil
}

func (c *Client) SendActorEmail(ctx context.Context, to, lpaUID string, email Email) error {
tracer := otel.GetTracerProvider().Tracer("mlpab")
ctx, span := tracer.Start(ctx, "Email",
trace.WithSpanKind(trace.SpanKindInternal))
span.SetAttributes(
attribute.KeyValue{Key: "template_id", Value: attribute.StringValue(email.emailID(c.isProduction))},
attribute.KeyValue{Key: "to", Value: attribute.StringValue(to)})
defer span.End()

if ok, err := c.recentlySent(ctx, c.makeReference(lpaUID, to, email)); err != nil || ok {
return err
}
Expand All @@ -174,6 +194,7 @@ func (c *Client) SendActorEmail(ctx context.Context, to, lpaUID string, email Em
c.logger.ErrorContext(ctx, "email send failed", slog.String("to", to))
return err
}
span.SetAttributes(attribute.KeyValue{Key: "notify_id", Value: attribute.StringValue(resp.ID)})

if !slices.Contains(simulatedEmails, to) {
if err := c.eventClient.SendNotificationSent(ctx, event.NotificationSent{
Expand All @@ -194,6 +215,14 @@ type smsWrapper struct {
}

func (c *Client) SendActorSMS(ctx context.Context, to, lpaUID string, sms SMS) error {
tracer := otel.GetTracerProvider().Tracer("mlpab")
ctx, span := tracer.Start(ctx, "SMS",
trace.WithSpanKind(trace.SpanKindInternal))
span.SetAttributes(
attribute.KeyValue{Key: "template_id", Value: attribute.StringValue(sms.smsID(c.isProduction))},
attribute.KeyValue{Key: "to", Value: attribute.StringValue(to)})
defer span.End()

req, err := c.newRequest(ctx, http.MethodPost, "/v2/notifications/sms", smsWrapper{
PhoneNumber: to,
TemplateID: sms.smsID(c.isProduction),
Expand All @@ -207,6 +236,7 @@ func (c *Client) SendActorSMS(ctx context.Context, to, lpaUID string, sms SMS) e
if err != nil {
return err
}
span.SetAttributes(attribute.KeyValue{Key: "notification_id", Value: attribute.StringValue(resp.ID)})

if !slices.Contains(simulatedPhones, to) {
if err := c.eventClient.SendNotificationSent(ctx, event.NotificationSent{
Expand Down
Loading

0 comments on commit 138fc3e

Please sign in to comment.