From f27a5f4b23e35ff05fb3f0bab0b07baf830757a8 Mon Sep 17 00:00:00 2001 From: ZdorovenkoKyrylo <92879521+ZdorovenkoKyrylo@users.noreply.github.com> Date: Mon, 14 Aug 2023 13:39:52 +0300 Subject: [PATCH] FixAreaAdminGetByFilter (#1189) * FixAreaAdminGetByFilter * Fix comments areaAdmin GetByFilter --- .../Models/Application.cs | 2 + .../Services/AreaAdminService.cs | 56 ++++++++----------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/OutOfSchool/OutOfSchool.DataAccess/Models/Application.cs b/OutOfSchool/OutOfSchool.DataAccess/Models/Application.cs index 31130acae8..7bac794177 100644 --- a/OutOfSchool/OutOfSchool.DataAccess/Models/Application.cs +++ b/OutOfSchool/OutOfSchool.DataAccess/Models/Application.cs @@ -1,5 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using OutOfSchool.Common.Enums; using OutOfSchool.Services.Enums; namespace OutOfSchool.Services.Models; diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/AreaAdminService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/AreaAdminService.cs index 3ee0d77d7d..104ca6d34f 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/AreaAdminService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/AreaAdminService.cs @@ -22,9 +22,12 @@ public class AreaAdminService : CommunicationService, IAreaAdminService private readonly IRegionAdminService regionAdminService; private IAreaAdminService areaAdminServiceImplementation; private ICodeficatorRepository codeficatorRepository; + private readonly ICodeficatorService codeficatorService; + public AreaAdminService( ICodeficatorRepository codeficatorRepository, + ICodeficatorService codeficatorService, IHttpClientFactory httpClientFactory, IOptions identityServerConfig, IOptions communicationConfig, @@ -51,6 +54,7 @@ public AreaAdminService( this.currentUserService = currentUserService; this.ministryAdminService = ministryAdminService; this.regionAdminService = regionAdminService; + this.codeficatorService = codeficatorService; } public async Task GetByIdAsync(string id) @@ -96,15 +100,18 @@ public async Task> CreateAreaAdminAsync( if (await IsSuchEmailExisted(areaAdminBaseDto.Email)) { - Logger.LogDebug("AreaAdmin creating is not possible. Username {Email} is already taken", areaAdminBaseDto.Email); + Logger.LogDebug("AreaAdmin creating is not possible. Username {Email} is already taken", + areaAdminBaseDto.Email); throw new InvalidOperationException($"Username {areaAdminBaseDto.Email} is already taken."); } bool isValidCatottg = await IsValidCatottg(areaAdminBaseDto.CATOTTGId); if (!isValidCatottg) { - Logger.LogDebug("AreaAdmin creating is not possible. Catottg with Id {CatottgId} does not contain to area", areaAdminBaseDto.CATOTTGId); - throw new InvalidOperationException($"Catottg with Id {areaAdminBaseDto.CATOTTGId} does not contain to area."); + Logger.LogDebug("AreaAdmin creating is not possible. Catottg with Id {CatottgId} does not contain to area", + areaAdminBaseDto.CATOTTGId); + throw new InvalidOperationException( + $"Catottg with Id {areaAdminBaseDto.CATOTTGId} does not contain to area."); } var request = new Request() @@ -147,43 +154,22 @@ public async Task> GetByFilter(AreaAdminFilter filter filter ??= new AreaAdminFilter(); ModelValidationHelper.ValidateOffsetFilter(filter); - if (currentUserService.IsMinistryAdmin()) { var ministryAdmin = await ministryAdminService.GetByUserId(currentUserService.UserId); filter.InstitutionId = ministryAdmin.InstitutionId; - - Logger.LogInformation( - $"Filter institutionId {filter.InstitutionId} is not equals to logined Ministry admin institutionId {ministryAdmin.InstitutionId}"); - - return new SearchResult() - { - TotalAmount = 0, - Entities = default, - }; } + var catottgs = new List(); if (currentUserService.IsRegionAdmin()) { - var areaAdminDto = await GetByUserId(currentUserService.UserId); - filter.InstitutionId = areaAdminDto.InstitutionId; - filter.CATOTTGId = areaAdminDto.CATOTTGId; - - - if (filter.InstitutionId != areaAdminDto.InstitutionId || filter.CATOTTGId != areaAdminDto.CATOTTGId) - { - Logger.LogInformation($"Filter institutionId {filter.InstitutionId} and CATOTTGId {filter.CATOTTGId} " + - $"is not equals to logined Otg admin institutionId {areaAdminDto.InstitutionId} and CATOTTGId {areaAdminDto.CATOTTGId}"); - - return new SearchResult() - { - TotalAmount = 0, - Entities = default, - }; - } + var regionAdminDto = await regionAdminService.GetByUserId(currentUserService.UserId).ConfigureAwait(false); + filter.InstitutionId = regionAdminDto.InstitutionId; + var childrenIds = await codeficatorService.GetAllChildrenIdsByParentIdAsync(filter.CATOTTGId); + catottgs = childrenIds.ToList(); } - Expression> filterPredicate = PredicateBuild(filter); + Expression> filterPredicate = PredicateBuild(filter, catottgs); int count = await areaAdminRepository.Count(filterPredicate).ConfigureAwait(false); @@ -492,7 +478,7 @@ public async Task IsAreaAdminSubordinateRegionCreateAsync(string regionAdm return regionAdmin.InstitutionId == institutionId && regionAdmin.CATOTTGId == catottgId; } - private static Expression> PredicateBuild(AreaAdminFilter filter) + private static Expression> PredicateBuild(AreaAdminFilter filter, List catottgs) { Expression> predicate = PredicateBuilder.True(); @@ -519,7 +505,11 @@ private static Expression> PredicateBuild(AreaAdminFilter predicate = predicate.And(a => a.Institution.Id == filter.InstitutionId); } - if (filter.CATOTTGId > 0) + if (catottgs.Count > 0) + { + predicate = predicate.And(a => catottgs.Contains(a.CATOTTGId)); + } + else if (filter.CATOTTGId > 0) { predicate = predicate.And(c => c.CATOTTG.Id == filter.CATOTTGId); } @@ -539,6 +529,6 @@ private async Task IsValidCatottg(long catottgId) { var catottg = await codeficatorRepository.GetById(catottgId); return catottg is not null && (catottg.Category == CodeficatorCategory.TerritorialCommunity.ToString() || - catottg.Category == CodeficatorCategory.SpecialStatusCity.ToString()); + catottg.Category == CodeficatorCategory.SpecialStatusCity.ToString()); } } \ No newline at end of file