Esempio n. 1
0
 private static void CheckCommitmentStatus(Commitment commitment)
 {
     if (commitment.CommitmentStatus != CommitmentStatus.New && commitment.CommitmentStatus != CommitmentStatus.Active)
     {
         throw new InvalidOperationException($"Commitment {commitment.Id} cannot be updated because status is {commitment.CommitmentStatus}");
     }
 }
Esempio n. 2
0
 public CommitmentView MapFrom(Commitment commitment, CallerType callerType)
 {
     return(new CommitmentView
     {
         Id = commitment.Id,
         Reference = commitment.Reference,
         ProviderId = commitment.ProviderId,
         ProviderName = commitment.ProviderName,
         EmployerAccountId = commitment.EmployerAccountId,
         LegalEntityId = commitment.LegalEntityId,
         LegalEntityName = commitment.LegalEntityName,
         EditStatus = (Types.Commitment.Types.EditStatus)commitment.EditStatus,
         AgreementStatus = (AgreementStatus)_commitmentRules.DetermineAgreementStatus(commitment.Apprenticeships),
         LastAction = (Types.Commitment.Types.LastAction)commitment.LastAction,
         CanBeApproved = callerType == CallerType.Employer ? commitment.EmployerCanApproveCommitment : commitment.ProviderCanApproveCommitment,
         EmployerLastUpdateInfo = new LastUpdateInfo {
             Name = commitment.LastUpdatedByEmployerName, EmailAddress = commitment.LastUpdatedByEmployerEmail
         },
         ProviderLastUpdateInfo = new LastUpdateInfo {
             Name = commitment.LastUpdatedByProviderName, EmailAddress = commitment.LastUpdatedByProviderEmail
         },
         Apprenticeships = MapApprenticeshipsFrom(commitment.Apprenticeships, callerType),
         Messages = MapMessagesFrom(commitment.Messages)
     });
 }
        public async Task ThenItShouldUpdatedTheAgreementStatusForAllApprenticeshipsOnTheSameCommitment()
        {
            var c = new Commitment
            {
                Id                = 123L,
                EditStatus        = EditStatus.ProviderOnly,
                ProviderId        = 111L,
                EmployerAccountId = 5L,
                CommitmentStatus  = CommitmentStatus.Active,
                Apprenticeships   =
                    new List <Domain.Entities.Apprenticeship>
                {
                    new Domain.Entities.Apprenticeship {
                        Id = 1, AgreementStatus = AgreementStatus.BothAgreed
                    },
                    new Domain.Entities.Apprenticeship {
                        Id = 2, AgreementStatus = AgreementStatus.EmployerAgreed
                    },
                    new Domain.Entities.Apprenticeship {
                        Id = 3, AgreementStatus = AgreementStatus.ProviderAgreed
                    },
                    new Domain.Entities.Apprenticeship {
                        Id = 4, AgreementStatus = AgreementStatus.NotAgreed
                    }
                }
            };

            _mockCommitmentRespository.Setup(m => m.GetCommitmentById(It.IsAny <long>())).Returns(Task.Run(() => c));
            await _handler.Handle(_exampleValidRequest);

            _mockApprenticeshipRepository.Verify(x =>
                                                 x.UpdateApprenticeshipStatus(123, It.IsAny <long>(), AgreementStatus.NotAgreed), Times.Exactly(3));
        }
Esempio n. 4
0
 private static void CheckCommitmentStatus(Commitment commitment)
 {
     if (commitment.CommitmentStatus != Domain.Entities.CommitmentStatus.New && commitment.CommitmentStatus != Domain.Entities.CommitmentStatus.Active)
     {
         throw new InvalidOperationException($"Cannot add apprenticeship in commitment {commitment.Id} because status is {commitment.CommitmentStatus}");
     }
 }
