ctucx.git: oeffisearch

[nimlang] fast and simple tripplanner

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
import json, tables, options, asyncdispatch, strutils
import nimhafas
import ../types, ../cache_types, ../cache

proc refreshJourneyEndpoint*(requestData: JsonNode): Future[JsonNode] {.async.} =
  let reqId     = requestData{"reqId"}.getStr()
  let journeyId = requestData{"journeyId"}.getStr()

  if reqId == "": raise newException(errorException, "MISSING_VALUES")
  if journeyId == "": raise newException(errorException, "MISSING_VALUES")
  if not cacheExists(reqId): raise newException(notFoundException, "REQUEST_NOT_FOUND")

  var cacheObj = getCacheObject(reqId)
  if not cacheObj.journeys.hasKey(journeyId):  raise newException(notFoundException, "JOURNEY_NOT_FOUND")

  let journey = await refreshJourney(RefreshJourneyParams(
      refreshToken: cacheObj.journeys[journeyId].refreshToken,
      stopovers:    some(true),
      polylines:    some(false),
      tickets:      some(true),
    ))

  cacheObj = await updateJourney(reqId, journeyId, journey)

  var response = %* {
      "reqId":       cacheObj.reqId,
      "lastUpdated": cacheObj.lastUpdated,
      "journeys":    {}
    }

  response["journeys"].add(journeyId, %* journey)
  delete(response["journeys"][journeyId], "refreshToken")
  delete(response["journeys"][journeyId], "cycle")
  for legKey, leg in pairs(journey.legs):
    delete(response["journeys"][journeyId]["legs"][legKey], "cycle")
    delete(response["journeys"][journeyId]["legs"][legKey], "tripId")

  return response