public async Task SyncPermissionsAsync() { var sites = await _siteRepository.GetAllAsync(); if (sites.Any()) { var adminRole = await _roleRepository.GetByIdAsync(1); if (adminRole != null) { if (!adminRole.IsAdmin) { _logger.LogError("Role ID 1 is not set as IsAdmin, fixing."); adminRole.IsAdmin = true; await _roleRepository.UpdateSaveAsync(-1, adminRole); } } else { _logger.LogError("Cannot find role ID 1 in order to ensure it is marked as IsAdmin"); } var permissionList = Enum.GetValues(typeof(Permission)) .Cast <Permission>() .Select(_ => _.ToString()); var currentPermissions = await _roleRepository.GetAllPermissionsAsync(); var permissionsToAdd = permissionList.Except(currentPermissions); var permissionsToRemove = currentPermissions.Except(permissionList); if (permissionsToAdd.Any() || permissionsToRemove.Any()) { if (permissionsToAdd.Any()) { var userId = await _siteLookupService.GetSystemUserId(); await _roleRepository.AddPermissionListAsync(userId, permissionsToAdd); } if (permissionsToRemove.Any()) { _roleRepository.RemovePermissionList(permissionsToRemove); } await _roleRepository.SaveAsync(); } } }
public async Task <int> GetSpatialDistanceIdForGeolocationAsync(string geolocation) { var siteId = GetCurrentSiteId(); var headerId = await _spatialDistanceRepository .GetIdByGeolocationAsync(siteId, geolocation); if (headerId.HasValue) { return(headerId.Value); } var spatialDistanceHeader = new SpatialDistanceHeader { CreatedAt = _dateTimeProvider.Now, CreatedBy = await _siteLookupService.GetSystemUserId(), Geolocation = geolocation, IsValid = true, SiteId = siteId }; var geocordinates = geolocation.Split(',').Select(_ => double.Parse(_)).ToArray(); var latitude = geocordinates[0]; var longitude = geocordinates[1]; var spatialDistanceDetailList = new List <SpatialDistanceDetail>(); var branches = await _branchRepository.GetAllAsync(siteId, true); foreach (var branch in branches) { var branchGeolocation = branch.Geolocation.Split(',') .Select(_ => double.Parse(_)).ToArray(); var distance = GetHaversineDistance(latitude, longitude, branchGeolocation[0], branchGeolocation[1]); var spatialDistanceDetail = new SpatialDistanceDetail { BranchId = branch.Id, Distance = distance }; spatialDistanceDetailList.Add(spatialDistanceDetail); } var locations = await _locationRepository.GetAll(siteId, true); foreach (var location in locations) { var locationGeolocation = location.Geolocation.Split(',') .Select(_ => double.Parse(_)).ToArray(); var distance = GetHaversineDistance(latitude, longitude, locationGeolocation[0], locationGeolocation[1]); var spatialDistanceDetail = new SpatialDistanceDetail { LocationId = location.Id, Distance = distance }; spatialDistanceDetailList.Add(spatialDistanceDetail); } spatialDistanceHeader = await _spatialDistanceRepository.AddHeaderWithDetailsListAsync( spatialDistanceHeader, spatialDistanceDetailList); return(spatialDistanceHeader.Id); }
public async Task SyncLanguagesAsync() { var siteCultures = _l10nOptions .Value .SupportedCultures; var databaseCultures = await _languageRepository.GetAllAsync(); foreach (var dbCulture in databaseCultures) { var siteCulture = siteCultures.SingleOrDefault(_ => _.Name == dbCulture.Name); if (siteCulture == null && dbCulture.IsActive) { // no longer active _logger.LogInformation("Marking language {Name} inactive in the database.", dbCulture.Name); dbCulture.IsActive = false; dbCulture.IsDefault = dbCulture.Name == Culture.DefaultName; await _languageRepository.UpdateSaveNoAuditAsync(dbCulture); } else if (siteCulture != null && !dbCulture.IsActive) { // valid but marked invalid in the database _logger.LogInformation("Marking language {Name} as active in the database.", dbCulture.Name); dbCulture.IsActive = true; dbCulture.IsDefault = dbCulture.Name == Culture.DefaultName; await _languageRepository.UpdateSaveNoAuditAsync(dbCulture); } else { bool doSave = false; // ensure default is set properly if ((dbCulture.IsDefault && dbCulture.Name != Culture.DefaultName) || (!dbCulture.IsDefault && dbCulture.Name == Culture.DefaultName)) { dbCulture.IsDefault = dbCulture.Name == Culture.DefaultName; doSave = true; } if (dbCulture.IsDefault && dbCulture.Description != Culture.DefaultCulture.DisplayName) { dbCulture.Description = Culture.DefaultCulture.DisplayName; doSave = true; } if (doSave) { await _languageRepository.UpdateSaveNoAuditAsync(dbCulture); } } if (_cache.Get(string.Format(CacheKey.LanguageId, dbCulture.Name)) != null) { _cache.Remove(string.Format(CacheKey.LanguageId, dbCulture.Name)); } } var namesMissingFromDb = siteCultures .Select(_ => _.Name) .Except(databaseCultures.Select(_ => _.Name)); int?systemUser = null; foreach (var missingCultureName in namesMissingFromDb) { if (systemUser == null) { systemUser = await _siteLookupService.GetSystemUserId(); } var culture = siteCultures.Single(_ => _.Name == missingCultureName); await _languageRepository.AddSaveNoAuditAsync(new Language { CreatedAt = _dateTimeProvider.Now, CreatedBy = (int)systemUser, Description = culture.DisplayName, IsActive = true, IsDefault = culture.Name == Culture.DefaultName, Name = culture.Name }); if (_cache.Get(string.Format(CacheKey.LanguageId, missingCultureName)) != null) { _cache.Remove(string.Format(CacheKey.LanguageId, missingCultureName)); } } _cache.Remove(CacheKey.DefaultLanguageId); }