fix: fetchUsers to fetchUsersV2 for backward compatibility

This commit is contained in:
mirarifhasan
2024-01-30 12:58:25 +06:00
committed by Andrew Bastin
parent b53cbb093c
commit 2eab86476e
4 changed files with 46 additions and 8 deletions

View File

@@ -46,17 +46,29 @@ export class AdminService {
private readonly configService: ConfigService, private readonly configService: ConfigService,
) {} ) {}
/**
* Fetch all the users in the infra.
* @param cursorID Users uid
* @param take number of users to fetch
* @returns an Either of array of user or error
* @deprecated use fetchUsersV2 instead
*/
async fetchUsers(cursorID: string, take: number) {
const allUsers = await this.userService.fetchAllUsers(cursorID, take);
return allUsers;
}
/** /**
* Fetch all the users in the infra. * Fetch all the users in the infra.
* @param searchString search on users displayName or email * @param searchString search on users displayName or email
* @param paginationOption pagination options * @param paginationOption pagination options
* @returns an Either of array of user or error * @returns an Either of array of user or error
*/ */
async fetchUsers( async fetchUsersV2(
searchString: string, searchString: string,
paginationOption: OffsetPaginationArgs, paginationOption: OffsetPaginationArgs,
) { ) {
const allUsers = await this.userService.fetchAllUsers( const allUsers = await this.userService.fetchAllUsersV2(
searchString, searchString,
paginationOption, paginationOption,
); );

View File

@@ -79,9 +79,19 @@ export class InfraResolver {
@ResolveField(() => [User], { @ResolveField(() => [User], {
description: 'Returns a list of all the users in infra', description: 'Returns a list of all the users in infra',
deprecationReason: 'Use allUsersV2 instead',
}) })
@UseGuards(GqlAuthGuard, GqlAdminGuard) @UseGuards(GqlAuthGuard, GqlAdminGuard)
async allUsers( async allUsers(@Args() args: PaginationArgs): Promise<AuthUser[]> {
const users = await this.adminService.fetchUsers(args.cursor, args.take);
return users;
}
@ResolveField(() => [User], {
description: 'Returns a list of all the users in infra',
})
@UseGuards(GqlAuthGuard, GqlAdminGuard)
async allUsersV2(
@Args({ @Args({
name: 'searchString', name: 'searchString',
nullable: true, nullable: true,
@@ -90,7 +100,7 @@ export class InfraResolver {
searchString: string, searchString: string,
@Args() paginationOption: OffsetPaginationArgs, @Args() paginationOption: OffsetPaginationArgs,
): Promise<AuthUser[]> { ): Promise<AuthUser[]> {
const users = await this.adminService.fetchUsers( const users = await this.adminService.fetchUsersV2(
searchString, searchString,
paginationOption, paginationOption,
); );

View File

@@ -418,19 +418,19 @@ describe('UserService', () => {
test('should resolve right and return 20 users when cursor is null', async () => { test('should resolve right and return 20 users when cursor is null', async () => {
mockPrisma.user.findMany.mockResolvedValueOnce(users); mockPrisma.user.findMany.mockResolvedValueOnce(users);
const result = await userService.fetchAllUsers(null, 20); const result = await userService.fetchAllUsersV2(null, 20);
expect(result).toEqual(users); expect(result).toEqual(users);
}); });
test('should resolve right and return next 20 users when cursor is provided', async () => { test('should resolve right and return next 20 users when cursor is provided', async () => {
mockPrisma.user.findMany.mockResolvedValueOnce(users); mockPrisma.user.findMany.mockResolvedValueOnce(users);
const result = await userService.fetchAllUsers('123344', 20); const result = await userService.fetchAllUsersV2('123344', 20);
expect(result).toEqual(users); expect(result).toEqual(users);
}); });
test('should resolve left and return an empty array when users not found', async () => { test('should resolve left and return an empty array when users not found', async () => {
mockPrisma.user.findMany.mockResolvedValueOnce([]); mockPrisma.user.findMany.mockResolvedValueOnce([]);
const result = await userService.fetchAllUsers(null, 20); const result = await userService.fetchAllUsersV2(null, 20);
expect(result).toEqual([]); expect(result).toEqual([]);
}); });
}); });

View File

@@ -321,13 +321,29 @@ export class UserService {
return E.right(jsonSession.right); return E.right(jsonSession.right);
} }
/**
* Fetch all the users in the `User` table based on cursor
* @param cursorID string of userUID or null
* @param take number of users to query
* @returns an array of `User` object
* @deprecated use fetchAllUsersV2 instead
*/
async fetchAllUsers(cursorID: string, take: number) {
const fetchedUsers = await this.prisma.user.findMany({
skip: cursorID ? 1 : 0,
take: take,
cursor: cursorID ? { uid: cursorID } : undefined,
});
return fetchedUsers;
}
/** /**
* Fetch all the users in the `User` table based on cursor * Fetch all the users in the `User` table based on cursor
* @param searchString search on user's displayName or email * @param searchString search on user's displayName or email
* @param paginationOption pagination options * @param paginationOption pagination options
* @returns an array of `User` object * @returns an array of `User` object
*/ */
async fetchAllUsers( async fetchAllUsersV2(
searchString: string, searchString: string,
paginationOption: OffsetPaginationArgs, paginationOption: OffsetPaginationArgs,
) { ) {