diff --git a/lambda/update/attorney_sign.go b/lambda/update/attorney_sign.go index 84285ead..505fc58e 100644 --- a/lambda/update/attorney_sign.go +++ b/lambda/update/attorney_sign.go @@ -31,7 +31,7 @@ func (a AttorneySign) Apply(lpa *shared.Lpa) []shared.FieldError { return nil } -func validateAttorneySign(changes []shared.Change) (AttorneySign, []shared.FieldError) { +func validateAttorneySign(changes []shared.Change, lpa *shared.Lpa) (AttorneySign, []shared.FieldError) { var data AttorneySign errors := parse.Changes(changes). @@ -43,18 +43,27 @@ func validateAttorneySign(changes []shared.Change) (AttorneySign, []shared.Field } data.Index = &i + data.Mobile = lpa.Attorneys[i].Mobile + data.ContactLanguagePreference = lpa.Attorneys[i].ContactLanguagePreference + data.Channel = lpa.Attorneys[i].Channel + data.Email = lpa.Attorneys[i].Email + + if lpa.Attorneys[i].SignedAt != nil { + data.SignedAt = *lpa.Attorneys[i].SignedAt + } + return p. - Field("/mobile", &data.Mobile). + Field("/mobile", &data.Mobile, parse.MustMatchExisting()). Field("/signedAt", &data.SignedAt, parse.Validate(func() []shared.FieldError { return validate.Time("", data.SignedAt) - })). + }), parse.MustMatchExisting()). Field("/contactLanguagePreference", &data.ContactLanguagePreference, parse.Validate(func() []shared.FieldError { return validate.IsValid("", data.ContactLanguagePreference) - })). + }), parse.MustMatchExisting()). Field("/channel", &data.Channel, parse.Validate(func() []shared.FieldError { return validate.IsValid("", data.Channel) - }), parse.Optional()). - Field("/email", &data.Email, parse.Optional()). + }), parse.MustMatchExisting()). + Field("/email", &data.Email, parse.MustMatchExisting(), parse.Optional()). Consumed() }). Consumed() diff --git a/lambda/update/attorney_sign_test.go b/lambda/update/attorney_sign_test.go index 27820786..da63e1fa 100644 --- a/lambda/update/attorney_sign_test.go +++ b/lambda/update/attorney_sign_test.go @@ -50,6 +50,7 @@ func TestValidateUpdateAttorneySign(t *testing.T) { testcases := map[string]struct { update shared.Update + lpa *shared.Lpa errors []shared.FieldError }{ "valid - no previous values": { @@ -57,64 +58,65 @@ func TestValidateUpdateAttorneySign(t *testing.T) { Type: "ATTORNEY_SIGN", Changes: []shared.Change{ { - Key: "/attorneys/1/mobile", + Key: "/attorneys/0/mobile", New: json.RawMessage(`"07777"`), Old: jsonNull, }, { - Key: "/attorneys/1/signedAt", - New: json.RawMessage(`"` + time.Now().Format(time.RFC3339) + `"`), + Key: "/attorneys/0/signedAt", + New: json.RawMessage(`"` + time.Now().Format(time.RFC3339Nano) + `"`), Old: jsonNull, }, { - Key: "/attorneys/1/contactLanguagePreference", + Key: "/attorneys/0/contactLanguagePreference", New: json.RawMessage(`"cy"`), Old: jsonNull, }, { - Key: "/attorneys/1/channel", + Key: "/attorneys/0/channel", New: json.RawMessage(`"online"`), Old: jsonNull, }, - { - Key: "/attorneys/1/email", - New: json.RawMessage(`"a@example.com"`), - Old: jsonNull, - }, }, }, + lpa: &shared.Lpa{LpaInit: shared.LpaInit{Attorneys: []shared.Attorney{ + {}, + }}}, }, "valid - with previous values": { update: shared.Update{ Type: "ATTORNEY_SIGN", Changes: []shared.Change{ { - Key: "/attorneys/1/mobile", + Key: "/attorneys/0/mobile", New: json.RawMessage(`"07777"`), Old: json.RawMessage(`"06666"`), }, { - Key: "/attorneys/1/signedAt", - New: json.RawMessage(`"` + now.Format(time.RFC3339) + `"`), - Old: json.RawMessage(`"` + yesterday.Format(time.RFC3339) + `"`), + Key: "/attorneys/0/signedAt", + New: json.RawMessage(`"` + now.Format(time.RFC3339Nano) + `"`), + Old: json.RawMessage(`"` + yesterday.Format(time.RFC3339Nano) + `"`), }, { - Key: "/attorneys/1/contactLanguagePreference", + Key: "/attorneys/0/contactLanguagePreference", New: json.RawMessage(`"cy"`), Old: jsonNull, }, { - Key: "/attorneys/1/channel", + Key: "/attorneys/0/channel", New: json.RawMessage(`"online"`), Old: json.RawMessage(`"paper"`), }, { - Key: "/attorneys/1/email", + Key: "/attorneys/0/email", New: json.RawMessage(`"b@example.com"`), Old: json.RawMessage(`"a@example.com"`), }, }, }, + lpa: &shared.Lpa{LpaInit: shared.LpaInit{Attorneys: []shared.Attorney{ + {Channel: shared.ChannelPaper, Email: "a@example.com", Mobile: "06666", SignedAt: &yesterday}, + }}}, }, "missing all": { update: shared.Update{Type: "ATTORNEY_SIGN"}, @@ -127,17 +129,17 @@ func TestValidateUpdateAttorneySign(t *testing.T) { Type: "ATTORNEY_SIGN", Changes: []shared.Change{ { - Key: "/attorneys/1/mobile", + Key: "/attorneys/0/mobile", New: json.RawMessage(`"0777"`), Old: jsonNull, }, { - Key: "/attorneys/1/signedAt", + Key: "/attorneys/0/signedAt", New: json.RawMessage(`"` + time.Now().Format(time.RFC3339) + `"`), Old: jsonNull, }, { - Key: "/attorneys/1/contactLanguagePreference", + Key: "/attorneys/0/contactLanguagePreference", New: json.RawMessage(`"` + shared.LangCy + `"`), Old: jsonNull, }, @@ -147,17 +149,25 @@ func TestValidateUpdateAttorneySign(t *testing.T) { Old: jsonNull, }, { - Key: "/attorneys/1/firstNames", + Key: "/attorneys/0/firstNames", New: json.RawMessage(`"John"`), Old: jsonNull, }, { - Key: "/attorneys/1/email", + Key: "/attorneys/0/email", New: json.RawMessage(`"a@example.com"`), Old: jsonNull, }, + { + Key: "/attorneys/0/channel", + New: json.RawMessage(`"paper"`), + Old: jsonNull, + }, }, }, + lpa: &shared.Lpa{LpaInit: shared.LpaInit{Attorneys: []shared.Attorney{ + {}, + }}}, errors: []shared.FieldError{ {Source: "/changes/3", Detail: "unexpected change provided"}, {Source: "/changes/4", Detail: "unexpected change provided"}, @@ -168,38 +178,41 @@ func TestValidateUpdateAttorneySign(t *testing.T) { Type: "ATTORNEY_SIGN", Changes: []shared.Change{ { - Key: "/attorneys/1/mobile", + Key: "/attorneys/0/mobile", New: json.RawMessage(`"07777"`), Old: jsonNull, }, { - Key: "/attorneys/1/signedAt", + Key: "/attorneys/0/signedAt", New: json.RawMessage(`"` + time.Now().Format(time.RFC3339) + `"`), Old: jsonNull, }, { - Key: "/attorneys/1/contactLanguagePreference", + Key: "/attorneys/0/contactLanguagePreference", New: json.RawMessage(`"xy"`), Old: jsonNull, }, { - Key: "/attorneys/1/channel", + Key: "/attorneys/0/channel", New: json.RawMessage(`"digital"`), Old: jsonNull, }, { - Key: "/attorneys/1/email", + Key: "/attorneys/0/email", New: json.RawMessage(`"b@example.com"`), Old: jsonNull, }, }, }, + lpa: &shared.Lpa{LpaInit: shared.LpaInit{Attorneys: []shared.Attorney{ + {}, + }}}, errors: []shared.FieldError{ {Source: "/changes/2/new", Detail: "invalid value"}, {Source: "/changes/3/new", Detail: "invalid value"}, }, }, - "multiple attorneys": { + "multiple attorneys - multiple attorney changes": { update: shared.Update{ Type: "ATTORNEY_SIGN", Changes: []shared.Change{ @@ -223,18 +236,61 @@ func TestValidateUpdateAttorneySign(t *testing.T) { New: json.RawMessage(`"a@example.com"`), Old: jsonNull, }, + { + Key: "/attorneys/0/channel", + New: json.RawMessage(`"online"`), + Old: jsonNull, + }, }, }, + lpa: &shared.Lpa{LpaInit: shared.LpaInit{Attorneys: []shared.Attorney{ + {}, {}, + }}}, errors: []shared.FieldError{ {Source: "/changes/1/key", Detail: "index out of range"}, {Source: "/changes", Detail: "missing /attorneys/0/signedAt"}, }, }, + "multiple attorneys - single attorney change": { + update: shared.Update{ + Type: "ATTORNEY_SIGN", + Changes: []shared.Change{ + { + Key: "/attorneys/1/mobile", + New: json.RawMessage(`"07777"`), + Old: json.RawMessage(`"06666"`), + }, + { + Key: "/attorneys/1/signedAt", + New: json.RawMessage(`"` + now.Format(time.RFC3339Nano) + `"`), + Old: json.RawMessage(`"` + yesterday.Format(time.RFC3339Nano) + `"`), + }, + { + Key: "/attorneys/1/contactLanguagePreference", + New: json.RawMessage(`"cy"`), + Old: jsonNull, + }, + { + Key: "/attorneys/1/channel", + New: json.RawMessage(`"online"`), + Old: json.RawMessage(`"paper"`), + }, + { + Key: "/attorneys/1/email", + New: json.RawMessage(`"b@example.com"`), + Old: json.RawMessage(`"a@example.com"`), + }, + }, + }, + lpa: &shared.Lpa{LpaInit: shared.LpaInit{Attorneys: []shared.Attorney{ + {}, {Channel: shared.ChannelPaper, Email: "a@example.com", Mobile: "06666", SignedAt: &yesterday}, + }}}, + }, } for name, tc := range testcases { t.Run(name, func(t *testing.T) { - _, errors := validateUpdate(tc.update, &shared.Lpa{}) + _, errors := validateUpdate(tc.update, tc.lpa) assert.ElementsMatch(t, tc.errors, errors) }) } diff --git a/lambda/update/trust_corporation_sign.go b/lambda/update/trust_corporation_sign.go index 6c1ccd8b..d314de0f 100644 --- a/lambda/update/trust_corporation_sign.go +++ b/lambda/update/trust_corporation_sign.go @@ -29,7 +29,7 @@ func (a TrustCorporationSign) Apply(lpa *shared.Lpa) []shared.FieldError { return nil } -func validateTrustCorporationSign(changes []shared.Change) (TrustCorporationSign, []shared.FieldError) { +func validateTrustCorporationSign(changes []shared.Change, lpa *shared.Lpa) (TrustCorporationSign, []shared.FieldError) { var data TrustCorporationSign errors := parse.Changes(changes). diff --git a/lambda/update/validate.go b/lambda/update/validate.go index 00578348..bdd998ab 100644 --- a/lambda/update/validate.go +++ b/lambda/update/validate.go @@ -13,9 +13,9 @@ func validateUpdate(update shared.Update, lpa *shared.Lpa) (Applyable, []shared. case "CERTIFICATE_PROVIDER_SIGN": return validateCertificateProviderSign(update.Changes, lpa) case "ATTORNEY_SIGN": - return validateAttorneySign(update.Changes) + return validateAttorneySign(update.Changes, lpa) case "TRUST_CORPORATION_SIGN": - return validateTrustCorporationSign(update.Changes) + return validateTrustCorporationSign(update.Changes, lpa) case "PERFECT": return validatePerfect(update.Changes) case "REGISTER":