oeffisearch/src/endpoints/refreshJourney.nim
2020-06-12 14:49:54 +00:00

37 lines
1.4 KiB
Nim

import json, tables, options, asyncdispatch, strutils
import ../types, ../cache_types, ../backend/hafas, ../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