Esempio n. 5
0
 public CommitmentView MapFrom(Commitment commitment, CallerType callerType)
 {
     return(commitment == null
         ? null
         : new CommitmentView
     {
         Id = commitment.Id,
         Reference = commitment.Reference,
         ProviderId = commitment.ProviderId,
         ProviderName = commitment.ProviderName,
         EmployerAccountId = commitment.EmployerAccountId,
         LegalEntityId = commitment.LegalEntityId,
         LegalEntityName = commitment.LegalEntityName,
         LegalEntityAddress = commitment.LegalEntityAddress,
         AccountLegalEntityPublicHashedId = commitment.AccountLegalEntityPublicHashedId,
         EditStatus = (Types.Commitment.Types.EditStatus)commitment.EditStatus,
         AgreementStatus = (AgreementStatus)_commitmentRules.DetermineAgreementStatus(commitment.Apprenticeships),
         LastAction = (Types.Commitment.Types.LastAction)commitment.LastAction,
         CanBeApproved = CommitmentCanBeApproved(callerType, commitment),
         EmployerLastUpdateInfo = new LastUpdateInfo {
             Name = commitment.LastUpdatedByEmployerName, EmailAddress = commitment.LastUpdatedByEmployerEmail
         },
         ProviderLastUpdateInfo = new LastUpdateInfo {
             Name = commitment.LastUpdatedByProviderName, EmailAddress = commitment.LastUpdatedByProviderEmail
         },
         Apprenticeships = MapApprenticeshipsFrom(commitment.Apprenticeships, callerType),
         Messages = MapMessagesFrom(commitment.Messages),
         TransferSender = MapTransferSenderInfo(commitment),
         ApprenticeshipEmployerTypeOnApproval = (ApprenticeshipEmployerTypeOnApproval?)commitment.ApprenticeshipEmployerTypeOnApproval,
         ChangeOfPartyRequestId = commitment.ChangeOfPartyRequestId
     });
 }
        private async Task CreateHistory(Commitment newCommitment, CallerType callerType, string userId, string userName)
        {
            var historyService = new HistoryService(_historyRepository);

            historyService.TrackInsert(newCommitment, CommitmentChangeType.Created.ToString(), newCommitment.Id, null, callerType, userId, newCommitment.ProviderId, newCommitment.EmployerAccountId, userName);
            await historyService.Save();
        }
Esempio n. 7
0
        private async Task CreateHistory(Commitment commitment, Domain.Entities.Apprenticeship apprenticeship, CallerType callerType, string userId, string userName)
        {
            var historyService = new HistoryService(_historyRepository);

            historyService.TrackUpdate(commitment, CommitmentChangeType.CreatedApprenticeship.ToString(), commitment.Id, null, callerType, userId, apprenticeship.ProviderId, apprenticeship.EmployerAccountId, userName);
            historyService.TrackInsert(apprenticeship, ApprenticeshipChangeType.Created.ToString(), null, apprenticeship.Id, callerType, userId, apprenticeship.ProviderId, apprenticeship.EmployerAccountId, userName);
            await historyService.Save();
        }
Esempio n. 8
0
        private async Task CreateEventsForUpdatedApprenticeships(Commitment commitment, IList <Apprenticeship> updatedApprenticeships)
        {
            var existingApprenticeships = await GetActiveApprenticeshipsForLearners(updatedApprenticeships);

            Parallel.ForEach(updatedApprenticeships, apprenticeship =>
            {
                AddApprenticeshipUpdatedEvent(commitment, apprenticeship, existingApprenticeships.Where(x => x.Uln == apprenticeship.ULN));
            });
        }
Esempio n. 9
0
        private static void CheckStateForApproval(Commitment commitment, Caller caller)
        {
            var canBeApprovedByParty = caller.CallerType == CallerType.Employer
                ? commitment.EmployerCanApproveCommitment
                : commitment.ProviderCanApproveCommitment;

            if (!canBeApprovedByParty)
            {
                throw new InvalidOperationException($"Commitment {commitment.Id} cannot be approved because apprentice information is incomplete");
            }
        }
