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

[IMPROVE] Add searchTerm support for livechat/rid/messages API #27214

Merged
merged 3 commits into from
Nov 21, 2022
Merged
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
3 changes: 2 additions & 1 deletion apps/meteor/app/livechat/imports/server/rest/visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ API.v1.addRoute(
async get() {
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();
const { searchTerm } = this.requestParams();

const room = LivechatRooms.findOneById(this.urlParams.rid);

Expand All @@ -174,7 +175,7 @@ API.v1.addRoute(
throw new Error('not-allowed');
}

const { cursor, totalCount } = Messages.findLivechatClosedMessages(this.urlParams.rid, {
const { cursor, totalCount } = Messages.findLivechatClosedMessages(this.urlParams.rid, searchTerm, {
sort: sort || { ts: -1 },
skip: offset,
limit: count,
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/server/models/raw/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,12 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
return this.col.aggregate(params).toArray();
}

findLivechatClosedMessages(rid: IRoom['_id'], options: FindOptions<IMessage>): FindPaginated<FindCursor<IMessage>> {
findLivechatClosedMessages(rid: IRoom['_id'], searchTerm?: string, options?: FindOptions<IMessage>): FindPaginated<FindCursor<IMessage>> {
return this.findPaginated(
{
rid,
$or: [{ t: { $exists: false } }, { t: 'livechat-close' }],
...(searchTerm && { msg: new RegExp(escapeRegExp(searchTerm), 'ig') }),
},
options,
);
Expand Down
57 changes: 57 additions & 0 deletions apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,63 @@ describe('LIVECHAT - rooms', function () {
expect(body.total).to.be.an('number').equal(1);
expect(body.messages[0]).to.have.property('msg', 'Hello');
});
it('should return the messages of the room matching by searchTerm', async () => {
const visitor = await createVisitor();
const room = await createLivechatRoom(visitor.token);
await sendMessage(room._id, 'Hello', visitor.token);
await sendMessage(room._id, 'Random', visitor.token);

const { body } = await request
.get(api(`livechat/${room._id}/messages`))
.query({ searchTerm: 'Ran' })
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200);

expect(body).to.have.property('success', true);
expect(body).to.have.property('messages');
expect(body.messages).to.be.an('array');
expect(body.total).to.be.an('number').equal(1);
expect(body.messages[0]).to.have.property('msg', 'Random');
});
it('should return the messages of the room matching by partial searchTerm', async () => {
const visitor = await createVisitor();
const room = await createLivechatRoom(visitor.token);
await sendMessage(room._id, 'Hello', visitor.token);
await sendMessage(room._id, 'Random', visitor.token);

const { body } = await request
.get(api(`livechat/${room._id}/messages`))
.query({ searchTerm: 'ndo' })
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200);

expect(body).to.have.property('success', true);
expect(body).to.have.property('messages');
expect(body.messages).to.be.an('array');
expect(body.total).to.be.an('number').equal(1);
expect(body.messages[0]).to.have.property('msg', 'Random');
});
it('should return everything when searchTerm is ""', async () => {
const visitor = await createVisitor();
const room = await createLivechatRoom(visitor.token);
await sendMessage(room._id, 'Hello', visitor.token);
await sendMessage(room._id, 'Random', visitor.token);

const { body } = await request
.get(api(`livechat/${room._id}/messages`))
.query({ searchTerm: '' })
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200);

expect(body).to.have.property('success', true);
expect(body).to.have.property('messages');
expect(body.messages).to.be.an('array');
expect(body.messages).to.be.an('array').with.lengthOf.greaterThan(1);
expect(body.messages[0]).to.have.property('msg');
});
});

describe('[GET] livechat/message/:_id', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/model-typings/src/models/IMessagesModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface IMessagesModel extends IBaseModel<IMessage> {

getTotalOfMessagesSentByDate(params: { start: Date; end: Date; options?: any }): Promise<any[]>;

findLivechatClosedMessages(rid: IRoom['_id'], options: FindOptions<IMessage>): FindPaginated<FindCursor<IMessage>>;
findLivechatClosedMessages(rid: IRoom['_id'], searchTerm?: string, options?: FindOptions<IMessage>): FindPaginated<FindCursor<IMessage>>;

countRoomsWithStarredMessages(options: AggregateOptions): Promise<number>;

Expand Down
6 changes: 5 additions & 1 deletion packages/rest-typings/src/v1/omnichannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ const LivechatRoomsSchema = {

export const isLivechatRoomsProps = ajv.compile<LivechatRoomsProps>(LivechatRoomsSchema);

type LivechatRidMessagesProps = PaginatedRequest;
type LivechatRidMessagesProps = PaginatedRequest<{ searchTerm?: string }>;

const LivechatRidMessagesSchema = {
type: 'object',
Expand All @@ -981,6 +981,10 @@ const LivechatRidMessagesSchema = {
type: 'string',
nullable: true,
},
searchTerm: {
type: 'string',
nullable: true,
},
},
required: [],
additionalProperties: false,
Expand Down