From d5ba4b38ad558a7b64fd4c3c362fdd0b68dc2aeb Mon Sep 17 00:00:00 2001 From: Elliot Smith Date: Thu, 13 Jun 2024 14:22:56 +0100 Subject: [PATCH] Incorporate improvements suggested by PR review --- lambda/update/confirm_identity.go | 55 ++++++++++--------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/lambda/update/confirm_identity.go b/lambda/update/confirm_identity.go index aea24367..68c1122a 100644 --- a/lambda/update/confirm_identity.go +++ b/lambda/update/confirm_identity.go @@ -27,57 +27,38 @@ func (idcc IdCheckComplete) Apply(lpa *shared.Lpa) []shared.FieldError { return nil } -func validateConfirmIdentity(prefix string, actor idccActor, changes []shared.Change, lpa *shared.Lpa) (IdCheckComplete, []shared.FieldError) { - var existing IdCheckComplete +func validateConfirmIdentity(prefix string, actor idccActor, ic *shared.IdentityCheck, changes []shared.Change) (IdCheckComplete, []shared.FieldError) { + var idcc IdCheckComplete - identityCheckParser := func(actor idccActor) func(p *parse.Parser) []shared.FieldError { - return func(p *parse.Parser) []shared.FieldError { - if existing.Actor != "" { - return []shared.FieldError{{Source: "/", Detail: "id check for multiple actors is not allowed"}} - } - - switch actor { - case donor: - existing.IdentityCheck = lpa.Donor.IdentityCheck - case certificateProvider: - existing.IdentityCheck = lpa.CertificateProvider.IdentityCheck - } + errors := parse.Changes(changes). + Prefix(prefix, func(p *parse.Parser) []shared.FieldError { + idcc.Actor = actor - if existing.IdentityCheck == nil { - existing.IdentityCheck = &shared.IdentityCheck{} + if ic == nil { + ic = &shared.IdentityCheck{} } - - existing.Actor = actor + idcc.IdentityCheck = ic return p. - Field("/type", &existing.IdentityCheck.Type, parse.Validate(func() []shared.FieldError { - return validate.IsValid("", existing.IdentityCheck.Type) + Field("/type", &ic.Type, parse.Validate(func() []shared.FieldError { + return validate.IsValid("", ic.Type) }), parse.MustMatchExisting()). - Field("/checkedAt", &existing.IdentityCheck.CheckedAt, parse.Validate(func() []shared.FieldError { - return validate.Time("", existing.IdentityCheck.CheckedAt) + Field("/checkedAt", &ic.CheckedAt, parse.Validate(func() []shared.FieldError { + return validate.Time("", ic.CheckedAt) }), parse.MustMatchExisting()). - Field("/reference", &existing.IdentityCheck.Reference, parse.Validate(func() []shared.FieldError { - return validate.Required("", existing.IdentityCheck.Reference) + Field("/reference", &ic.Reference, parse.Validate(func() []shared.FieldError { + return validate.Required("", ic.Reference) }), parse.MustMatchExisting()). Consumed() - } - } - - errors := parse.Changes(changes). - Prefix(prefix, identityCheckParser(actor)). - Errors() - - if existing.Actor == "" { - return existing, append(errors, shared.FieldError{Source: "/", Detail: "id check for unknown actor type"}) - } + }).Errors() - return existing, errors + return idcc, errors } func validateDonorConfirmIdentity(changes []shared.Change, lpa *shared.Lpa) (IdCheckComplete, []shared.FieldError) { - return validateConfirmIdentity("/donor/identityCheck", donor, changes, lpa) + return validateConfirmIdentity("/donor/identityCheck", donor, lpa.Donor.IdentityCheck, changes) } func validateCertificateProviderConfirmIdentity(changes []shared.Change, lpa *shared.Lpa) (IdCheckComplete, []shared.FieldError) { - return validateConfirmIdentity("/certificateProvider/identityCheck", certificateProvider, changes, lpa) + return validateConfirmIdentity("/certificateProvider/identityCheck", certificateProvider, lpa.CertificateProvider.IdentityCheck, changes) }