function newStrategizerGateway () { const MODULE_NAME = 'Strategizer Gateway' const logger = newWebDebugLog() logger.fileName = MODULE_NAME let thisObject = { idAtStrategizer: undefined, strategizerData: undefined, container: undefined, loadFromStrategyzer: loadFromStrategyzer, saveToStrategyzer: saveToStrategyzer } let colletionItems = [] thisObject.container = newContainer() thisObject.container.name = MODULE_NAME thisObject.container.initialize() thisObject.container.frame.width = 0 thisObject.container.frame.height = 0 thisObject.container.frame.position.x = 0 thisObject.container.frame.position.y = 0 thisObject.container.isDraggeable = false thisObject.container.isClickeable = false let graphQLServer return thisObject async function loadFromStrategyzer () { try { const accessToken = window.localStorage.getItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY) if (accessToken === null) { logger.write('[ERROR] loadFromStrategyzer -> Can load from strategizer because the accessToken is missing.') return } let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY) if (user === null) { logger.write('[ERROR] saveToStrategyzer -> Can not load from strategizer because user is not logged in. ') return } user = JSON.parse(user) let fbSlug = 'simulator' + '-' + 'bot' + '-' + user.alias.replace('.', '') graphQLServer = await axios({ url: window.canvasApp.graphQL.masterAppApiUrl, method: 'post', data: { query: ` query($fbSlug: String!){ strategizer_TradingSystemByFb(fbSlug: $fbSlug){ id fbSlug data } } `, variables: { fbSlug: fbSlug } }, headers: { authorization: 'Bearer ' + accessToken } }) if (graphQLServer.data.errors) { logger.write('[ERROR] loadFromStrategyzer -> GraphQL Error: ' + JSON.stringify(graphQLServer.data.errors)) return false } else { thisObject.strategizerData = JSON.parse(JSON.stringify(graphQLServer.data.data.strategizer_TradingSystemByFb.data)) thisObject.idAtStrategizer = graphQLServer.data.data.strategizer_TradingSystemByFb.id window.localStorage.setItem(CANVAS_APP_NAME + '.' + MODULE_NAME + '.' + user.alias, thisObject.idAtStrategizer) return true } } catch (err) { logger.write('[ERROR] loadFromStrategyzer -> err = ' + err.stack) } } async function saveToStrategyzer () { try { const accessToken = window.localStorage.getItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY) if (accessToken === null) { logger.write('[ERROR] saveToStrategyzer -> Can not save because the accessToken is missing.') return } let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY) if (user === null) { logger.write('[ERROR] saveToStrategyzer -> Can not save because user is not logged in. ') return } user = JSON.parse(user) let fbSlug = 'simulator' + '-' + 'bot' + '-' + user.alias.replace('.', '') /* See if we need to update or create a new record at the strategizer */ let tradingSystem = canvas.strategySpace.workspace.getProtocolTradingSystem() if (tradingSystem === undefined || tradingSystem === null) { logger.write('[ERROR] saveToStrategyzer -> Can not save when tradingSystem is null or undefined.') return } let idAtStrategizer = window.localStorage.getItem(CANVAS_APP_NAME + '.' + MODULE_NAME + '.' + user.alias) if (idAtStrategizer === undefined || idAtStrategizer === null) { logger.write('[ERROR] saveToStrategyzer -> Can not save when idAtStrategizer is null or undefined.') return } graphQLServer = await axios({ url: window.canvasApp.graphQL.masterAppApiUrl, method: 'post', data: { query: ` mutation strategizer_EditTradingSystem ( $id: ID!, $data: strategizer_JSON! ) { strategizer_EditTradingSystem(id: $id, tradingSystem: { data: $data }) { id } } `, variables: { id: idAtStrategizer, data: tradingSystem } }, headers: { authorization: 'Bearer ' + accessToken } }) if (graphQLServer.data.errors) { logger.write('[ERROR] saveToStrategyzer -> GraphsQL Error -> graphQLServer.data.errors = ' + JSON.stringify(graphQLServer.data.errors)) } return true } catch (err) { logger.write('[ERROR] saveToStrategyzer -> err = ' + err.stack) } } }