Esempio n. 10
0
 private async Task UpdateStatusOfApprenticeship(Commitment commitment)
 {
     // TODO: Should we do just a blanket update accross all apprenticeships in the Commitment?
     foreach (var apprenticeship in commitment.Apprenticeships)
     {
         if (apprenticeship.AgreementStatus != Domain.Entities.AgreementStatus.NotAgreed)
         {
             await _apprenticeshipRepository.UpdateApprenticeshipStatus(commitment.Id, apprenticeship.Id, Domain.Entities.AgreementStatus.NotAgreed);
         }
     }
 }
        public void ThenWhenCommitmentStatusIsIncorrectAnInvalidRequestExceptionIsThrown(CommitmentStatus commitmentStatus)
        {
            var c = new Commitment {
                EditStatus = EditStatus.EmployerOnly, ProviderId = 5L, EmployerAccountId = 5L, CommitmentStatus = commitmentStatus
            };

            _exampleValidRequest.Caller = new Caller {
                Id = 5L, CallerType = CallerType.Employer
            };
            _mockCommitmentRespository.Setup(m => m.GetCommitmentById(It.IsAny <long>())).Returns(Task.Run(() => c));
            Func <Task> act = async() => await _handler.Handle(_exampleValidRequest);

            act.ShouldThrow <InvalidOperationException>();
        }
        public void ThenWhenEditStatusIsIncorrectAnInvalidRequestExceptionIsThrown(EditStatus editStatus, CallerType callerType)
        {
            var c = new Commitment {
                EditStatus = editStatus, ProviderId = 5L, EmployerAccountId = 5L, CommitmentStatus = CommitmentStatus.Active
            };

            _exampleValidRequest.Caller = new Caller {
                Id = 5L, CallerType = callerType
            };
            _mockCommitmentRespository.Setup(m => m.GetCommitmentById(It.IsAny <long>())).Returns(Task.Run(() => c));
            Func <Task> act = async() => await _handler.Handle(_exampleValidRequest);

            act.ShouldThrow <UnauthorizedException>();
        }
Esempio n. 13
0
        private static bool CommitmentCanBeApproved(CallerType callerType, Commitment commitment)
        {
            switch (callerType)
            {
            case CallerType.Employer:
                return(commitment.EmployerCanApproveCommitment);

            case CallerType.Provider:
                return(commitment.ProviderCanApproveCommitment);

            case CallerType.TransferSender:
                return(commitment.TransferApprovalStatus == TransferApprovalStatus.Pending);
            }
            return(false);
        }
Esempio n. 14
0
 private TransferSender MapTransferSenderInfo(Commitment commitment)
 {
     if (commitment.TransferSenderId == null)
     {
         return(null);
     }
     return(new TransferSender
     {
         Id = commitment.TransferSenderId,
         Name = commitment.TransferSenderName,
         TransferApprovalStatus = (Types.TransferApprovalStatus?)commitment.TransferApprovalStatus,
         TransferApprovalSetBy = commitment.TransferApprovalActionedByEmployerName,
         TransferApprovalSetOn = commitment.TransferApprovalActionedOn
     });
 }
        public async Task ThenHistoryRecordsAreCreated()
        {
            var testCommitment = new Commitment
            {
                ProviderId = _exampleValidRequest.Caller.Id,
                Id         = _exampleValidRequest.CommitmentId
            };
            var expectedOriginalState = JsonConvert.SerializeObject(testCommitment);

            _mockCommitmentRespository.Setup(x => x.GetCommitmentById(It.IsAny <long>())).ReturnsAsync(testCommitment);
            _mockApprenticeshipRepository.Setup(x => x.CreateApprenticeship(It.IsAny <Domain.Entities.Apprenticeship>())).ReturnsAsync(expectedApprenticeshipId);

            await _handler.Handle(_exampleValidRequest);

            _mockHistoryRepository.Verify(
                x =>
                x.InsertHistory(
                    It.Is <IEnumerable <HistoryItem> >(
                        y =>
                        y.First().ChangeType == CommitmentChangeType.CreatedApprenticeship.ToString() &&
                        y.First().CommitmentId == testCommitment.Id &&
                        y.First().ApprenticeshipId == null &&
                        y.First().OriginalState == expectedOriginalState &&
                        y.First().UpdatedByRole == _exampleValidRequest.Caller.CallerType.ToString() &&
                        y.First().UpdatedState == expectedOriginalState &&
                        y.First().UserId == _exampleValidRequest.UserId &&
                        y.First().ProviderId == _providerId &&
                        y.First().EmployerAccountId == _employerAccountId &&
                        y.First().UpdatedByName == _exampleValidRequest.UserName)), Times.Once);

            _mockHistoryRepository.Verify(
                x =>
                x.InsertHistory(
                    It.Is <IEnumerable <HistoryItem> >(
                        y =>
                        y.Last().ChangeType == ApprenticeshipChangeType.Created.ToString() &&
                        y.Last().CommitmentId == null &&
                        y.Last().ApprenticeshipId == expectedApprenticeshipId &&
                        y.Last().OriginalState == null &&
                        y.Last().UpdatedByRole == _exampleValidRequest.Caller.CallerType.ToString() &&
                        y.Last().UpdatedState != null &&
                        y.Last().UserId == _exampleValidRequest.UserId &&
                        y.Last().ProviderId == _providerId &&
                        y.Last().EmployerAccountId == _employerAccountId &&
                        y.Last().UpdatedByName == _exampleValidRequest.UserName)), Times.Once);
        }
