Esempio n. 1
0
        public async Task <string> DenyAnswer(string orderNumber, string brokerKey, string denyMessage = "API Said NO!")
        {
            await _hubContext.Clients.All.SendAsync("OutgoingCall", $"Avslå ordersvar från {await GetBrokerName(brokerKey)}!");

            var payload = new DenyAnswerModel
            {
                CallingUser      = "******",
                OrderNumber      = orderNumber,
                BrokerIdentifier = brokerKey,
                Message          = denyMessage
            };

            using var content = new StringContent(JsonConvert.SerializeObject(payload, Formatting.Indented), Encoding.UTF8, "application/json");
            var response = await client.PostAsync(_options.TolkApiBaseUrl.BuildUri("Order/DenyAnswer"), content);

            if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                return("Anropet saknar autentisering");
            }
            if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
            {
                var message = JsonConvert.DeserializeObject <ValidationProblemDetails>(await response.Content.ReadAsStringAsync());
                return($"Svar kunde INTE avslås. Problem med data-valideringen: {message.Title}");
            }
            if (JsonConvert.DeserializeObject <ResponseBase>(await response.Content.ReadAsStringAsync()).Success)
            {
                var info = JsonConvert.DeserializeObject <CreateOrderResponse>(await response.Content.ReadAsStringAsync());
                return($"Svar har avslagits för order: {info.OrderNumber}");
            }
            else
            {
                var errorResponse = JsonConvert.DeserializeObject <ErrorResponse>(await response.Content.ReadAsStringAsync());
                return($"Svar kunde INTE avslås. Felmeddelande: {errorResponse.ErrorMessage}");
            }
        }
        [OpenApiIgnore]//Not applicable for broker api, hence hiding it from swagger
        public async Task <IActionResult> DenyAnswer([FromBody] DenyAnswerModel model)
        {
            var method = $"{nameof(OrderController)}.{nameof(DenyAnswer)}";

            _logger.LogDebug($"{method} was called");
            if (model == null)
            {
                return(ReturnError(ErrorCodes.IncomingPayloadIsMissing, method));
            }
            if (!_tolkBaseOptions.EnableCustomerApi)
            {
                _logger.LogWarning($"{model.CallingUser} called {method}, but CustomerApi is not enabled!");
                return(BadRequest(new ValidationProblemDetails {
                    Title = "CustomerApi is not enabled!"
                }));
            }
            if (string.IsNullOrEmpty(model.CallingUser))
            {
                return(ReturnError(ErrorCodes.CallingUserMissing, method));
            }
            _logger.LogInformation($"{model.CallingUser} is denying request answer on {model.OrderNumber} from {model.BrokerIdentifier} ");
            if (ModelState.IsValid)
            {
                AspNetUser apiUser = await _dbContext.Users.GetUserWithCustomerOrganisationById(User.UserId());

                var request = await _apiOrderService.GetRequestFromOrderAndBrokerIdentifier(model.OrderNumber, model.BrokerIdentifier);

                if (request == null && request.Order.CustomerOrganisationId != apiUser.CustomerOrganisationId)
                {
                    return(ReturnError(ErrorCodes.OrderNotFound, method));
                }
                if (!request.CanApprove)
                {
                    return(ReturnError(ErrorCodes.OrderNotInCorrectState, method));
                }
                var user = await _apiUserService.GetCustomerUser(model.CallingUser, apiUser.CustomerOrganisationId);

                if (user == null)
                {
                    return(ReturnError(ErrorCodes.CallingUserMissing, method));
                }

                await _orderService.DenyRequestAnswer(request, user.Id, apiUser.Id, model.Message);

                await _dbContext.SaveChangesAsync();

                _logger.LogInformation($"{request.RequestId} was denied");
                return(Ok(new ResponseBase()));
            }
            return(ReturnError(ErrorCodes.OrderNotValid, method));
        }