private static void DetermineNextActionUsingCurrrentStatus(
     CertificatePostApprovalViewModel certificateApprovalViewModel)
 {
     //If we are on the New Screen
     if (certificateApprovalViewModel.ActionHint == CertificateStatus.SentForApproval)
     {
         //If all certificates are rejected just go to the rejected screen
         if (certificateApprovalViewModel.ApprovalResults.All(
                 x => x.IsApproved != CertificateStatus.ToBeApproved))
         {
             certificateApprovalViewModel.ActionHint = CertificateStatus.Rejected;
         }
     }
     else
     {
         //We got here that means our current screen is the Sent For Approval screen, since you can't submit from
         //Approved and Reject screen
         if (!certificateApprovalViewModel.HaveAllRecordsBeenProcessed)
         {
             //If there are still some certificates left to be approved|rejected stay on the Sent For Approval screen
             certificateApprovalViewModel.ActionHint = CertificateStatus.SentForApproval;
         }
         else if (certificateApprovalViewModel.ApprovalResults.Any(
                      x => x.IsApproved == CertificateStatus.Submitted))
         {
             //If there where any certificates marked as approved, set the next screen as Approved
             certificateApprovalViewModel.ActionHint = CertificateStatus.Approved;
         }
         else
         {
             //Only go to reject screen when all certificate where rejected
             certificateApprovalViewModel.ActionHint = CertificateStatus.Rejected;
         }
     }
 }
        private async Task <IActionResult> ValidateReasonForChange1(
            CertificatePostApprovalViewModel certificateApprovalViewModel)
        {
            ValidateReasonForChange(certificateApprovalViewModel);
            if (ModelState.IsValid)
            {
                return(null);
            }

            if (certificateApprovalViewModel.ApprovalResults.Any(
                    x => x.IsApproved == CertificateStatus.Draft && string.IsNullOrEmpty(x.PrivatelyFundedStatus)))
            {
                var certificates = await GetCertificatesToBeApproved(certificateApprovalViewModel,
                                                                     CertificateStatus.ToBeApproved, null);

                return(View("~/Views/CertificateApprovals/New.cshtml",
                            GetCertificateToBeApproved(Mapper.Map <List <CertificateDetailApprovalViewModel> >(certificates.Items),
                                                       certificates, certificateApprovalViewModel.PageIndex)));
            }

            if (certificateApprovalViewModel.ApprovalResults.Any(
                    x => (x.IsApproved == CertificateStatus.ToBeApproved || x.IsApproved == CertificateStatus.Draft) &&
                    x.PrivatelyFundedStatus == CertificateStatus.SentForApproval))
            {
                var certificates = await GetCertificatesToBeApproved(certificateApprovalViewModel,
                                                                     CertificateStatus.ToBeApproved, CertificateStatus.SentForApproval);

                return(View("~/Views/CertificateApprovals/SentForApproval.cshtml",
                            GetCertificateToBeApproved(Mapper.Map <List <CertificateDetailApprovalViewModel> >(certificates.Items),
                                                       certificates, certificateApprovalViewModel.PageIndex)));
            }

            return(null);
        }
        private static void SetPrivatelyFundedCertApprovalStatus(
            CertificatePostApprovalViewModel certificateApprovalViewModel)
        {
            foreach (var approvalResult in certificateApprovalViewModel.ApprovalResults)
            {
                switch (approvalResult.IsApproved)
                {
                //When its a new certificate that needs approval the PrivatelyFundedStatus will always be null
                case CertificateStatus.ToBeApproved when string.IsNullOrEmpty(approvalResult.PrivatelyFundedStatus):
                    approvalResult.PrivatelyFundedStatus = null;     //Incase was empty

                    approvalResult.PrivatelyFundedStatus = CertificateStatus.SentForApproval;
                    break;

                case CertificateStatus.Submitted
                    when approvalResult.PrivatelyFundedStatus == CertificateStatus.SentForApproval:
                    approvalResult.PrivatelyFundedStatus = CertificateStatus.Approved;
                    break;

                case CertificateStatus.Draft when(string.IsNullOrEmpty(approvalResult.PrivatelyFundedStatus) ||
                                                  approvalResult.PrivatelyFundedStatus ==
                                                  CertificateStatus.SentForApproval):
                    approvalResult.PrivatelyFundedStatus = CertificateStatus.Rejected;

                    break;

                case CertificateStatus.Submitted
                    when(approvalResult.PrivatelyFundedStatus == CertificateStatus.Rejected):
                    approvalResult.PrivatelyFundedStatus = CertificateStatus.Approved;

                    break;
                }
            }
        }
 private async Task <PaginatedList <CertificateSummaryResponse> > GetCertificatesToBeApproved(
     CertificatePostApprovalViewModel certificateApprovalViewModel, string certificateStatus,
     string privatedFundedStatus)
 {
     return(await ApiClient.GetCertificatesToBeApproved(PageSize,
                                                        certificateApprovalViewModel.PageIndex ?? 1, certificateStatus,
                                                        privatedFundedStatus));
 }
        private void ValidateReasonForChange(CertificatePostApprovalViewModel certificateApprovalViewModel)
        {
            var count = 0;

            foreach (var approvalResult in certificateApprovalViewModel.ApprovalResults)
            {
                //Will only have status of draft if rejected, so check reason for change is updated
                if (string.IsNullOrEmpty(approvalResult.ReasonForChange) &&
                    approvalResult.IsApproved == CertificateStatus.Draft)
                {
                    ModelState.AddModelError($"approvalResults[{count++}].ReasonForChange",
                                             "Please enter a reason for rejection");
                }
            }
        }
        public async Task <IActionResult> Approvals([FromBody] CertificatePostApprovalViewModel certificateApprovalViewModel)
        {
            var approvalsValidationFailed = await ValidateReasonForChange1(certificateApprovalViewModel);

            if (approvalsValidationFailed != null)
            {
                return(approvalsValidationFailed);
            }

            certificateApprovalViewModel.UserName = ContextAccessor.HttpContext.User
                                                    .FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn")?.Value;

            SetPrivatelyFundedCertApprovalStatus(certificateApprovalViewModel);
            DetermineNextActionUsingCurrrentStatus(certificateApprovalViewModel);

            await ApiClient.ApproveCertificates(certificateApprovalViewModel);

            return(Ok(certificateApprovalViewModel.ActionHint));
        }
        public void ShouldReturnRejections()
        {
            var certificatePostApprovalViewModel = new CertificatePostApprovalViewModel
            {
                ActionHint      = "Approved",
                ApprovalResults = new[]
                {
                    new ApprovalResult
                    {
                        CertificateReference  = "SomeRef",
                        IsApproved            = CertificateStatus.Draft,
                        PrivatelyFundedStatus = CertificateStatus.Rejected,
                        ReasonForChange       = "SomeReason"
                    }
                }
            };

            var returnResult = certificateApprovalsController.Rejected(0).GetAwaiter().GetResult();

            returnResult.As <ViewResult>().Model.As <CertificateApprovalViewModel>().RejectedCertificates.Items.Count()
            .Should().Be(3);
        }
        public void ShouldReturnToBeApproveds()
        {
            var certificatePostApprovalViewModel = new CertificatePostApprovalViewModel
            {
                ActionHint      = "Approved",
                ApprovalResults = new[]
                {
                    new ApprovalResult
                    {
                        CertificateReference  = "SomeRef",
                        IsApproved            = CertificateStatus.ToBeApproved,
                        PrivatelyFundedStatus = CertificateStatus.SentForApproval,
                        ReasonForChange       = "SomeReason"
                    }
                },
                PageIndex = 0,
            };

            var returnResult = certificateApprovalsController.SentForApproval(0).GetAwaiter().GetResult();

            returnResult.As <ViewResult>().Model.As <CertificateApprovalViewModel>().SentForApprovalCertificates.Items.Count()
            .Should().Be(4);
        }
 public async Task ApproveCertificates(CertificatePostApprovalViewModel certificatePostApprovalViewModel)
 {
     await Post <CertificatePostApprovalViewModel>("api/v1/certificates/approvals",
                                                   certificatePostApprovalViewModel);
 }