public async Task GIVEN_query_with_ClientId_set_EXPECT_only_capabilities_with_given_id()
        {
            // Arrange
            var idOfWantedSender     = Guid.NewGuid();
            var capabilityRepository = new StubCapabilityRepository();
            await capabilityRepository.Add(Capability.Create(
                                               idOfWantedSender,
                                               "foo",
                                               "slackChannelId",
                                               "slackUserGroupId"
                                               ));

            await capabilityRepository.Add(Capability.Create(
                                               idOfWantedSender,
                                               "bar",
                                               "slackChannelId",
                                               "slackUserGroupId"
                                               ));

            await capabilityRepository.Add(Capability.Create(
                                               Guid.NewGuid(),
                                               "sheep",
                                               "slackChannelId",
                                               "slackUserGroupId"
                                               ));

            var slackFacadeSpy = new SlackFacadeSpy();

            var sut = new FindConnectionsByClientTypeClientIdChannelTypeChannelIdHandler(capabilityRepository, slackFacadeSpy);

            var clientIdOfWantedSender = new ClientId(idOfWantedSender.ToString());
            var query = new FindConnectionsByClientTypeClientIdChannelTypeChannelId(
                new ClientTypeCapability(),
                clientIdOfWantedSender,
                new ChannelTypeSlack(),
                null
                );


            // Act
            var results = await sut.HandleAsync(query);

            // Assert
            Assert.All(
                results,
                connection =>
                Assert.Equal(clientIdOfWantedSender, connection.ClientId)
                );
        }
Exemple #2
0
        public async Task <IActionResult> GetConnections(
            [FromQuery] string clientType,
            [FromQuery] string clientId,
            [FromQuery] string channelType,
            [FromQuery] string channelId
            )
        {
            var query = new FindConnectionsByClientTypeClientIdChannelTypeChannelId();

            if (!string.IsNullOrEmpty(clientType))
            {
                query.ClientType = ClientType.Create(clientType);
            }

            if (!string.IsNullOrEmpty(clientId))
            {
                query.ClientId = ClientId.Create(clientId);
            }

            if (!string.IsNullOrEmpty(channelType))
            {
                query.ChannelType = ChannelType.Create(channelType);
            }

            if (!string.IsNullOrEmpty(channelId))
            {
                query.ChannelId = ChannelId.Create(channelId);
            }

            IEnumerable <Connection> connections;

            try
            {
                connections =
                    await _findConnectionsByClientTypeClientIdChannelTypeChannelIdQueryHandler.HandleAsync(query);
            }
            catch (ValidationException validationException)
            {
                return(StatusCode(
                           (int)HttpStatusCode.UnprocessableEntity,
                           new { message = validationException.MessageToUser }
                           ));
            }

            var connectionDtos = connections.Select(ConnectionDto.CreateFromConnection);

            return(Ok(new ItemsEnvelope <ConnectionDto>(connectionDtos)));
        }
        private async Task <IEnumerable <Capability> > QueryCapabilities(
            FindConnectionsByClientTypeClientIdChannelTypeChannelId query)
        {
            if (query.ChannelType != null && !query.ChannelType.IsEmpty && !(query.ChannelType is ChannelTypeSlack))
            {
                return(Enumerable.Empty <Capability>());
            }

            if (query.ClientType != null && !query.ClientType.IsEmpty && !(query.ClientType is ClientTypeCapability))
            {
                return(Enumerable.Empty <Capability>());
            }

            var capabilities = await _capabilityRepository.GetAll();

            if (query.ChannelId != null && !query.ChannelId.IsEmpty)
            {
                capabilities = capabilities
                               .Where(c =>
                                      c.SlackChannelId.Equals(query.ChannelId)
                                      );
            }

            if (query.ClientId == null || query.ClientId.IsEmpty)
            {
                return(capabilities);
            }


            Guid capabilityId;
            var  capabilityIdIsValid = Guid.TryParse(query.ClientId, out capabilityId);

            if (capabilityIdIsValid == false)
            {
                throw new ValidationException(
                          $"The given capability id: '{query.ClientId}' is not valid. Expected format is Guid with 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).");
            }

            capabilities = capabilities
                           .Where(c =>
                                  c.Id.Equals(capabilityId));


            return(capabilities);
        }
        public async Task GIVEN_query_with_SenderType_not_equal_to_SenderTypeCapability_EXPECT_empty_result()
        {
            // Arrange
            var sut   = new FindConnectionsByClientTypeClientIdChannelTypeChannelIdHandler(null, null);
            var query = new FindConnectionsByClientTypeClientIdChannelTypeChannelId(
                new ClientTypeTest(),
                null,
                null,
                null
                );

            // Act
            var results = await sut.HandleAsync(query);


            // Assert
            Assert.Empty(results);
        }
        public async Task <IEnumerable <Connection> > HandleAsync(
            FindConnectionsByClientTypeClientIdChannelTypeChannelId query
            )
        {
            var capabilities = await QueryCapabilities(query);

            if (capabilities.Any() == false)
            {
                return(Enumerable.Empty <Connection>());
            }

            var ConversationIds = capabilities.Select(c => c.SlackChannelId.ToString());

            var channelDtos = await GetSlackConversationsByIds(ConversationIds);

            var connections = MergeIntoConnections(capabilities, channelDtos);

            return(connections);
        }
        public async Task GIVEN_empty_query_EXPECT_connections_for_all_capabilities()
        {
            // Arrange
            var capabilityRepository = new StubCapabilityRepository();
            await capabilityRepository.Add(Capability.Create(
                                               Guid.NewGuid(),
                                               "foo",
                                               "slackChannelId",
                                               "slackUserGroupId"
                                               ));

            await capabilityRepository.Add(Capability.Create(
                                               Guid.NewGuid(),
                                               "bar",
                                               "slackChannelId",
                                               "slackUserGroupId"
                                               ));

            var slackFacadeSpy = new SlackFacadeSpy();

            slackFacadeSpy.Conversations.AddRange(new [] { new ChannelDto {
                                                               Id = "slackChannelId", Name = "slackChannelName"
                                                           } });
            var sut = new FindConnectionsByClientTypeClientIdChannelTypeChannelIdHandler(capabilityRepository, slackFacadeSpy);

            var query = new FindConnectionsByClientTypeClientIdChannelTypeChannelId(
                null,
                null,
                null,
                null
                );

            // Act
            var results = await sut.HandleAsync(query);

            Assert.All(
                capabilityRepository.GetAll().Result,
                capability =>
                results.Single(r =>
                               r.ClientId.ToString() == capability.Id.ToString()
                               )
                );
        }
