Facebook
From Md. Raihan Nishat, 1 Week ago, written in C#.
Embed
Download Paste or View Raw
Hits: 77
  1. using LanguageExt.Common;
  2. using ParticipantGroupModule.DTOs;
  3.  
  4. namespace ParticipantGroupModule.Services;
  5.  
  6. public class ParticipantGroupFilterService : IParticipantGroupFilterService
  7. {
  8.     private readonly CommonContext _commonContext;
  9.     private readonly IUserAccessControlService _userAccessControlService;
  10.     private readonly ILoggedInUserService _loggedInUserService;
  11.  
  12.     public ParticipantGroupFilterService(
  13.         CommonContext commonContext,
  14.         IUserAccessControlService userAccessControlService,
  15.         ILoggedInUserService loggedInUserService)
  16.     {
  17.         _commonContext = commonContext;
  18.         _userAccessControlService = userAccessControlService;
  19.         _loggedInUserService = loggedInUserService;
  20.     }
  21.  
  22.     private bool GroupFilterHasAnyValue(object model)
  23.     {
  24.         if (model == null) return false;
  25.         var properties = model.GetType().GetProperties();
  26.         foreach (var property in properties)
  27.         {
  28.             var value = property.GetValue(model);
  29.             if (value != null && property.PropertyType == typeof(string) && !string.IsNullOrWhiteSpace((string)value))
  30.             {
  31.                 return true;
  32.             }
  33.         }
  34.  
  35.         return false;
  36.     }
  37.  
  38.     public object GetFilteredParticipantGroups(ParticipantGroupFilterDto participantGroupFilterDto)
  39.     {
  40.         var hasData = GroupFilterHasAnyValue(participantGroupFilterDto);
  41.  
  42.         var participantGroups = _commonContext.ParticipantGroups.AsQueryable();
  43.  
  44.         if (string.IsNullOrEmpty(participantGroupFilterDto.CountryId))
  45.         {
  46.             participantGroups = FilterByCountryAccess(participantGroups);
  47.         }
  48.         if (string.IsNullOrEmpty(participantGroupFilterDto.ProjectId))
  49.         {
  50.             participantGroups = FilterByProjectAccess(participantGroups);
  51.         }
  52.         if (string.IsNullOrEmpty(participantGroupFilterDto.CatchmentId))
  53.         {
  54.             participantGroups = FilterByVillageAccess(participantGroups);
  55.         }
  56.  
  57.         participantGroups = participantGroups.Where(participantGroup =>
  58.             (string.IsNullOrEmpty(participantGroupFilterDto.CountryId) || participantGroup.CountryId == participantGroupFilterDto.CountryId) &&
  59.             (string.IsNullOrEmpty(participantGroupFilterDto.ProjectId) || participantGroup.ProjectId == participantGroupFilterDto.ProjectId) &&
  60.             (string.IsNullOrEmpty(participantGroupFilterDto.CatchmentId) || participantGroup.CatchmentId == participantGroupFilterDto.CatchmentId) &&
  61.             (string.IsNullOrEmpty(participantGroupFilterDto.ServicePointId) || participantGroup.ServicePointId == participantGroupFilterDto.ServicePointId) &&
  62.             (string.IsNullOrEmpty(participantGroupFilterDto.GroupTypeId) || participantGroup.ParticipantGroupTypeId == participantGroupFilterDto.GroupTypeId)
  63.         );
  64.  
  65.         var totalParticipantGroups = GetFinalParticipantGroups(participantGroups);
  66.  
  67.         if (!hasData)
  68.         {
  69.             return new
  70.             {
  71.                 ParticipantGroups = totalParticipantGroups.Take(200).AsEnumerable(),
  72.                 totalGroups = totalParticipantGroups.Count()
  73.             };
  74.         }
  75.  
  76.         return new
  77.         {
  78.             ParticipantGroups = totalParticipantGroups.AsEnumerable(),
  79.             totalGroups = totalParticipantGroups.Count()
  80.         };
  81.     }
  82.  
  83.     private IQueryable<ParticipantGroup> FilterByCountryAccess(IQueryable<ParticipantGroup> participantGroups)
  84.     {
  85.         if (_loggedInUserService.IsSystemAdmin())
  86.         {
  87.             // System admin can get all participant groups. Hence, don't need to check accessed countries.
  88.             return participantGroups;
  89.         }
  90.  
  91.         var permittedCountryIds = _userAccessControlService.GetAllCountry()
  92.             .Select(x => x.Id)
  93.             .Distinct();
  94.  
  95.         return participantGroups.Join(
  96.             permittedCountryIds,
  97.             p => p.CountryId,
  98.             countryId => countryId,
  99.             (p, c) => p);
  100.     }
  101.  
  102.     private IQueryable<ParticipantGroup> FilterByProjectAccess(IQueryable<ParticipantGroup> participantGroups)
  103.     {
  104.         if (_loggedInUserService.IsSystemAdmin())
  105.         {
  106.             // System admin can get all participant groups. Hence, don't need to check accessed projects.
  107.             return participantGroups;
  108.         }
  109.  
  110.         var permittedProjectIds = _userAccessControlService.GetAllProject()
  111.             .Select(x => x.Id)
  112.             .Distinct();
  113.  
  114.         return participantGroups.Join(
  115.             permittedProjectIds,
  116.             participantGroup => participantGroup.ProjectId,
  117.             projectId => projectId,
  118.             (participantGroup, projectId) => participantGroup);
  119.     }
  120.  
  121.     private IQueryable<ParticipantGroup> FilterByVillageAccess(IQueryable<ParticipantGroup> participantGroups)
  122.     {
  123.         if (_loggedInUserService.IsSystemAdmin())
  124.         {
  125.             // System admin can get all participant groups. Hence, don't need to check accessed villages.
  126.             return participantGroups;
  127.         }
  128.  
  129.         var permittedVillageIds = _userAccessControlService.GetPermittedVillageIdsAsQueryable();
  130.  
  131.         return participantGroups.
  132.             Where(x => permittedVillageIds.Contains(x.CatchmentId!));
  133.     }
  134.  
  135.     private IQueryable&lt;object&gt; GetFinalParticipantGroups(IQueryable<ParticipantGroup> participantGroups)
  136.     {
  137.         return
  138.             (from pg in participantGroups
  139.              join b in _commonContext.BranchOfficeVillageMappings
  140.                 on new { pg.CatchmentId, pg.ProjectId } equals new { b.CatchmentId, b.ProjectId }
  141.                 into branchOffices
  142.              from branchOffice in branchOffices.DefaultIfEmpty()
  143.              join pgm in _commonContext.ParticipantGroupMembers
  144.                 on pg.Id equals pgm.GroupId
  145.                 into pgMembers
  146.              group pg by new
  147.              {
  148.                  pg.Id,
  149.                  pg.Name,
  150.                  pg.ParticipantGroupTypeId,
  151.                  pg.StakeholderTypeId,
  152.                  pg.CatchmentId,
  153.                  pg.ServicePointId,
  154.                  pg.CatchmentIdsForMemberSelection,
  155.                  pg.CountryId,
  156.                  pg.ProjectId,
  157.                  pg.FiscalYearId,
  158.                  branchOffice.BranchOfficeId,
  159.                  NumberOfParticipant = pgMembers.Count()
  160.              } into pgGrouped
  161.              select new
  162.              {
  163.                  pgGrouped.Key.Id,
  164.                  pgGrouped.Key.Name,
  165.                  pgGrouped.Key.ParticipantGroupTypeId,
  166.                  pgGrouped.Key.StakeholderTypeId,
  167.                  pgGrouped.Key.CatchmentId,
  168.                  pgGrouped.Key.ServicePointId,
  169.                  pgGrouped.Key.CatchmentIdsForMemberSelection,
  170.                  pgGrouped.Key.CountryId,
  171.                  pgGrouped.Key.ProjectId,
  172.                  pgGrouped.Key.FiscalYearId,
  173.                  pgGrouped.Key.BranchOfficeId,
  174.                  pgGrouped.Key.NumberOfParticipant
  175.              });
  176.     }
  177. }
  178.