Skip to content

Commit

Permalink
Merge 937d100 into ff84342
Browse files Browse the repository at this point in the history
  • Loading branch information
acsauk authored Feb 1, 2024
2 parents ff84342 + 937d100 commit 3f0cc7e
Show file tree
Hide file tree
Showing 18 changed files with 562 additions and 115 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
go.opentelemetry.io/otel/trace v1.22.0
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
golang.org/x/mod v0.14.0
golang.org/x/time v0.5.0
golang.org/x/tools v0.17.0
google.golang.org/grpc v1.61.0
)
Expand Down Expand Up @@ -127,7 +128,6 @@ require (
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.153.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand Down
4 changes: 3 additions & 1 deletion internal/actor/organisation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package actor

import "time"
import (
"time"
)

const memberInviteExpireAfter = time.Hour * 48

Expand Down
8 changes: 7 additions & 1 deletion internal/app/donor_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,14 @@ func (s *donorStore) Get(ctx context.Context) (*actor.DonorProvidedDetails, erro
return nil, errors.New("donorStore.Get requires LpaID and SessionID")
}

sk := donorKey(data.SessionID)

if data.OrganisationID != "" {
sk = organisationKey(data.OrganisationID)
}

var donor *actor.DonorProvidedDetails
err = s.dynamoClient.One(ctx, lpaKey(data.LpaID), donorKey(data.SessionID), &donor)
err = s.dynamoClient.One(ctx, lpaKey(data.LpaID), sk, &donor)
return donor, err
}

Expand Down
43 changes: 37 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 Expand Up @@ -93,6 +93,37 @@ func (s *organisationStore) CreateMemberInvite(ctx context.Context, organisation
return nil
}

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

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

lpaID := s.uuidString()

donor := &actor.DonorProvidedDetails{
PK: lpaKey(lpaID),
SK: organisationKey(organisationID),
LpaID: lpaID,
CreatedAt: s.now(),
Version: 1,
}

if donor.Hash, err = donor.GenerateHash(); err != nil {
return nil, err
}

if err := s.dynamoClient.Create(ctx, donor); err != nil {
return nil, err
}

return donor, err
}

func organisationKey(s string) string {
return "ORGANISATION#" + s
}
Expand Down
5 changes: 3 additions & 2 deletions internal/page/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
)

type SessionData struct {
SessionID string
LpaID string
SessionID string
LpaID string
OrganisationID string
}

type SessionMissingError struct{}
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
8 changes: 4 additions & 4 deletions internal/page/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,11 @@ var Paths = AppPaths{
},

Supporter: SupporterPaths{
Start: "/supporter-start",
Login: "/supporter-login",
LoginCallback: "/supporter-login-callback",
Start: "/supporter-start",
Login: "/supporter-login",
LoginCallback: "/supporter-login-callback",
EnterOrganisationName: "/enter-the-name-of-your-organisation-or-company",

EnterOrganisationName: "/enter-the-name-of-your-organisation-or-company",
OrganisationCreated: "/organisation-or-company-created",
Dashboard: "/supporter-dashboard",
InviteMember: "/invite-member",
Expand Down
30 changes: 30 additions & 0 deletions internal/page/supporter/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package supporter

import (
"net/http"

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

type DashboardData struct {
App page.AppData
Errors validation.List
}

func Dashboard(tmpl template.Template, organisationStore OrganisationStore) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, organisation *actor.Organisation) error {
if r.Method == http.MethodPost {
donorProvided, err := organisationStore.CreateLPA(r.Context(), organisation.ID)
if err != nil {
return err
}

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

return tmpl(w, DashboardData{App: appData})
}
}
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
Loading

0 comments on commit 3f0cc7e

Please sign in to comment.