Skip to content

Commit

Permalink
Merge 77968a3 into 6214b24
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Nov 7, 2023
2 parents 6214b24 + 77968a3 commit 566bc51
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 415 deletions.
30 changes: 17 additions & 13 deletions internal/app/donor_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"context"
"errors"
"log"
"time"

"github.com/aws/aws-sdk-go-v2/service/s3"
Expand Down Expand Up @@ -189,40 +190,43 @@ func (s *donorStore) Put(ctx context.Context, lpa *page.Lpa) error {
}

if lpa.UID != "" && lpa.Tasks.PayForLpa.IsPending() {
log.Println("reduced-fee-requested event start")

documents, err := s.documentStore.GetAll(ctx)
if err != nil {
s.logger.Print(err)
return s.dynamoClient.Put(ctx, lpa)
}

var unsentKeys []string
var unsentDocuments page.Documents

for _, document := range documents {
if document.Sent.IsZero() && !document.Scanned {
unsentKeys = append(unsentKeys, document.Key)
log.Printf("%#v", document)
if document.Sent.IsZero() && document.Scanned && !document.VirusDetected {
unsentDocuments = append(unsentDocuments, document)
}
}

if len(unsentKeys) > 0 {
log.Println("reduced-fee-requested unsentDocuments: ", len(unsentDocuments))

if len(unsentDocuments) > 0 {
log.Println("reduced-fee-requested sending event")

if err := s.eventClient.SendReducedFeeRequested(ctx, event.ReducedFeeRequested{
UID: lpa.UID,
RequestType: lpa.FeeType.String(),
Evidence: unsentKeys,
Evidence: unsentDocuments.Keys(),
EvidenceDelivery: lpa.EvidenceDelivery.String(),
}); err != nil {
return err
}
log.Println("reduced-fee-requested sent event")

var updatedDocuments page.Documents

for _, document := range documents {
if document.Sent.IsZero() && !document.Scanned {
document.Sent = s.now()
updatedDocuments = append(updatedDocuments, document)
}
for i := range unsentDocuments {
unsentDocuments[i].Sent = s.now()
}

if err := s.documentStore.BatchPut(ctx, updatedDocuments); err != nil {
if err := s.documentStore.BatchPut(ctx, unsentDocuments); err != nil {
s.logger.Print(err)
}
}
Expand Down
10 changes: 5 additions & 5 deletions internal/app/donor_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,10 @@ func TestDonorStorePutWhenReducedFeeRequestedAndUnsentDocuments(t *testing.T) {
documentStore.
On("GetAll", ctx).
Return(page.Documents{
{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf"},
{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Scanned: true},
}, nil)
documentStore.
On("BatchPut", ctx, []page.Document{{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Sent: now}}).
On("BatchPut", ctx, []page.Document{{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Scanned: true, Sent: now}}).
Return(nil)

donorStore := &donorStore{dynamoClient: dynamoClient, eventClient: eventClient, now: func() time.Time { return now }, documentStore: documentStore}
Expand Down Expand Up @@ -585,7 +585,7 @@ func TestDonorStorePutWhenReducedFeeRequestedAndUnsentDocumentsWhenEventClientSe
documentStore.
On("GetAll", ctx).
Return(page.Documents{
{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf"},
{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Scanned: true},
}, nil)

donorStore := &donorStore{eventClient: eventClient, now: func() time.Time { return now }, documentStore: documentStore}
Expand Down Expand Up @@ -622,10 +622,10 @@ func TestDonorStorePutWhenReducedFeeRequestedAndUnsentDocumentsWhenDocumentStore
documentStore.
On("GetAll", ctx).
Return(page.Documents{
{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf"},
{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Scanned: true},
}, nil)
documentStore.
On("BatchPut", ctx, []page.Document{{Key: "lpa-uid-evidence-a-uid", Filename: "whatever.pdf", Sent: now}}).
On("BatchPut", ctx, mock.Anything).
Return(expectedError)

logger := newMockLogger(t)
Expand Down
28 changes: 1 addition & 27 deletions internal/page/donor/payment_confirmation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package donor
import (
"fmt"
"net/http"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/gorilla/sessions"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
Expand All @@ -25,7 +22,7 @@ type paymentConfirmationData struct {
EvidenceDelivery pay.EvidenceDelivery
}

func PaymentConfirmation(logger Logger, tmpl template.Template, payClient PayClient, donorStore DonorStore, sessionStore sessions.Store, evidenceS3Client S3Client, now func() time.Time, documentStore DocumentStore) Handler {
func PaymentConfirmation(logger Logger, tmpl template.Template, payClient PayClient, donorStore DonorStore, sessionStore sessions.Store) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, lpa *page.Lpa) error {
paymentSession, err := sesh.Payment(sessionStore, r)
if err != nil {
Expand Down Expand Up @@ -62,29 +59,6 @@ func PaymentConfirmation(logger Logger, tmpl template.Template, payClient PayCli
lpa.Tasks.PayForLpa = actor.PaymentTaskCompleted
} else {
lpa.Tasks.PayForLpa = actor.PaymentTaskPending

documents, err := documentStore.GetAll(r.Context())
if err != nil {
return err
}

for _, document := range documents {
if document.Sent.IsZero() {
err := evidenceS3Client.PutObjectTagging(r.Context(), document.Key, []types.Tag{
{Key: aws.String("replicate"), Value: aws.String("true")},
})

if err != nil {
logger.Print(fmt.Sprintf("error tagging evidence: %s", err.Error()))
return err
}

document.Sent = now()
if err := documentStore.Put(r.Context(), document); err != nil {
return err
}
}
}
}

if err := donorStore.Put(r.Context(), lpa); err != nil {
Expand Down
157 changes: 6 additions & 151 deletions internal/page/donor/payment_confirmation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/gorilla/sessions"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
Expand Down Expand Up @@ -55,7 +52,7 @@ func TestGetPaymentConfirmationFullFee(t *testing.T) {
}).
Return(nil)

err := PaymentConfirmation(newMockLogger(t), template.Execute, payClient, donorStore, sessionStore, nil, nil, nil)(testAppData, w, r, &page.Lpa{
err := PaymentConfirmation(newMockLogger(t), template.Execute, payClient, donorStore, sessionStore)(testAppData, w, r, &page.Lpa{
FeeType: pay.FullFee,
CertificateProvider: actor.CertificateProvider{
Email: "[email protected]",
Expand Down Expand Up @@ -87,8 +84,6 @@ func TestGetPaymentConfirmationHalfFee(t *testing.T) {
withPaySession(r).
withExpiredPaySession(r, w)

now := time.Now()

donorStore := newMockDonorStore(t)
donorStore.
On("Put", r.Context(), &page.Lpa{
Expand All @@ -107,25 +102,7 @@ func TestGetPaymentConfirmationHalfFee(t *testing.T) {
}).
Return(nil)

documentStore := newMockDocumentStore(t)
documentStore.
On("GetAll", r.Context()).
Return(page.Documents{
{Key: "evidence-key"},
{Key: "another-evidence-key", Sent: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC)},
}, nil)
documentStore.
On("Put", r.Context(), page.Document{Key: "evidence-key", Sent: now}).
Return(nil)

s3Client := newMockS3Client(t)
s3Client.
On("PutObjectTagging", r.Context(), "evidence-key", []types.Tag{
{Key: aws.String("replicate"), Value: aws.String("true")},
}).
Return(nil)

err := PaymentConfirmation(newMockLogger(t), template.Execute, payClient, donorStore, sessionStore, s3Client, func() time.Time { return now }, documentStore)(testAppData, w, r, &page.Lpa{
err := PaymentConfirmation(newMockLogger(t), template.Execute, payClient, donorStore, sessionStore)(testAppData, w, r, &page.Lpa{
FeeType: pay.HalfFee,
CertificateProvider: actor.CertificateProvider{
Email: "[email protected]",
Expand All @@ -148,7 +125,7 @@ func TestGetPaymentConfirmationWhenErrorGettingSession(t *testing.T) {
On("Get", r, "pay").
Return(&sessions.Session{}, expectedError)

err := PaymentConfirmation(nil, template.Execute, newMockPayClient(t), nil, sessionStore, nil, nil, nil)(testAppData, w, r, &page.Lpa{})
err := PaymentConfirmation(nil, template.Execute, newMockPayClient(t), nil, sessionStore)(testAppData, w, r, &page.Lpa{})
resp := w.Result()

assert.Equal(t, expectedError, err)
Expand All @@ -174,7 +151,7 @@ func TestGetPaymentConfirmationWhenErrorGettingPayment(t *testing.T) {

template := newMockTemplate(t)

err := PaymentConfirmation(logger, template.Execute, payClient, nil, sessionStore, nil, nil, nil)(testAppData, w, r, &page.Lpa{})
err := PaymentConfirmation(logger, template.Execute, payClient, nil, sessionStore)(testAppData, w, r, &page.Lpa{})
resp := w.Result()

assert.Equal(t, expectedError, err)
Expand Down Expand Up @@ -208,7 +185,7 @@ func TestGetPaymentConfirmationWhenErrorExpiringSession(t *testing.T) {
On("Execute", w, mock.Anything).
Return(nil)

err := PaymentConfirmation(logger, template.Execute, payClient, donorStore, sessionStore, nil, nil, nil)(testAppData, w, r, &page.Lpa{CertificateProvider: actor.CertificateProvider{
err := PaymentConfirmation(logger, template.Execute, payClient, donorStore, sessionStore)(testAppData, w, r, &page.Lpa{CertificateProvider: actor.CertificateProvider{
Email: "[email protected]",
}})
resp := w.Result()
Expand All @@ -217,113 +194,6 @@ func TestGetPaymentConfirmationWhenErrorExpiringSession(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestGetPaymentConfirmationHalfFeeWhenDocumentStoreGetAllError(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/payment-confirmation", nil)

payClient := newMockPayClient(t).
withASuccessfulPayment("abc123", "123456789012", 4100)

sessionStore := newMockSessionStore(t).
withPaySession(r).
withExpiredPaySession(r, w)

now := time.Now()

documentStore := newMockDocumentStore(t)
documentStore.
On("GetAll", r.Context()).
Return(page.Documents{}, expectedError)

err := PaymentConfirmation(nil, nil, payClient, nil, sessionStore, nil, func() time.Time { return now }, documentStore)(testAppData, w, r, &page.Lpa{
FeeType: pay.HalfFee,
CertificateProvider: actor.CertificateProvider{
Email: "[email protected]",
},
})
resp := w.Result()

assert.Equal(t, expectedError, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestGetPaymentConfirmationHalfFeeWhenS3ClientPutTaggingObjectError(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/payment-confirmation", nil)

payClient := newMockPayClient(t).
withASuccessfulPayment("abc123", "123456789012", 4100)

sessionStore := newMockSessionStore(t).
withPaySession(r).
withExpiredPaySession(r, w)

now := time.Now()

documentStore := newMockDocumentStore(t)
documentStore.
On("GetAll", r.Context()).
Return(page.Documents{{}}, nil)

s3Client := newMockS3Client(t)
s3Client.
On("PutObjectTagging", mock.Anything, mock.Anything, mock.Anything).
Return(expectedError)

logger := newMockLogger(t)
logger.
On("Print", fmt.Sprintf("error tagging evidence: %s", expectedError))

err := PaymentConfirmation(logger, nil, payClient, nil, sessionStore, s3Client, func() time.Time { return now }, documentStore)(testAppData, w, r, &page.Lpa{
FeeType: pay.HalfFee,
CertificateProvider: actor.CertificateProvider{
Email: "[email protected]",
},
})
resp := w.Result()

assert.Equal(t, expectedError, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestGetPaymentConfirmationHalfFeeWhenDocumentStorePutError(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/payment-confirmation", nil)

payClient := newMockPayClient(t).
withASuccessfulPayment("abc123", "123456789012", 4100)

sessionStore := newMockSessionStore(t).
withPaySession(r).
withExpiredPaySession(r, w)

now := time.Now()

documentStore := newMockDocumentStore(t)
documentStore.
On("GetAll", r.Context()).
Return(page.Documents{{}}, nil)
documentStore.
On("Put", r.Context(), mock.Anything).
Return(expectedError)

s3Client := newMockS3Client(t)
s3Client.
On("PutObjectTagging", mock.Anything, mock.Anything, mock.Anything).
Return(nil)

err := PaymentConfirmation(nil, nil, payClient, nil, sessionStore, s3Client, func() time.Time { return now }, documentStore)(testAppData, w, r, &page.Lpa{
FeeType: pay.HalfFee,
CertificateProvider: actor.CertificateProvider{
Email: "[email protected]",
},
})
resp := w.Result()

assert.Equal(t, expectedError, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestGetPaymentConfirmationHalfFeeWhenDonorStorePutError(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/payment-confirmation", nil)
Expand All @@ -335,31 +205,16 @@ func TestGetPaymentConfirmationHalfFeeWhenDonorStorePutError(t *testing.T) {
withPaySession(r).
withExpiredPaySession(r, w)

now := time.Now()

donorStore := newMockDonorStore(t)
donorStore.
On("Put", r.Context(), mock.Anything).
Return(expectedError)

documentStore := newMockDocumentStore(t)
documentStore.
On("GetAll", r.Context()).
Return(page.Documents{{}}, nil)
documentStore.
On("Put", r.Context(), mock.Anything).
Return(nil)

s3Client := newMockS3Client(t)
s3Client.
On("PutObjectTagging", mock.Anything, mock.Anything, mock.Anything).
Return(nil)

logger := newMockLogger(t)
logger.
On("Print", fmt.Sprintf("unable to update lpa in donorStore: %s", expectedError))

err := PaymentConfirmation(logger, nil, payClient, donorStore, sessionStore, s3Client, func() time.Time { return now }, documentStore)(testAppData, w, r, &page.Lpa{
err := PaymentConfirmation(logger, nil, payClient, donorStore, sessionStore)(testAppData, w, r, &page.Lpa{
FeeType: pay.HalfFee,
CertificateProvider: actor.CertificateProvider{
Email: "[email protected]",
Expand Down
Loading

0 comments on commit 566bc51

Please sign in to comment.