Ejemplo n.º 1
0
            public void Should_Invoke_Registered_Domain_Event_Handler()
            {
                TestDomainEventHandler handler = new TestDomainEventHandler(_testOutput);

                IEventSourcedAggregateRepository <TestAggregate, Guid> repository = Factory.CreateTestAggregateRepository(reg =>
                {
                    reg.Register <TestAggregateCreatedEvent>(() => handler);
                    reg.Register <OperationExecutedEvent>(() => handler);
                });

                TestAggregate aggregate = new TestAggregate(Guid.NewGuid());

                aggregate.ExecuteSomeOperation("Test Operation");
                repository.Save(aggregate);

                // Event may not have yet been handled in background.
                Thread.Sleep(1000);

                // Aggregate should be stored.
                TestAggregate storedAggregate = repository.GetById(aggregate.Id);

                Assert.Equal(aggregate.Id, storedAggregate.Id);

                // Aggregate should have 2 events.
                // 1. TestAggregateCreated
                // 2. TestAggregateModified
                Assert.Equal(2, handler.HandledEvents.Count);
                Assert.Contains(handler.HandledEvents, (e) => e is TestAggregateCreatedEvent);
                Assert.Contains(handler.HandledEvents, (e) => e is OperationExecutedEvent);
            }
Ejemplo n.º 2
0
        public IEventSourcedAggregate Execute(UpdateFakeAggregate cmd)
        {
            var aggregate = _repository.GetById <FakeAggregate>(cmd.Id);

            aggregate.UpdateText(cmd.Text);

            return(aggregate);
        }
Ejemplo n.º 3
0
            public void Should_Retrieve_Aggregate()
            {
                IEventSourcedAggregateRepository <TestAggregate, Guid> repository = Factory.CreateTestAggregateRepository();

                TestAggregate aggregate = new TestAggregate(Guid.NewGuid());

                repository.Save(aggregate);

                TestAggregate fromRepo = repository.GetById(aggregate.Id);

                Assert.NotNull(fromRepo);
                Assert.Equal(aggregate.Id, fromRepo.Id);
            }
        public async Task <CreatePexaWorkspaceResponse> Handle(CreatePexaWorkspaceCommand command, CancellationToken cancellationToken)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            await _validator.ValidateAndThrowAsync(command);

            // Check that the user has valid Actionstep credentials for the given Actionstep Matter in Subscriber Reference
            if (!_wCADbContext.ActionstepCredentials.UserHasValidCredentialsForOrg(command.AuthenticatedUser, command.OrgKey))
            {
                throw new UnauthorizedAccessException($"User {command.AuthenticatedUser.Id} does not have valid Actionstep credentials for org {command.OrgKey} which is specified in the PEXA Subscriber Reference field.");
            }

            // Load Actionstep matter aggregate. SubscriberReference should already be in correct Aggregate Id format
            var actionstepMatter = _actionstepMatterRepository.GetById(command.PexaWorkspaceCreationRequest.SubscriberReference);

            if (actionstepMatter.Id == null)
            {
                actionstepMatter = new ActionstepMatter(_clock.GetCurrentInstant(), command.OrgKey, command.MatterId, command.AuthenticatedUser.Id);
            }

            PexaWorkspaceCreationRequested pexaWorkspaceCreationRequested = null;

            try
            {
                // Mark request for PEXA workspace creation
                pexaWorkspaceCreationRequested = actionstepMatter.RequestPexaWorkspaceCreation(_clock.GetCurrentInstant(), command.AuthenticatedUser.Id);
                _actionstepMatterRepository.Save(actionstepMatter);

                // Call PEXA and attempt to create workspace
                var workspaceCreationResponse = await _pexaService.Handle <WorkspaceCreationResponse>(
                    new WorkspaceCreationRequestCommand(command.PexaWorkspaceCreationRequest, command.AccessToken), command.AuthenticatedUser, cancellationToken);

                var urlSafeId = Uri.EscapeDataString(workspaceCreationResponse.WorkspaceId);

                if (string.IsNullOrEmpty(urlSafeId))
                {
                    throw new PexaUnexpectedErrorResponseException("PEXA Request came back successful but there was no PEXA Workspace ID.");
                }

                // If sucessful, save immediately
                actionstepMatter.MarkPexaWorkspaceCreated(_clock.GetCurrentInstant(), workspaceCreationResponse.WorkspaceId, pexaWorkspaceCreationRequested);
                _actionstepMatterRepository.Save(actionstepMatter);

                var workspaceUri  = _pexaService.GetWorkspaceUri(workspaceCreationResponse.WorkspaceId, command.PexaWorkspaceCreationRequest.Role);
                var invitationUri = _pexaService.GetInvitationUri(workspaceCreationResponse.WorkspaceId, command.PexaWorkspaceCreationRequest.Role);

                return(new CreatePexaWorkspaceResponse(workspaceUri, workspaceCreationResponse.WorkspaceId, invitationUri));
            }
            catch (PexaWorkspaceAlreadyExistsException ex)
            {
                var workspaceUri  = _pexaService.GetWorkspaceUri(ex.WorkspaceId, command.PexaWorkspaceCreationRequest.Role);
                var invitationUri = _pexaService.GetInvitationUri(ex.WorkspaceId, command.PexaWorkspaceCreationRequest.Role);
                return(new CreatePexaWorkspaceResponse(workspaceUri, ex.WorkspaceId, invitationUri, true));
            }
            catch (Exception ex)
            {
                actionstepMatter.MarkPexaWorkspaceCreationFailed(_clock.GetCurrentInstant(), ex.Message, pexaWorkspaceCreationRequested);
                _actionstepMatterRepository.Save(actionstepMatter);
                throw;
            }
        }