diff --git a/lambda/update/correction.go b/lambda/update/correction.go index 35e3e395..88d69583 100644 --- a/lambda/update/correction.go +++ b/lambda/update/correction.go @@ -9,30 +9,69 @@ import ( ) type Correction struct { - DonorFirstNames string - DonorLastName string - DonorOtherNames string - DonorDob shared.Date - DonorAddress shared.Address - DonorEmail string - LPASignedAt time.Time - Index *int - AttorneyFirstNames string - AttorneyLastName string - AttorneyDob shared.Date - AttorneyAddress shared.Address - AttorneyEmail string - AttorneyMobile string - AttorneySignedAt time.Time + Donor DonorCorrection + Attorney AttorneyCorrection +} + +type DonorCorrection struct { + FirstNames string + LastName string + OtherNames string + Dob shared.Date + Address shared.Address + Email string + LpaSignedAt time.Time +} + +type AttorneyCorrection struct { + Index *int + FirstNames string + LastName string + Dob shared.Date + Address shared.Address + Email string + Mobile string + SignedAt time.Time } const signedAt = "/signedAt" func (c Correction) Apply(lpa *shared.Lpa) []shared.FieldError { - if !c.LPASignedAt.IsZero() && lpa.Channel == shared.ChannelOnline { + var errors []shared.FieldError + + donorErrors := c.Donor.Apply(lpa) + errors = append(errors, donorErrors...) + + if c.Attorney.Index != nil { + attorneyErrors := c.Attorney.Apply(lpa) + errors = append(errors, attorneyErrors...) + } + + return errors +} + +func (c DonorCorrection) Apply(lpa *shared.Lpa) []shared.FieldError { + if !c.LpaSignedAt.IsZero() && lpa.Channel == shared.ChannelOnline { return []shared.FieldError{{Source: signedAt, Detail: "LPA Signed on date cannot be changed for online LPAs"}} } + if lpa.Status == shared.LpaStatusRegistered { + return []shared.FieldError{{Source: "/type", Detail: "Cannot make corrections to a Registered LPA"}} + } + + lpa.Donor.FirstNames = c.FirstNames + lpa.Donor.LastName = c.LastName + lpa.Donor.OtherNamesKnownBy = c.OtherNames + lpa.Donor.DateOfBirth = c.Dob + lpa.Donor.Address = c.Address + lpa.Donor.Email = c.Email + lpa.SignedAt = c.LpaSignedAt + + return nil +} + +func (c AttorneyCorrection) Apply(lpa *shared.Lpa) []shared.FieldError { + 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"}} @@ -42,22 +81,14 @@ func (c Correction) Apply(lpa *shared.Lpa) []shared.FieldError { return []shared.FieldError{{Source: "/type", Detail: "Cannot make corrections to a Registered LPA"}} } - lpa.Donor.FirstNames = c.DonorFirstNames - lpa.Donor.LastName = c.DonorLastName - lpa.Donor.OtherNamesKnownBy = c.DonorOtherNames - lpa.Donor.DateOfBirth = c.DonorDob - lpa.Donor.Address = c.DonorAddress - lpa.Donor.Email = c.DonorEmail - lpa.SignedAt = c.LPASignedAt - if c.Index != nil { - lpa.Attorneys[*c.Index].FirstNames = c.AttorneyFirstNames - lpa.Attorneys[*c.Index].LastName = c.AttorneyLastName - lpa.Attorneys[*c.Index].DateOfBirth = c.AttorneyDob - lpa.Attorneys[*c.Index].Address = c.AttorneyAddress - lpa.Attorneys[*c.Index].Email = c.AttorneyEmail - lpa.Attorneys[*c.Index].Mobile = c.AttorneyMobile - lpa.Attorneys[*c.Index].SignedAt = &c.AttorneySignedAt + lpa.Attorneys[*c.Index].FirstNames = c.FirstNames + lpa.Attorneys[*c.Index].LastName = c.LastName + lpa.Attorneys[*c.Index].DateOfBirth = c.Dob + lpa.Attorneys[*c.Index].Address = c.Address + lpa.Attorneys[*c.Index].Email = c.Email + lpa.Attorneys[*c.Index].Mobile = c.Mobile + lpa.Attorneys[*c.Index].SignedAt = &c.SignedAt } return nil @@ -66,86 +97,64 @@ func (c Correction) Apply(lpa *shared.Lpa) []shared.FieldError { func validateCorrection(changes []shared.Change, lpa *shared.Lpa) (Correction, []shared.FieldError) { var data Correction - data.DonorFirstNames = lpa.LpaInit.Donor.FirstNames - data.DonorLastName = lpa.LpaInit.Donor.LastName - data.DonorOtherNames = lpa.LpaInit.Donor.OtherNamesKnownBy - data.DonorDob = lpa.LpaInit.Donor.DateOfBirth - data.DonorAddress = lpa.LpaInit.Donor.Address - data.DonorEmail = lpa.LpaInit.Donor.Email - data.LPASignedAt = lpa.LpaInit.SignedAt + data.Donor.FirstNames = lpa.LpaInit.Donor.FirstNames + data.Donor.LastName = lpa.LpaInit.Donor.LastName + data.Donor.OtherNames = lpa.LpaInit.Donor.OtherNamesKnownBy + data.Donor.Dob = lpa.LpaInit.Donor.DateOfBirth + data.Donor.Address = lpa.LpaInit.Donor.Address + data.Donor.Email = lpa.LpaInit.Donor.Email + data.Donor.LpaSignedAt = lpa.LpaInit.SignedAt errors := parse.Changes(changes). - Prefix("/donor/address", func(p *parse.Parser) []shared.FieldError { - return p. - Field("/line1", &data.DonorAddress.Line1, parse.Optional()). - Field("/line2", &data.DonorAddress.Line2, parse.Optional()). - Field("/line3", &data.DonorAddress.Line3, parse.Optional()). - Field("/town", &data.DonorAddress.Town, parse.Optional()). - Field("/postcode", &data.DonorAddress.Postcode, parse.Optional()). - Field("/country", &data.DonorAddress.Country, parse.Validate(func() []shared.FieldError { - return validate.Country("", data.DonorAddress.Country) - }), parse.Optional()). - Consumed() - }, parse.Optional()). - Field("/donor/firstNames", &data.DonorFirstNames, parse.Validate(func() []shared.FieldError { - return validate.Required("", data.DonorFirstNames) + Prefix("/donor/address", validateAddress(&data.Donor.Address), parse.Optional()). + Field("/donor/firstNames", &data.Donor.FirstNames, parse.Validate(func() []shared.FieldError { + return validate.Required("", data.Donor.FirstNames) }), parse.Optional()). - Field("/donor/lastName", &data.DonorLastName, parse.Validate(func() []shared.FieldError { - return validate.Required("", data.DonorLastName) + Field("/donor/lastName", &data.Donor.LastName, parse.Validate(func() []shared.FieldError { + return validate.Required("", data.Donor.LastName) }), parse.Optional()). - Field("/donor/otherNamesKnownBy", &data.DonorOtherNames, parse.Optional()). - Field("/donor/email", &data.DonorEmail, parse.Optional()). - Field("/donor/dateOfBirth", &data.DonorDob, parse.Validate(func() []shared.FieldError { - return validate.Date("", data.DonorDob) + Field("/donor/otherNamesKnownBy", &data.Donor.OtherNames, parse.Optional()). + Field("/donor/email", &data.Donor.Email, parse.Optional()). + Field("/donor/dateOfBirth", &data.Donor.Dob, parse.Validate(func() []shared.FieldError { + return validate.Date("", data.Donor.Dob) }), parse.Optional()). - Field(signedAt, &data.LPASignedAt, parse.Validate(func() []shared.FieldError { - return validate.Time("", data.LPASignedAt) + Field(signedAt, &data.Donor.LpaSignedAt, parse.Validate(func() []shared.FieldError { + return validate.Time("", data.Donor.LpaSignedAt) }), parse.Optional()). Prefix("/attorneys", func(p *parse.Parser) []shared.FieldError { return p. Each(func(i int, p *parse.Parser) []shared.FieldError { - if data.Index != nil && *data.Index != i { + if data.Attorney.Index != nil && *data.Attorney.Index != i { return p.OutOfRange() } - data.Index = &i - data.AttorneyFirstNames = lpa.Attorneys[i].FirstNames - data.AttorneyLastName = lpa.Attorneys[i].LastName - data.AttorneyDob = lpa.Attorneys[i].DateOfBirth - data.AttorneyAddress = lpa.Attorneys[i].Address - data.AttorneyEmail = lpa.Attorneys[i].Email - data.AttorneyMobile = lpa.Attorneys[i].Mobile + data.Attorney.Index = &i + data.Attorney.FirstNames = lpa.Attorneys[i].FirstNames + data.Attorney.LastName = lpa.Attorneys[i].LastName + data.Attorney.Dob = lpa.Attorneys[i].DateOfBirth + data.Attorney.Address = lpa.Attorneys[i].Address + data.Attorney.Email = lpa.Attorneys[i].Email + data.Attorney.Mobile = lpa.Attorneys[i].Mobile if lpa.Attorneys[i].SignedAt != nil { - data.AttorneySignedAt = *lpa.Attorneys[i].SignedAt + data.Attorney.SignedAt = *lpa.Attorneys[i].SignedAt } return p. - Field("/firstNames", &data.AttorneyFirstNames, parse.Validate(func() []shared.FieldError { - return validate.Required("", data.AttorneyFirstNames) + Field("/firstNames", &data.Attorney.FirstNames, parse.Validate(func() []shared.FieldError { + return validate.Required("", data.Attorney.FirstNames) }), parse.Optional()). - Field("/lastName", &data.AttorneyLastName, parse.Validate(func() []shared.FieldError { - return validate.Required("", data.AttorneyLastName) + Field("/lastName", &data.Attorney.LastName, parse.Validate(func() []shared.FieldError { + return validate.Required("", data.Attorney.LastName) }), parse.Optional()). - Field("/dateOfBirth", &data.AttorneyDob, parse.Validate(func() []shared.FieldError { - return validate.Date("", data.AttorneyDob) + Field("/dateOfBirth", &data.Attorney.Dob, parse.Validate(func() []shared.FieldError { + return validate.Date("", data.Attorney.Dob) }), parse.Optional()). - Field("/email", &data.AttorneyEmail, parse.Optional()). - Field("/mobile", &data.AttorneyMobile, parse.Optional()). - Prefix("/address", func(p *parse.Parser) []shared.FieldError { - return p. - Field("/line1", &data.AttorneyAddress.Line1, parse.Optional()). - Field("/line2", &data.AttorneyAddress.Line2, parse.Optional()). - Field("/line3", &data.AttorneyAddress.Line3, parse.Optional()). - Field("/town", &data.AttorneyAddress.Town, parse.Optional()). - Field("/postcode", &data.AttorneyAddress.Postcode, parse.Optional()). - Field("/country", &data.AttorneyAddress.Country, parse.Validate(func() []shared.FieldError { - return validate.Country("", data.AttorneyAddress.Country) - }), parse.Optional()). - Consumed() - }, parse.Optional()). - Field(signedAt, &data.AttorneySignedAt, parse.Validate(func() []shared.FieldError { - return validate.Time("", data.AttorneySignedAt) + Field("/email", &data.Attorney.Email, parse.Optional()). + Field("/mobile", &data.Attorney.Mobile, parse.Optional()). + Prefix("/address", validateAddress(&data.Attorney.Address), parse.Optional()). + Field(signedAt, &data.Attorney.SignedAt, parse.Validate(func() []shared.FieldError { + return validate.Time("", data.Attorney.SignedAt) }), parse.Optional()). Consumed() }). @@ -155,3 +164,18 @@ func validateCorrection(changes []shared.Change, lpa *shared.Lpa) (Correction, [ return data, errors } + +func validateAddress(address *shared.Address) func(p *parse.Parser) []shared.FieldError { + return func(p *parse.Parser) []shared.FieldError { + return p. + Field("/line1", &address.Line1, parse.Optional()). + Field("/line2", &address.Line2, parse.Optional()). + Field("/line3", &address.Line3, parse.Optional()). + Field("/town", &address.Town, parse.Optional()). + Field("/postcode", &address.Postcode, parse.Optional()). + Field("/country", &address.Country, parse.Validate(func() []shared.FieldError { + return validate.Country("", address.Country) + }), parse.Optional()). + Consumed() + } +}