public async Task <IActionResult> CreateOrganizationConfigurationManagement(Guid organizationId, [FromBody] OrganizationCMSPostRp organizationCMSRp)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(ModelState));
            }

            if ((organizationCMSRp.Type == ConfigurationManagementService.VSTS ||
                 organizationCMSRp.Type == ConfigurationManagementService.Bitbucket) && string.IsNullOrEmpty(organizationCMSRp.AccessSecret))
            {
                if (organizationCMSRp.Type == ConfigurationManagementService.VSTS)
                {
                    ModelState.AddModelError("", "Personal Access Token is required");
                }

                if (organizationCMSRp.Type == ConfigurationManagementService.Bitbucket)
                {
                    ModelState.AddModelError("", "App Password is required");
                }

                return(this.BadRequest(ModelState));
            }

            await _organizationCMSService.CreateConfigurationManagementService(organizationId, organizationCMSRp);

            if (_domainManagerService.HasNotFounds())
            {
                return(this.Forbidden(_domainManagerService.GetNotFounds()));
            }

            if (_domainManagerService.HasForbidden())
            {
                return(this.Forbidden(_domainManagerService.GetForbidden()));
            }

            if (_domainManagerService.HasConflicts())
            {
                return(this.Conflict(_domainManagerService.GetConflicts()));
            }

            return(this.Ok());
        }
        public async Task CreateConfigurationManagementService(Guid organizationId, OrganizationCMSPostRp resource)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to create settings in this organization.");

                return;
            }

            //OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceByType(resource.Type);
            //if (organizationCMS != null && organizationCMS.ConnectionType == resource.ConnectionType)
            //{
            //    await _domainManagerService.AddConflict($"The configuration management service with type {resource.Type} already exists.");
            //    return;
            //}

            OrganizationCMS existingCMP = organization.GetConfigurationManagementServiceByName(resource.Name);

            if (existingCMP != null)
            {
                await _domainManagerService.AddConflict($"The configuration management service {resource.Name} has already been taken.");

                return;
            }

            //existing same connection in other account
            OrganizationCMS existingInOtherOrganization = await _organizationCMSRepository.FindOrganizationCMSByTypeAndAccountName(resource.Type, resource.AccountName);

            if (existingInOtherOrganization != null)
            {
                await _domainManagerService.AddConflict($"The configuration management service {resource.Type}/{resource.AccountName} has already been taken in other organization.");

                return;
            }

            user.AddConfigurationManagementService(organizationId,
                                                   resource.Name,
                                                   resource.Type,
                                                   resource.ConnectionType,
                                                   _dataProtectorService.Protect(resource.AccountId),
                                                   _dataProtectorService.Protect(resource.AccountName),
                                                   _dataProtectorService.Protect(resource.AccessId),
                                                   _dataProtectorService.Protect(resource.AccessSecret),
                                                   _dataProtectorService.Protect(resource.AccessToken));

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }