def apply()

in src/main/scala/payment_failure_comms/models/brazeTrackEvents.scala [63:86]


  def apply(
      records: Seq[PaymentFailureRecordWithBrazeId],
      zuoraAppId: String
  ): Either[Failure, Seq[BrazeTrackRequest]] = {

    val processRecordFunc = processRecord(zuoraAppId) _

    def process(
        soFar: Seq[CustomEventWithAttributes],
        toGo: Seq[PaymentFailureRecordWithBrazeId]
    ): Either[Failure, Seq[CustomEventWithAttributes]] =
      toGo match {
        case currRecord :: restOfRecords =>
          processRecordFunc(currRecord).flatMap(event => process(soFar :+ event, restOfRecords))
        case _ => Right(soFar)
      }

    process(Nil, records).map { eventsAndAttributes =>
      // Send events in groups of 9 to Braze so we do not hit the attribute limit of 75 per API call
      // https://www.braze.com/docs/api/endpoints/user_data/post_user_track/#rate-limit
      eventsAndAttributes
        .grouped(9).map(group => BrazeTrackRequest(group.flatMap(_.attributes), group.map(_.event))).toSeq
    }
  }