Exemplo n.º 1
0
        public async Task <ActionResultResponse> Insert(string currentTenant, TenantMeta tenantMeta)
        {
            if (tenantMeta.Languages == null || !tenantMeta.Languages.Any())
            {
                return(new ActionResultResponse(-1, _resourceService.GetString("Please select at least one language.")));
            }

            var tenantId = Guid.NewGuid().ToString();
            var isExists = await _tenantRepository.CheckExists(tenantId, tenantMeta.PhoneNumber, tenantMeta.Email);

            if (isExists)
            {
                return(new ActionResultResponse(-2, _resourceService.GetString("Email or phone number already exists.")));
            }

            var tenant = new Tenant(tenantId, tenantMeta.Name, tenantMeta.Email, tenantMeta.PhoneNumber, tenantMeta.Address,
                                    tenantMeta.IsActive, tenantMeta.Note, tenantMeta.Logo);
            var result = await _tenantRepository.Insert(tenant);

            if (result <= 0)
            {
                return(new ActionResultResponse(-3, _sharedResourceService.GetString("Something went wrong. Please contact with administrator.")));
            }

            // update userId
            var userAccount = await _userAccountRepository.GetInfo(currentTenant, tenantMeta.UserId, false);

            if (userAccount == null)
            {
                return(new ActionResultResponse(-4, _resourceService.GetString("user account does not exist")));
            }

            userAccount.TenantId = tenant.Id;
            await _userAccountRepository.UpdateUserAccount(userAccount);

            //insert tenant page
            foreach (var tenantPage in tenantMeta.Pages)
            {
                var tenantPages = new TenantPage();
                tenantPages.TenantId = tenant.Id;
                tenantPages.PageId   = tenantPage.PageId;
                await _tenantPageRepository.Insert(tenantPages);
            }

            //update UpserRole admin strator
            var userRoleExist = await _userRoleRepository.CheckExist(tenantMeta.UserId);

            if (userRoleExist)
            {
                await _userRoleRepository.DeleteByUserId(tenantMeta.UserId);
            }


            var insertRoleUser = await _userRoleRepository.Insert(tenantMeta.UserId, "SuperAdministrator");

            if (insertRoleUser < 0)
            {
                return(new ActionResultResponse(-5, _resourceService.GetString("insert user role does not success")));
            }

            // Insert tenant languages.
            await InsertTenantLanguage(tenantId, tenantMeta.Languages);

            return(new ActionResultResponse(result, result <= 0
                ? _sharedResourceService.GetString("Something went wrong. Please contact with administrator.")
                : _resourceService.GetString("Add new tenant successful.")));
        }