Skip to content

Commit

Permalink
implement get user chats logic and testing b00tc4mp#84
Browse files Browse the repository at this point in the history
  • Loading branch information
Eden23 committed Aug 27, 2024
1 parent 3cc5beb commit 7ca0ce1
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 1 deletion.
4 changes: 4 additions & 0 deletions staff/marti-herms/project/G-HUB/core/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ const chat = new Schema({
messages: {
type: [ObjectId],
ref: 'Message'
},
date: {
type: Date,
default: Date.now
}
})

Expand Down
31 changes: 31 additions & 0 deletions staff/marti-herms/project/G-HUB/core/logic/getUserChats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { User, Chat } from '../data/models.js'

import { validate, errors } from 'com'

const { SystemError, NotFoundError } = errors

export default (userId, targetUserId) => {
validate.string(userId, 'userId')
validate.string(targetUserId, 'targetUserId')

return Promise.all([User.findById(userId).lean(), User.findById(targetUserId).lean()])
.catch(error => { throw new SystemError(error.message) })
.then(([user, targetUser]) => {
if (!user) throw new NotFoundError('user not found')

if (!targetUser) throw new NotFoundError('targetUser not found')

return Chat.find({ participants: { $in: targetUserId } }, { __v: 0 }).sort({ date: -1 }).lean()
.catch(error => { throw new SystemError(error.message) })
.then(chats => {
const promises = chats.map(chat => {
chat.id = chat._id.toString()
delete chat._id

return chat
})

return Promise.all(promises)
})
})
}
83 changes: 83 additions & 0 deletions staff/marti-herms/project/G-HUB/core/logic/getUserChats.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'dotenv/config'
import mongoose from 'mongoose'
import { expect } from 'chai'

import getUserChats from './getUserChats.js'
import { User, Chat } from '../data/models.js'

import { errors } from 'com'

const { NotFoundError, ValidationError } = errors

describe('getUserChats', () => {
before(() => mongoose.connect(process.env.MONGODB_URI))

beforeEach(() => Promise.all([User.deleteMany(), Chat.deleteMany()]))

it('succeeds on returning existing chat', () => {
return Promise.all([User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' }), User.create({ username: 'eden', email: '[email protected]', password: '123123123' })])
.then(([user1, user2]) =>
Chat.create({ participants: [user1._id, user2._id] })
.then(chat =>
getUserChats(user1.id, user2.id)
.then(chats => {
expect(chats[0].id).to.equal(chat.id)
expect(chats[0].participants[0].toString()).to.equal(user1.id)
expect(chats[0].participants[1].toString()).to.equal(user2.id)
})
)
)
})

it('fails on non exisiting user', () => {
let error
return User.create({ username: 'eden', email: '[email protected]', password: '123123123' })
.then((user) => getUserChats('66c0ae22930f95c985427ece', user.id))
.catch(_error => error = _error)
.finally(() => {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('user not found')
})

})

it('fails on non exisiting targetUser', () => {
let error
return User.create({ username: 'eden', email: '[email protected]', password: '123123123' })
.then((user) => getUserChats(user.id, '66c0ae22930f95c985427ece'))
.catch(_error => error = _error)
.finally(() => {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('targetUser not found')
})

})

it('fails on non-string userId', () => {
let error
try {
getUserChats(123, '66c0ae22930f95c985427ece')
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('userId is not a string')
}
})

it('fails on non-string targetUserId', () => {
let error
try {
getUserChats('66c0ae22930f95c985427ece', 123)
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('targetUserId is not a string')
}
})

afterEach(() => Promise.all([User.deleteMany(), Chat.deleteMany()]))

after(() => mongoose.disconnect())
})
10 changes: 10 additions & 0 deletions staff/marti-herms/project/G-HUB/core/logic/getUserChats.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'dotenv/config'
import mongoose from 'mongoose'

import getUserChats from './getUserChats.js'

mongoose.connect(process.env.MONGODB_URI)
.then(() => getUserChats('66acb2b1730b0f09da259589', '66acb2b1730b0f09da259589'))
.then(games => console.log(games))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())
2 changes: 2 additions & 0 deletions staff/marti-herms/project/G-HUB/core/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import editUserUsername from './editUserUsername.js'
import openChat from './openChat.js'
import sendMessage from './sendMessage.js'
import getChatMessages from './getChatMessages.js'
import getUserChats from './getUserChats.js'

const logic = {
authenticateUser,
Expand Down Expand Up @@ -50,6 +51,7 @@ const logic = {
toggleAddGame,
toggleFavGame,
toggleFollowUser,
getUserChats
}

export default logic
2 changes: 1 addition & 1 deletion staff/marti-herms/project/G-HUB/core/logic/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default (userId, chatId, content) => {
.catch(error => { throw new SystemError(error.message) })
})
.then(message =>
Chat.findByIdAndUpdate(chatId, { $push: { messages: message._id } })
Chat.findByIdAndUpdate(chatId, { date: Date.now(), $push: { messages: message._id } })
.catch(error => { throw new SystemError(error.message) })
)
.then(() => { })
Expand Down

0 comments on commit 7ca0ce1

Please sign in to comment.