public async Task AddLocationAsync(CreateLocationViewModel newLocation, EntityHeader addedByUser) { ValidationCheck(newLocation, Core.Validation.Actions.Create); var location = new OrganizationLocation(); location.SetId(); var currentUser = await _appUserRepo.FindByIdAsync(addedByUser.Id); var organization = await _organizationRepo.GetOrganizationAsync(newLocation.OrganizationId); location.Organization = organization.ToEntityHeader(); if (EntityHeader.IsNullOrEmpty(location.AdminContact)) { location.AdminContact = addedByUser; } if (EntityHeader.IsNullOrEmpty(location.TechnicalContact)) { location.TechnicalContact = addedByUser; } SetCreatedBy(location, addedByUser); await _locationRepo.AddLocationAsync(location); }
public async Task CreateNewOrganizationAsync(CreateOrganizationViewModel organizationViewModel, EntityHeader user) { ValidationCheck(organizationViewModel, Core.Validation.Actions.Create); //HACK: Very, small chance, but it does exist...two entries could be added at exact same time and check would fail...need to make progress, can live with rish for now. if (await _organizationRepo.QueryNamespaceInUseAsync(organizationViewModel.Namespace)) { throw new ValidationException(Resources.UserManagementResources.Organization_CantCreate, false, UserManagementResources.Organization_NamespaceInUse.Replace(Tokens.NAMESPACE, organizationViewModel.Namespace)); } var organization = new Organization(); organization.SetId(); organization.SetCreationUpdatedFields(user); organizationViewModel.MapToOrganization(organization); organization.Status = UserManagementResources.Organization_Status_Active; organization.TechnicalContact = user; organization.AdminContact = user; organization.BillingContact = user; var location = new OrganizationLocation(); location.SetId(); organizationViewModel.MapToOrganizationLocation(location); location.SetCreationUpdatedFields(user); location.AdminContact = user; location.TechnicalContact = user; location.Organization = organization.ToEntityHeader(); organization.PrimaryLocation = location.ToEntityHeader(); if (organization.Locations == null) { organization.Locations = new List <EntityHeader>(); } organization.Locations.Add(location.ToEntityHeader()); var currentUser = await _appUserRepo.FindByIdAsync(user.Id); var locationUser = new LocationAccount(organization.Id, location.Id, user.Id) { Email = currentUser.Email, OrganizationName = organization.Name, AccountName = currentUser.Name, ProfileImageUrl = currentUser.ProfileImageUrl.ImageUrl, LocationName = location.Name }; locationUser.SetCreationUpdatedFields(user); await _organizationRepo.AddOrganizationAsync(organization); await AddAccountToOrgAsync(currentUser.ToEntityHeader(), organization.ToEntityHeader(), currentUser.ToEntityHeader()); await _locationRepo.AddLocationAsync(location); await _locationAccountRepo.AddAccountToLocationAsync(locationUser); if (EntityHeader.IsNullOrEmpty(currentUser.CurrentOrganization)) { currentUser.CurrentOrganization = organization.ToEntityHeader(); } if (currentUser.Organizations == null) { currentUser.Organizations = new List <EntityHeader>(); } currentUser.Organizations.Add(organization.ToEntityHeader()); await _appUserRepo.UpdateAsync(currentUser); }