def updateSubmissionsTable()

in formstack-baton-requests/src/main/scala/com/gu/identity/formstackbatonrequests/services/DynamoUpdateService.scala [143:169]


  def updateSubmissionsTable(formsPage: Int, minTimeUTC: LocalDateTime, maxTimeUTC: Option[LocalDateTime], count: Int, token: FormstackAccountToken, context: Context): Either[Throwable, UpdateStatus] = {
    logger.info(s"----Getting page $formsPage of forms----")
    formstackClient.accountFormsForGivenPage(formsPage, token) match {
      case Left(err) => Left(err)
      case Right(response) =>
        val forms = response.forms
        val formResults: Seq[Either[Throwable, Unit]] = forms.map { form =>
          // The amount of new submissions to these forms often exceeds an apparent formstack limit on how many can be fetched in a single query (even if the api call is paginated)
          // The submissions would be ignored anyway since the form doesn't have any relevant data, so we just skip them at the form level to avoid the problem for now.
          if (form.name.toLowerCase.startsWith("ybtj")) {
            logger.info(s"skipping ybtj form id: ${form.id} name: ${form.name}")
            Right(())
          } else {
            logger.info(s"Processing results for form ${form.id}")
            writeSubmissions(form = form, minTimeUTC = minTimeUTC, maxTimeUTC = maxTimeUTC, token = token)
          }
        }
        val errors = formResults.collect { case Left(err) => err }
        if (errors.nonEmpty) {
          Left(new Exception(errors.toString))
        } else if (count < response.total & context.getRemainingTimeInMillis > 600000) {
          updateSubmissionsTable(formsPage + 1, minTimeUTC, maxTimeUTC, count + FormstackService.formResultsPerPage, token, context)
        } else if (count < response.total) {
          Right(UpdateStatus(completed = false, Some(formsPage + 1), Some(count + FormstackService.formResultsPerPage), token))
        } else Right(UpdateStatus(completed = true, None, None, token))
    }
  }