Skip to content

Commit

Permalink
fixed sorting groups by manager
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksimChegulov committed Mar 7, 2024
1 parent 7018860 commit 7c00aae
Showing 1 changed file with 25 additions and 41 deletions.
66 changes: 25 additions & 41 deletions common/ASC.Core.Common/Data/DbUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task<IEnumerable<Group>> GetGroupsAsync(int tenant)
.ProjectTo<Group>(mapper.ConfigurationProvider)
.ToListAsync();
}

public async IAsyncEnumerable<Group> GetGroupsAsync(int tenant, string text, Guid userId, bool manager, GroupSortType sortBy, bool sortOrderAsc, int offset = 0, int count = -1)
{
if (count == 0)
Expand All @@ -60,53 +60,32 @@ public async IAsyncEnumerable<Group> GetGroupsAsync(int tenant, string text, Gui

await using var userDbContext = await dbContextFactory.CreateDbContextAsync();

var q = userDbContext.Groups.Where(g => g.TenantId == tenant && !g.Removed);

var q = BuildBaseGroupQuery(tenant, userDbContext);
q = BuildTextSearch(text, q);

if (userId != Guid.Empty)
{
var q1 = BuildUserGroupSearch(userId, manager, q, userDbContext);
q = BuildUserGroupSearch(userId, manager, q, userDbContext);
}

if (sortBy == GroupSortType.Manager)
if (sortBy == GroupSortType.Manager)
{
var q1 = q.Select(g => new
{
var q2 = q1.Join(userDbContext.UserGroups, group => group.Id, userGroup => userGroup.UserGroupId,
(group, userGroup) => new { group, userGroup })
.Where(r => !r.userGroup.Removed && r.userGroup.RefType == UserGroupRefType.Manager)
.Join(userDbContext.Users, record => record.userGroup.Userid,
user => user.Id, (record, user) => new { record.group, user });

q = (sortOrderAsc ? q2.OrderBy(r => r.user.FirstName) : q2.OrderByDescending(r => r.user.FirstName))
.Select(r => r.group);
}
else
{
q = sortOrderAsc ? q1.OrderBy(g => g.Name) : q1.OrderByDescending(g => g.Name);
}
Group = g,
Manager = userDbContext.UserGroups
.Where(ug => !ug.Removed && ug.UserGroupId == g.Id && ug.RefType == UserGroupRefType.Manager)
.Join(userDbContext.Users, ug => ug.Userid, u => u.Id, (ug, u) => u)
.FirstOrDefault()
});

q = (sortOrderAsc ?
q1.OrderBy(r => r.Manager.FirstName) :
q1.OrderByDescending(r => r.Manager.FirstName)).Select(r => r.Group);
}
else
{
if (sortBy == GroupSortType.Manager)
{
var q1 = from dbGroup in q
join userGroup in userDbContext.UserGroups on dbGroup.Id equals userGroup.UserGroupId
into userGroups
from userGroup in userGroups.DefaultIfEmpty()
where userGroup == null || (userGroup.RefType == UserGroupRefType.Manager && !userGroup.Removed)
join user in userDbContext.Users on userGroup.Userid equals user.Id
into users
from user in users.DefaultIfEmpty()
select new { dbGroup, user };

q = (sortOrderAsc
? q1.OrderBy(r => r.user == null).ThenBy(r => r.user.FirstName)
: q1.OrderBy(r => r.user == null).ThenByDescending(r => r.user.FirstName))
.Select(r => r.dbGroup);
}
else
{
q = sortOrderAsc ? q.OrderBy(g => g.Name) : q.OrderByDescending(g => g.Name);
}
q = sortOrderAsc ? q.OrderBy(g => g.Name) : q.OrderByDescending(g => g.Name);
}

if (offset > 0)
Expand All @@ -129,7 +108,7 @@ public async Task<int> GetGroupsCountAsync(int tenant, string text, Guid userId,
{
await using var userDbContext = await dbContextFactory.CreateDbContextAsync();

var q = userDbContext.Groups.Where(g => g.TenantId == tenant && !g.Removed);
var q = BuildBaseGroupQuery(tenant, userDbContext);

q = BuildTextSearch(text, q);

Expand Down Expand Up @@ -925,7 +904,7 @@ private static IQueryable<DbGroup> BuildTextSearch(string text, IQueryable<DbGro
if (string.IsNullOrEmpty(text))
{
return q;
}
}

text = text.ToLower().Trim();

Expand All @@ -943,6 +922,11 @@ private static IQueryable<DbGroup> BuildUserGroupSearch(Guid userId, bool manage
.Where(r => !r.userGroup.Removed && r.userGroup.Userid == userId && r.userGroup.RefType == refType).Select(r => r.group);
return q1;
}

private static IQueryable<DbGroup> BuildBaseGroupQuery(int tenant, UserDbContext userDbContext)
{
return userDbContext.Groups.Where(g => g.TenantId == tenant && !g.Removed);
}
}

public class DbUserSecurity
Expand Down

0 comments on commit 7c00aae

Please sign in to comment.