def getPastInvoiceItems()

in src/main/scala/com/gu/invoicing/preview/Impl.scala [119:148]


  def getPastInvoiceItems(
      invoiceOwnerAccountId: String,
      subscriptionName: String,
      startDate: LocalDate,
      endDate: LocalDate,
  ): List[InvoiceItem] = {
    def fetchNextPage(nextPageUrl: String) = {
      Http(s"$zuoraApiHost/$nextPageUrl")
        .header("Authorization", s"Bearer $accessToken")
        .asString
        .body
        .pipe(read[Invoices](_))
    }

    @tailrec def go(nextPage: String, acc: List[Invoice]): List[Invoice] = {
      val response = fetchNextPage(nextPage)
      response.nextPage match {
        case Some(nextPageUrl) => go(nextPageUrl, acc ++ response.invoices)
        case None => acc ++ response.invoices
      }
    }

    go(s"v1/transactions/invoices/accounts/$invoiceOwnerAccountId?pageSize=40", Nil).iterator
      .filter { _.status == "Posted" }
      .flatMap { _.invoiceItems }
      .filter { _.subscriptionName == subscriptionName }
      .filterNot { _.serviceEndDate.isBefore(startDate) }
      .toList
      .sortBy(_.serviceStartDate)
  }