Ejemplo n.º 1
0
        public async Task <IActionResult> ConfirmNoAnswer([FromBody] ConfirmNoAnswerModel model)
        {
            if (model == null)
            {
                return(ReturnError(ErrorCodes.IncomingPayloadIsMissing));
            }
            try
            {
                var brokerId  = User.TryGetBrokerId().Value;
                var apiUserId = User.UserId();
                _ = await _apiOrderService.GetOrderAsync(model.OrderNumber, brokerId);

                var user = await _apiUserService.GetBrokerUser(model.CallingUser, brokerId);

                Request request = await GetConfirmedRequest(model.OrderNumber, brokerId, new[] { RequestStatus.ResponseNotAnsweredByCreator });

                await _requestService.ConfirmNoAnswer(
                    request,
                    _timeService.SwedenNow,
                    user?.Id ?? apiUserId,
                    user != null?(int?)apiUserId : null
                    );

                return(Ok(new ResponseBase()));
            }
            catch (InvalidApiCallException ex)
            {
                return(ReturnError(ex.ErrorCode));
            }
        }
Ejemplo n.º 2
0
        public async Task <string> ConfirmNoAnswer(string orderNumber)
        {
            await _hubContext.Clients.All.SendAsync("OutgoingCall", $"Arkivera att man sett att ingen svarat på order {orderNumber}");

            var payload = new ConfirmNoAnswerModel
            {
                CallingUser = "******",
                OrderNumber = orderNumber,
            };

            using var content = new StringContent(JsonConvert.SerializeObject(payload, Formatting.Indented), Encoding.UTF8, "application/json");
            var response = await client.PostAsync(_options.TolkApiBaseUrl.BuildUri("Order/ConfirmNoAnswer"), 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($"Det gick inte att arkivera order {orderNumber}: {message.Title}");
            }
            if (JsonConvert.DeserializeObject <ResponseBase>(await response.Content.ReadAsStringAsync()).Success)
            {
                var info = JsonConvert.DeserializeObject <CreateOrderResponse>(await response.Content.ReadAsStringAsync());
                return($"Order: {info.OrderNumber} har arkiverats");
            }
            else
            {
                var errorResponse = JsonConvert.DeserializeObject <ErrorResponse>(await response.Content.ReadAsStringAsync());
                return($"Det gick inte att arkivera order {orderNumber}. Felmeddelande: {errorResponse.ErrorMessage}");
            }
        }
Ejemplo n.º 3
0
        [OpenApiIgnore]//Not applicable for broker api, hence hiding it from swagger
        public async Task <IActionResult> ConfirmNoAnswer([FromBody] ConfirmNoAnswerModel model)
        {
            var method = $"{nameof(OrderController)}.{nameof(ConfirmNoAnswer)}";

            _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 confirming that no-one accepted {model.OrderNumber}");
            if (ModelState.IsValid)
            {
                AspNetUser apiUser = await _dbContext.Users.GetUserWithCustomerOrganisationById(User.UserId());

                Order order = await _dbContext.Orders.GetOrderByOrderNumber(model.OrderNumber);

                if (order == null || order.CustomerOrganisationId != apiUser.CustomerOrganisationId)
                {
                    return(ReturnError(ErrorCodes.OrderNotFound, method));
                }
                if (order.Status != OrderStatus.NoBrokerAcceptedOrder)
                {
                    return(ReturnError(ErrorCodes.OrderNotInCorrectState, method));
                }
                var user = await _apiUserService.GetCustomerUser(model.CallingUser, apiUser.CustomerOrganisationId);

                if (user == null)
                {
                    return(ReturnError(ErrorCodes.CallingUserMissing, method));
                }
                Order fullOrder = await _dbContext.Orders.GetFullOrderById(order.OrderId);

                await _orderService.ConfirmNoAnswer(fullOrder, user.Id, apiUser.Id);

                await _dbContext.SaveChangesAsync();

                _logger.LogInformation($"{order.OrderId} was confirmed that no-one accepted");
                return(Ok(new ResponseBase()));
            }
            return(ReturnError(ErrorCodes.OrderNotValid, method));
        }