private def extractNotifications()

in reportextractor/src/main/scala/com/gu/notifications/extractor/Lambda.scala [113:146]


  private def extractNotifications(day: LocalDate, notificationType: NotificationType): List[JsValue] = {
    val sentTimeIndex = "sentTime-index"

    val from = day.atStartOfDay().toString
    val to = day.plusDays(1).atStartOfDay().toString

    val query = new QueryRequest(tableName)
      .withIndexName(sentTimeIndex)
      .withKeyConditions(Map(
        "type" -> keyEquals(NotificationType.toRep(notificationType)),
        "sentTime" -> keyBetween(from, to)
      ).asJava)
      .withFilterExpression("attribute_not_exists(notification.dryRun)")

    @tailrec
    def recursiveFetch(startKey: Option[java.util.Map[String, AttributeValue]], agg: List[Map[String, AttributeValue]]): List[Map[String, AttributeValue]] = {
      val queryWithStartKey = startKey.fold(query)(key => query.withExclusiveStartKey(key))

      val results = dynamoDB.query(queryWithStartKey)
      val items = results.getItems.asScala.toList.map(item => item.asScala.toMap)

      val lastKey = Option(results.getLastEvaluatedKey).filter(!_.isEmpty)

      lastKey match {
        case Some(key) => recursiveFetch(Some(key), items)
        case None => items
      }
    }


    val results = recursiveFetch(None, Nil)

    results.map(DynamoJsonConversions.jsonFromAttributeMap)
  }