Skip to content

Commit 0fc11ad

Browse files
Johan BookJohan Book
Johan Book
authored and
Johan Book
committed
feat(api): include profile name and photo in chat messages
1 parent 96926db commit 0fc11ad

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class ChatMessageProfileDetails {
2+
id!: number;
3+
imageUrl?: string;
4+
name!: string;
5+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import { ChatMessageProfileDetails } from "./chat-message-profile.dto";
2+
13
export class ChatMessageDetails {
24
id!: number;
35
message!: string;
4-
read!: boolean;
6+
profile!: ChatMessageProfileDetails;
57
sentByCurrentUser!: boolean;
8+
read!: boolean;
69
}

services/api/src/features/chat/application/handlers/query-handlers/get-chat-messages.handler.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe(GetChatMessagesHandler.name, () => {
2525

2626
const chatMessage = new ChatMessage();
2727
chatMessage.conversationId = conversation.id;
28+
chatMessage.sender = sendingProfile;
2829
chatMessage.senderId = sendingProfile.id;
2930

3031
chatMessages = createMockRepository<ChatMessage>([chatMessage]);
@@ -35,10 +36,13 @@ describe(GetChatMessagesHandler.name, () => {
3536
fetchCurrentProfileId: jest.fn(() => sendingProfile.id),
3637
} as any;
3738

39+
const photoService = { getUrl: jest.fn() } as any;
40+
3841
queryHandler = new GetChatMessagesHandler(
3942
chatMessages,
4043
currentOrganizationService,
4144
currentProfileService,
45+
photoService,
4246
);
4347
});
4448

services/api/src/features/chat/application/handlers/query-handlers/get-chat-messages.handler.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { IQueryHandler, QueryHandler } from "@nestjs/cqrs";
22
import { InjectRepository } from "@nestjs/typeorm";
33
import { Repository } from "typeorm";
44

5-
import { mapArray } from "src/core/mapper";
5+
import { map, mapArray } from "src/core/mapper";
66
import { CurrentOrganizationService } from "src/core/organizations";
7+
import { PhotoService } from "src/core/photos";
78
import { CurrentProfileService } from "src/core/profiles";
89

910
import { ChatMessage } from "../../..//infrastructure/entities/chat-message.entity";
10-
import { ChatMessageDetails } from "../../contracts/dtos/chat.dto";
11+
import { ChatMessageProfileDetails } from "../../contracts/dtos/chat-message-profile.dto";
12+
import { ChatMessageDetails } from "../../contracts/dtos/chat-message.dto";
1113
import { GetChatMessagesQuery } from "../../contracts/queries/get-chat-messages.query";
1214

1315
@QueryHandler(GetChatMessagesQuery)
@@ -19,6 +21,7 @@ export class GetChatMessagesHandler
1921
private readonly chatMessages: Repository<ChatMessage>,
2022
private readonly currentOrganizationService: CurrentOrganizationService,
2123
private readonly currentProfileService: CurrentProfileService,
24+
private readonly photoService: PhotoService,
2225
) {}
2326

2427
async execute(query: GetChatMessagesQuery) {
@@ -29,6 +32,11 @@ export class GetChatMessagesHandler
2932
await this.currentOrganizationService.fetchCurrentOrganizationId();
3033

3134
const foundChatMessages = await this.chatMessages.find({
35+
relations: {
36+
sender: {
37+
profilePhoto: true,
38+
},
39+
},
3240
where: {
3341
conversation: {
3442
id: query.conversationId,
@@ -40,8 +48,15 @@ export class GetChatMessagesHandler
4048
return mapArray(ChatMessageDetails, foundChatMessages, (item) => ({
4149
id: item.id,
4250
message: item.message,
43-
read: false,
51+
profile: map(ChatMessageProfileDetails, {
52+
id: item.sender.id,
53+
name: item.sender.name,
54+
imageUrl:
55+
item.sender.profilePhoto &&
56+
this.photoService.getUrl(item.sender.profilePhoto, "profile-photo"),
57+
}),
4458
sentByCurrentUser: item.senderId == currentProfileId,
59+
read: false,
4560
}));
4661
}
4762
}

services/api/src/features/chat/client/controllers/chats.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ApiTags } from "@nestjs/swagger";
55
import { CreateChatCommand } from "../../application/contracts/commands/create-chat.command";
66
import { PostChatMessageCommand } from "../../application/contracts/commands/post-chat-message.command";
77
import { ChatConversationDetails } from "../../application/contracts/dtos/chat-conversation.dto";
8-
import { ChatMessageDetails } from "../../application/contracts/dtos/chat.dto";
8+
import { ChatMessageDetails } from "../../application/contracts/dtos/chat-message.dto";
99
import { GetChatMessagesQuery } from "../../application/contracts/queries/get-chat-messages.query";
1010
import { GetConversationListQuery } from "../../application/contracts/queries/get-conversation-list.query";
1111

0 commit comments

Comments
 (0)