fix pagination issue where user is not part of any space

This commit is contained in:
Philipinho
2025-04-07 19:09:02 +01:00
parent 06270ff747
commit 3559358d14
3 changed files with 15 additions and 6 deletions
@@ -18,6 +18,7 @@ export async function executeWithPagination<O, DB, TB extends keyof DB>(
perPage: number; perPage: number;
page: number; page: number;
experimental_deferredJoinPrimaryKey?: StringReference<DB, TB>; experimental_deferredJoinPrimaryKey?: StringReference<DB, TB>;
hasEmptyIds?: boolean; // in cases where we pass empty whereIn ids
}, },
): Promise<PaginationResult<O>> { ): Promise<PaginationResult<O>> {
if (opts.page < 1) { if (opts.page < 1) {
@@ -39,15 +40,14 @@ export async function executeWithPagination<O, DB, TB extends keyof DB>(
qb = qb qb = qb
.where((eb) => .where((eb) =>
primaryKeys.length > 0 primaryKeys.length > 0
? ? eb(deferredJoinPrimaryKey, 'in', primaryKeys as any)
eb(deferredJoinPrimaryKey, 'in', primaryKeys as any)
: eb(sql`1`, '=', 0), : eb(sql`1`, '=', 0),
) )
.clearOffset() .clearOffset()
.clearLimit(); .clearLimit();
} }
const rows = await qb.execute(); const rows = opts.hasEmptyIds ? [] : await qb.execute();
const hasNextPage = rows.length > 0 ? rows.length > opts.perPage : false; const hasNextPage = rows.length > 0 ? rows.length > opts.perPage : false;
const hasPrevPage = rows.length > 0 ? opts.page > 1 : false; const hasPrevPage = rows.length > 0 ? opts.page > 1 : false;
@@ -107,7 +107,11 @@ export class PageRepo {
return dbOrTx(this.db, trx) return dbOrTx(this.db, trx)
.updateTable('pages') .updateTable('pages')
.set({ ...updatePageData, updatedAt: new Date() }) .set({ ...updatePageData, updatedAt: new Date() })
.where(pageIds.some(pageId => !isValidUUID(pageId)) ? "slugId" : "id", "in", pageIds) .where(
pageIds.some((pageId) => !isValidUUID(pageId)) ? 'slugId' : 'id',
'in',
pageIds,
)
.executeTakeFirst(); .executeTakeFirst();
} }
@@ -161,9 +165,11 @@ export class PageRepo {
.where('spaceId', 'in', userSpaceIds) .where('spaceId', 'in', userSpaceIds)
.orderBy('updatedAt', 'desc'); .orderBy('updatedAt', 'desc');
const hasEmptyIds = userSpaceIds.length === 0;
const result = executeWithPagination(query, { const result = executeWithPagination(query, {
page: pagination.page, page: pagination.page,
perPage: pagination.limit, perPage: pagination.limit,
hasEmptyIds,
}); });
return result; return result;
@@ -221,7 +221,7 @@ export class SpaceMemberRepo {
let query = this.db let query = this.db
.selectFrom('spaces') .selectFrom('spaces')
.selectAll('spaces') .selectAll()
.select((eb) => [this.spaceRepo.withMemberCount(eb)]) .select((eb) => [this.spaceRepo.withMemberCount(eb)])
//.where('workspaceId', '=', workspaceId) //.where('workspaceId', '=', workspaceId)
.where('id', 'in', userSpaceIds) .where('id', 'in', userSpaceIds)
@@ -237,9 +237,12 @@ export class SpaceMemberRepo {
); );
} }
const hasEmptyIds = userSpaceIds.length === 0;
const result = executeWithPagination(query, { const result = executeWithPagination(query, {
page: pagination.page, page: pagination.page,
perPage: pagination.limit, perPage: pagination.limit,
hasEmptyIds,
}); });
return result; return result;