Exemple #7
0
        public async Task <IActionResult> DeleteConnection(
            [FromQuery] string clientType,
            [FromQuery] string clientId,
            [FromQuery] string channelType,
            [FromQuery] string channelId)
        {
            if (string.IsNullOrEmpty(clientId))
            {
                return(BadRequest(new { message = "ClientId ID is required." }));
            }

            ClientType  clientTypeValueObject;
            ChannelType channelTypeValueObject;

            try
            {
                clientTypeValueObject  = string.IsNullOrEmpty(clientType) ? null : (ClientType)clientType;
                channelTypeValueObject = string.IsNullOrEmpty(channelType) ? null : (ChannelType)channelType;
            }
            catch (ValidationException validationException)
            {
                return(StatusCode(
                           (int)HttpStatusCode.UnprocessableEntity,
                           new { message = validationException.MessageToUser }
                           ));
            }

            var getMatchedConnectionsQuery = new FindConnectionsByClientTypeClientIdChannelTypeChannelId(
                clientTypeValueObject,
                (ClientId)clientId,
                channelTypeValueObject,
                (ChannelId)channelId);

            var matchedConnections =
                await _findConnectionsByClientTypeClientIdChannelTypeChannelIdQueryHandler.HandleAsync(
                    getMatchedConnectionsQuery);

            foreach (var connection in matchedConnections)
            {
                await _slackFacade.LeaveChannel(connection.ChannelId.ToString());

                var getAllChannelConnectionsQuery = new FindConnectionsByClientTypeClientIdChannelTypeChannelId(
                    null,
                    null,
                    connection.ChannelType,
                    connection.ChannelId);

                var allChannelConnections =
                    await _findConnectionsByClientTypeClientIdChannelTypeChannelIdQueryHandler.HandleAsync(
                        getAllChannelConnectionsQuery);

                var capability = Capability.Create(Guid.Parse(connection.ClientId), connection.ClientName,
                                                   connection.ChannelId, "");
                await _capabilityRepository.Remove(capability);

                if (allChannelConnections.All(c => c.ClientId.ToString().Equals(clientId)))
                {
                    var channelsAll = await _slackFacade.GetConversations();

                    var channelsWhereConnectionIdAndChannelCreatorMatches = channelsAll.Channels.Where(ch =>
                                                                                                       ch.Creator.Equals(_slackFacade.GetBotUserId())
                                                                                                       &&
                                                                                                       ch.Id.Equals(connection.ChannelId));

                    if (channelsWhereConnectionIdAndChannelCreatorMatches.Any())
                    {
                        await _slackFacade.ArchiveChannel(connection.ChannelId.ToString());
                    }
                }
            }

            return(Accepted());
        }