public async Task <Unit> Handle(DeleteReservationCommand command, CancellationToken cancellationToken)
        {
            var validationResult = await _validator.ValidateAsync(command);

            if (!validationResult.IsValid())
            {
                throw new ArgumentException(
                          "The following parameters have failed validation",
                          validationResult.ValidationDictionary.Select(c => c.Key).Aggregate((item1, item2) => item1 + ", " + item2));
            }

            var reservationToDelete = await _reservationService.GetReservation(command.ReservationId);

            var deletedEvent = new ReservationDeletedEvent(
                command.ReservationId,
                reservationToDelete.AccountId,
                reservationToDelete.AccountLegalEntityId,
                reservationToDelete.AccountLegalEntityName,
                reservationToDelete.StartDate.GetValueOrDefault(),
                reservationToDelete.ExpiryDate.GetValueOrDefault(),
                reservationToDelete.CreatedDate,
                reservationToDelete.Course?.CourseId,
                reservationToDelete.Course?.Title,
                reservationToDelete.Course?.Level,
                reservationToDelete.ProviderId,
                command.EmployerDeleted);

            await _reservationService.DeleteReservation(command.ReservationId);

            _context.AddEvent(deletedEvent);

            return(Unit.Value);
        }
Exemplo n.º 2
0
        public async Task <CreateAccountReservationResult> Handle(CreateAccountReservationCommand request, CancellationToken cancellationToken)
        {
            var validationResult = await _validator.ValidateAsync(request);

            if (!validationResult.IsValid())
            {
                throw new ArgumentException(
                          "The following parameters have failed validation",
                          validationResult.ValidationDictionary.Select(c => c.Key).Aggregate((item1, item2) => item1 + ", " + item2));
            }

            var globalRule = await _globalRulesService.CheckReservationAgainstRules(request);

            if (globalRule != null)
            {
                var shouldReturnError = false;
                switch (globalRule.Restriction)
                {
                case AccountRestriction.All:
                case AccountRestriction.Account:
                    shouldReturnError = true;
                    break;

                case AccountRestriction.Levy:
                    if (request.IsLevyAccount)
                    {
                        shouldReturnError = true;
                    }

                    break;

                case AccountRestriction.NonLevy:
                    if (!request.IsLevyAccount)
                    {
                        shouldReturnError = true;
                    }

                    break;
                }

                if (shouldReturnError)
                {
                    return(new CreateAccountReservationResult
                    {
                        Reservation = null,
                        Rule = globalRule
                    });
                }
            }

            var accountLegalEntity =
                await _accountLegalEntitiesService.GetAccountLegalEntity(request.AccountLegalEntityId);

            if (request.IsLevyAccount)
            {
                request.AccountLegalEntityName = accountLegalEntity.AccountLegalEntityName;
            }
            else if (!accountLegalEntity.IsLevy && !accountLegalEntity.AgreementSigned)
            {
                return(new CreateAccountReservationResult
                {
                    Reservation = null,
                    Rule = null,
                    AgreementSigned = false
                });
            }

            var reservation = await _accountReservationService.CreateAccountReservation(request);

            _context.AddEvent(() =>
            {
                var startDate = DateTime.MinValue;
                if (reservation.StartDate.HasValue)
                {
                    startDate = reservation.StartDate.Value;
                }
                var expiryDate = DateTime.MinValue;
                if (reservation.ExpiryDate.HasValue)
                {
                    expiryDate = reservation.ExpiryDate.Value;
                }

                return(new ReservationCreatedEvent(reservation.Id,
                                                   reservation.AccountId,
                                                   reservation.AccountLegalEntityId,
                                                   reservation.AccountLegalEntityName,
                                                   startDate,
                                                   expiryDate,
                                                   reservation.CreatedDate,
                                                   reservation.Course?.CourseId,
                                                   reservation.Course?.Title,
                                                   reservation.Course?.Level,
                                                   reservation.ProviderId
                                                   ));
            });

            return(new CreateAccountReservationResult
            {
                Reservation = reservation,
                AgreementSigned = true
            });
        }
        public Task Publish <T>(T message) where T : Event
        {
            _unitOfWorkContext.AddEvent(message);

            return(Task.CompletedTask);
        }