fix: user table to load on scroll (#29593)

* fix: load more users on table scroll

Signed-off-by: Chayan Das <[email protected]>

* refactor: move user listing logic to UserRepository

Add a dedicated listUsers method with search and cursor pagination support.

Add repository tests covering filtering and pagination.

Ensure user listing metadata remains consistent with the returned results.

Signed-off-by: Chayan Das <[email protected]>

---------

Signed-off-by: Chayan Das <[email protected]>
This commit is contained in:
Chayan Das authored and GitHub committed 2026-06-18 18:09:12 +00:00
1 parent 561cf889ab
commit 4e529cbad1
4 files changed
+250 -75

No files matched your search

@@ -112,3 +112,158 @@ describe("UserRepository", () => {
});
});
});
describe("listUsers", () => {
test("Should return all users matching search term with default pagination", async () => {
await new UserRepository(prismock).create({
username: "alice",
email: "[email protected]",
organizationId: null,
creationSource: CreationSource.WEBAPP,
locked: false,
});
await new UserRepository(prismock).create({
username: "bob",
email: "[email protected]",
organizationId: null,
creationSource: CreationSource.WEBAPP,
locked: false,
});
const { users, total } = await new UserRepository(prismock).listUsers({
searchTerm: null,
cursor: null,
limit: 10,
});
expect(users).toHaveLength(2);
expect(total).toEqual(2);
});
test("Should filter users by searchTerm matching username (case-insensitive)", async () => {
await new UserRepository(prismock).create({
username: "alice",
email: "[email protected]",
organizationId: null,
creationSource: CreationSource.WEBAPP,
locked: false,
});
await new UserRepository(prismock).create({
username: "bob",
email: "[email protected]",
organizationId: null,
creationSource: CreationSource.WEBAPP,
locked: false,
});
const { users, total } = await new UserRepository(prismock).listUsers({
searchTerm: "BOB",
cursor: null,
limit: 10,
});
expect(users).toHaveLength(1);
expect(users[0]).toEqual(
expect.objectContaining({
username: "bob",
})
);
const result = await new UserRepository(prismock).listUsers({
searchTerm: "ALiCE",
cursor: null,
limit: 10,
});
expect(result.users).toHaveLength(1);
expect(result.users[0]).toEqual(
expect.objectContaining({
username: "alice",
})
);
});
test("Should include both locked and unlocked users", async () => {
await new UserRepository(prismock).create({
username: "locked-user",
email: "[email protected]",
organizationId: null,
creationSource: CreationSource.WEBAPP,
locked: true,
});
await new UserRepository(prismock).create({
username: "unlocked-user",
email: "[email protected]",
organizationId: null,
creationSource: CreationSource.WEBAPP,
locked: false,
});
const { users, total } = await new UserRepository(prismock).listUsers({
searchTerm: null,
cursor: null,
limit: 10,
});
expect(total).toEqual(2);
expect(users.map((u) => u.locked).sort()).toEqual([false, true]);
});
test("Should respect limit by returning limit + 1 rows for cursor calculation", async () => {
for (let i = 0; i < 5; i++) {
await new UserRepository(prismock).create({
username: `user${i}`,
email: `user${i}@example.com`,
organizationId: null,
creationSource: CreationSource.WEBAPP,
locked: false,
});
}
const { users, total } = await new UserRepository(prismock).listUsers({
searchTerm: null,
cursor: null,
limit: 3,
});
// take = limit + 1, so up to 4 rows come back to let the caller detect "has more"
expect(users.length).toBeLessThanOrEqual(4);
expect(total).toEqual(5);
});
test("Should return all users when limit is not passed (no cap)", async () => {
for (let i = 0; i < 5; i++) {
await new UserRepository(prismock).create({
username: `nolimituser${i}`,
email: `nolimituser${i}@example.com`,
organizationId: null,
creationSource: CreationSource.WEBAPP,
locked: false,
});
}
const { users, total } = await new UserRepository(prismock).listUsers({
searchTerm: null,
cursor: null,
limit: undefined,
});
expect(users).toHaveLength(5);
expect(total).toEqual(5);
});
test("Should return empty array when no users match searchTerm", async () => {
await new UserRepository(prismock).create({
username: "alice",
email: "[email protected]",
organizationId: null,
creationSource: CreationSource.WEBAPP,
locked: false,
});
const { users, total } = await new UserRepository(prismock).listUsers({
searchTerm: "nonexistent-term-xyz",
cursor: null,
limit: 10,
});
expect(users).toEqual([]);
expect(total).toEqual(0);
});
});
@@ -1587,4 +1587,79 @@ export class UserRepository {
select: { id: true },
});
}
}
async listUsers({
searchTerm,
cursor,
limit,
}: {
searchTerm?: string | null;
cursor: number | null | undefined;
limit?: number | null;
}) {
const bothLockedAndUnlockedWhere: Prisma.UserWhereInput = {
OR: [{ locked: false }, { locked: true }],
};
const trimmedSearchTerm = searchTerm?.trim();
const searchFilters: Prisma.UserWhereInput = trimmedSearchTerm
? {
AND: [
// To bypass the excludeLockedUsersExtension
bothLockedAndUnlockedWhere,
{
OR: [
{ email: { contains: trimmedSearchTerm, mode: "insensitive" } },
{ username: { contains: trimmedSearchTerm, mode: "insensitive" } },
{
profiles: {
some: {
username: { contains: trimmedSearchTerm, mode: "insensitive" },
},
},
},
],
},
],
}
// To bypass the excludeLockedUsersExtension
: bothLockedAndUnlockedWhere;
const hasLimit = limit !== undefined && limit !== null;
const take = hasLimit ? limit + 1 : undefined; // +1 lets us detect "has more" for the cursor
const users = await this.prismaClient.user.findMany({
cursor: cursor ? { id: cursor } : undefined,
skip: cursor ? 1 : 0,
...(take !== undefined ? { take } : {}),
where: searchFilters,
orderBy: {
id: "asc",
},
select: {
id: true,
locked: true,
email: true,
username: true,
name: true,
timeZone: true,
role: true,
profiles: {
select: {
username: true,
},
},
},
});
if (!hasLimit) {
return { users, nextCursor: undefined, total: users.length };
}
const total = await this.prismaClient.user.count({
where: searchFilters,
});
const hasMore = users.length > limit;
const items = hasMore ? users.slice(0, limit) : users;
const nextCursor = hasMore ? items[items.length - 1].id : undefined;
return { users: items, nextCursor, total };
}
}