def apply()

in handlers/identity-backfill/src/main/scala/com/gu/identityBackfill/FindExistingIdentityId.scala [16:37]


  def apply(
      getByEmail: EmailAddress => ClientFailableOp[GetByEmail.IdentityAccount],
      getByIdentityId: IdentityId => ClientFailableOp[GetByIdentityId.IdentityUser],
  )(emailAddress: EmailAddress): ApiGatewayOp[Option[IdentityId]] = {

    def continueIfNoPassword(identityId: IdentityId) = {
      getByIdentityId(identityId) match {
        case ClientSuccess(IdentityUser(_, false)) => ContinueProcessing(Some(identityId))
        case _ =>
          ReturnWithResponse(notFound(s"Identity account not validated but password is set: ${identityId.value}"))
      }
    }

    val result = getByEmail(emailAddress) match {
      case ClientSuccess(IdentityAccount(identityId, true)) => ContinueProcessing(Some(identityId))
      case ClientSuccess(IdentityAccount(identityId, false)) => continueIfNoPassword(identityId)
      case _: NotFound => ContinueProcessing(None)
      case other: ClientFailure => ReturnWithResponse(ApiGatewayResponse.internalServerError(other.toString))
    }

    result.withLogging("FindExistingIdentityId")
  }