Esempio n. 16
0
        private static void CheckEditStatus(CreateApprenticeshipCommand message, Commitment commitment)
        {
            switch (message.Caller.CallerType)
            {
            case CallerType.Provider:
                if (commitment.EditStatus != Domain.Entities.EditStatus.Both && commitment.EditStatus != Domain.Entities.EditStatus.ProviderOnly)
                {
                    throw new UnauthorizedException($"Provider {message.Caller.Id} not allowed to add apprenticeship {message.Apprenticeship.Id} to commitment {message.CommitmentId}");
                }
                break;

            case CallerType.Employer:
                if (commitment.EditStatus != Domain.Entities.EditStatus.Both && commitment.EditStatus != Domain.Entities.EditStatus.EmployerOnly)
                {
                    throw new UnauthorizedException($"Employer {message.Caller.Id} not allowed to add apprenticeship {message.Apprenticeship.Id} to commitment {message.CommitmentId}");
                }
                break;
            }
        }
Esempio n. 17
0
        private static void CheckAuthorization(CreateApprenticeshipCommand message, Commitment commitment)
        {
            switch (message.Caller.CallerType)
            {
            case CallerType.Provider:
                if (commitment.ProviderId != message.Caller.Id)
                {
                    throw new UnauthorizedException($"Provider {message.Caller.Id} not authorised to access commitment: {message.CommitmentId}, expected provider {commitment.ProviderId}");
                }
                break;

            case CallerType.Employer:
            default:
                if (commitment.EmployerAccountId != message.Caller.Id)
                {
                    throw new UnauthorizedException($"Employer {message.Caller.Id} not authorised to access commitment: {message.CommitmentId}, expected employer {commitment.EmployerAccountId}");
                }
                break;
            }
        }
Esempio n. 18
0
        public Commitment MapFrom(Types.Commitment.Commitment commitment)
        {
            var domainCommitment = new Commitment
            {
                Reference                   = commitment.Reference,
                EmployerAccountId           = commitment.EmployerAccountId,
                LegalEntityId               = commitment.LegalEntityId,
                LegalEntityName             = commitment.LegalEntityName,
                LegalEntityAddress          = commitment.LegalEntityAddress,
                LegalEntityOrganisationType = (SFA.DAS.Common.Domain.Types.OrganisationType)commitment.LegalEntityOrganisationType,
                ProviderId                  = commitment.ProviderId,
                ProviderName                = commitment.ProviderName,
                CommitmentStatus            = (CommitmentStatus)commitment.CommitmentStatus,
                EditStatus                  = (EditStatus)commitment.EditStatus,
                LastAction                  = LastAction.None,
                LastUpdatedByEmployerName   = commitment.EmployerLastUpdateInfo.Name,
                LastUpdatedByEmployerEmail  = commitment.EmployerLastUpdateInfo.EmailAddress,
            };

            return(domainCommitment);
        }
Esempio n. 19
0
        private async Task <GetOverlappingApprenticeshipsResponse> GetOverlappingApprenticeships(Commitment commitment)
        {
            var overlapRequests =
                commitment.Apprenticeships
                .Where(x => !string.IsNullOrWhiteSpace(x.ULN) && x.StartDate.HasValue && x.EndDate.HasValue)
                .Select(apprenticeship =>
                        new ApprenticeshipOverlapValidationRequest
            {
                Uln       = apprenticeship.ULN,
                StartDate = apprenticeship.StartDate.Value,
                EndDate   = apprenticeship.EndDate.Value
            })
                .ToList();

            var response = await _mediator.SendAsync(new GetOverlappingApprenticeshipsRequest
            {
                OverlappingApprenticeshipRequests = overlapRequests
            });

            return(response);
        }
Esempio n. 20
0
        private async Task CreateCommitmentMessage(UpdateCommitmentAgreementCommand command, Commitment commitment)
        {
            var message = new Message
            {
                Author    = command.LastUpdatedByName,
                Text      = command.Message ?? string.Empty,
                CreatedBy = command.Caller.CallerType
            };

            commitment.Messages.Add(message);

            await _commitmentRepository.SaveMessage(command.CommitmentId, message);
        }
