Fix thread pagination (#10485)

Fixes https://github.com/twentyhq/twenty/issues/10308.

The issue came from the fact that the pagination inside the
`findAndCount` wasn't working properly. The limit was applied on the
joint table. Adding a `groupBy` fixes this, but since it isn't available
on the `findAndCount` I had to modify the query using the query builder.
This commit is contained in:
Raphaël Bosi
2025-02-25 17:44:42 +01:00
committed by GitHub
parent 2331176c53
commit 42ea02329a

View File

@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Any, Not } from 'typeorm'; import { In, Not } from 'typeorm';
import { TimelineThread } from 'src/engine/core-modules/messaging/dtos/timeline-thread.dto'; import { TimelineThread } from 'src/engine/core-modules/messaging/dtos/timeline-thread.dto';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager'; import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
@ -32,34 +32,32 @@ export class TimelineMessagingService {
'messageThread', 'messageThread',
); );
const [messageThreadIds, totalNumberOfThreads] = const totalNumberOfThreads = await messageThreadRepository
await messageThreadRepository.findAndCount({ .createQueryBuilder('messageThread')
select: { .innerJoin('messageThread.messages', 'messages')
id: true, .innerJoin('messages.messageParticipants', 'messageParticipants')
}, .where('messageParticipants.personId IN(:...personIds)', { personIds })
where: { .groupBy('messageThread.id')
messages: { .getCount();
messageParticipants: {
personId: Any(personIds), const threadIdsQuery = await messageThreadRepository
}, .createQueryBuilder('messageThread')
}, .select('messageThread.id', 'id')
}, .addSelect('MAX(messages.receivedAt)', 'max_received_at')
order: { .innerJoin('messageThread.messages', 'messages')
messages: { .innerJoin('messages.messageParticipants', 'messageParticipants')
receivedAt: 'DESC', .where('messageParticipants.personId IN (:...personIds)', { personIds })
}, .groupBy('messageThread.id')
}, .orderBy('max_received_at', 'DESC')
skip: offset, .offset(offset)
take: pageSize, .limit(pageSize)
relations: ['messages'], .getRawMany();
});
const messageThreadIds = threadIdsQuery.map((thread) => thread.id);
const messageThreads = await messageThreadRepository.find({ const messageThreads = await messageThreadRepository.find({
select: {
id: true,
},
where: { where: {
id: Any(messageThreadIds.map((thread) => thread.id)), id: In(messageThreadIds),
}, },
order: { order: {
messages: { messages: {
@ -187,7 +185,7 @@ export class TimelineMessagingService {
id: true, id: true,
}, },
where: { where: {
id: Any(messageThreadIds), id: In(messageThreadIds),
messages: { messages: {
messageChannelMessageAssociations: { messageChannelMessageAssociations: {
messageChannel: { messageChannel: {