public async Task <IActionResult> ConfirmCancellation([FromBody] ConfirmCancellationModel 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.CancelledByCreator, RequestStatus.CancelledByCreatorWhenApproved }); await _requestService.ConfirmCancellation( request, _timeService.SwedenNow, user?.Id ?? apiUserId, user != null?(int?)apiUserId : null ); return(Ok(new ResponseBase())); } catch (InvalidApiCallException ex) { return(ReturnError(ex.ErrorCode)); } }
public async Task <string> ConfirmCancellation(string orderNumber) { await _hubContext.Clients.All.SendAsync("OutgoingCall", $"Arkivera att man sett att förmedlnig har avbokat order {orderNumber}"); var payload = new ConfirmCancellationModel { 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/ConfirmCancellation"), 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}"); } }
[OpenApiIgnore]//Not applicable for broker api, hence hiding it from swagger public async Task <IActionResult> ConfirmCancellation([FromBody] ConfirmCancellationModel model) { var method = $"{nameof(OrderController)}.{nameof(ConfirmCancellation)}"; _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 broker cancelled {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.CancelledByBroker) { return(ReturnError(ErrorCodes.OrderNotInCorrectState, method)); } var user = await _apiUserService.GetCustomerUser(model.CallingUser, apiUser.CustomerOrganisationId); if (user == null) { return(ReturnError(ErrorCodes.CallingUserMissing, method)); } var request = await _dbContext.Requests.GetLastRequestForOrder(order.OrderId); await _orderService.ConfirmCancellationByBroker(request, user.Id, apiUser.Id); await _dbContext.SaveChangesAsync(); _logger.LogInformation($"{order.OrderId} was confirmed that broker cancelled"); return(Ok(new ResponseBase())); } return(ReturnError(ErrorCodes.OrderNotValid, method)); }