diff --git a/backend/src/graphql/rooms.ts b/backend/src/graphql/rooms.ts index e48ffe943..554c346dc 100644 --- a/backend/src/graphql/rooms.ts +++ b/backend/src/graphql/rooms.ts @@ -8,6 +8,7 @@ export const createRoomMutation = () => { roomId roomName lastMessageAt + unreadCount users { _id id @@ -29,6 +30,7 @@ export const roomQuery = () => { roomId roomName lastMessageAt + unreadCount lastMessage { _id id diff --git a/backend/src/schema/resolvers/rooms.spec.ts b/backend/src/schema/resolvers/rooms.spec.ts index daed6815e..0ff8669fb 100644 --- a/backend/src/schema/resolvers/rooms.spec.ts +++ b/backend/src/schema/resolvers/rooms.spec.ts @@ -126,6 +126,7 @@ describe('Room', () => { id: expect.any(String), roomId: result.data.CreateRoom.id, roomName: 'Other Chatting User', + unreadCount: 0, users: expect.arrayContaining([ { _id: 'chatting-user', @@ -241,6 +242,7 @@ describe('Room', () => { id: expect.any(String), roomId: result.data.Room[0].id, roomName: 'Chatting User', + unreadCount: 0, users: expect.arrayContaining([ { _id: 'chatting-user', diff --git a/backend/src/schema/resolvers/rooms.ts b/backend/src/schema/resolvers/rooms.ts index 885688643..f87f89f2e 100644 --- a/backend/src/schema/resolvers/rooms.ts +++ b/backend/src/schema/resolvers/rooms.ts @@ -34,7 +34,8 @@ export default { RETURN room { .*, users: [properties(currentUser), properties(user)], - roomName: roomName + roomName: roomName, + unreadCount: toString(0) } ` const createRommTxResponse = await transaction.run(createRoomCypher, { diff --git a/backend/src/schema/types/type/Room.gql b/backend/src/schema/types/type/Room.gql index 2cf51014e..a1ff56e34 100644 --- a/backend/src/schema/types/type/Room.gql +++ b/backend/src/schema/types/type/Room.gql @@ -23,6 +23,13 @@ type Room { WITH message ORDER BY message.createdAt DESC LIMIT 1 RETURN message """) + + unreadCount: Int @cypher(statement: """ + MATCH (this)<-[:INSIDE]-(message:Message)<-[:CREATED]-(user:User) + WHERE NOT user.id = $cypherParams.currentUserId + AND NOT message.seen + RETURN count(message) + """) } type Mutation {