Skip to content

Commit

Permalink
Add scheduled events to remind certificate provider
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx committed Dec 13, 2024
1 parent 67d71c6 commit bd173fd
Show file tree
Hide file tree
Showing 21 changed files with 696 additions and 72 deletions.
24 changes: 18 additions & 6 deletions cmd/event-received/sirius_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/pay"
"github.com/ministryofjustice/opg-modernising-lpa/internal/scheduled"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sharecode"
"github.com/ministryofjustice/opg-modernising-lpa/internal/task"
)
Expand Down Expand Up @@ -231,16 +232,27 @@ func handleDonorSubmissionCompleted(ctx context.Context, client dynamodbClient,
lpaID := uuidString()

donor := &donordata.Provided{
PK: dynamo.LpaKey(lpaID),
SK: dynamo.LpaOwnerKey(dynamo.DonorKey("PAPER")),
LpaID: lpaID,
LpaUID: v.UID,
CreatedAt: now(),
Version: 1,
PK: dynamo.LpaKey(lpaID),
SK: dynamo.LpaOwnerKey(dynamo.DonorKey("PAPER")),
LpaID: lpaID,
LpaUID: v.UID,
CreatedAt: now(),
Version: 1,
CertificateProviderInvitedAt: now(),
}

transaction := dynamo.NewTransaction().
Create(donor).
Create(scheduled.Event{
PK: dynamo.ScheduledDayKey(donor.CertificateProviderInvitedAt.AddDate(0, 3, 0)),
SK: dynamo.ScheduledKey(donor.CertificateProviderInvitedAt.AddDate(0, 3, 0), int(scheduled.ActionRemindCertificateProviderToComplete)),
CreatedAt: now(),
At: donor.CertificateProviderInvitedAt.AddDate(0, 3, 0),
Action: scheduled.ActionRemindCertificateProviderToComplete,
TargetLpaKey: donor.PK,
TargetLpaOwnerKey: donor.SK,
LpaUID: donor.LpaUID,
}).
Create(dynamo.Keys{PK: dynamo.UIDKey(v.UID), SK: dynamo.MetadataKey("")}).
Create(dynamo.Keys{PK: donor.PK, SK: dynamo.ReservedKey(dynamo.DonorKey)})

Expand Down
24 changes: 18 additions & 6 deletions cmd/event-received/sirius_event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore/lpadata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/pay"
"github.com/ministryofjustice/opg-modernising-lpa/internal/scheduled"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sharecode"
"github.com/ministryofjustice/opg-modernising-lpa/internal/task"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -756,12 +757,23 @@ func TestHandleDonorSubmissionCompleted(t *testing.T) {
WriteTransaction(ctx, &dynamo.Transaction{
Creates: []any{
&donordata.Provided{
PK: dynamo.LpaKey(testUuidString),
SK: dynamo.LpaOwnerKey(dynamo.DonorKey("PAPER")),
LpaID: testUuidString,
LpaUID: "M-1111-2222-3333",
CreatedAt: testNow,
Version: 1,
PK: dynamo.LpaKey(testUuidString),
SK: dynamo.LpaOwnerKey(dynamo.DonorKey("PAPER")),
LpaID: testUuidString,
LpaUID: "M-1111-2222-3333",
CreatedAt: testNow,
Version: 1,
CertificateProviderInvitedAt: testNow,
},
scheduled.Event{
PK: dynamo.ScheduledDayKey(testNow.AddDate(0, 3, 0)),
SK: dynamo.ScheduledKey(testNow.AddDate(0, 3, 0), int(scheduled.ActionRemindCertificateProviderToComplete)),
CreatedAt: testNow,
At: testNow.AddDate(0, 3, 0),
Action: scheduled.ActionRemindCertificateProviderToComplete,
TargetLpaKey: dynamo.LpaKey(testUuidString),
TargetLpaOwnerKey: dynamo.LpaOwnerKey(dynamo.DonorKey("PAPER")),
LpaUID: "M-1111-2222-3333",
},
dynamo.Keys{PK: dynamo.UIDKey("M-1111-2222-3333"), SK: dynamo.MetadataKey("")},
dynamo.Keys{PK: dynamo.LpaKey(testUuidString), SK: dynamo.ReservedKey(dynamo.DonorKey)},
Expand Down
6 changes: 4 additions & 2 deletions cmd/schedule-runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
Expand Down Expand Up @@ -81,8 +82,9 @@ func handleRunSchedule(ctx context.Context) error {
return err
}

donorStore := donor.NewStore(dynamoClient, eventClient, logger, searchClient)
scheduledStore := scheduled.NewStore(dynamoClient)
donorStore := donor.NewStore(dynamoClient, eventClient, logger, searchClient)
certificateProviderStore := certificateprovider.NewStore(dynamoClient)

if Tag == "" {
Tag = os.Getenv("TAG")
Expand All @@ -91,7 +93,7 @@ func handleRunSchedule(ctx context.Context) error {
client := cloudwatch.NewFromConfig(cfg)
metricsClient := telemetry.NewMetricsClient(client, Tag)

runner := scheduled.NewRunner(logger, scheduledStore, donorStore, notifyClient, metricsClient, metricsEnabled)
runner := scheduled.NewRunner(logger, scheduledStore, donorStore, certificateProviderStore, notifyClient, bundle, metricsClient, metricsEnabled)

if err = runner.Run(ctx); err != nil {
logger.Error("runner error", slog.Any("err", err))
Expand Down
6 changes: 5 additions & 1 deletion internal/certificateprovider/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ func (s *Store) GetAny(ctx context.Context) (*certificateproviderdata.Provided,
return nil, errors.New("certificateProviderStore.GetAny requires LpaID")
}

return s.One(ctx, dynamo.LpaKey(data.LpaID))
}

func (s *Store) One(ctx context.Context, pk dynamo.LpaKeyType) (*certificateproviderdata.Provided, error) {
var certificateProvider certificateproviderdata.Provided
err = s.dynamoClient.OneByPartialSK(ctx, dynamo.LpaKey(data.LpaID), dynamo.CertificateProviderKey(""), &certificateProvider)
err := s.dynamoClient.OneByPartialSK(ctx, pk, dynamo.CertificateProviderKey(""), &certificateProvider)

return &certificateProvider, err
}
Expand Down
20 changes: 19 additions & 1 deletion internal/donor/donordata/provided.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ type Provided struct {
// for, if applying for a repeat of an LPA with reference prefixed M.
CostOfRepeatApplication pay.CostOfRepeatApplication

// CertificateProviderInvitedAt records when the invite is sent to the
// certificate provider to act.
CertificateProviderInvitedAt time.Time

HasSentApplicationUpdatedEvent bool `hash:"-"`
}

Expand Down Expand Up @@ -218,7 +222,8 @@ func (c toCheck) HashInclude(field string, _ any) (bool, error) {
"WantVoucher",
"Voucher",
"FailedVouchAttempts",
"CostOfRepeatApplication":
"CostOfRepeatApplication",
"CertificateProviderInvitedAt":
return false, nil
}

Expand Down Expand Up @@ -294,6 +299,19 @@ func (p *Provided) CourtOfProtectionSubmissionDeadline() time.Time {
return p.SignedAt.AddDate(0, 6, 0)
}

// ExpiresAt gives the date the LPA expires.
func (p *Provided) ExpiresAt() time.Time {
if p.DonorIdentityConfirmed() && !p.WitnessedByCertificateProviderAt.IsZero() {
return p.SignedAt.AddDate(0, 24, 0)
}

if !p.SignedAt.IsZero() {
return p.SignedAt.AddDate(0, 6, 0)
}

return time.Time{}
}

type Under18ActorDetails struct {
FullName string
DateOfBirth date.Date
Expand Down
4 changes: 2 additions & 2 deletions internal/donor/donordata/provided_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func TestGenerateHash(t *testing.T) {
}

// DO change this value to match the updates
const modified uint64 = 0x42eaf05fa4af6121
const modified uint64 = 0xed86effa3514f49d

// DO NOT change these initial hash values. If a field has been added/removed
// you will need to handle the version gracefully by modifying
// (*Provided).HashInclude and adding another testcase for the new
// version.
testcases := map[uint8]uint64{
0: 0xe929f7d694b60743,
0: 0xfcc0efdd2824800c,
}

for version, initial := range testcases {
Expand Down
20 changes: 19 additions & 1 deletion internal/donor/donorpage/check_your_lpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package donorpage
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/scheduled"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sharecode"
"github.com/ministryofjustice/opg-modernising-lpa/internal/task"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
Expand All @@ -33,7 +35,9 @@ type checkYourLpaNotifier struct {
notifyClient NotifyClient
shareCodeSender ShareCodeSender
certificateProviderStore CertificateProviderStore
scheduledStore ScheduledStore
appPublicURL string
now func() time.Time
}

func (n *checkYourLpaNotifier) Notify(ctx context.Context, appData appcontext.Data, donor *donordata.Provided, wasCompleted bool) error {
Expand Down Expand Up @@ -66,6 +70,18 @@ func (n *checkYourLpaNotifier) sendPaperNotification(ctx context.Context, appDat

func (n *checkYourLpaNotifier) sendOnlineNotification(ctx context.Context, appData appcontext.Data, donor *donordata.Provided, wasCompleted bool) error {
if !wasCompleted {
donor.CertificateProviderInvitedAt = n.now()

if err := n.scheduledStore.Create(ctx, scheduled.Event{
At: donor.CertificateProviderInvitedAt.AddDate(0, 3, 0),
Action: scheduled.ActionRemindCertificateProviderToComplete,
TargetLpaKey: donor.PK,
TargetLpaOwnerKey: donor.SK,
LpaUID: donor.LpaUID,
}); err != nil {
return fmt.Errorf("could not schedule certificate provider prompt: %w", err)
}

return n.shareCodeSender.SendCertificateProviderInvite(ctx, appData, sharecode.CertificateProviderInvite{
LpaKey: donor.PK,
LpaOwnerKey: donor.SK,
Expand Down Expand Up @@ -101,12 +117,14 @@ func (n *checkYourLpaNotifier) sendOnlineNotification(ctx context.Context, appDa
return n.notifyClient.SendActorSMS(ctx, notify.ToProvidedCertificateProvider(certificateProvider, donor.CertificateProvider), donor.LpaUID, sms)
}

func CheckYourLpa(tmpl template.Template, donorStore DonorStore, shareCodeSender ShareCodeSender, notifyClient NotifyClient, certificateProviderStore CertificateProviderStore, now func() time.Time, appPublicURL string) Handler {
func CheckYourLpa(tmpl template.Template, donorStore DonorStore, shareCodeSender ShareCodeSender, notifyClient NotifyClient, certificateProviderStore CertificateProviderStore, scheduledStore ScheduledStore, now func() time.Time, appPublicURL string) Handler {
notifier := &checkYourLpaNotifier{
notifyClient: notifyClient,
shareCodeSender: shareCodeSender,
certificateProviderStore: certificateProviderStore,
appPublicURL: appPublicURL,
now: now,
scheduledStore: scheduledStore,
}

return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *donordata.Provided) error {
Expand Down
Loading

0 comments on commit bd173fd

Please sign in to comment.