public FilterResult GetConstructionManagers(string userId, string userLevel, string projectId) { FilterResult result = new FilterResult(); result.Properties["enabled"] = false; if (!string.IsNullOrWhiteSpace(userLevel)) { switch (userLevel) { case FilterResult.USER_ADMIN: result.Properties["enabled"] = true; result.Data = _session.Sites.Where(s => s.ProjectId.TrimEnd().Equals(projectId, StringComparison.InvariantCultureIgnoreCase)) .Select(s => s.Manager.ManagerName) .Distinct() .OrderBy(s => s) .ToList<string>(); break; case FilterResult.USER_MARKET_MANAGER: result.Properties["enabled"] = true; result.Data = _session.Sites.Where(s => s.ProjectId.TrimEnd().Equals(projectId, StringComparison.InvariantCultureIgnoreCase) && s.Market.ManagerId.Equals(userId, StringComparison.InvariantCultureIgnoreCase)) .Select(s => s.Manager.ManagerName) .Distinct() .OrderBy(s => s) .ToList<string>(); break; case FilterResult.USER_CONSTRUCTION_MANAGER: // But check if the user has an Alias // If any Manager aliases are found, they are kept in the "userAliasManagers" member // otherwise, the "userAliasManagers" will contain only the current user ID IEnumerable<string> userAliasManagers = _session.PermissionsAliases.Where(p => p.AliasUserName.Equals(userId, StringComparison.InvariantCultureIgnoreCase)).Select(p => p.ManagerUserName.ToLower()).ToList<string>(); if (userAliasManagers == null || userAliasManagers.Count() == 0) userAliasManagers = new List<string> { userId }; var isConstructionManager = false; foreach (string userAliasManager in userAliasManagers) { Site userSite = _session.Sites.Where(m => m.ProjectId.TrimEnd().Equals(projectId, StringComparison.InvariantCultureIgnoreCase) && m.ManagerId.Equals(userAliasManager, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault(); if (userSite != null) { isConstructionManager = true; result.Properties["userAliasManager"] = userAliasManager; result.Data.Add(userSite.Manager.ManagerName); break; } } if (!isConstructionManager) { result.Code = FilterResult.ERROR; result.Description = "No data returned from server."; } break; } } return result; }
//TODO: move to another operations class #region Sites FILTERS public FilterResult GetMarketManagers(string userId, string userLevel, string projectId) { FilterResult result = new FilterResult(); result.Properties["enabled"] = false; //TODO: do it in a single query IEnumerable<Market> markets = _session.Markets.Where( m => m.Sites.Any( s => s.ProjectId.TrimEnd().Equals(projectId, StringComparison.InvariantCultureIgnoreCase))) .Distinct(); if (!string.IsNullOrWhiteSpace(userId)) { if (userLevel == FilterResult.USER_ADMIN) { result.Properties["userLevel"] = FilterResult.USER_ADMIN; result.Properties["enabled"] = true; result.Data = markets.Select(m => m.Manager.ManagerName).Distinct().OrderBy(m => m).ToList(); } else { // User is Not Admin // But check if it has an Alias // If any Manager aliases are found, they are kept in the "userAliasManagers" member // otherwise, the "userAliasManagers" will contain only the current user ID IEnumerable<string> userAliasManagers = _session.PermissionsAliases.Where(p => p.AliasUserName.Equals(userId, StringComparison.InvariantCultureIgnoreCase)).Select(p => p.ManagerUserName.ToLower()).ToList(); if (userAliasManagers == null || !userAliasManagers.Any()) userAliasManagers = new List<string> { userId }; // Search for the logged in user inside the [Market] table // and determine if he is a Market Manager var isMarketManager = false; foreach (string userAliasManager in userAliasManagers) { Market userMarket = markets.Where(m => m.ManagerId.Equals(userAliasManager, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault(); if (userMarket != null) { var marketManagerName = userMarket.ManagerId; isMarketManager = true; result.Properties["userLevel"] = FilterResult.USER_MARKET_MANAGER; result.Properties["userAliasManager"] = userAliasManager; var marketManager = _session.Managers.Where(m => m.ManagerId.Equals(userAliasManager, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault(); if (marketManager != null) { marketManagerName = marketManager.ManagerName; } result.Data.Add(marketManagerName); break; } } // in case the user was not found through the market managers, // he will be considered as a construction manager if (!isMarketManager) { result.Properties["userLevel"] = FilterResult.USER_CONSTRUCTION_MANAGER; } } } return result; }