Skip to content

Commit

Permalink
[IMPROVE] Add searchTerm support for livechat/rid/messages API (#27214)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored and MartinSchoeler committed Nov 28, 2022
1 parent f786a63 commit c73bd6a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
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' })
.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 @@ -970,7 +970,7 @@ const LivechatRoomsSchema = {

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

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

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

0 comments on commit c73bd6a

Please sign in to comment.