public async Task <SystemTenant> Register(SystemTenantModel model) { SystemTenant tenant = null; // If tenant exists in container, return. if (await TenantExists(model.Moniker)) { throw new MonikerAlreadyExistsException(model.Moniker.ToUpper()); } // Get all lookup items. List <LookupGroupEntity> systemLookupItems = (await _systemLookupItemService.GetItems()).ToList(); // Get current subscription. Subscription currentSubscription = await _systemSubscriptionService.GetItem(model.SubscriptionId); // Create and populate tenant object. tenant = new SystemTenant(model, currentSubscription, systemLookupItems); tenant = await _systemTenantManager.CreateItemAsync(tenant); return(tenant); }
public async Task <IActionResult> Get() { try { var model = await _systemLookupItemService.GetItems(); responseModels.Add("Lookup Items", model); response = new ApiResponse(HttpStatusCode.OK, "System LookupItems found.", null, responseModels); return(Ok(new { response })); } catch (SystemLookupItemsNotFoundException exception) { response = new ApiResponse(HttpStatusCode.Conflict, "System LookupItems not found.", null); return(Ok(new { response })); } catch (Exception exception) { return(BadRequest("Error setting up tenant service desk. Error: " + exception.Message)); } }
public async Task <CustomerModel> Create(CustomerModel model) { model = await Validate(model); model.AdminMoniker = model.AdminMoniker.ToUpper(); model.TenantMonikers = new List <string>() { model.AdminMoniker }; // Create customer entity CustomerEntity customerEntity = new CustomerEntity(model); // Create customer item in system database. customerEntity = await _customersManager.CreateItemAsync(customerEntity); // Create customer admin database. DatabaseResponse databaseResponse = await _adminService.CreateDatabase(); if (databaseResponse.StatusCode != HttpStatusCode.Created) { throw new SystemDatabaseNotCreatedException(); } // Clone 'LookupItems' container and clone items from system database. var containerResponse = await _adminService.CreateContainer("LookupItems", "/canonicalName"); if (containerResponse.StatusCode != HttpStatusCode.Created) { throw new AdminContainerNotCreatedException("LookupItems"); } var systemLookupItemModels = await _systemLookupItemService.GetItems(); var systemLookupItems = SystemLookupItem.Construct(systemLookupItemModels); foreach (SystemLookupItem systemLookupItem in systemLookupItems) { if (systemLookupItem.CloneToAdminDatabase) { var adminLookupItemModel = new AdminLookupItemModel(systemLookupItem); await _adminLookupItemsService.CreateItem(adminLookupItemModel); } } // Create 'Users' container and clone items from system database. containerResponse = await _adminService.CreateContainer("Users", "/username"); if (containerResponse.StatusCode != HttpStatusCode.Created) { throw new AdminContainerNotCreatedException("Users"); } var systemAuthenticateUsers = await _systemUserService.GetItems(); foreach (SystemAuthenticateUser systemAuthenticateUser in systemAuthenticateUsers) { if (systemAuthenticateUser.CloneToAdminDatabase) { AdminAuthenticateUserModel adminAuthenticateUserModel = new AdminAuthenticateUserModel(systemAuthenticateUser); await _adminUsersService.CreateItem(adminAuthenticateUserModel); } } // Create customer 'admin' user. foreach (AdminAuthenticateUserModel adminUserModel in model.Users) { adminUserModel.NameFirst = adminUserModel.NameFirst.Replace("{moniker}", model.AdminMoniker.ToUpper()); adminUserModel.Username = adminUserModel.Username.Replace("{moniker}", model.AdminMoniker.ToLower()); adminUserModel.DisplayAs = adminUserModel.DisplayAs.Replace("{moniker}", model.AdminMoniker.ToUpper()); adminUserModel.EmailAddresses.ForEach(x => x.Address = x.Address.Replace("{moniker}", model.AdminMoniker.ToLower())); //for (int i = 0; i < adminUserModel.Roles.Count; i++) adminUserModel.Roles[i] = adminUserModel.Roles[i].Replace("{moniker}", model.AdminMoniker.ToUpper()); var randomWord = await Helpers.GetRandomWordFromWordsApi(); var joPassword = JObject.Parse(randomWord)["word"]; adminUserModel.Password = _hashingService.EncryptString(joPassword.ToString()); adminUserModel.EmailAddresses = await _adminEmailAddressService.Validate(adminUserModel.EmailAddresses); adminUserModel.PhoneNumbers = await _adminPhoneNumberService.Validate(adminUserModel.PhoneNumbers); await _adminUsersService.CreateItem(adminUserModel); } // Create Tenants container. containerResponse = await _adminService.CreateContainer("Tenants", "/moniker"); if (containerResponse.StatusCode != HttpStatusCode.Created) { throw new AdminContainerNotCreatedException("Tenants"); } model = new CustomerModel(customerEntity); return(model); }