Skip to content

Commit

Permalink
fix get threads pagination query
Browse files Browse the repository at this point in the history
  • Loading branch information
rbennettcw authored and ilijabojanovic committed Dec 18, 2024
1 parent 0ac8af2 commit 0d4091f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
41 changes: 13 additions & 28 deletions libs/model/src/thread/GetThreads.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,6 @@ export function GetThreads(): Query<typeof schemas.GetThreads> {
pastWinners: ' AND CON.end_time <= NOW()',
all: '',
};
const baseWhereClause = `
community_id = :community_id AND
deleted_at IS NULL AND
archived_at IS ${archived ? 'NOT' : ''} NULL
${topic_id ? ' AND topic_id = :topic_id' : ''}
${stage ? ' AND stage = :stage' : ''}
${from_date ? ' AND T.created_at > :from_date' : ''}
${to_date ? ' AND T.created_at < :to_date' : ''}
${contestAddress ? ' AND id IN (SELECT * FROM "contest_ids")' : ''}
`;
const responseThreadsQuery = models.sequelize.query<
z.infer<typeof schemas.ThreadView>
>(
Expand All @@ -96,10 +86,18 @@ export function GetThreads(): Query<typeof schemas.GetThreads> {
pinned, community_id, T.created_at, updated_at, locked_at as thread_locked, links,
has_poll, last_commented_on, comment_count as "numberOfComments",
marked_as_spam_at, archived_at, topic_id, reaction_weights_sum, canvas_signed_data,
canvas_msg_id, last_edited, address_id, reaction_count
canvas_msg_id, last_edited, address_id, reaction_count,
(COUNT(id) OVER())::INTEGER AS total_num_thread_results
FROM "Threads" T
WHERE ${baseWhereClause}
WHERE
community_id = :community_id AND
deleted_at IS NULL AND
archived_at IS ${archived ? 'NOT' : ''} NULL
${topic_id ? ' AND topic_id = :topic_id' : ''}
${stage ? ' AND stage = :stage' : ''}
${from_date ? ' AND T.created_at > :from_date' : ''}
${to_date ? ' AND T.created_at < :to_date' : ''}
${contestAddress ? ' AND id IN (SELECT * FROM "contest_ids")' : ''}
ORDER BY pinned DESC, ${orderByQueries[order_by ?? 'newest']}
LIMIT :limit OFFSET :offset
), thread_metadata AS (
Expand Down Expand Up @@ -252,37 +250,24 @@ export function GetThreads(): Query<typeof schemas.GetThreads> {
},
);

const countThreadsQuery = models.sequelize.query<{ count: number }>(
`
SELECT COUNT(*) AS count
FROM "Threads" T
WHERE ${baseWhereClause}
`,
{
replacements,
type: QueryTypes.SELECT,
},
);

const numVotingThreadsQuery = models.Thread.count({
where: {
community_id,
stage: 'voting',
},
});

const [threads, numVotingThreads, countResult] = await Promise.all([
const [threads, numVotingThreads] = await Promise.all([
responseThreadsQuery,
numVotingThreadsQuery,
countThreadsQuery,
]);

return {
limit: replacements.limit,
page: replacements.page,
threads,
numVotingThreads,
threadCount: Number(countResult[0]?.count) || 0,
threadsCount: threads.at(0)?.total_num_thread_results || 0,
};
},
};
Expand Down
4 changes: 4 additions & 0 deletions libs/schemas/src/queries/thread.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ export const ThreadView = Thread.extend({
Comments: z.array(CommentView).optional(),
ThreadVersionHistories: z.array(ThreadVersionHistoryView).nullish(),
search: z.union([z.string(), z.record(z.any())]).nullish(),
total_num_thread_results: z
.number()
.nullish()
.describe('total number of thread results for the query'),
});

export const OrderByQueriesKeys = z.enum([
Expand Down

0 comments on commit 0d4091f

Please sign in to comment.