public string CreateTenant(TenantViewModel model)
        {
            var tenant = new Tenant()
            {
                Id         = model.Id,
                Created    = model.Created,
                CreatedBy  = model.CreatedBy,
                Modified   = model.Modified,
                ModifiedBy = model.ModifiedBy,
                IsActive   = true,
                Active     = model.Active,

                EditionId        = model.EditionId,
                ConnectionString = model.ConnectionString,
                Name             = model.Name,
                TenancyName      = model.TenancyName,
                Url                  = model.Url,
                IsInTrialPeriod      = model.IsInTrialPeriod,
                SubscriptionEndTime  = model.SubscriptionEndTime,
                LogoId               = model.LogoId,
                LogoFilePath         = model.LogoFilePath,
                NoOfShowroom         = model.NoOfShowroom,
                Package              = model.Package,
                BonusUserAccessCount = 1
            };

            _tenantRepository.CreateAsHost(tenant);
            _tenantRepository.Commit();

            return(model.Id);
        }
        public bool UpdateTenantSubscriptionInfo(string tenantId, string editionId, string subscriptionId)
        {
            var tenant = _repository.GetById(tenantId);

            var currentSubscription = _subscriptionRepository.AsNoTracking().SingleOrDefault(x => x.Id == subscriptionId);

            var subscriptions = _subscriptionRepository.AsNoTracking()
                                .Where(x => x.TenantId == tenantId && x.ExpireOn > DateTime.Today).ToList();

            //int CurrentSubscriptionEquivalentDays(int convertibleDays, int convertibleAmount)
            //{
            //    return convertibleDays / (convertibleAmount * currentSubscription.NoOfShowroom) + 1;
            //}

            //int GetPackageDays(SubscriptionPackage package)
            //{
            //    int packageDays = 0;

            //    if (package == SubscriptionPackage.Trial) packageDays = 14;
            //    else if (package == SubscriptionPackage.Monthly) packageDays = 30;
            //    else if (package == SubscriptionPackage.Quarter) packageDays = 365 / 4;
            //    else if (package == SubscriptionPackage.HalfYearly) packageDays = 365 / 2;
            //    else if (package == SubscriptionPackage.Annual) packageDays = 365;

            //    return packageDays;
            //}

            var days = 0;

            foreach (var subscription in subscriptions)
            {
                if (subscription.ExpireOn.HasValue)
                {
                    var convertibleDays = (subscription.ExpireOn.GetValueOrDefault() - DateTime.Today).Days;

                    days += (convertibleDays * subscription.NoOfShowroom) / currentSubscription.NoOfShowroom + 1;
                }
            }
            tenant.SubscriptionEndTime = DateTime.Today.AddDays(days + 1).AddSeconds(-1);

            if (currentSubscription != null)
            {
                tenant.SubscriptionEndTime = currentSubscription.Package == SubscriptionPackage.LifeTime
                    ? null
                    : tenant.SubscriptionEndTime;

                tenant.SubscriptionId = currentSubscription.Id;
                tenant.NoOfShowroom   = currentSubscription.NoOfShowroom;
                tenant.Package        = currentSubscription.Package;
            }

            tenant.EditionId       = editionId;
            tenant.IsInTrialPeriod = false;


            _repository.EditAsTenant(tenant);

            return(_repository.Commit());
        }
        public async Task <(Tenant, ExceptionKey?)> Add(Tenant tenant, CancellationToken cancelationToken = default(CancellationToken))
        {
            tenant.Name = tenant.DisplayName.Slugify();

            if (await tenantRepository.GetTenantByName(tenant.Name, cancelationToken) != null)
            {
                return(null, ExceptionKey.ERROR_IN_USE);
            }

            tenant.Host = String.Format("{0}.{1}", tenant.Name, configuration["ClientDomain"]);


            tenantRepository.Add(tenant, cancelationToken);

            if (await tenantRepository.Commit(cancelationToken) > 0)
            {
                return(tenant, null);
            }
            else
            {
                return(null, ExceptionKey.ERROR_CREATE);
            }
        }