import json, tables, options, asyncdispatch import nimhafas import ../../types, ../../cache when not defined(release): import hotcodereloading proc journeysEndpoint*(requestData: JsonNode): Future[JsonNode] {.async.} = when not defined(release): performCodeReload() var reqId = requestData{"reqId"}.getStr() if reqId != "": if not cacheExists(reqId): raise newException(notFoundException, "REQUEST_NOT_FOUND") let cacheObj = getCacheObject(reqId) var response = %* { "reqId": cacheObj.reqId, "lastUpdated": cacheObj.lastUpdated, "params": cacheObj.params, "journeys": {} } for key, journey in pairs(cacheObj.journeys): response["journeys"].add(key, %* journey) delete(response["journeys"][key], "refreshToken") delete(response["journeys"][key], "cycle") for legKey, leg in pairs(journey.legs): delete(response["journeys"][key]["legs"][legKey], "cycle") delete(response["journeys"][key]["legs"][legKey], "tripId") return response else: var params = requestData["params"].to(JourneysParams) params.stopovers = some(true) params.polylines = some(true) params.remarks = some(true) params.tickets = some(true) let cacheObj = (await saveJourneys(params, await journeys(params))) var response = %* { "reqId": cacheObj.reqId, "lastUpdated": cacheObj.lastUpdated, "params": cacheObj.params, "journeys": {} } for key, journey in pairs(cacheObj.journeys): response["journeys"].add(key, %* journey) delete(response["journeys"][key], "refreshToken") delete(response["journeys"][key], "cycle") for legKey, leg in pairs(journey.legs): delete(response["journeys"][key]["legs"][legKey], "cycle") delete(response["journeys"][key]["legs"][legKey], "tripId") return response