예제 #1
0
        private async Task <InterpreterAnswerDto> GetInterpreter(InterpreterAnswerModel interpreterModel, int brokerId)
        {
            if (interpreterModel.InterpreterId == Constants.DeclineInterpreterId)
            {
                return(new InterpreterAnswerDto
                {
                    Accepted = false,
                    DeclineMessage = interpreterModel.DeclineMessage
                });
            }
            var newInterpreterInformation = new InterpreterInformation
            {
                FirstName             = interpreterModel.NewInterpreterFirstName,
                LastName              = interpreterModel.NewInterpreterLastName,
                Email                 = interpreterModel.NewInterpreterEmail,
                PhoneNumber           = interpreterModel.NewInterpreterPhoneNumber,
                OfficialInterpreterId = interpreterModel.NewInterpreterOfficialInterpreterId
            };
            var interpreter = await _interpreterService.GetInterpreter(interpreterModel.InterpreterId.Value, newInterpreterInformation, brokerId);

            var requirementAnswers = interpreterModel.RequiredRequirementAnswers?.Select(ra => new OrderRequirementRequestAnswer
            {
                OrderRequirementId    = ra.OrderRequirementId,
                Answer                = ra.Answer,
                CanSatisfyRequirement = ra.CanMeetRequirement
            }).ToList() ?? new List <OrderRequirementRequestAnswer>();

            if (interpreterModel.DesiredRequirementAnswers != null)
            {
                requirementAnswers.AddRange(interpreterModel.DesiredRequirementAnswers.Select(ra => new OrderRequirementRequestAnswer
                {
                    OrderRequirementId    = ra.OrderRequirementId,
                    Answer                = ra.Answer,
                    CanSatisfyRequirement = ra.CanMeetRequirement
                }).ToList());
            }
            //Collect the interpreter information
            return(new InterpreterAnswerDto
            {
                Accepted = true,
                CompetenceLevel = interpreterModel.InterpreterCompetenceLevel.Value,
                ExpectedTravelCosts = interpreterModel.ExpectedTravelCosts,
                ExpectedTravelCostInfo = interpreterModel.ExpectedTravelCostInfo,
                RequirementAnswers = requirementAnswers,
                Interpreter = interpreter
            });
        }
