|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | + |
| 3 | +import { TIMELINE_THREADS_DEFAULT_PAGE_SIZE } from 'src/engine/core-modules/messaging/constants/messaging.constants'; |
| 4 | +import { TimelineThreadsWithTotal } from 'src/engine/core-modules/messaging/dtos/timeline-threads-with-total.dto'; |
| 5 | +import { TimelineMessagingService } from 'src/engine/core-modules/messaging/services/timeline-messaging.service'; |
| 6 | +import { formatThreads } from 'src/engine/core-modules/messaging/utils/format-threads.util'; |
| 7 | +import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager'; |
| 8 | +import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity'; |
| 9 | + |
| 10 | +@Injectable() |
| 11 | +export class GetMessagesService { |
| 12 | + constructor( |
| 13 | + private readonly twentyORMManager: TwentyORMManager, |
| 14 | + private readonly timelineMessagingService: TimelineMessagingService, |
| 15 | + ) {} |
| 16 | + |
| 17 | + async getMessagesFromPersonIds( |
| 18 | + workspaceMemberId: string, |
| 19 | + personIds: string[], |
| 20 | + page = 1, |
| 21 | + pageSize: number = TIMELINE_THREADS_DEFAULT_PAGE_SIZE, |
| 22 | + ): Promise<TimelineThreadsWithTotal> { |
| 23 | + const offset = (page - 1) * pageSize; |
| 24 | + |
| 25 | + const { messageThreads, totalNumberOfThreads } = |
| 26 | + await this.timelineMessagingService.getAndCountMessageThreads( |
| 27 | + personIds, |
| 28 | + offset, |
| 29 | + pageSize, |
| 30 | + ); |
| 31 | + |
| 32 | + if (!messageThreads) { |
| 33 | + return { |
| 34 | + totalNumberOfThreads: 0, |
| 35 | + timelineThreads: [], |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + const messageThreadIds = messageThreads.map( |
| 40 | + (messageThread) => messageThread.id, |
| 41 | + ); |
| 42 | + |
| 43 | + const threadParticipantsByThreadId = |
| 44 | + await this.timelineMessagingService.getThreadParticipantsByThreadId( |
| 45 | + messageThreadIds, |
| 46 | + ); |
| 47 | + |
| 48 | + const threadVisibilityByThreadId = |
| 49 | + await this.timelineMessagingService.getThreadVisibilityByThreadId( |
| 50 | + messageThreadIds, |
| 51 | + workspaceMemberId, |
| 52 | + ); |
| 53 | + |
| 54 | + return { |
| 55 | + totalNumberOfThreads, |
| 56 | + timelineThreads: formatThreads( |
| 57 | + messageThreads, |
| 58 | + threadParticipantsByThreadId, |
| 59 | + threadVisibilityByThreadId, |
| 60 | + ), |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + async getMessagesFromCompanyId( |
| 65 | + workspaceMemberId: string, |
| 66 | + companyId: string, |
| 67 | + page = 1, |
| 68 | + pageSize: number = TIMELINE_THREADS_DEFAULT_PAGE_SIZE, |
| 69 | + ): Promise<TimelineThreadsWithTotal> { |
| 70 | + const personRepository = |
| 71 | + await this.twentyORMManager.getRepository<PersonWorkspaceEntity>( |
| 72 | + 'person', |
| 73 | + ); |
| 74 | + const personIds = ( |
| 75 | + await personRepository.find({ |
| 76 | + where: { |
| 77 | + companyId, |
| 78 | + }, |
| 79 | + select: { |
| 80 | + id: true, |
| 81 | + }, |
| 82 | + }) |
| 83 | + ).map((person) => person.id); |
| 84 | + |
| 85 | + if (personIds.length === 0) { |
| 86 | + return { |
| 87 | + totalNumberOfThreads: 0, |
| 88 | + timelineThreads: [], |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + const messageThreads = await this.getMessagesFromPersonIds( |
| 93 | + workspaceMemberId, |
| 94 | + personIds, |
| 95 | + page, |
| 96 | + pageSize, |
| 97 | + ); |
| 98 | + |
| 99 | + return messageThreads; |
| 100 | + } |
| 101 | +} |
0 commit comments