Esempio n. 1
0
        public async Task <IntegrationClientCreateResponse> CreateIntegrationClient(IntegrationClientCreateRequest request)
        {
            var response = new IntegrationClientCreateResponse();

            var currentUser = _cacheManager.GetCachedCurrentUser(request.CurrentUserId);

            if (!currentUser.IsAdmin)
            {
                response.SetInvalid();
                return(response);
            }

            if (await _organizationRepository.Any(x => x.Id == currentUser.OrganizationId && !x.IsActive))
            {
                response.SetInvalidBecauseNotActive("organization");
                return(response);
            }

            var integration = await _integrationRepository.Select(x => x.Uid == request.IntegrationUid);

            if (integration.IsNotExist())
            {
                response.SetInvalidBecauseNotFound("integration");
                return(response);
            }

            if (integration.OrganizationId != currentUser.OrganizationId)
            {
                response.SetInvalid();
                return(response);
            }

            var entity = _integrationClientFactory.CreateEntity(integration);
            await _integrationClientRepository.Insert(request.CurrentUserId, entity);

            response.Item   = _integrationClientFactory.CreateDtoFromEntity(entity);
            response.Status = ResponseStatus.Success;
            return(response);
        }
Esempio n. 2
0
        public async Task <(bool, Organization, User)> DoWork(Organization organization, User user, UserLoginLog userLoginLog,
                                                              Integration integration, IntegrationClient integrationClient, Project project)
        {
            var(organizationResult,
                userResult) = await _transactionalExecutor.ExecuteAsync(async connection =>
            {
                _userRepository.SetSqlExecutorForTransaction(connection);
                _organizationRepository.SetSqlExecutorForTransaction(connection);
                _userLoginLogRepository.SetSqlExecutorForTransaction(connection);
                _integrationRepository.SetSqlExecutorForTransaction(connection);
                _integrationClientRepository.SetSqlExecutorForTransaction(connection);
                _projectRepository.SetSqlExecutorForTransaction(connection);

                var organizationId = await _organizationRepository.Insert(0, organization);
                organization.Id    = organizationId;

                user.OrganizationId = organizationId;
                var userId          = await _userRepository.Insert(0, user);
                user.Id             = userId;

                userLoginLog.OrganizationId = organizationId;
                userLoginLog.UserId         = userId;
                await _userLoginLogRepository.Insert(userId, userLoginLog);

                organization.CreatedBy = userId;
                organization.UserCount++;
                user.CreatedBy = userId;
                await _organizationRepository.Update(userId, organization);
                await _userRepository.Update(userId, user);

                integration.OrganizationId = organizationId;
                var integrationId          = await _integrationRepository.Insert(userId, integration);

                integrationClient.OrganizationId = organizationId;
                integrationClient.IntegrationId  = integrationId;
                await _integrationClientRepository.Insert(userId, integrationClient);

                project.OrganizationId = organizationId;
                var projectId          = await _projectRepository.Insert(userId, project);

                if (!organization.IsSuperOrganization)
                {
                    var superProject = await _projectRepository.Select(x => x.IsSuperProject);
                    if (superProject.IsExist())
                    {
                        var labels            = await _labelRepository.SelectAll(x => x.ProjectId == superProject.Id);
                        var labelTranslations = await _labelTranslationRepository.SelectAll(x => x.ProjectId == superProject.Id);

                        for (var i = 0; i < labels.Count; i++)
                        {
                            var label              = labels[i];
                            label.Uid              = Guid.NewGuid();
                            label.OrganizationId   = project.OrganizationId;
                            label.OrganizationUid  = project.OrganizationUid;
                            label.OrganizationName = project.OrganizationName;
                            label.ProjectId        = projectId;
                            label.ProjectUid       = project.Uid;
                            label.ProjectName      = project.Name;

                            var labelId = await _labelRepository.Insert(userId, label);

                            var labelsTranslations = labelTranslations.Where(x => x.LabelName == label.Name).ToList();
                            for (var j = 0; j < labelsTranslations.Count; j++)
                            {
                                var labelTranslation              = labelsTranslations[j];
                                labelTranslation.Uid              = Guid.NewGuid();
                                labelTranslation.OrganizationId   = project.OrganizationId;
                                labelTranslation.OrganizationUid  = project.OrganizationUid;
                                labelTranslation.OrganizationName = project.OrganizationName;
                                labelTranslation.ProjectId        = projectId;
                                labelTranslation.ProjectUid       = project.Uid;
                                labelTranslation.ProjectName      = project.Name;
                                labelTranslation.LabelId          = labelId;
                                labelTranslation.LabelUid         = label.Uid;
                                labelTranslation.LabelName        = label.Name;

                                await _labelTranslationRepository.Insert(userId, labelTranslation);
                            }
                        }
                    }
                }

                return(organization,
                       user);
            });

            return(true,
                   organizationResult,
                   userResult);
        }