def apply()

in handlers/identity-backfill/src/main/scala/com/gu/identityBackfill/Handler.scala [44:127]


  def apply(inputStream: InputStream, outputStream: OutputStream, context: Context): Unit =
    runForLegacyTestsSeeTestingMd(
      RawEffects.stage,
      GetFromS3.fetchString,
      RawEffects.response,
      LambdaIO(inputStream, outputStream, context),
    )

  def runForLegacyTestsSeeTestingMd(
      stage: Stage,
      fetchString: StringFromS3,
      response: Request => Response,
      lambdaIO: LambdaIO,
  ): Unit =
    ApiGatewayHandler(lambdaIO)(operationForEffects(stage, fetchString, response))

  def operationForEffects(
      stage: Stage,
      fetchString: StringFromS3,
      response: Request => Response,
  ): ApiGatewayOp[Operation] = {
    def operation(
        zuoraRestConfig: ZuoraRestConfig,
        sfAuth: HttpOp[StringHttpRequest, BodyAsString],
        identityConfig: IdentityConfig,
    ) = {
      val zuoraRequests = ZuoraRestRequestMaker(response, zuoraRestConfig)
      val zuoraQuerier = ZuoraQuery(zuoraRequests)
      val identityClient = IdentityClient(response, identityConfig)
      val createGuestAccount = identityClient.wrapWith(JsonHttp.post).wrapWith(CreateGuestAccount.wrapper)
      val getByEmail = identityClient.wrapWith(JsonHttp.getWithParams).wrapWith(GetByEmail.wrapper)
      val getById = identityClient.wrapWith(JsonHttp.get).wrapWith(GetByIdentityId.wrapper)
      val findExistingIdentityId = FindExistingIdentityId(getByEmail.runRequest, getById.runRequest) _

      val countZuoraAccounts: IdentityId => ClientFailableOp[Int] = CountZuoraAccountsForIdentityId(zuoraQuerier)
      val updateZuoraAccounts =
        IdentityBackfillSteps.updateZuoraBillingAccountsIdentityId(AddIdentityIdToAccount(zuoraRequests))(_, _)

      lazy val sfPatch = sfAuth.wrapWith(JsonHttp.patch)
      lazy val sfGet = sfAuth.wrapWith(JsonHttp.get)
      lazy val checkSfContactsSyncable =
        PreReqCheck.checkSfContactsSyncable(getSFBillingContactIfSyncable(sfGet, stage)) _
      lazy val updateBuyersIdentityId =
        IdentityBackfillSteps.updateBuyersIdentityId(updateSalesforceContactIdentityId(sfPatch)) _

      def findAndValidateZuoraAccounts(zuoraQuerier: ZuoraQuerier)(
          emailAddress: EmailAddress,
      ): ApiGatewayOp[List[ZuoraAccountIdentitySFContact]] =
        PreReqCheck.validateZuoraAccountsFound(GetZuoraAccountsForEmail(zuoraQuerier)(emailAddress))(emailAddress)

      Operation(
        steps = WireRequestToDomainObject(
          IdentityBackfillSteps(
            PreReqCheck(
              findExistingIdentityId,
              findAndValidateZuoraAccounts(zuoraQuerier),
              countZuoraAccounts andThen PreReqCheck.noZuoraAccountsForIdentityId,
              GetZuoraSubTypeForAccount(zuoraQuerier) _ andThen PreReqCheck.acceptableReaderType,
              checkSfContactsSyncable,
            ),
            createGuestAccount.runRequest,
            updateZuoraAccounts,
            updateBuyersIdentityId,
          ),
        ),
        healthcheck = () =>
          Healthcheck(
            getByEmail,
            countZuoraAccounts,
          ),
      )
    }

    val loadConfig = LoadConfigModule(stage, fetchString)
    val fOperation = for {
      zuoraRestConfig <- loadConfig.load[ZuoraRestConfig].toApiGatewayOp("load zuora config")
      sfAuthConfig <- loadConfig.load[SFAuthConfig].toApiGatewayOp("load sfAuth config")
      identityConfig <- loadConfig.load[IdentityConfig].toApiGatewayOp("load identity config")
      sfAuth <- SalesforceClient.auth(response, sfAuthConfig).toApiGatewayOp("sf auth")
      configuredOp = operation(zuoraRestConfig, sfAuth, identityConfig)

    } yield configuredOp
    fOperation
  }