Skip to content

Commit

Permalink
VEGA-2501 : Manual Status Change #minor (#264)
Browse files Browse the repository at this point in the history
* VEGA-2501 : Initial changes to update the status

* VEGA-2501 : New file to update the status

* VEGA-2501 : Updates to handle the status change update

* VEGA-2501 : Corrected the error field type

* VEGA-2501 : Reverted the port number for apigw service

* VEGA-2501 : Update the change type
  • Loading branch information
ndasmoj authored Oct 17, 2024
1 parent 916f84c commit d1d0a7b
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/opg-status-change.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "OPG_STATUS_CHANGE",
"changes": [
{
"key": "/status",
"new": "cannot-register",
"old": "in-progress"
}
]
}
5 changes: 5 additions & 0 deletions internal/shared/lpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ const (
LpaStatusRegistered = LpaStatus("registered")
LpaStatusCannotRegister = LpaStatus("cannot-register")
LpaStatusWithdrawn = LpaStatus("withdrawn")
LpaStatusCancelled = LpaStatus("cancelled")
)

func (l LpaStatus) IsValid() bool {
return l == LpaStatusInProgress || l == LpaStatusStatutoryWaitingPeriod || l == LpaStatusRegistered || l == LpaStatusCannotRegister || l == LpaStatusWithdrawn || l == LpaStatusCancelled
}
45 changes: 45 additions & 0 deletions lambda/update/opg_change_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"github.com/ministryofjustice/opg-data-lpa-store/internal/shared"
"github.com/ministryofjustice/opg-data-lpa-store/internal/validate"
"github.com/ministryofjustice/opg-data-lpa-store/lambda/update/parse"
)

type OpgChangeStatus struct {
Status shared.LpaStatus
}

func (r OpgChangeStatus) Apply(lpa *shared.Lpa) []shared.FieldError {

if r.Status != shared.LpaStatusCannotRegister {
return []shared.FieldError{{Source: "/status", Detail: "Status to be updated should be cannot register"}}
}

if lpa.Status == shared.LpaStatusRegistered {
return []shared.FieldError{{Source: "/status", Detail: "Lpa status cannot be registered"}}
}

if lpa.Status == shared.LpaStatusCancelled {
return []shared.FieldError{{Source: "/status", Detail: "Lpa status cannot be cancelled"}}
}

lpa.Status = r.Status

return nil
}

func validateOpgChangeStatus(changes []shared.Change, lpa *shared.Lpa) (OpgChangeStatus, []shared.FieldError) {

var data OpgChangeStatus

data.Status = lpa.Status

errors := parse.Changes(changes).
Field("/status", &data.Status, parse.Validate(func() []shared.FieldError {
return validate.IsValid("", data.Status)
})).
Consumed()

return data, errors
}
94 changes: 94 additions & 0 deletions lambda/update/opg_change_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"encoding/json"
"github.com/ministryofjustice/opg-data-lpa-store/internal/shared"
"github.com/stretchr/testify/assert"
"testing"
)

func TestOpgChangeStatusApply(t *testing.T) {
lpa := &shared.Lpa{
Status: shared.LpaStatusInProgress,
}
c := OpgChangeStatus{
Status: shared.LpaStatusCannotRegister,
}

errors := c.Apply(lpa)
assert.Empty(t, errors)
assert.Equal(t, c.Status, lpa.Status)
}

func TestOpgChangeStatusInvalidNewStatus(t *testing.T) {
lpa := &shared.Lpa{
Status: shared.LpaStatusInProgress,
}
c := OpgChangeStatus{
Status: shared.LpaStatusWithdrawn,
}

errors := c.Apply(lpa)
assert.Equal(t, errors, []shared.FieldError{{Source: "/status", Detail: "Status to be updated should be cannot register"}})
}

func TestOpgChangeStatusIncorrectExistingStatus(t *testing.T) {
lpa := &shared.Lpa{
Status: shared.LpaStatusRegistered,
}
c := OpgChangeStatus{
Status: shared.LpaStatusCannotRegister,
}

errors := c.Apply(lpa)
assert.Equal(t, errors, []shared.FieldError{{Source: "/status", Detail: "Lpa status cannot be registered"}})
}

func TestValidateUpdateOPGChangeStatus(t *testing.T) {
testcases := map[string]struct {
update shared.Update
lpa *shared.Lpa
errors []shared.FieldError
}{
"valid - with previous values": {
update: shared.Update{
Type: "OPG_STATUS_CHANGE",
Changes: []shared.Change{
{
Key: "/status",
New: json.RawMessage(`"cannot-register"`),
Old: json.RawMessage(`"in-progress"`),
},
},
},
lpa: &shared.Lpa{
Status: shared.LpaStatusInProgress,
},
},
"invalid status": {
update: shared.Update{
Type: "OPG_STATUS_CHANGE",
Changes: []shared.Change{
{
Key: "/status",
New: json.RawMessage(`"never-register"`),
Old: json.RawMessage(`"in-progress"`),
},
},
},
lpa: &shared.Lpa{
Status: shared.LpaStatusInProgress,
},
errors: []shared.FieldError{
{Source: "/changes/0/new", Detail: "invalid value"},
},
},
}

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
_, errors := validateUpdate(tc.update, tc.lpa)
assert.ElementsMatch(t, tc.errors, errors)
})
}
}
7 changes: 7 additions & 0 deletions lambda/update/parse/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ func oldEqualsExisting(old any, existing any) bool {

return shared.IdentityCheckType(old.(string)) == *v

case *shared.LpaStatus:
if old == nil {
return *v == ""
}

return shared.LpaStatus(old.(string)) == *v

case *string:
if old == nil {
return *v == ""
Expand Down
2 changes: 2 additions & 0 deletions lambda/update/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func validateUpdate(update shared.Update, lpa *shared.Lpa) (Applyable, []shared.
return validateStatutoryWaitingPeriod(update.Changes)
case "REGISTER":
return validateRegister(update.Changes)
case "OPG_STATUS_CHANGE":
return validateOpgChangeStatus(update.Changes, lpa)
case "TRUST_CORPORATION_SIGN":
return validateTrustCorporationSign(update.Changes, lpa)
case "DONOR_CONFIRM_IDENTITY":
Expand Down

0 comments on commit d1d0a7b

Please sign in to comment.