Skip to content

Commit

Permalink
Move stores to root packages and paths for actors (#1405)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Aug 6, 2024
1 parent e2a6c4c commit 2027cda
Show file tree
Hide file tree
Showing 65 changed files with 1,820 additions and 1,471 deletions.
3 changes: 3 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ all: true
packages:
github.com/ministryofjustice/opg-modernising-lpa/cmd/event-received:
github.com/ministryofjustice/opg-modernising-lpa/internal/app:
github.com/ministryofjustice/opg-modernising-lpa/internal/attorney:
github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneydata:
github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneypage:
github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider:
github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider/certificateproviderdata:
github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider/certificateproviderpage:
github.com/ministryofjustice/opg-modernising-lpa/internal/document:
github.com/ministryofjustice/opg-modernising-lpa/internal/donor:
github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donordata:
github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donorpage:
github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo:
Expand Down
12 changes: 6 additions & 6 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
dynamodbtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneydata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneypage"
"github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider/certificateproviderdata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider"
"github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider/certificateproviderpage"
"github.com/ministryofjustice/opg-modernising-lpa/internal/document"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donordata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donorpage"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
Expand Down Expand Up @@ -94,9 +94,9 @@ func App(
) http.Handler {
documentStore := document.NewStore(lpaDynamoClient, s3Client, eventClient)

donorStore := donordata.NewStore(lpaDynamoClient, eventClient, logger, searchClient)
certificateProviderStore := certificateproviderdata.NewStore(lpaDynamoClient)
attorneyStore := attorneydata.NewStore(lpaDynamoClient)
donorStore := donor.NewStore(lpaDynamoClient, eventClient, logger, searchClient)
certificateProviderStore := certificateprovider.NewStore(lpaDynamoClient)
attorneyStore := attorney.NewStore(lpaDynamoClient)
shareCodeStore := sharecode.NewStore(lpaDynamoClient)
dashboardStore := &dashboardStore{dynamoClient: lpaDynamoClient, lpaStoreResolvingService: lpastore.NewResolvingService(donorStore, lpaStoreClient)}
evidenceReceivedStore := &evidenceReceivedStore{dynamoClient: lpaDynamoClient}
Expand Down
47 changes: 24 additions & 23 deletions internal/attorney/attorneypage/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneydata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
Expand Down Expand Up @@ -120,32 +121,32 @@ func Register(

handleAttorney := makeAttorneyHandle(rootMux, sessionStore, errorHandler, attorneyStore)

handleAttorney(page.Paths.Attorney.CodeOfConduct, None,
handleAttorney(attorney.PathCodeOfConduct, None,
Guidance(tmpls.Get("code_of_conduct.gohtml"), lpaStoreResolvingService))
handleAttorney(page.Paths.Attorney.TaskList, None,
handleAttorney(attorney.PathTaskList, None,
TaskList(tmpls.Get("task_list.gohtml"), lpaStoreResolvingService))
handleAttorney(page.Paths.Attorney.MobileNumber, None,
handleAttorney(attorney.PathMobileNumber, None,
MobileNumber(tmpls.Get("mobile_number.gohtml"), attorneyStore))
handleAttorney(page.Paths.Attorney.YourPreferredLanguage, CanGoBack,
handleAttorney(attorney.PathYourPreferredLanguage, CanGoBack,
YourPreferredLanguage(commonTmpls.Get("your_preferred_language.gohtml"), attorneyStore, lpaStoreResolvingService))
handleAttorney(page.Paths.Attorney.ConfirmYourDetails, None,
handleAttorney(attorney.PathConfirmYourDetails, None,
ConfirmYourDetails(tmpls.Get("confirm_your_details.gohtml"), attorneyStore, lpaStoreResolvingService))
handleAttorney(page.Paths.Attorney.ReadTheLpa, None,
handleAttorney(attorney.PathReadTheLpa, None,
ReadTheLpa(tmpls.Get("read_the_lpa.gohtml"), lpaStoreResolvingService, attorneyStore))
handleAttorney(page.Paths.Attorney.RightsAndResponsibilities, None,
handleAttorney(attorney.PathRightsAndResponsibilities, None,
Guidance(tmpls.Get("legal_rights_and_responsibilities.gohtml"), nil))
handleAttorney(page.Paths.Attorney.WhatHappensWhenYouSign, CanGoBack,
handleAttorney(attorney.PathWhatHappensWhenYouSign, CanGoBack,
Guidance(tmpls.Get("what_happens_when_you_sign.gohtml"), lpaStoreResolvingService))
handleAttorney(page.Paths.Attorney.Sign, CanGoBack,
handleAttorney(attorney.PathSign, CanGoBack,
Sign(tmpls.Get("sign.gohtml"), lpaStoreResolvingService, attorneyStore, lpaStoreClient, time.Now))
handleAttorney(page.Paths.Attorney.WouldLikeSecondSignatory, None,
handleAttorney(attorney.PathWouldLikeSecondSignatory, None,
WouldLikeSecondSignatory(tmpls.Get("would_like_second_signatory.gohtml"), attorneyStore, lpaStoreResolvingService, lpaStoreClient))
handleAttorney(page.Paths.Attorney.WhatHappensNext, None,
handleAttorney(attorney.PathWhatHappensNext, None,
Guidance(tmpls.Get("what_happens_next.gohtml"), lpaStoreResolvingService))
handleAttorney(page.Paths.Attorney.Progress, None,
handleAttorney(attorney.PathProgress, None,
Progress(tmpls.Get("progress.gohtml"), lpaStoreResolvingService))

handleAttorney(page.Paths.Attorney.ConfirmDontWantToBeAttorney, CanGoBack,
handleAttorney(attorney.PathConfirmDontWantToBeAttorney, CanGoBack,
ConfirmDontWantToBeAttorney(tmpls.Get("confirm_dont_want_to_be_attorney.gohtml"), lpaStoreResolvingService, attorneyStore, notifyClient, appPublicURL))
}

Expand Down Expand Up @@ -184,8 +185,8 @@ func makeHandle(mux *http.ServeMux, store SessionStore, errorHandler page.ErrorH
}
}

func makeAttorneyHandle(mux *http.ServeMux, store SessionStore, errorHandler page.ErrorHandler, attorneyStore AttorneyStore) func(page.AttorneyPath, handleOpt, Handler) {
return func(path page.AttorneyPath, opt handleOpt, h Handler) {
func makeAttorneyHandle(mux *http.ServeMux, store SessionStore, errorHandler page.ErrorHandler, attorneyStore AttorneyStore) func(attorney.Path, handleOpt, Handler) {
return func(path attorney.Path, opt handleOpt, h Handler) {
mux.HandleFunc(path.String(), func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

Expand All @@ -210,30 +211,30 @@ func makeAttorneyHandle(mux *http.ServeMux, store SessionStore, errorHandler pag
ctx = appcontext.ContextWithSession(ctx, &appcontext.Session{SessionID: appData.SessionID, LpaID: appData.LpaID})
}

attorney, err := attorneyStore.Get(ctx)
provided, err := attorneyStore.Get(ctx)
if err != nil {
errorHandler(w, r, err)
return
}

if !page.AttorneyCanGoTo(attorney, r.URL.String()) {
page.Paths.Attorney.TaskList.Redirect(w, r, appData, attorney.LpaID)
if !attorney.CanGoTo(provided, r.URL.String()) {
page.Paths.Attorney.TaskList.Redirect(w, r, appData, provided.LpaID)
return
}

appData.Page = path.Format(appData.LpaID)
appData.AttorneyUID = attorney.UID
if attorney.IsTrustCorporation && attorney.IsReplacement {
appData.AttorneyUID = provided.UID
if provided.IsTrustCorporation && provided.IsReplacement {
appData.ActorType = actor.TypeReplacementTrustCorporation
} else if attorney.IsTrustCorporation {
} else if provided.IsTrustCorporation {
appData.ActorType = actor.TypeTrustCorporation
} else if attorney.IsReplacement {
} else if provided.IsReplacement {
appData.ActorType = actor.TypeReplacementAttorney
} else {
appData.ActorType = actor.TypeAttorney
}

if err := h(appData, w, r.WithContext(appcontext.ContextWithData(ctx, appData)), attorney); err != nil {
if err := h(appData, w, r.WithContext(appcontext.ContextWithData(ctx, appData)), provided); err != nil {
errorHandler(w, r, err)
}
})
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
@@ -1,4 +1,4 @@
package donordata
package attorney

import (
"context"
Expand Down
76 changes: 76 additions & 0 deletions internal/attorney/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package attorney

import (
"net/http"
"net/url"
"strings"

"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneydata"
)

const (
PathCodeOfConduct = Path("/code-of-conduct")
PathConfirmDontWantToBeAttorney = Path("/confirm-you-do-not-want-to-be-an-attorney")
PathConfirmYourDetails = Path("/confirm-your-details")
PathMobileNumber = Path("/mobile-number")
PathProgress = Path("/progress")
PathReadTheLpa = Path("/read-the-lpa")
PathRightsAndResponsibilities = Path("/legal-rights-and-responsibilities")
PathSign = Path("/sign")
PathTaskList = Path("/task-list")
PathWhatHappensNext = Path("/what-happens-next")
PathWhatHappensWhenYouSign = Path("/what-happens-when-you-sign-the-lpa")
PathWouldLikeSecondSignatory = Path("/would-like-second-signatory")
PathYourPreferredLanguage = Path("/your-preferred-language")
)

type Path string

func (p Path) String() string {
return "/attorney/{id}" + string(p)
}

func (p Path) Format(id string) string {
return "/attorney/" + id + string(p)
}

func (p Path) Redirect(w http.ResponseWriter, r *http.Request, appData appcontext.Data, lpaID string) error {
http.Redirect(w, r, appData.Lang.URL(p.Format(lpaID)), http.StatusFound)
return nil
}

func (p Path) RedirectQuery(w http.ResponseWriter, r *http.Request, appData appcontext.Data, lpaID string, query url.Values) error {
http.Redirect(w, r, appData.Lang.URL(p.Format(lpaID))+"?"+query.Encode(), http.StatusFound)
return nil
}

func (p Path) canVisit(attorney *attorneydata.Provided) bool {
switch p {
case PathRightsAndResponsibilities,
PathWhatHappensWhenYouSign,
PathSign,
PathWhatHappensNext:
return attorney.Tasks.ConfirmYourDetails.Completed() && attorney.Tasks.ReadTheLpa.Completed()

case PathWouldLikeSecondSignatory:
return attorney.Tasks.ConfirmYourDetails.Completed() && attorney.Tasks.ReadTheLpa.Completed() && attorney.IsTrustCorporation

default:
return true
}
}

func CanGoTo(attorney *attorneydata.Provided, url string) bool {
path, _, _ := strings.Cut(url, "?")
if path == "" {
return false
}

if strings.HasPrefix(path, "/attorney/") {
_, attorneyPath, _ := strings.Cut(strings.TrimPrefix(path, "/attorney/"), "/")
return Path("/" + attorneyPath).canVisit(attorney)
}

return true
}
118 changes: 118 additions & 0 deletions internal/attorney/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package attorney

import (
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneydata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/task"
"github.com/stretchr/testify/assert"
)

func TestAttorneyPathString(t *testing.T) {
assert.Equal(t, "/attorney/{id}/anything", Path("/anything").String())
}

func TestAttorneyPathFormat(t *testing.T) {
assert.Equal(t, "/attorney/abc/anything", Path("/anything").Format("abc"))
}

func TestAttorneyPathRedirect(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
p := Path("/something")

err := p.Redirect(w, r, appcontext.Data{Lang: localize.En}, "lpa-id")
resp := w.Result()

assert.Nil(t, err)
assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, p.Format("lpa-id"), resp.Header.Get("Location"))
}

func TestAttorneyPathRedirectQuery(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
p := Path("/something")

err := p.RedirectQuery(w, r, appcontext.Data{Lang: localize.En}, "lpa-id", url.Values{"q": {"1"}})
resp := w.Result()

assert.Nil(t, err)
assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, p.Format("lpa-id")+"?q=1", resp.Header.Get("Location"))
}

func TestAttorneyCanGoTo(t *testing.T) {
testCases := map[string]struct {
attorney *attorneydata.Provided
url string
expected bool
}{
"empty path": {
attorney: &attorneydata.Provided{},
url: "",
expected: false,
},
"unexpected path": {
attorney: &attorneydata.Provided{},
url: "/whatever",
expected: true,
},
"unrestricted path": {
attorney: &attorneydata.Provided{},
url: PathConfirmYourDetails.Format("123"),
expected: true,
},
"sign without task": {
attorney: &attorneydata.Provided{
Tasks: attorneydata.Tasks{
ConfirmYourDetails: task.StateCompleted,
},
},
url: PathSign.Format("123"),
expected: false,
},
"sign with task": {
attorney: &attorneydata.Provided{
Tasks: attorneydata.Tasks{
ConfirmYourDetails: task.StateCompleted,
ReadTheLpa: task.StateCompleted,
},
},
url: PathSign.Format("123"),
expected: true,
},
"would like second signatory not trust corp": {
attorney: &attorneydata.Provided{
Tasks: attorneydata.Tasks{
ConfirmYourDetails: task.StateCompleted,
ReadTheLpa: task.StateCompleted,
},
},
url: PathWouldLikeSecondSignatory.Format("123"),
expected: false,
},
"would like second signatory as trust corp": {
attorney: &attorneydata.Provided{
Tasks: attorneydata.Tasks{
ConfirmYourDetails: task.StateCompleted,
ReadTheLpa: task.StateCompleted,
},
IsTrustCorporation: true,
},
url: PathWouldLikeSecondSignatory.Format("123"),
expected: true,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.expected, CanGoTo(tc.attorney, tc.url))
})
}
}
Loading

0 comments on commit 2027cda

Please sign in to comment.