Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change mid to string array #745

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion api/src/channel/lib/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions api/src/chat/controllers/message.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,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');
Expand All @@ -162,8 +166,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,
Expand All @@ -174,8 +183,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,
Expand Down
6 changes: 3 additions & 3 deletions api/src/chat/dto/message.dto.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,8 +11,8 @@ import {
IsBoolean,
IsNotEmpty,
IsObject,
IsString,
IsOptional,
IsString,
} from 'class-validator';

import { IsObjectId } from '@/utils/validation-rules/is-object-id';
Expand All @@ -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()
Expand Down
40 changes: 37 additions & 3 deletions api/src/chat/repositories/message.repository.spec.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -60,7 +60,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']);
Expand All @@ -71,8 +75,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({
...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({
...messageFixtures.find(({ mid }) => mid === message.mid),
...expectedFixture,
mid: toArray(expectedFixture?.mid),
sender,
recipient,
sentBy: user.id,
Expand Down
11 changes: 7 additions & 4 deletions api/src/chat/schemas/message.schema.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion api/src/chat/services/message.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,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');
Expand All @@ -114,8 +117,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,
Expand Down
4 changes: 2 additions & 2 deletions api/src/extensions/channels/web/base-web-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,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 {
Expand All @@ -251,7 +251,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,
});
Expand Down
4 changes: 2 additions & 2 deletions api/src/utils/test/fixtures/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -27,7 +27,7 @@ const messages: MessageCreateDto[] = [
delivery: true,
},
{
mid: 'mid-2',
mid: ['mid-2', 'mid-2.1'],
sender: '1',
recipient: '1',
sentBy: '0',
Expand Down