diff --git a/lambda/update/correction.go b/lambda/update/correction.go index d7262968..28b8478d 100644 --- a/lambda/update/correction.go +++ b/lambda/update/correction.go @@ -1,10 +1,10 @@ package main import ( - "fmt" "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" + "strconv" "time" ) @@ -31,6 +31,11 @@ func (c Correction) Apply(lpa *shared.Lpa) []shared.FieldError { return []shared.FieldError{{Source: "/signedAt", Detail: "LPA Signed on date cannot be changed for online LPAs"}} } + if c.Index != nil && lpa.Attorneys[*c.Index].SignedAt != nil && !lpa.Attorneys[*c.Index].SignedAt.IsZero() && lpa.Channel == shared.ChannelOnline { + source := "/attorney/" + strconv.Itoa(*c.Index) + "/signedAt" + return []shared.FieldError{{Source: source, Detail: "The attorney signed at date cannot be changed for online LPA"}} + } + if lpa.Status == shared.LpaStatusRegistered { return []shared.FieldError{{Source: "/type", Detail: "Cannot make corrections to a Registered LPA"}} } @@ -102,8 +107,6 @@ func validateCorrection(changes []shared.Change, lpa *shared.Lpa) (Correction, [ } data.Index = &i - fmt.Println(data.Index) - fmt.Println(lpa.Attorneys[i].LastName) data.AttorneyFirstNames = lpa.Attorneys[i].FirstNames data.AttorneyLastName = lpa.Attorneys[i].LastName data.AttorneyDob = lpa.Attorneys[i].DateOfBirth diff --git a/lambda/update/correction_test.go b/lambda/update/correction_test.go index 7a87f7e2..ab545ba3 100644 --- a/lambda/update/correction_test.go +++ b/lambda/update/correction_test.go @@ -15,7 +15,7 @@ func createDate(date string) shared.Date { return d } -func TestCorrectionApply(t *testing.T) { +func TestDonorCorrectionApply(t *testing.T) { now := time.Now() yesterday := now.Add(-24 * time.Hour) lpa := &shared.Lpa{ @@ -67,6 +67,62 @@ func TestCorrectionApply(t *testing.T) { assert.Equal(t, correction.LPASignedAt, lpa.SignedAt) } +func TestAttorneyCorrectionApply(t *testing.T) { + now := time.Now() + yesterday := now.Add(-24 * time.Hour) + lpa := &shared.Lpa{ + LpaInit: shared.LpaInit{ + Attorneys: []shared.Attorney{ + shared.Attorney{}, + { + Person: shared.Person{ + FirstNames: "attorney-firstname", + LastName: "attorney-lastname", + }, + DateOfBirth: createDate("1990-01-02"), + Address: shared.Address{ + Line1: "123 Main St", + Town: "Anytown", + Postcode: "A11 B22", + Country: "IE", + }, + Email: "test@test.com", + Mobile: "0123456789", + SignedAt: &yesterday, + }, + }, + }, + } + + index := 1 + correction := Correction{ + Index: &index, + AttorneyFirstNames: "Jane", + AttorneyLastName: "Smith", + AttorneyDob: createDate("2000-11-11"), + AttorneyAddress: shared.Address{ + Line1: "456 Another St", + Town: "Othertown", + Postcode: "B22 A11", + Country: "GB", + }, + AttorneyEmail: "jane.smith@example.com", + AttorneyMobile: "987654321", + AttorneySignedAt: now, + } + + errors := correction.Apply(lpa) + + assert.Empty(t, errors) + assert.Equal(t, correction.AttorneyFirstNames, lpa.Attorneys[index].FirstNames) + assert.Equal(t, correction.AttorneyLastName, lpa.Attorneys[index].LastName) + assert.Equal(t, correction.AttorneyDob, lpa.Attorneys[index].DateOfBirth) + assert.Equal(t, correction.AttorneyAddress, lpa.Attorneys[index].Address) + assert.Equal(t, correction.AttorneyEmail, lpa.Attorneys[index].Email) + assert.Equal(t, correction.AttorneyMobile, lpa.Attorneys[index].Mobile) + assert.Equal(t, correction.AttorneySignedAt, *lpa.Attorneys[index].SignedAt) +} + func TestCorrectionRegisteredLpa(t *testing.T) { lpa := &shared.Lpa{ Status: shared.LpaStatusRegistered, @@ -106,6 +162,30 @@ func TestCorrectionLpaSignedOnlineChannel(t *testing.T) { assert.Equal(t, errors, []shared.FieldError{{Source: "/signedAt", Detail: "LPA Signed on date cannot be changed for online LPAs"}}) } +func TestCorrectionAttorneySignedAtChannel(t *testing.T) { + now := time.Now() + yesterday := now.Add(-24 * time.Hour) + lpa := &shared.Lpa{ + LpaInit: shared.LpaInit{ + Channel: "online", + Attorneys: []shared.Attorney{ + { + SignedAt: &yesterday, + }, + }, + }, + } + + index := 0 + correction := Correction{ + Index: &index, + AttorneySignedAt: now, + } + errors := correction.Apply(lpa) + + assert.Equal(t, errors, []shared.FieldError{{Source: "/attorney/0/signedAt", Detail: "The attorney signed at date cannot be changed for online LPA"}}) +} + func TestValidateCorrection(t *testing.T) { now := time.Now() @@ -131,53 +211,84 @@ func TestValidateCorrection(t *testing.T) { }, "valid attorney update": { changes: []shared.Change{ - {Key: "/attorneys/0/firstNames", New: json.RawMessage(`"Jane"`), Old: jsonNull}, - {Key: "/attorneys/0/lastName", New: json.RawMessage(`"Doe"`), Old: jsonNull}, - {Key: "/attorneys/0/dateOfBirth", New: json.RawMessage(`"2000-01-01"`), Old: jsonNull}, + {Key: "/attorneys/0/firstNames", New: json.RawMessage(`"Shanelle"`), Old: jsonNull}, + {Key: "/attorneys/0/lastName", New: json.RawMessage(`"Kerluke"`), Old: jsonNull}, + {Key: "/attorneys/0/dateOfBirth", New: json.RawMessage(`"1949-10-20"`), Old: jsonNull}, {Key: "/attorneys/0/email", New: json.RawMessage(`"test@test.com"`), Old: jsonNull}, {Key: "/attorneys/0/mobile", New: json.RawMessage(`"123456789"`), Old: jsonNull}, - {Key: "/attorneys/0/address/line1", New: json.RawMessage(`"123 Main St"`), Old: jsonNull}, - {Key: "/attorneys/0/address/town", New: json.RawMessage(`"City"`), Old: jsonNull}, + {Key: "/attorneys/0/address/line1", New: json.RawMessage(`"13 Park Avenue"`), Old: jsonNull}, + {Key: "/attorneys/0/address/town", New: json.RawMessage(`"Clwyd"`), Old: jsonNull}, + {Key: "/attorneys/0/address/postcode", New: json.RawMessage(`"OH03 2LM"`), Old: jsonNull}, {Key: "/attorneys/0/address/country", New: json.RawMessage(`"GB"`), Old: jsonNull}, {Key: "/attorneys/0/signedAt", New: json.RawMessage(`"` + now.Format(time.RFC3339Nano) + `"`), Old: jsonNull}, }, lpa: &shared.Lpa{ LpaInit: shared.LpaInit{ - Donor: shared.Donor{}, Attorneys: []shared.Attorney{ shared.Attorney{}, }, }, }, }, + "valid replacement attorney update": { + changes: []shared.Change{ + {Key: "/attorneys/1/firstNames", New: json.RawMessage(`"Anthony"`), Old: jsonNull}, + {Key: "/attorneys/1/lastName", New: json.RawMessage(`"Leannon"`), Old: jsonNull}, + {Key: "/attorneys/1/dateOfBirth", New: json.RawMessage(`"1963-11-08"`), Old: jsonNull}, + {Key: "/attorneys/1/address/town", New: json.RawMessage(`"Cheshire"`), Old: jsonNull}, + {Key: "/attorneys/1/address/country", New: json.RawMessage(`"GB"`), Old: jsonNull}, + }, + lpa: &shared.Lpa{ + LpaInit: shared.LpaInit{ + Attorneys: []shared.Attorney{ + { + AppointmentType: shared.AppointmentTypeOriginal, + Status: shared.AttorneyStatusActive, + }, + { + AppointmentType: shared.AppointmentTypeReplacement, + Status: shared.AttorneyStatusReplacement, + }, + }, + }, + }, + }, "missing required fields": { changes: []shared.Change{ {Key: "/donor/firstNames", New: jsonNull, Old: jsonNull}, {Key: "/donor/lastName", New: jsonNull, Old: jsonNull}, + {Key: "/attorneys/0/firstNames", New: jsonNull, Old: jsonNull}, + {Key: "/attorneys/0/lastName", New: jsonNull, Old: jsonNull}, }, lpa: &shared.Lpa{ LpaInit: shared.LpaInit{ - Donor: shared.Donor{}, + Donor: shared.Donor{}, + Attorneys: []shared.Attorney{{}}, }, }, errors: []shared.FieldError{ {Source: "/changes/0/new", Detail: "field is required"}, {Source: "/changes/1/new", Detail: "field is required"}, + {Source: "/changes/2/new", Detail: "field is required"}, + {Source: "/changes/3/new", Detail: "field is required"}, }, }, "invalid country": { changes: []shared.Change{ {Key: "/donor/address/country", New: json.RawMessage(`"United Kingdom"`), Old: jsonNull}, + {Key: "/attorneys/0/address/country", New: json.RawMessage(`"United Kingdom"`), Old: jsonNull}, }, lpa: &shared.Lpa{ LpaInit: shared.LpaInit{ Donor: shared.Donor{ Address: shared.Address{}, }, + Attorneys: []shared.Attorney{{}}, }, }, errors: []shared.FieldError{ {Source: "/changes/0/new", Detail: "must be a valid ISO-3166-1 country code"}, + {Source: "/changes/1/new", Detail: "must be a valid ISO-3166-1 country code"}, }, }, }