Skip to content

Commit

Permalink
fix(conversations): fix ordering (#326)
Browse files Browse the repository at this point in the history
* fix(conversations): fix ordering

* fix
  • Loading branch information
samuelmasse committed Jan 27, 2022
1 parent c381196 commit fb40676
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/server/src/conversations/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class ConversationService extends Service {
.leftJoin(
`${getTableId('msg_messages')}`,
`${getTableId('msg_messages')}.conversationId`,
`${getTableId('msg_messages')}.id`
`${getTableId('msg_conversations')}.id`
)
.where({
clientId,
Expand All @@ -162,7 +162,7 @@ export class ConversationService extends Service {
.orWhereNull('sentOn')
})
.groupBy(`${getTableId('msg_conversations')}.id`, `${getTableId('msg_messages')}.id`)
.orderBy('sentOn', 'desc')
.orderBy('sentOn', 'desc', 'first')
.orderBy('createdOn', 'desc')
}

Expand Down
21 changes: 21 additions & 0 deletions packages/server/test/integration/conversations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import { Conversation, User } from '@botpress/messaging-base/src'
import { validate as validateUuid } from 'uuid'
import { Client } from '../../src/clients/types'
import { ConversationService } from '../../src/conversations/service'
import { MessageService } from '../../src/messages/service'
import { UserService } from '../../src/users/service'
import { app, randStr, setupApp } from './utils'

describe('Conversations', () => {
let conversations: ConversationService
let users: UserService
let messages: MessageService
let querySpy: jest.SpyInstance
let state: { client: Client; user: User; conversation?: Conversation }

beforeAll(async () => {
await setupApp()
conversations = app.conversations
users = app.users
messages = app.messages
querySpy = jest.spyOn(conversations as any, 'query')

const provider = await app.providers.create(randStr(), false)
Expand Down Expand Up @@ -78,6 +81,24 @@ describe('Conversations', () => {
expect(await conversations.listByUserId(state.client.id, user.id)).toEqual([convo3, convo2, convo1])
})

test('List conversations by user should be ordered by most recently used', async () => {
const user = await users.create(state.client.id)
const convo1 = await conversations.create(state.client.id, user.id)
const convo2 = await conversations.create(state.client.id, user.id)
const convo3 = await conversations.create(state.client.id, user.id)

const otherUser = await users.create(state.client.id)
const otherConvo = await conversations.create(state.client.id, otherUser.id)

// We create a message for convo 1 here, so convo 1 should be first in the list now
await messages.create(convo1.id, user.id, {})
expect(await conversations.listByUserId(state.client.id, user.id)).toEqual([convo1, convo3, convo2])

// We create a message for convo 3 here, so convo 3 should be first in the list now
await messages.create(convo3.id, user.id, {})
expect(await conversations.listByUserId(state.client.id, user.id)).toEqual([convo3, convo1, convo2])
})

test('Deleting conversation clears cache and persists in changes', async () => {
await conversations.delete(state.conversation!.id)
const calls = querySpy.mock.calls.length
Expand Down

0 comments on commit fb40676

Please sign in to comment.