Skip to content

Commit

Permalink
prefer passing org ID
Browse files Browse the repository at this point in the history
  • Loading branch information
acsauk committed Feb 1, 2024
1 parent 8f55518 commit d32570e
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 105 deletions.
12 changes: 6 additions & 6 deletions internal/app/organisation_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ type organisationStore struct {
now func() time.Time
}

func (s *organisationStore) Create(ctx context.Context, name string) error {
func (s *organisationStore) Create(ctx context.Context, name string) (*actor.Organisation, error) {
data, err := page.SessionDataFromContext(ctx)
if err != nil {
return err
return nil, err
}

if data.SessionID == "" {
return errors.New("organisationStore.Create requires SessionID")
return nil, errors.New("organisationStore.Create requires SessionID")
}

organisationID := s.uuidString()
Expand All @@ -38,7 +38,7 @@ func (s *organisationStore) Create(ctx context.Context, name string) error {
}

if err := s.dynamoClient.Create(ctx, organisation); err != nil {
return fmt.Errorf("error creating organisation: %w", err)
return nil, fmt.Errorf("error creating organisation: %w", err)
}

member := &actor.Member{
Expand All @@ -48,10 +48,10 @@ func (s *organisationStore) Create(ctx context.Context, name string) error {
}

if err := s.dynamoClient.Create(ctx, member); err != nil {
return fmt.Errorf("error creating organisation member: %w", err)
return nil, fmt.Errorf("error creating organisation member: %w", err)
}

return nil
return organisation, nil
}

func (s *organisationStore) Get(ctx context.Context) (*actor.Organisation, error) {
Expand Down
12 changes: 9 additions & 3 deletions internal/page/donor/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,15 +436,21 @@ func makeLpaHandle(mux *http.ServeMux, store sesh.Store, defaultOptions page.Han
appData.ActorType = actor.TypeDonor
appData.AppPublicURL = appPublicURL

donorSession, err := sesh.Login(store, r)
loginSession, err := sesh.Login(store, r)
if err != nil {
http.Redirect(w, r, page.Paths.Start.Format(), http.StatusFound)
return
}

appData.SessionID = donorSession.SessionID()

sessionData, err := page.SessionDataFromContext(ctx)

appData.SessionID = loginSession.SessionID()

if loginSession.OrganisationID != "" {
appData.IsSupporter = true
sessionData.OrganisationID = loginSession.OrganisationID
}

if err == nil {
sessionData.SessionID = appData.SessionID
ctx = page.ContextWithSessionData(ctx, sessionData)
Expand Down
17 changes: 12 additions & 5 deletions internal/page/fixtures/supporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"encoding/base64"
"net/http"

"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/random"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
)

type OrganisationStore interface {
Create(context.Context, string) error
Create(context.Context, string) (*actor.Organisation, error)
}

func Supporter(sessionStore sesh.Store, organisationStore OrganisationStore) page.Handler {
Expand All @@ -25,14 +26,20 @@ func Supporter(sessionStore sesh.Store, organisationStore OrganisationStore) pag
ctx = page.ContextWithSessionData(r.Context(), &page.SessionData{SessionID: supporterSessionID})
)

if err := sesh.SetLoginSession(sessionStore, r, w, &sesh.LoginSession{Sub: supporterSub, Email: testEmail}); err != nil {
return err
}
loginSession := &sesh.LoginSession{Sub: supporterSub, Email: testEmail}

if organisation == "1" {
if err := organisationStore.Create(ctx, random.String(12)); err != nil {
org, err := organisationStore.Create(ctx, random.String(12))

if err != nil {
return err
}

loginSession.OrganisationID = org.ID
}

if err := sesh.SetLoginSession(sessionStore, r, w, loginSession); err != nil {
return err
}

if redirect != page.Paths.Supporter.EnterOrganisationName.Format() {
Expand Down
4 changes: 0 additions & 4 deletions internal/page/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ func (p LpaPath) Redirect(w http.ResponseWriter, r *http.Request, appData AppDat
rurl = fromURL
}

if appData.IsSupporter {
rurl = "/supporter/" + rurl
}

if CanGoTo(donor, rurl) {
http.Redirect(w, r, appData.Lang.URL(rurl), http.StatusFound)
} else {
Expand Down
2 changes: 1 addition & 1 deletion internal/page/supporter/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Dashboard(tmpl template.Template, organisationStore OrganisationStore) Hand
return err
}

return page.Paths.Supporter.DonorDetails.RedirectToLpa(w, r.WithContext(r.Context()), appData, donorProvided)
return page.Paths.YourDetails.Redirect(w, r.WithContext(r.Context()), appData, donorProvided)
}

return tmpl(w, DashboardData{App: appData})
Expand Down
16 changes: 14 additions & 2 deletions internal/page/supporter/enter_organisation_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)

Expand All @@ -14,7 +15,7 @@ type enterOrganisationNameData struct {
Form *enterOrganisationNameForm
}

func EnterOrganisationName(tmpl template.Template, organisationStore OrganisationStore) page.Handler {
func EnterOrganisationName(tmpl template.Template, organisationStore OrganisationStore, sessionStore sesh.Store) page.Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request) error {
data := &enterOrganisationNameData{
App: appData,
Expand All @@ -26,7 +27,18 @@ func EnterOrganisationName(tmpl template.Template, organisationStore Organisatio
data.Errors = data.Form.Validate()

if !data.Errors.Any() {
if err := organisationStore.Create(r.Context(), data.Form.Name); err != nil {
organisation, err := organisationStore.Create(r.Context(), data.Form.Name)
if err != nil {
return err
}

loginSession, err := sesh.Login(sessionStore, r)
if err != nil {
return page.Paths.Supporter.Start.Redirect(w, r, appData)
}

loginSession.OrganisationID = organisation.ID
if err := sesh.SetLoginSession(sessionStore, r, w, loginSession); err != nil {
return err
}

Expand Down
24 changes: 16 additions & 8 deletions internal/page/supporter/login_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,30 @@ func LoginCallback(oneLoginClient LoginCallbackOneLoginClient, sessionStore sesh

session := &sesh.LoginSession{
IDToken: idToken,
Sub: userInfo.Sub,
Sub: "supporter-" + userInfo.Sub,
Email: userInfo.Email,
}

if err := sesh.SetLoginSession(sessionStore, r, w, session); err != nil {
return err
}

ctx := page.ContextWithSessionData(r.Context(), &page.SessionData{SessionID: session.SessionID()})

_, err = organisationStore.Get(ctx)
organisation, err := organisationStore.Get(ctx)
if err == nil {
session.OrganisationID = organisation.ID
if err := sesh.SetLoginSession(sessionStore, r, w, session); err != nil {
return err
}

return page.Paths.Supporter.Dashboard.Redirect(w, r, appData)
}
if !errors.Is(err, dynamo.NotFoundError{}) {
return err

if errors.Is(err, dynamo.NotFoundError{}) {
if err := sesh.SetLoginSession(sessionStore, r, w, &sesh.LoginSession{
IDToken: idToken,
Sub: "supporter-" + userInfo.Sub,
Email: userInfo.Email,
}); err != nil {
return err
}
}

return page.Paths.Supporter.EnterOrganisationName.Redirect(w, r, appData)
Expand Down
26 changes: 15 additions & 11 deletions internal/page/supporter/mock_Handler_test.go

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

28 changes: 20 additions & 8 deletions internal/page/supporter/mock_OrganisationStore_test.go

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

Loading

0 comments on commit d32570e

Please sign in to comment.