Ejemplo n.º 1
0
        private void CallTravelPlannerSso(string id)
        {
            var opportunity = CrmService.GetOpportunity(_client.CrmInterface, _logger, id);

            if (opportunity == null)
            {
                FireEventOnOwrError("There is no opportunity in context.");
                return;
            }

            var rooms             = CrmService.GetTravelPlannerRooms(id, _logger, _client.CrmInterface);
            var createdByInitials = opportunity.GetAttributeValue <string>(Opportunity.Initials);
            var login             = CrmService.GetSsoDetails(_client.CrmInterface.GetMyCrmUserId(), _logger, _client.CrmInterface);

            if (login == null)
            {
                FireEventOnOwrError("Login details are missing for the logged-in user.");
                return;
            }

            var privateKey = CrmService.GetPrivateInfo(_logger, _client.CrmInterface);

            if (privateKey == null)
            {
                FireEventOnOwrError("Private Key is missing in the system");
                return;
            }
            var expiredSeconds   = CrmService.GetConfig(Configuration.OwrSsoTokenExpired, _logger, _client.CrmInterface);
            var notBeforeSeconds = CrmService.GetConfig(Configuration.OwrSsoTokenNotBefore, _logger, _client.CrmInterface);

            if (expiredSeconds == null || notBeforeSeconds == null)
            {
                FireEventOnOwrError("Missing payload configuration");
                return;
            }
            var payload = GetPayload(login, expiredSeconds, notBeforeSeconds, createdByInitials);
            var token   = _jtiService.CreateJwtToken(privateKey, payload);

            if (token == null)
            {
                FireEventOnOwrError("JWT token is null");
                return;
            }
            var owrJsonHelper = new OwrJsonHelper(_client.CrmInterface, opportunity);
            var data          = owrJsonHelper.GetCustomerTravelPlannerJson(rooms);

            _logger.LogInformation($"Json to be passed to owr: {data}");
            var serviceUrl = CrmService.GetConfig(Configuration.OwrUrlConfigName, _logger, _client.CrmInterface);

            if (serviceUrl == null)
            {
                FireEventOnOwrError("Service Url is null");
                return;
            }
            var content = _jtiService.SendHttpRequest(HttpMethod.Post, serviceUrl, token, data).Content;

            if (content == null)
            {
                FireEventOnOwrError("Owr response content is null. Please See Details in log output");
                return;
            }
            var ssoResponse = WebServiceExchangeHelper.DeserializeOwrResponseJson(content, _logger);

            if (ssoResponse == null)
            {
                FireEventOnOwrError($"SSO Response could not be parsed. Response is {content}");
                return;
            }
            var eventParams = WebServiceExchangeHelper.ContentToEventParams(ssoResponse, _logger);

            if (eventParams == null)
            {
                FireEventOnOwrError("SSO Response could not be parsed. Please See Details in log output");
                return;
            }
            FireEventOnOwrSuccess(eventParams);
        }