public async Task <IActionResult> Register([FromBody] TenantRegistrationModel model) { try { await _tenantRegistrationService.Register(model); response = new ApiResponse(HttpStatusCode.Created, string.Format("Tenant with moniker '{0}' created successfully.", model.Moniker), null); return(Ok(new { response })); } catch (CustomerNotFoundException exception) { response = new ApiResponse(HttpStatusCode.BadRequest, string.Format("Tenant with moniker '{0}' not found in system DB.", model.Moniker), exception, null); return(BadRequest(new { response })); } catch (Exception exception) { response = new ApiResponse(HttpStatusCode.BadRequest, "Tenant setup failed.", exception, null); return(BadRequest(new { response })); } }
public async Task Register(TenantRegistrationModel model) { //Subscription systemSubscription = await _systemSubscriptionService.GetItem(model.SystemTenant.Subscription.Id); //await _systemTenantsService.Create(model.SystemTenant); //if (await _systemSubscriptionService.NotFound(model.SystemTenant.Subscription.Id)) throw new SubscriptionNotFoundException(model.SystemTenant.Subscription.Id); //CustomerEntity systemTenant = await _systemTenantsService.Get(model.Moniker); //DatabaseResponse databaseResponse = await _cosmosDbService.CreateDatabase(systemTenant); //if (databaseResponse.StatusCode != HttpStatusCode.Created && databaseResponse.StatusCode != HttpStatusCode.OK) throw new TenantRegistrationDatabaseCouldNotBeCreatedException(model.Moniker); //// Retrieve tenant containers to create. //var containers = _configuration.GetSection("tenant:setup:containers:create").Get<List<DatabaseContainer>>(); // from tenant-setup.json //if (!containers.Any()) throw new TenantRegistrationNoContainersFoundToCreateException(); //// Retrieve lookup groups to omit. //var omitLookupGroups = _configuration.GetSection("tenant:setup:lookupGroups:omit").GetChildren().Select(x => x.Value).ToList(); // from tenant-setup.json //foreach (DatabaseContainer container in containers) //{ // ContainerResponse containerResponse = await _cosmosDbService.CreateContainer(databaseResponse.Database, container.Name, container.PartitionKey); // if (containerResponse.StatusCode != HttpStatusCode.Created && containerResponse.StatusCode != HttpStatusCode.OK) throw new TenantRegistrationCreateContainerFailedException(container.Name); // if (container.CloneItems) // { // // Populate lookup items from system database. // List<LookupGroupEntity> lookupGroups = (await _systemLookupItemService.GetItems()).ToList(); // foreach (LookupGroupEntity group in lookupGroups) // { // // Skip over groups to ommit. // if (omitLookupGroups.Contains(group.Group)) continue; // // Overwrite existing ID with new GUID unique to tenant. // group.Items.ToList().ForEach(x => x.Id = Guid.NewGuid().ToString()); // // Persist item. // await _tenantLookupItemService.CreateItem(group); // } // } //} //// Create tenant subscription. //TenantSubscriptionModel tenantSubscriptionModel = new TenantSubscriptionModel(systemSubscription, DateTime.Now) { IsActive = true }; //TenantSubscription tenantSubscription = new TenantSubscription(tenantSubscriptionModel); //tenantSubscription = await _tenantSubscriptionService.CreateItem(tenantSubscription); //// Create tenant users. //var users = _configuration.GetSection("tenant:setup:users").Get<List<UserModel>>(); // from tenant-setup.json //// Persist tenant users. //foreach (UserModel user in users) //{ // user.Id = Guid.NewGuid().ToString(); // user.EmployeeId = user.Username == "tangled.admin" ? user.EmployeeId.Replace("{moniker}", model.Moniker) : await _tenantUserService.GetUniqueEmployeeId(model.Moniker); // user.Username = user.Username.Replace("{moniker}", model.Moniker); // user.Password = _hashingService.EncryptString(user.Password); // // Complete each email address. // foreach (EmailAddressModel emailAddress in user.EmailAddresses) // { // emailAddress.Address = emailAddress.Address.Replace("{moniker}", model.Moniker); // emailAddress.Type = await _tenantLookupItemService.GetItem("Email Address Types", emailAddress.Type.Id); // } // // Complete each phone number. // foreach (PhoneNumberModel phoneNumber in user.PhoneNumbers) // { // phoneNumber.Type = await _tenantLookupItemService.GetItem("Phone Number Types", phoneNumber.Type.Id); // } // Entities.User userEntity = new Entities.User(user); // // Persist item. // await _tenantUserService.CreateItem(userEntity); //} }