forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use twentyORM in Timeline messaging (twentyhq#6595)
- Remove raw queries and replace them by using `twentyORM` - Refactor into services and utils --------- Co-authored-by: Charles Bochet <[email protected]>
- Loading branch information
1 parent
6927f46
commit 08c7947
Showing
19 changed files
with
513 additions
and
559 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...s/twenty-server/src/engine/core-modules/messaging/dtos/timeline-thread-participant.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
packages/twenty-server/src/engine/core-modules/messaging/services/get-messages.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { TIMELINE_THREADS_DEFAULT_PAGE_SIZE } from 'src/engine/core-modules/messaging/constants/messaging.constants'; | ||
import { TimelineThreadsWithTotal } from 'src/engine/core-modules/messaging/dtos/timeline-threads-with-total.dto'; | ||
import { TimelineMessagingService } from 'src/engine/core-modules/messaging/services/timeline-messaging.service'; | ||
import { formatThreads } from 'src/engine/core-modules/messaging/utils/format-threads.util'; | ||
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager'; | ||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity'; | ||
|
||
@Injectable() | ||
export class GetMessagesService { | ||
constructor( | ||
private readonly twentyORMManager: TwentyORMManager, | ||
private readonly timelineMessagingService: TimelineMessagingService, | ||
) {} | ||
|
||
async getMessagesFromPersonIds( | ||
workspaceMemberId: string, | ||
personIds: string[], | ||
page = 1, | ||
pageSize: number = TIMELINE_THREADS_DEFAULT_PAGE_SIZE, | ||
): Promise<TimelineThreadsWithTotal> { | ||
const offset = (page - 1) * pageSize; | ||
|
||
const { messageThreads, totalNumberOfThreads } = | ||
await this.timelineMessagingService.getAndCountMessageThreads( | ||
personIds, | ||
offset, | ||
pageSize, | ||
); | ||
|
||
if (!messageThreads) { | ||
return { | ||
totalNumberOfThreads: 0, | ||
timelineThreads: [], | ||
}; | ||
} | ||
|
||
const messageThreadIds = messageThreads.map( | ||
(messageThread) => messageThread.id, | ||
); | ||
|
||
const threadParticipantsByThreadId = | ||
await this.timelineMessagingService.getThreadParticipantsByThreadId( | ||
messageThreadIds, | ||
); | ||
|
||
const threadVisibilityByThreadId = | ||
await this.timelineMessagingService.getThreadVisibilityByThreadId( | ||
messageThreadIds, | ||
workspaceMemberId, | ||
); | ||
|
||
return { | ||
totalNumberOfThreads, | ||
timelineThreads: formatThreads( | ||
messageThreads, | ||
threadParticipantsByThreadId, | ||
threadVisibilityByThreadId, | ||
), | ||
}; | ||
} | ||
|
||
async getMessagesFromCompanyId( | ||
workspaceMemberId: string, | ||
companyId: string, | ||
page = 1, | ||
pageSize: number = TIMELINE_THREADS_DEFAULT_PAGE_SIZE, | ||
): Promise<TimelineThreadsWithTotal> { | ||
const personRepository = | ||
await this.twentyORMManager.getRepository<PersonWorkspaceEntity>( | ||
'person', | ||
); | ||
const personIds = ( | ||
await personRepository.find({ | ||
where: { | ||
companyId, | ||
}, | ||
select: { | ||
id: true, | ||
}, | ||
}) | ||
).map((person) => person.id); | ||
|
||
if (personIds.length === 0) { | ||
return { | ||
totalNumberOfThreads: 0, | ||
timelineThreads: [], | ||
}; | ||
} | ||
|
||
const messageThreads = await this.getMessagesFromPersonIds( | ||
workspaceMemberId, | ||
personIds, | ||
page, | ||
pageSize, | ||
); | ||
|
||
return messageThreads; | ||
} | ||
} |
Oops, something went wrong.