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 17, 2024
1 parent 860e2f4 commit 108aa0c
Show file tree
Hide file tree
Showing 47 changed files with 1,957 additions and 223 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, 1)),
SK: dynamo.ScheduledKey(donor.CertificateProviderInvitedAt.AddDate(0, 3, 1), int(scheduled.ActionRemindCertificateProviderToComplete)),
CreatedAt: now(),
At: donor.CertificateProviderInvitedAt.AddDate(0, 3, 1),
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, 1)),
SK: dynamo.ScheduledKey(testNow.AddDate(0, 3, 1), int(scheduled.ActionRemindCertificateProviderToComplete)),
CreatedAt: testNow,
At: testNow.AddDate(0, 3, 1),
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
21 changes: 16 additions & 5 deletions cmd/schedule-runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import (
"os"
"time"

"github.com/aws/aws-lambda-go/lambda"
awslambda "github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"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"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lambda"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/scheduled"
"github.com/ministryofjustice/opg-modernising-lpa/internal/search"
Expand All @@ -40,6 +44,8 @@ var (
searchIndexingEnabled = os.Getenv("SEARCH_INDEXING_DISABLED") != "1"
tableName = os.Getenv("LPAS_TABLE")
xrayEnabled = os.Getenv("XRAY_ENABLED") == "1"
lpaStoreBaseURL = os.Getenv("LPA_STORE_BASE_URL")
lpaStoreSecretARN = os.Getenv("LPA_STORE_SECRET_ARN")

Tag string

Expand Down Expand Up @@ -81,8 +87,13 @@ func handleRunSchedule(ctx context.Context) error {
return err
}

donorStore := donor.NewStore(dynamoClient, eventClient, logger, searchClient)
lambdaClient := lambda.New(cfg, v4.NewSigner(), httpClient, time.Now)
lpaStoreClient := lpastore.New(lpaStoreBaseURL, secretsClient, lpaStoreSecretARN, lambdaClient)

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

if Tag == "" {
Tag = os.Getenv("TAG")
Expand All @@ -91,7 +102,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, lpaStoreResolvingService, notifyClient, eventClient, bundle, metricsClient, metricsEnabled)

if err = runner.Run(ctx); err != nil {
logger.Error("runner error", slog.Any("err", err))
Expand Down Expand Up @@ -154,8 +165,8 @@ func main() {
}
}(ctx)

lambda.Start(otellambda.InstrumentHandler(handleRunSchedule, xrayconfig.WithRecommendedOptions(tp)...))
awslambda.Start(otellambda.InstrumentHandler(handleRunSchedule, xrayconfig.WithRecommendedOptions(tp)...))
} else {
lambda.Start(handleRunSchedule)
awslambda.Start(handleRunSchedule)
}
}
1 change: 1 addition & 0 deletions internal/actor/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ const (
TypeTrustCorporation // trustCorporation
TypeReplacementTrustCorporation // replacementTrustCorporation
TypeVoucher // voucher
TypeCorrespondent // correspondent
)
1 change: 1 addition & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func App(
lpaStoreResolvingService,
donorStore,
eventClient,
scheduledStore,
appPublicURL,
)

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package certificateproviderpage

import (
"errors"
"time"

"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
Expand All @@ -22,4 +23,6 @@ var (
LpaID: "lpa-id",
Lang: localize.En,
}
testNow = time.Now()
testNowFn = func() time.Time { return testNow }
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/scheduled"
"github.com/ministryofjustice/opg-modernising-lpa/internal/task"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
Expand All @@ -31,13 +32,10 @@ func ProvideCertificate(
notifyClient NotifyClient,
shareCodeSender ShareCodeSender,
lpaStoreClient LpaStoreClient,
scheduledStore ScheduledStore,
now func() time.Time,
) Handler {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, certificateProvider *certificateproviderdata.Provided, lpa *lpadata.Lpa) error {
if !lpa.SignedForDonor() {
return certificateprovider.PathTaskList.Redirect(w, r, appData, lpa.LpaID)
}

if !certificateProvider.SignedAt.IsZero() {
return certificateprovider.PathCertificateProvided.Redirect(w, r, appData, lpa.LpaID)
}
Expand Down Expand Up @@ -65,7 +63,7 @@ func ProvideCertificate(

if lpa.CertificateProvider.SignedAt == nil || lpa.CertificateProvider.SignedAt.IsZero() {
if err := lpaStoreClient.SendCertificateProvider(r.Context(), certificateProvider, lpa); err != nil {
return err
return fmt.Errorf("error sending certificate provider to lpa-store: %w", err)
}
} else {
certificateProvider.SignedAt = *lpa.CertificateProvider.SignedAt
Expand All @@ -82,11 +80,27 @@ func ProvideCertificate(
}

if err := shareCodeSender.SendAttorneys(r.Context(), appData, lpa); err != nil {
return err
return fmt.Errorf("error sending sharecode to attorneys: %w", err)
}

if !certificateProvider.Tasks.ConfirmYourDetails.IsCompleted() {
if err := scheduledStore.Create(r.Context(), scheduled.Event{
At: certificateProvider.SignedAt.AddDate(0, 3, 1),
Action: scheduled.ActionRemindCertificateProviderToConfirmIdentity,
TargetLpaKey: certificateProvider.PK,
LpaUID: lpa.LpaUID,
}, scheduled.Event{
At: lpa.SignedAt.AddDate(0, 21, 1),
Action: scheduled.ActionRemindCertificateProviderToConfirmIdentity,
TargetLpaKey: certificateProvider.PK,
LpaUID: lpa.LpaUID,
}); err != nil {
return fmt.Errorf("error scheduling certificate provider prompt: %w", err)
}
}

if err := certificateProviderStore.Put(r.Context(), certificateProvider); err != nil {
return err
return fmt.Errorf("error updating certificate provider: %w", err)
}

return certificateprovider.PathCertificateProvided.Redirect(w, r, appData, certificateProvider.LpaID)
Expand Down
Loading

0 comments on commit 108aa0c

Please sign in to comment.