public async Task <IActionResult> Create([FromBody] RequisitionModel model) { if (model == null) { return(ReturnError(ErrorCodes.IncomingPayloadIsMissing)); } try { var brokerId = User.TryGetBrokerId().Value; var apiUserId = User.UserId(); var order = await _apiOrderService.GetOrderAsync(model.OrderNumber, brokerId); var user = await _apiUserService.GetBrokerUser(model.CallingUser, brokerId); var request = await _dbContext.Requests.GetActiveRequestForApiWithBrokerAndOrderNumber(model.OrderNumber, brokerId); if (request == null) { return(ReturnError(ErrorCodes.RequestNotFound)); } if (!request.IsApprovedOrDelivered) { return(ReturnError(ErrorCodes.RequestNotInCorrectState)); } try { await _requisitionService.Create(request, user?.Id ?? apiUserId, (user != null ? (int?)apiUserId : null), model.Message, model.Outlay, model.AcctualStartedAt, model.AcctualEndedAt, model.WasteTime, model.WasteTimeInconvenientHour, EnumHelper.GetEnumByCustomName <TaxCardType>(model.TaxCard).Value, new List <RequisitionAttachment>(), Guid.NewGuid(), model.MealBreaks.Select(m => new MealBreak { StartAt = m.StartedAt, EndAt = m.EndedAt, }).ToList(), model.CarCompensation, model.PerDiem); await _dbContext.SaveChangesAsync(); } catch (InvalidOperationException) { //TODO: Should log the acctual exception here!! return(ReturnError(ErrorCodes.RequisitionNotInCorrectState)); } return(Ok(new ResponseBase())); } catch (InvalidApiCallException ex) { return(ReturnError(ex.ErrorCode)); } }
public async Task <IActionResult> Create(RequisitionModel model) { if (ModelState.IsValid) { var request = await _dbContext.Requests.GetRequestForRequisitionCreateById(model.RequestId); List <MealBreak> mealbreaks = new List <MealBreak>(); if (model.MealBreaks != null) { foreach (MealBreak mb in model.MealBreaks) { mealbreaks.Add(new MealBreak { StartAt = mb.StartAtTemp.ToDateTimeOffsetSweden(), EndAt = mb.EndAtTemp.ToDateTimeOffsetSweden() }); } } if ((await _authorizationService.AuthorizeAsync(User, request, Policies.CreateRequisition)).Succeeded) { Requisition requisition; try { requisition = await _requisitionService.Create(request, User.GetUserId(), User.TryGetImpersonatorId(), model.Message, model.Outlay, model.SessionStartedAt, model.SessionEndedAt, model.TimeWasteTotalTime.HasValue?(model.TimeWasteTotalTime ?? 0) - (model.TimeWasteIWHTime ?? 0) : model.TimeWasteTotalTime, model.TimeWasteIWHTime, model.InterpreterTaxCard.Value, model.Files?.Select(f => new RequisitionAttachment { AttachmentId = f.Id }).ToList(), model.FileGroupKey.Value, mealbreaks, model.CarCompensation, model.PerDiem); } catch (InvalidOperationException ex) { _logger.LogError("Failed to create requisition for request {request.RequestId}, message {errorMessage}.", request.RequestId, ex.Message); return(RedirectToAction("Index", "Home", new { errorMessage = $"Det gick inte att registrera rekvisition för {request.Order.OrderNumber}, det kan bero på att det redan finns en rekvisition registrerad." })); } return(RedirectToAction("View", "Request", new { id = requisition.RequestId, tab = "requisition" })); } return(Forbid()); } return(View(nameof(Create), model)); }