3675 inbox count is wrong in emailthreads (#3677)
* add type * query total number of threads * graphql data generate * wip * wip * Fix fetch more * fix --------- Co-authored-by: Thomas Trompette <thomast@twenty.com>
This commit is contained in:
@ -0,0 +1,12 @@
|
||||
import { Field, Int, ObjectType } from '@nestjs/graphql';
|
||||
|
||||
import { TimelineThread } from 'src/core/messaging/dtos/timeline-thread.dto';
|
||||
|
||||
@ObjectType('TimelineThreadsWithTotal')
|
||||
export class TimelineThreadsWithTotal {
|
||||
@Field(() => Int)
|
||||
totalNumberOfThreads: number;
|
||||
|
||||
@Field(() => [TimelineThread])
|
||||
timelineThreads: TimelineThread[];
|
||||
}
|
||||
@ -15,8 +15,8 @@ import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
|
||||
import { Workspace } from 'src/core/workspace/workspace.entity';
|
||||
import { AuthWorkspace } from 'src/decorators/auth-workspace.decorator';
|
||||
import { TimelineMessagingService } from 'src/core/messaging/timeline-messaging.service';
|
||||
import { TimelineThread } from 'src/core/messaging/dtos/timeline-thread.dto';
|
||||
import { TIMELINE_THREADS_MAX_PAGE_SIZE } from 'src/core/messaging/constants/messaging.constants';
|
||||
import { TimelineThreadsWithTotal } from 'src/core/messaging/dtos/timeline-threads-with-total.dto';
|
||||
|
||||
@ArgsType()
|
||||
class GetTimelineThreadsFromPersonIdArgs {
|
||||
@ -45,13 +45,13 @@ class GetTimelineThreadsFromCompanyIdArgs {
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Resolver(() => [TimelineThread])
|
||||
@Resolver(() => TimelineThreadsWithTotal)
|
||||
export class TimelineMessagingResolver {
|
||||
constructor(
|
||||
private readonly timelineMessagingService: TimelineMessagingService,
|
||||
) {}
|
||||
|
||||
@Query(() => [TimelineThread])
|
||||
@Query(() => TimelineThreadsWithTotal)
|
||||
async getTimelineThreadsFromPersonId(
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
@Args() { personId, page, pageSize }: GetTimelineThreadsFromPersonIdArgs,
|
||||
@ -67,7 +67,7 @@ export class TimelineMessagingResolver {
|
||||
return timelineThreads;
|
||||
}
|
||||
|
||||
@Query(() => [TimelineThread])
|
||||
@Query(() => TimelineThreadsWithTotal)
|
||||
async getTimelineThreadsFromCompanyId(
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
@Args() { companyId, page, pageSize }: GetTimelineThreadsFromCompanyIdArgs,
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { TIMELINE_THREADS_DEFAULT_PAGE_SIZE } from 'src/core/messaging/constants/messaging.constants';
|
||||
import { TimelineThread } from 'src/core/messaging/dtos/timeline-thread.dto';
|
||||
import { TimelineThreadsWithTotal } from 'src/core/messaging/dtos/timeline-threads-with-total.dto';
|
||||
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
|
||||
import { DataSourceService } from 'src/metadata/data-source/data-source.service';
|
||||
|
||||
@ -27,7 +27,7 @@ export class TimelineMessagingService {
|
||||
personIds: string[],
|
||||
page: number = 1,
|
||||
pageSize: number = TIMELINE_THREADS_DEFAULT_PAGE_SIZE,
|
||||
): Promise<TimelineThread[]> {
|
||||
): Promise<TimelineThreadsWithTotal> {
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const dataSourceMetadata =
|
||||
@ -82,7 +82,10 @@ export class TimelineMessagingService {
|
||||
);
|
||||
|
||||
if (!messageThreads) {
|
||||
return [];
|
||||
return {
|
||||
totalNumberOfThreads: 0,
|
||||
timelineThreads: [],
|
||||
};
|
||||
}
|
||||
|
||||
const messageThreadIds = messageThreads.map(
|
||||
@ -239,6 +242,23 @@ export class TimelineMessagingService {
|
||||
[messageThreadIds],
|
||||
);
|
||||
|
||||
const totalNumberOfThreads = await workspaceDataSource?.query(
|
||||
`
|
||||
SELECT COUNT(DISTINCT "messageThread".id)
|
||||
FROM
|
||||
${dataSourceMetadata.schema}."message" message
|
||||
LEFT JOIN
|
||||
${dataSourceMetadata.schema}."messageThread" "messageThread" ON "messageThread".id = message."messageThreadId"
|
||||
LEFT JOIN
|
||||
${dataSourceMetadata.schema}."messageParticipant" "messageParticipant" ON "messageParticipant"."messageId" = message.id
|
||||
LEFT JOIN
|
||||
${dataSourceMetadata.schema}."person" person ON person.id = "messageParticipant"."personId"
|
||||
WHERE
|
||||
person.id = ANY($1)
|
||||
`,
|
||||
[personIds],
|
||||
);
|
||||
|
||||
const threadParticipantsByThreadId: {
|
||||
[key: string]: TimelineThreadParticipant[];
|
||||
} = messageThreadIds.reduce((messageThreadIdAcc, messageThreadId) => {
|
||||
@ -350,7 +370,10 @@ export class TimelineMessagingService {
|
||||
};
|
||||
});
|
||||
|
||||
return timelineThreads;
|
||||
return {
|
||||
totalNumberOfThreads: totalNumberOfThreads[0]?.count ?? 0,
|
||||
timelineThreads,
|
||||
};
|
||||
}
|
||||
|
||||
async getMessagesFromCompanyId(
|
||||
@ -358,7 +381,7 @@ export class TimelineMessagingService {
|
||||
companyId: string,
|
||||
page: number = 1,
|
||||
pageSize: number = TIMELINE_THREADS_DEFAULT_PAGE_SIZE,
|
||||
) {
|
||||
): Promise<TimelineThreadsWithTotal> {
|
||||
const dataSourceMetadata =
|
||||
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceIdOrFail(
|
||||
workspaceId,
|
||||
@ -380,7 +403,10 @@ export class TimelineMessagingService {
|
||||
);
|
||||
|
||||
if (!personIds) {
|
||||
return [];
|
||||
return {
|
||||
totalNumberOfThreads: 0,
|
||||
timelineThreads: [],
|
||||
};
|
||||
}
|
||||
|
||||
const formattedPersonIds = personIds.map(
|
||||
|
||||
Reference in New Issue
Block a user