This commit is contained in:
medtaher 2025-05-09 21:13:34 +00:00 committed by GitHub
commit 704cb76798
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 76 additions and 17 deletions

View File

@ -214,7 +214,7 @@ export default abstract class ChannelHandler<
envelope: StdOutgoingEnvelope,
options: any,
context: any,
): Promise<{ mid: string }>;
): Promise<{ mid: string | string[] }>;
/**
* Calls the channel handler to fetch attachments and stores them

View File

@ -144,6 +144,10 @@ describe('MessageController', () => {
afterAll(closeInMongodConnection);
function toArray(value?: string | string[]): string[] {
return value ? (Array.isArray(value) ? value : [value]) : [];
}
describe('count', () => {
it('should count messages', async () => {
jest.spyOn(messageService, 'count');
@ -165,8 +169,13 @@ describe('MessageController', () => {
expect(messageService.findOneAndPopulate).toHaveBeenCalledWith(
message.id,
);
const expectedFixture = messageFixtures.find(
({ mid }) =>
JSON.stringify(toArray(mid)) === JSON.stringify(message.mid),
);
expect(result).toEqualPayload({
...messageFixtures.find(({ mid }) => mid === message.mid),
...expectedFixture,
mid: toArray(expectedFixture?.mid),
sender,
recipient,
sentBy: user.id,
@ -177,8 +186,13 @@ describe('MessageController', () => {
const result = await messageController.findOne(message.id, []);
expect(messageService.findOne).toHaveBeenCalledWith(message.id);
const expectedFixture = messageFixtures.find(
({ mid }) =>
JSON.stringify(toArray(mid)) === JSON.stringify(message.mid),
);
expect(result).toEqualPayload({
...messageFixtures.find(({ mid }) => mid === message.mid),
...expectedFixture,
mid: toArray(expectedFixture?.mid),
sender: sender.id,
recipient: recipient.id,
sentBy: user.id,

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -11,8 +11,8 @@ import {
IsBoolean,
IsNotEmpty,
IsObject,
IsString,
IsOptional,
IsString,
} from 'class-validator';
import { IsObjectId } from '@/utils/validation-rules/is-object-id';
@ -27,7 +27,7 @@ export class MessageCreateDto {
@ApiProperty({ description: 'Message id', type: String })
@IsOptional()
@IsString()
mid?: string;
mid?: string | string[];
@ApiProperty({ description: 'Reply to Message id', type: String })
@IsOptional()

View File

@ -56,7 +56,11 @@ describe('MessageRepository', () => {
afterAll(closeInMongodConnection);
describe('findOneAndPopulate', () => {
it('should find one message by id, and populate its sender and recipient', async () => {
function toArray(value?: string | string[]): string[] {
return value ? (Array.isArray(value) ? value : [value]) : [];
}
it('should find one message by id, and populate its sender and recipient (with mid being a string)', async () => {
jest.spyOn(messageModel, 'findById');
const message = (await messageRepository.findOne({ mid: 'mid-1' }))!;
const sender = await subscriberRepository.findOne(message!['sender']);
@ -67,8 +71,38 @@ describe('MessageRepository', () => {
const result = await messageRepository.findOneAndPopulate(message.id);
expect(messageModel.findById).toHaveBeenCalledWith(message.id, undefined);
const expectedFixture = messageFixtures.find(
({ mid }) =>
JSON.stringify(toArray(mid)) === JSON.stringify(message.mid),
);
expect(result).toEqualPayload({
...messageFixtures.find(({ mid }) => mid === message.mid),
...expectedFixture,
mid: toArray(expectedFixture?.mid),
sender,
recipient,
sentBy: user.id,
});
});
it('should find one message by id, and populate its sender and recipient (with mid being a string array)', async () => {
jest.spyOn(messageModel, 'findById');
const message = (await messageRepository.findOne({ mid: 'mid-2' }))!;
const sender = await subscriberRepository.findOne(message!['sender']);
const recipient = await subscriberRepository.findOne(
message!['recipient'],
);
const user = (await userRepository.findOne(message!['sentBy']))!;
const result = await messageRepository.findOneAndPopulate(message.id);
expect(messageModel.findById).toHaveBeenCalledWith(message.id, undefined);
const expectedFixture = messageFixtures.find(
({ mid }) =>
JSON.stringify(toArray(mid)) === JSON.stringify(message.mid),
);
expect(result).toEqualPayload({
...expectedFixture,
mid: toArray(expectedFixture?.mid),
sender,
recipient,
sentBy: user.id,

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -20,11 +20,11 @@ import { StdIncomingMessage, StdOutgoingMessage } from './types/message';
@Schema({ timestamps: true })
export class MessageStub extends BaseSchema {
@Prop({
type: String,
type: [String],
required: false,
//TODO : add default value for mid
})
mid?: string;
mid?: string | string[];
@Prop({
type: MongooseSchema.Types.ObjectId,
@ -96,9 +96,12 @@ export class MessageFull extends MessageStub {
sentBy?: string; // sendBy is never populate
}
const MessageSchema = SchemaFactory.createForClass(MessageStub);
MessageSchema.index({ mid: 1 });
export const MessageModel: ModelDefinition = LifecycleHookManager.attach({
name: Message.name,
schema: SchemaFactory.createForClass(MessageStub),
schema: MessageSchema,
});
export default MessageModel.schema;

View File

@ -107,6 +107,9 @@ describe('MessageService', () => {
afterEach(jest.clearAllMocks);
afterAll(closeInMongodConnection);
function toArray(value?: string | string[]): string[] {
return value ? (Array.isArray(value) ? value : [value]) : [];
}
describe('findOneAndPopulate', () => {
it('should find message by id, and populate its corresponding sender and recipient', async () => {
jest.spyOn(messageRepository, 'findOneAndPopulate');
@ -116,8 +119,13 @@ describe('MessageService', () => {
message.id,
undefined,
);
const expectedFixture = messageFixtures.find(
({ mid }) =>
JSON.stringify(toArray(mid)) === JSON.stringify(message.mid),
);
expect(result).toEqualPayload({
...messageFixtures.find(({ mid }) => mid === message.mid),
...expectedFixture,
mid: toArray(expectedFixture?.mid),
sender,
recipient,
sentBy: user.id,

View File

@ -241,7 +241,7 @@ export default abstract class BaseWebChannelHandler<
...message,
author: anyMessage.sender,
read: true, // Temporary fix as read is false in the bd
mid: anyMessage.mid,
mid: anyMessage.mid?.[0],
createdAt: anyMessage.createdAt,
});
} else {
@ -250,7 +250,7 @@ export default abstract class BaseWebChannelHandler<
...message,
author: 'chatbot',
read: true, // Temporary fix as read is false in the bd
mid: anyMessage.mid || this.generateId(),
mid: anyMessage.mid?.[0] || this.generateId(),
handover: !!anyMessage.handover,
createdAt: anyMessage.createdAt,
});

View File

@ -9,7 +9,7 @@
import mongoose from 'mongoose';
import { MessageCreateDto } from '@/chat/dto/message.dto';
import { MessageModel, Message } from '@/chat/schemas/message.schema';
import { Message, MessageModel } from '@/chat/schemas/message.schema';
import { getFixturesWithDefaultValues } from '../defaultValues';
import { TFixturesDefaultValues } from '../types';
@ -27,7 +27,7 @@ const messages: MessageCreateDto[] = [
delivery: true,
},
{
mid: 'mid-2',
mid: ['mid-2', 'mid-2.1'],
sender: '1',
recipient: '1',
sentBy: '0',