Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/authorization/service/group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SearchEntity } from '../../constants/search.entity.enum';
import {
GroupInputFilter,
NewGroupInput,
SortDirection,
UpdateGroupInput,
UpdateGroupPermissionInput,
UpdateGroupRoleInput,
Expand Down Expand Up @@ -60,7 +61,10 @@ export class GroupService implements GroupServiceInterface {
* @returns
*/
async getAllGroups(input?: GroupInputFilter): Promise<[Group[], number]> {
const SortFieldMapping = new Map([['name', 'Group.name']]);
const SortFieldMapping = new Map([
['name', 'group.name'],
['updatedAt', 'group.updated_at'],
]);
let queryBuilder = this.groupRepository.createQueryBuilder('group');

if (input?.search) {
Expand All @@ -72,7 +76,11 @@ export class GroupService implements GroupServiceInterface {
}
if (input?.sort) {
const field = SortFieldMapping.get(input.sort.field);
field && queryBuilder.orderBy(field, input.sort.direction);
field
? queryBuilder.orderBy(field, input.sort.direction)
: queryBuilder.orderBy('group.updated_at', SortDirection.DESC);
} else {
queryBuilder.orderBy('group.updated_at', SortDirection.DESC);
}
if (input?.pagination) {
queryBuilder
Expand Down
16 changes: 13 additions & 3 deletions src/authorization/service/role.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SearchEntity } from '../../constants/search.entity.enum';
import {
NewRoleInput,
RoleInputFilter,
SortDirection,
UpdateRoleInput,
UpdateRolePermissionInput,
} from '../../schema/graphql.schema';
Expand Down Expand Up @@ -37,7 +38,10 @@ export class RoleService implements RoleServiceInterface {
) {}

async getAllRoles(input?: RoleInputFilter): Promise<[Role[], number]> {
const SortFieldMapping = new Map([['name', 'Role.name']]);
const SortFieldMapping = new Map([
['name', 'role.name'],
['updatedAt', 'role.updated_at'],
]);
let queryBuilder = this.rolesRepository.createQueryBuilder('role');
if (input?.search) {
queryBuilder = this.searchService.generateSearchTermForEntity(
Expand All @@ -46,10 +50,16 @@ export class RoleService implements RoleServiceInterface {
input.search,
);
}

if (input?.sort) {
const field = SortFieldMapping.get(input.sort.field);
field && queryBuilder.orderBy(field, input.sort.direction);
field
? queryBuilder.orderBy(field, input.sort.direction)
: queryBuilder.orderBy('role.updated_at', SortDirection.DESC);
} else {
queryBuilder.orderBy('role.updated_at', SortDirection.DESC);
}

if (input?.pagination) {
queryBuilder
.limit(input?.pagination?.limit ?? 10)
Expand Down Expand Up @@ -96,8 +106,8 @@ export class RoleService implements RoleServiceInterface {
}

await this.dataSource.manager.transaction(async (entityManager) => {
const roleRepo = entityManager.getRepository(Role);
const rolePermissionsRepo = entityManager.getRepository(RolePermission);
const roleRepo = entityManager.getRepository(Role);
await rolePermissionsRepo.softDelete({ roleId: id });
await roleRepo.softDelete(id);
});
Expand Down
16 changes: 12 additions & 4 deletions src/authorization/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SearchEntity } from '../../constants/search.entity.enum';
import {
FilterField,
OperationType,
SortDirection,
Status,
UpdateUserGroupInput,
UpdateUserInput,
Expand Down Expand Up @@ -53,8 +54,11 @@ export class UserService implements UserServiceInterface {
) {}

getAllUsers(input?: UserInputFilter): Promise<[User[], number]> {
const SortFieldMapping = new Map([['firstName', 'User.firstName']]);
const filterFieldMapping = new Map([['status', 'User.status']]);
const SortFieldMapping = new Map([
['firstName', 'user.firstName'],
['updatedAt', 'user.updated_at'],
]);
const filterFieldMapping = new Map([['status', 'user.status']]);

const applyUserGroupFilter = (
field: FilterField,
Expand All @@ -64,7 +68,7 @@ export class UserService implements UserServiceInterface {
queryBuilder.innerJoin(
UserGroup,
'userGroup',
'userGroup.userId = User.id AND userGroup.groupId IN (:...groupIds)',
'userGroup.userId = user.id AND userGroup.groupId IN (:...groupIds)',
{ groupIds: field.value },
);
}
Expand All @@ -83,7 +87,11 @@ export class UserService implements UserServiceInterface {
}
if (input?.sort) {
const sortField = SortFieldMapping.get(input.sort.field);
sortField && qb.orderBy(sortField, input.sort.direction);
sortField
? qb.orderBy(sortField, input.sort.direction)
: qb.orderBy('user.updated_at', SortDirection.DESC);
} else {
qb.orderBy('user.updated_at', SortDirection.DESC);
}
if (input?.pagination) {
qb.limit(input?.pagination?.limit ?? 10).offset(
Expand Down
1 change: 1 addition & 0 deletions test/authorization/service/group.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ describe('test Group Service', () => {
.mockReturnValue({
leftJoinAndSelect: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
orderBy: jest.fn().mockReturnThis(),
getManyAndCount: (getManyAndCountMock = jest.fn()),
});

Expand Down
1 change: 1 addition & 0 deletions test/authorization/service/role.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe('test Role Service', () => {
.mockReturnValue({
leftJoinAndSelect: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
orderBy: jest.fn().mockReturnThis(),
getManyAndCount: (getManyAndCountMock = jest.fn()),
});

Expand Down
2 changes: 2 additions & 0 deletions test/authorization/service/user.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ describe('test UserService', () => {
.mockReturnValue({
leftJoinAndSelect: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
orderBy: jest.fn().mockReturnThis(),
getManyAndCount: (getManyAndCountMock = jest.fn()),
});
});
Expand All @@ -165,6 +166,7 @@ describe('test UserService', () => {
const result = await userService.getAllUsers();

expect(createQueryBuilderMock.mock.calls[0][0]).toStrictEqual('user');

expect(getManyAndCountMock).toBeCalledTimes(1);

expect(result).toEqual([users, 1]);
Expand Down