Skip to content

Commit

Permalink
FixAreaAdminGetByFilter (#1189)
Browse files Browse the repository at this point in the history
* FixAreaAdminGetByFilter

* Fix comments areaAdmin GetByFilter
  • Loading branch information
ZdorovenkoKyrylo authored Aug 14, 2023
1 parent 0d9250b commit f27a5f4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
2 changes: 2 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/Application.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
56 changes: 23 additions & 33 deletions OutOfSchool/OutOfSchool.WebApi/Services/AreaAdminService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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> identityServerConfig,
IOptions<CommunicationConfig> communicationConfig,
Expand All @@ -51,6 +54,7 @@ public AreaAdminService(
this.currentUserService = currentUserService;
this.ministryAdminService = ministryAdminService;
this.regionAdminService = regionAdminService;
this.codeficatorService = codeficatorService;
}

public async Task<AreaAdminDto> GetByIdAsync(string id)
Expand Down Expand Up @@ -96,15 +100,18 @@ public async Task<Either<ErrorResponse, AreaAdminBaseDto>> 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()
Expand Down Expand Up @@ -147,43 +154,22 @@ public async Task<SearchResult<AreaAdminDto>> 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<AreaAdminDto>()
{
TotalAmount = 0,
Entities = default,
};
}

var catottgs = new List<long>();
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<AreaAdminDto>()
{
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<Func<AreaAdmin, bool>> filterPredicate = PredicateBuild(filter);
Expression<Func<AreaAdmin, bool>> filterPredicate = PredicateBuild(filter, catottgs);

int count = await areaAdminRepository.Count(filterPredicate).ConfigureAwait(false);

Expand Down Expand Up @@ -492,7 +478,7 @@ public async Task<bool> IsAreaAdminSubordinateRegionCreateAsync(string regionAdm
return regionAdmin.InstitutionId == institutionId && regionAdmin.CATOTTGId == catottgId;
}

private static Expression<Func<AreaAdmin, bool>> PredicateBuild(AreaAdminFilter filter)
private static Expression<Func<AreaAdmin, bool>> PredicateBuild(AreaAdminFilter filter, List<long> catottgs)
{
Expression<Func<AreaAdmin, bool>> predicate = PredicateBuilder.True<AreaAdmin>();

Expand All @@ -519,7 +505,11 @@ private static Expression<Func<AreaAdmin, bool>> 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);
}
Expand All @@ -539,6 +529,6 @@ private async Task<bool> 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());
}
}

0 comments on commit f27a5f4

Please sign in to comment.