Skip to content

Commit a4e6e6b

Browse files
FEAT: Add default sort (#74)
1 parent d594c4e commit a4e6e6b

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

src/authorization/service/group.service.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SearchEntity } from '../../constants/search.entity.enum';
44
import {
55
GroupInputFilter,
66
NewGroupInput,
7+
SortDirection,
78
UpdateGroupInput,
89
UpdateGroupPermissionInput,
910
UpdateGroupRoleInput,
@@ -60,7 +61,10 @@ export class GroupService implements GroupServiceInterface {
6061
* @returns
6162
*/
6263
async getAllGroups(input?: GroupInputFilter): Promise<[Group[], number]> {
63-
const SortFieldMapping = new Map([['name', 'Group.name']]);
64+
const SortFieldMapping = new Map([
65+
['name', 'group.name'],
66+
['updatedAt', 'group.updated_at'],
67+
]);
6468
let queryBuilder = this.groupRepository.createQueryBuilder('group');
6569

6670
if (input?.search) {
@@ -72,7 +76,11 @@ export class GroupService implements GroupServiceInterface {
7276
}
7377
if (input?.sort) {
7478
const field = SortFieldMapping.get(input.sort.field);
75-
field && queryBuilder.orderBy(field, input.sort.direction);
79+
field
80+
? queryBuilder.orderBy(field, input.sort.direction)
81+
: queryBuilder.orderBy('group.updated_at', SortDirection.DESC);
82+
} else {
83+
queryBuilder.orderBy('group.updated_at', SortDirection.DESC);
7684
}
7785
if (input?.pagination) {
7886
queryBuilder

src/authorization/service/role.service.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SearchEntity } from '../../constants/search.entity.enum';
44
import {
55
NewRoleInput,
66
RoleInputFilter,
7+
SortDirection,
78
UpdateRoleInput,
89
UpdateRolePermissionInput,
910
} from '../../schema/graphql.schema';
@@ -37,7 +38,10 @@ export class RoleService implements RoleServiceInterface {
3738
) {}
3839

3940
async getAllRoles(input?: RoleInputFilter): Promise<[Role[], number]> {
40-
const SortFieldMapping = new Map([['name', 'Role.name']]);
41+
const SortFieldMapping = new Map([
42+
['name', 'role.name'],
43+
['updatedAt', 'role.updated_at'],
44+
]);
4145
let queryBuilder = this.rolesRepository.createQueryBuilder('role');
4246
if (input?.search) {
4347
queryBuilder = this.searchService.generateSearchTermForEntity(
@@ -46,10 +50,16 @@ export class RoleService implements RoleServiceInterface {
4650
input.search,
4751
);
4852
}
53+
4954
if (input?.sort) {
5055
const field = SortFieldMapping.get(input.sort.field);
51-
field && queryBuilder.orderBy(field, input.sort.direction);
56+
field
57+
? queryBuilder.orderBy(field, input.sort.direction)
58+
: queryBuilder.orderBy('role.updated_at', SortDirection.DESC);
59+
} else {
60+
queryBuilder.orderBy('role.updated_at', SortDirection.DESC);
5261
}
62+
5363
if (input?.pagination) {
5464
queryBuilder
5565
.limit(input?.pagination?.limit ?? 10)
@@ -96,8 +106,8 @@ export class RoleService implements RoleServiceInterface {
96106
}
97107

98108
await this.dataSource.manager.transaction(async (entityManager) => {
99-
const roleRepo = entityManager.getRepository(Role);
100109
const rolePermissionsRepo = entityManager.getRepository(RolePermission);
110+
const roleRepo = entityManager.getRepository(Role);
101111
await rolePermissionsRepo.softDelete({ roleId: id });
102112
await roleRepo.softDelete(id);
103113
});

src/authorization/service/user.service.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { SearchEntity } from '../../constants/search.entity.enum';
66
import {
77
FilterField,
88
OperationType,
9+
SortDirection,
910
Status,
1011
UpdateUserGroupInput,
1112
UpdateUserInput,
@@ -53,8 +54,11 @@ export class UserService implements UserServiceInterface {
5354
) {}
5455

5556
getAllUsers(input?: UserInputFilter): Promise<[User[], number]> {
56-
const SortFieldMapping = new Map([['firstName', 'User.firstName']]);
57-
const filterFieldMapping = new Map([['status', 'User.status']]);
57+
const SortFieldMapping = new Map([
58+
['firstName', 'user.firstName'],
59+
['updatedAt', 'user.updated_at'],
60+
]);
61+
const filterFieldMapping = new Map([['status', 'user.status']]);
5862

5963
const applyUserGroupFilter = (
6064
field: FilterField,
@@ -64,7 +68,7 @@ export class UserService implements UserServiceInterface {
6468
queryBuilder.innerJoin(
6569
UserGroup,
6670
'userGroup',
67-
'userGroup.userId = User.id AND userGroup.groupId IN (:...groupIds)',
71+
'userGroup.userId = user.id AND userGroup.groupId IN (:...groupIds)',
6872
{ groupIds: field.value },
6973
);
7074
}
@@ -83,7 +87,11 @@ export class UserService implements UserServiceInterface {
8387
}
8488
if (input?.sort) {
8589
const sortField = SortFieldMapping.get(input.sort.field);
86-
sortField && qb.orderBy(sortField, input.sort.direction);
90+
sortField
91+
? qb.orderBy(sortField, input.sort.direction)
92+
: qb.orderBy('user.updated_at', SortDirection.DESC);
93+
} else {
94+
qb.orderBy('user.updated_at', SortDirection.DESC);
8795
}
8896
if (input?.pagination) {
8997
qb.limit(input?.pagination?.limit ?? 10).offset(

test/authorization/service/group.service.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ describe('test Group Service', () => {
175175
.mockReturnValue({
176176
leftJoinAndSelect: jest.fn().mockReturnThis(),
177177
where: jest.fn().mockReturnThis(),
178+
orderBy: jest.fn().mockReturnThis(),
178179
getManyAndCount: (getManyAndCountMock = jest.fn()),
179180
});
180181

test/authorization/service/role.service.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ describe('test Role Service', () => {
107107
.mockReturnValue({
108108
leftJoinAndSelect: jest.fn().mockReturnThis(),
109109
where: jest.fn().mockReturnThis(),
110+
orderBy: jest.fn().mockReturnThis(),
110111
getManyAndCount: (getManyAndCountMock = jest.fn()),
111112
});
112113

test/authorization/service/user.service.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ describe('test UserService', () => {
154154
.mockReturnValue({
155155
leftJoinAndSelect: jest.fn().mockReturnThis(),
156156
where: jest.fn().mockReturnThis(),
157+
orderBy: jest.fn().mockReturnThis(),
157158
getManyAndCount: (getManyAndCountMock = jest.fn()),
158159
});
159160
});
@@ -165,6 +166,7 @@ describe('test UserService', () => {
165166
const result = await userService.getAllUsers();
166167

167168
expect(createQueryBuilderMock.mock.calls[0][0]).toStrictEqual('user');
169+
168170
expect(getManyAndCountMock).toBeCalledTimes(1);
169171

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

0 commit comments

Comments
 (0)