Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/__tests__/chat/chatService/addReaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('ChatService.addReaction() test suite', () => {
let chatService: ChatService;
const chatMessageBuilder = ChatBuilderFactory.getBuilder('ChatMessage');
const chatModel = ChatModule.getChatModel();
const mockSenderId = '60f7c2d9a2d3c7b7e56d01df';

const senderId = new ObjectId();
const reactPlayerName = 'ReactMan420';
Expand All @@ -27,11 +28,12 @@ describe('ChatService.addReaction() test suite', () => {
chat._id.toString(),
reactPlayerName,
'👍',
mockSenderId,
);

expect(err).toBeNull();
expect(updated.reactions).toEqual([
{ playerName: reactPlayerName, emoji: '👍' },
{ playerName: reactPlayerName, emoji: '👍', sender_id: mockSenderId },
]);
});

Expand All @@ -42,11 +44,12 @@ describe('ChatService.addReaction() test suite', () => {
chat._id.toString(),
reactPlayerName,
'😂',
mockSenderId,
);

expect(err).toBeNull();
expect(updated.reactions).toEqual([
{ playerName: reactPlayerName, emoji: '😂' },
{ playerName: reactPlayerName, emoji: '😂', sender_id: mockSenderId },
]);
});

Expand All @@ -57,6 +60,7 @@ describe('ChatService.addReaction() test suite', () => {
chat._id.toString(),
reactPlayerName,
'',
mockSenderId,
);

expect(err).toBeNull();
Expand All @@ -69,6 +73,7 @@ describe('ChatService.addReaction() test suite', () => {
fakeId,
'NoPlayer',
'🔥',
mockSenderId,
);

expect(updated).toBeNull();
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/chat/data/builder/ReactionDtoBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default class ReactionDtoBuilder implements IDataBuilder<ReactionDto> {
private readonly base: ReactionDto = {
playerName: 'TestPlayer420',
emoji: '👍',
sender_id: '60f7c2d9a2d3c7b7e56d01df',
};

build(): ReactionDto {
Expand Down
7 changes: 7 additions & 0 deletions src/chat/dto/reaction.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ export class ReactionDto {
*/
@Expose()
emoji: string;

/**
* Player id of the user
* @example "123456789"
*/
@Expose()
sender_id: string;
}
3 changes: 3 additions & 0 deletions src/chat/schema/reaction.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ export class Reaction {

@Prop({ type: String, required: true })
emoji: string;

@Prop({ type: String, required: true })
sender_id: string;
}
1 change: 1 addition & 0 deletions src/chat/service/baseChat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export abstract class BaseChatService {
reaction.message_id,
client.user.name,
reaction.emoji,
client.user.playerId,
);

if (error) {
Expand Down
40 changes: 5 additions & 35 deletions src/chat/service/chat.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import {} from '../../common/base/decorator/AddBasicService.decorator';
import { ChatMessage } from '../schema/chatMessage.schema';
import { Model } from 'mongoose';
import BasicService from '../../common/service/basicService/BasicService';
Expand All @@ -16,39 +15,34 @@ import { SEReason } from '../../common/service/basicService/SEReason';

@Injectable()
export class ChatService {
private readonly basicService: BasicService;
public constructor(
@InjectModel(ChatMessage.name)
public readonly model: Model<ChatMessage>,
) {
this.basicService = new BasicService(model);
}

private readonly basicService: BasicService;

/**
* Creates a message in database.
*
* @param message - Message data to create.
* @returns Created message.
*/
async createChatMessage(message: CreateChatMessageDto) {
return this.basicService.createOne<CreateChatMessageDto, ChatMessageDto>(
message,
);
}

/**
* Adds an reaction to chat message.
* Adds a reaction to chat message.
*
* @param messageId - ID of the message reaction is to.
* @param playerName - Name of the player who reacted.
* @param emoji - String representation of the emoji.
* @param sender_id - Unique ID of the player reacting.
* @returns Message with added reaction.
*/
async addReaction(
messageId: string,
playerName: string,
emoji: string,
sender_id: string,
): Promise<IServiceReturn<ChatMessageDto>> {
const [message, error] =
await this.basicService.readOneById<ChatMessageDto>(messageId);
Expand All @@ -59,7 +53,7 @@ export class ChatService {
(r) => r.playerName !== playerName,
);

if (emoji) message.reactions.push({ playerName, emoji });
if (emoji) message.reactions.push({ playerName, emoji, sender_id });

const [, updateError] = await this.basicService.updateOneById(
message._id,
Expand All @@ -71,12 +65,6 @@ export class ChatService {
return [message, null];
}

/**
* Retrieves messages from database.
*
* @param options - Database query options.
* @returns An array of chat messages.
*/
async getMessages(
options?: TIServiceReadManyOptions,
): Promise<IServiceReturn<ChatMessageDto[]>> {
Expand All @@ -87,15 +75,6 @@ export class ChatService {
return await this.basicService.readMany<ChatMessageDto>(opts);
}

/**
* Updates a ChatMessage by its _id in DB. The _id field is read-only and must be found from the parameter
*
* @param chat - The data needs to be updated of the ChatMessage.
* @returns _true_ if ChatMessage was updated successfully, _false_ if nothing was updated for the ChatMessage,
* or a ServiceError:
* - NOT_FOUND if the ChatMessage was not found
* - REQUIRED if _id is not provided
*/
async updateOneById(
chat: Partial<UpdateChatMessageDto>,
): Promise<[boolean | null, ServiceError[] | null]> {
Expand All @@ -122,15 +101,6 @@ export class ChatService {
return [isSuccess, errors];
}

/**
* Deletes the chatmessage.
*
* @param chatId - The ID of the chatmessage to delete.
* @returns _true_ if ChatMessage was deleted successfully, _false_ if nothing was deleted
* or a ServiceError:
* - NOT_FOUND if the ChatMessage was not found
* - REQUIRED if _id is not provided
*/
async deleteChatMessageById(chatId: string): Promise<IServiceReturn<true>> {
return await this.basicService.deleteOneById(chatId);
}
Expand Down