예제 #1
0
        public void Configuration(IAppBuilder app)
        {
            //Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Migrations.Configuration>());
            //Database.SetInitializer(new MigrateDatabaseToLatestVersion<BusinessDbContext, Model.Migrations.Configuration>());

            app.MapSignalR();

            AuthStartup.Register(app);

            HangfireConfig.RegisterHangfire(app);

            LoggerConfig.Register();

            //BusinessModelSeedDataManager.RunSeed();
            SecurityModelSeedDataManager.RunSeed();
            BusinessModelSeedDataManager.FillTenantSubscription();
            BusinessModelSeedDataManager.CreateFiscalYear(BusinessDbContext.Create());
        }
        public async Task <IHttpActionResult> CreateTenant(TenantViewModel model)
        {
            model.TenancyName = model.Name.ToTenancyName();
            if (!string.IsNullOrWhiteSpace(model.TenancyName))
            {
                ModelState.Remove("model.TenancyName");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            bool isReservedName = _service.IsReservedName(model.TenancyName);

            if (isReservedName)
            {
                return(BadRequest($"Sorry, {model.TenancyName} is reserved. Please select another name"));
            }

            //if (model.SubscriptionEndTime == null) return BadRequest("Subscription end time can not be null. Please select a package.");

            var isTenantExist = _service.IsTenantExist(model.TenancyName);

            if (isTenantExist)
            {
                return(BadRequest("Company name '" + model.Name + "' already exist. Please try with different one."));
            }

            //bool isEmailExist = _tenantManager.IsEmailExist(model.Email);
            //if (isEmailExist) return BadRequest(model.Email + " email address already exist.");

            if (model.PasswordHash != model.RetypePassword)
            {
                return(BadRequest("Password and confirm password could not match."));
            }

            if (string.IsNullOrWhiteSpace(model.Id))
            {
                model.Id = Guid.NewGuid().ToString();
            }
            model.Active = true;

            if (model.IsLifeTimeSubscription)
            {
                model.SubscriptionEndTime = null;
            }

            string tenantId = model.Id;
            string companyId;
            string branchId;
            string subscriptionId;
            string adminRoleId;

            try
            {
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
                {
                    IsolationLevel = IsolationLevel.ReadCommitted,
                    Timeout = TransactionManager.MaximumTimeout
                }))
                {
                    tenantId = _tenantManager.CreateTenant(model);

                    companyId = _tenantManager.CreateTenantCompany(tenantId, model.Name, model.Email, model.PhoneNumber);
                    //_tenantManager.CreateTenantCompanySettings(tenantId, company);

                    branchId = _tenantManager.CreateTenantHeadOfficeBranch(tenantId, companyId);
                    //_tenantManager.CreateTenantHeadOfficeWarehouse(tenantId, companyId);

                    subscriptionId = _tenantManager.CreateTenantSubscription(model, tenantId, companyId, branchId, model.IsLifeTimeSubscription);

                    _tenantManager.UpdateSubscriptionId(tenantId, subscriptionId);

                    //_tenantManager.CreateSupplier(tenantId, tenantCompanyId);


                    adminRoleId = _tenantManager.CreateTenantRole(tenantId, companyId);

                    scope.Complete();
                }
            }
            catch (Exception e)
            {
                Serilog.Log.Error(e.ToString());

                await _tenantManager.RollbackAsync(tenantId);

                return(BadRequest("Registration failed! please try again."));
            }


            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
                {
                    IsolationLevel = IsolationLevel.ReadCommitted,
                    Timeout = TimeSpan.MaxValue
                }))
                {
                    var userId = _tenantManager.CreateTenantAdminUser(model, tenantId, companyId, branchId, adminRoleId);
                    _tenantManager.CreateTenantAdminPermission(tenantId);

                    BusinessModelSeedDataManager.CreateFiscalYear(BusinessDbContext.Create(), tenantId);

                    scope.Complete();

                    var smsResponseModel = _smsService.SendOneToOneSingleSmsUsingApi(model.PhoneNumber, SmsHelper.TenantRegistrationMessage);

                    _tenantManager.ConfirmCompanyMobileNumber(smsResponseModel, companyId);
                    await _tenantManager.ConfirmAdminMobileNumberAsync(smsResponseModel, userId);
                }
            }
            catch (Exception e)
            {
                Serilog.Log.Error(e.ToString());

                await _tenantManager.RollbackAsync(tenantId);

                return(BadRequest("Registration failed! please try again."));
            }


            try
            {
                if (string.IsNullOrWhiteSpace(companyId) || string.IsNullOrWhiteSpace(tenantId))
                {
                    throw new Exception("Company Id or Tenant Id can not be null");
                }

                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,
                                                                     new TransactionOptions()
                {
                    IsolationLevel = IsolationLevel.ReadCommitted,
                    Timeout = TransactionManager.MaximumTimeout
                }))
                {
                    scope.Complete();
                }
            }
            catch (Exception e)
            {
                Serilog.Log.Error(e.ToString());

                await _tenantManager.RollbackAsync(tenantId);

                return(BadRequest("Registration failed! please try again."));
            }



            return(Ok(model));
        }