Esempio n. 21
0
        private async Task UpdateCommitmentStatuses(UpdateCommitmentAgreementCommand command, Commitment updatedCommitment, bool areAnyApprenticeshipsPendingAgreement, LastAction latestAction)
        {
            var updatedEditStatus = _apprenticeshipUpdateRules.DetermineNewEditStatus(updatedCommitment.EditStatus, command.Caller.CallerType, areAnyApprenticeshipsPendingAgreement,
                                                                                      updatedCommitment.Apprenticeships.Count, latestAction);
            var changeType     = DetermineHistoryChangeType(latestAction, updatedEditStatus);
            var historyService = new HistoryService(_historyRepository);

            historyService.TrackUpdate(updatedCommitment, changeType.ToString(), updatedCommitment.Id, null, command.Caller.CallerType, command.UserId, updatedCommitment.ProviderId, updatedCommitment.EmployerAccountId, command.LastUpdatedByName);

            updatedCommitment.EditStatus       = updatedEditStatus;
            updatedCommitment.CommitmentStatus = _apprenticeshipUpdateRules.DetermineNewCommmitmentStatus(areAnyApprenticeshipsPendingAgreement);
            updatedCommitment.LastAction       = latestAction;

            SetLastUpdatedDetails(command, updatedCommitment);
            await _commitmentRepository.UpdateCommitment(updatedCommitment);

            await historyService.Save();
        }
Esempio n. 22
0
 private static void SetLastUpdatedDetails(UpdateCommitmentAgreementCommand command, Commitment updatedCommitment)
 {
     if (command.Caller.CallerType == CallerType.Employer)
     {
         updatedCommitment.LastUpdatedByEmployerEmail = command.LastUpdatedByEmail;
         updatedCommitment.LastUpdatedByEmployerName  = command.LastUpdatedByName;
     }
     else
     {
         updatedCommitment.LastUpdatedByProviderEmail = command.LastUpdatedByEmail;
         updatedCommitment.LastUpdatedByProviderName  = command.LastUpdatedByName;
     }
 }
Esempio n. 23
0
        private async Task <IList <Apprenticeship> > UpdateApprenticeshipAgreementStatuses(UpdateCommitmentAgreementCommand command, Commitment commitment, LastAction latestAction)
        {
            var updatedApprenticeships = new List <Apprenticeship>();

            foreach (var apprenticeship in commitment.Apprenticeships)
            {
                //todo: extract status stuff outside loop and set all apprenticeships to same agreement status?
                var hasChanged = UpdateApprenticeshipStatuses(command, latestAction, apprenticeship);

                if (hasChanged)
                {
                    updatedApprenticeships.Add(apprenticeship);
                }
            }

            await _apprenticeshipRepository.UpdateApprenticeshipStatuses(updatedApprenticeships);

            return(updatedApprenticeships);
        }
Esempio n. 24
0
        private void AddApprenticeshipUpdatedEvent(Commitment commitment, Apprenticeship apprenticeship, IEnumerable <ApprenticeshipResult> existingApprenticeships)
        {
            var effectiveFromDate = DetermineEffectiveFromDate(apprenticeship.AgreementStatus, existingApprenticeships, apprenticeship.StartDate);

            _apprenticeshipEventsList.Add(commitment, apprenticeship, "APPRENTICESHIP-AGREEMENT-UPDATED", effectiveFromDate);
        }
Esempio n. 25
0
        private static void CheckEditStatus(UpdateCommitmentAgreementCommand message, Commitment commitment)
        {
            switch (message.Caller.CallerType)
            {
            case CallerType.Provider:
                if (commitment.EditStatus != EditStatus.Both && commitment.EditStatus != EditStatus.ProviderOnly)
                {
                    throw new UnauthorizedException($"Provider {message.Caller.Id} not allowed to edit commitment: {message.CommitmentId}");
                }
                break;

            case CallerType.Employer:
                if (commitment.EditStatus != EditStatus.Both && commitment.EditStatus != EditStatus.EmployerOnly)
                {
                    throw new UnauthorizedException($"Employer {message.Caller.Id} not allowed to edit commitment: {message.CommitmentId}");
                }
                break;
            }
        }