예제 #2
0
        public async Task <IActionResult> Accept(RequestAcceptModel model)
        {
            if (ModelState.IsValid)
            {
                var request = await _dbContext.Requests.GetRequestForAcceptById(model.RequestId);

                if ((await _authorizationService.AuthorizeAsync(User, request, Policies.Accept)).Succeeded)
                {
                    if (!request.IsToBeProcessedByBroker && model.Status != RequestStatus.AcceptedNewInterpreterAppointed)
                    {
                        return(RedirectToAction("Index", "Home", new { ErrorMessage = "Förfrågan är redan behandlad" }));
                    }
                    else if (model.Status == RequestStatus.AcceptedNewInterpreterAppointed && !request.CanChangeInterpreter(_clock.SwedenNow))
                    {
                        return(RedirectToAction("Index", "Home", new { ErrorMessage = "Det gick inte att byta tolk, kontrollera tiden för uppdragsstart" }));
                    }
                    var requirementAnswers = model.RequiredRequirementAnswers.Select(ra => new OrderRequirementRequestAnswer
                    {
                        RequestId             = model.Status == RequestStatus.AcceptedNewInterpreterAppointed ? 0 : request.RequestId,
                        OrderRequirementId    = ra.OrderRequirementId,
                        Answer                = ra.Answer,
                        CanSatisfyRequirement = ra.CanMeetRequirement
                    }).ToList();
                    requirementAnswers.AddRange(model.DesiredRequirementAnswers.Select(ra => new OrderRequirementRequestAnswer
                    {
                        RequestId             = model.Status == RequestStatus.AcceptedNewInterpreterAppointed ? 0 : request.RequestId,
                        OrderRequirementId    = ra.OrderRequirementId,
                        Answer                = ra.Answer,
                        CanSatisfyRequirement = ra.CanMeetRequirement
                    }).ToList());
                    try
                    {
                        //if user can choose between phone/video and an interpreter location with travel, user might have set costs, LatestAnswerTimeForCustomer etc
                        if (model.InterpreterLocation == InterpreterLocation.OffSitePhone || model.InterpreterLocation == InterpreterLocation.OffSiteVideo)
                        {
                            model.LatestAnswerTimeForCustomer = null;
                            model.ExpectedTravelCostInfo      = null;
                            model.ExpectedTravelCosts         = null;
                        }
                        //if change interpreter or normal accept (no replacementorder)
                        if (model.Status == RequestStatus.AcceptedNewInterpreterAppointed || (!request.Order.ReplacingOrderId.HasValue && model.Status != RequestStatus.AcceptedNewInterpreterAppointed))
                        {
                            var interpreter = await _interpreterService.GetInterpreter(model.InterpreterId.Value, model.GetNewInterpreterInformation(), request.Ranking.BrokerId);

                            if (model.Status == RequestStatus.AcceptedNewInterpreterAppointed)
                            {
                                try
                                {
                                    await _requestService.ChangeInterpreter(
                                        request,
                                        _clock.SwedenNow,
                                        User.GetUserId(),
                                        User.TryGetImpersonatorId(),
                                        interpreter,
                                        model.InterpreterLocation.Value,
                                        model.InterpreterCompetenceLevel.Value,
                                        requirementAnswers,
                                        model.Files?.Select(f => new RequestAttachment {
                                        AttachmentId = f.Id
                                    }) ?? Enumerable.Empty <RequestAttachment>(),
                                        model.ExpectedTravelCosts,
                                        model.ExpectedTravelCostInfo,
                                        (model.SetLatestAnswerTimeForCustomer != null && EnumHelper.Parse <TrueFalse>(model.SetLatestAnswerTimeForCustomer.SelectedItem.Value) == TrueFalse.Yes)?model.LatestAnswerTimeForCustomer : null,
                                        model.BrokerReferenceNumber
                                        );
                                }
                                catch (InvalidOperationException ex)
                                {
                                    _logger.LogError("Change Interpreter for request {model.RequestId} failed. Message: {ex.Message}", model.RequestId, ex.Message);
                                    return(RedirectToAction("Index", "Home", new { errormessage = ex.Message }));
                                }
                            }
                            else
                            {
                                await _requestService.Accept(
                                    request,
                                    _clock.SwedenNow,
                                    User.GetUserId(),
                                    User.TryGetImpersonatorId(),
                                    interpreter,
                                    model.InterpreterLocation.Value,
                                    model.InterpreterCompetenceLevel.Value,
                                    requirementAnswers,
                                    model.Files?.Select(f => new RequestAttachment {
                                    AttachmentId = f.Id
                                }).ToList(),
                                    model.ExpectedTravelCosts,
                                    model.ExpectedTravelCostInfo,
                                    (model.SetLatestAnswerTimeForCustomer != null && EnumHelper.Parse <TrueFalse>(model.SetLatestAnswerTimeForCustomer.SelectedItem.Value) == TrueFalse.Yes)?model.LatestAnswerTimeForCustomer : null,
                                    model.BrokerReferenceNumber
                                    );
                            }
                        }
                        else
                        {
                            await _requestService.AcceptReplacement(
                                request,
                                _clock.SwedenNow,
                                User.GetUserId(),
                                User.TryGetImpersonatorId(),
                                model.InterpreterLocation.Value,
                                model.ExpectedTravelCosts,
                                model.ExpectedTravelCostInfo,
                                (model.SetLatestAnswerTimeForCustomer != null && EnumHelper.Parse <TrueFalse>(model.SetLatestAnswerTimeForCustomer.SelectedItem.Value) == TrueFalse.Yes)?model.LatestAnswerTimeForCustomer : null,
                                model.BrokerReferenceNumber
                                );
                        }
                        await _dbContext.SaveChangesAsync();
                    }
                    catch (ArgumentException ex)
                    {
                        _logger.LogError("Accept for request {model.RequestId} failed. Message: {ex.Message}", model.RequestId, ex.Message);
                        return(RedirectToAction("Index", "Home", new { errormessage = "Något gick fel i behandlingen av bokningsförfrågan" }));
                    }
                    catch (InvalidOperationException ex)
                    {
                        _logger.LogError("Accept for request {model.RequestId} failed. Message: {ex.Message}", model.RequestId, ex.Message);
                        return(RedirectToAction("Index", "Home", new { errormessage = ex.Message }));
                    }
                    return(RedirectToAction("Index", "Home", new { message = model.Status == RequestStatus.AcceptedNewInterpreterAppointed ? "Tolk har bytts ut för uppdraget" : "Svar har skickats" }));
                }
                return(Forbid());
            }
            return(RedirectToAction(nameof(Process), new { id = model.RequestId }));
        }