Skip to content

Commit

Permalink
change createChat name to openChat; implement getChatMessages logic a…
Browse files Browse the repository at this point in the history
…nd testing b00tc4mp#84
  • Loading branch information
Eden23 committed Aug 26, 2024
1 parent f4ddbbe commit 3c04792
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 14 deletions.
40 changes: 40 additions & 0 deletions staff/marti-herms/project/G-HUB/core/logic/getChatMessages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { User, Chat, Message } from '../data/models.js'

import { validate, errors } from 'com'

const { SystemError, NotFoundError } = errors

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

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

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

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

return User.findById(message.author).lean()
.catch(error => { throw new SystemError(error.message) })
.then(author => {
message.author = {
id: author._id.toString(),
username: author.username
}

return message
})
})

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

const { ObjectId } = Types

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

import { errors } from 'com'

const { NotFoundError, ValidationError } = errors

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

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

it('succeeds on returning chat messages', () => {
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 =>
Message.create({ author: user1.id, content: 'hello' })
.then(message => Chat.findByIdAndUpdate(chat.id, { $push: { messages: message.id } }))
.then(() => getChatMessages(user1.id, chat.id))
.then(messages => {
expect(messages).to.be.an('array')
expect(messages.length).to.equal(1)
expect(messages[0].content).to.equal('hello')
})
)
)
})

it('succeeds on existing user and no posts returning empty array ', () => {
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 => getChatMessages(user1.id, chat.id))
.then(messages => {
expect(messages).to.be.an('array')
expect(messages.length).to.equal(0)
})
)
})

it('fails on non-existing user', () => {
let _error

return Chat.create({ participants: [new ObjectId(), new ObjectId()] })
.then(chat => getChatMessages('66ba007f874aa7b84ec54491', chat.id))
.catch(error => _error = error)
.finally(() => {
expect(_error).to.be.instanceOf(NotFoundError)
expect(_error.message).to.equal('user not found')
})
})

it('fails on non-existing chat', () => {
let _error

return User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' })
.then(user => getChatMessages(user.id, '66ba007f874aa7b84ec54491'))
.catch(error => _error = error)
.finally(() => {
expect(_error).to.be.instanceOf(NotFoundError)
expect(_error.message).to.equal('chat not found')
})
})

it('fails on non-string userId', () => {
let error

try {
getChatMessages(123, '66ba007f874aa7b84ec54491')
} 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 chatId', () => {
let error

try {
getChatMessages('66ba007f874aa7b84ec54491', 123)
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('chatId is not a string')
}
})

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

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

import getChatMessages from './getChatMessages.js'

mongoose.connect(process.env.MONGODB_URI)
.then(() => getChatMessages('66acb2b1730b0f09da259589', '66acb2b1730b0f09da259589'))
.then(messages => console.log(messages))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('getDevUserGames', () => {
})

it('succeeds on existing user and no posts returning empty array ', () => {
return User.create({ name: 'Mono', surname: 'Loco', email: '[email protected]', username: 'monoloco', password: '123123123' })
return User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' })
.then(user => getDevUserGames(user.id, user.id))
.then(games => {
expect(games).to.be.an('array')
Expand Down
6 changes: 4 additions & 2 deletions staff/marti-herms/project/G-HUB/core/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ import getUserAvatar from './getUserAvatar.js'
import getUser from './getUser.js'
import editUserAvatar from './editUserAvatar.js'
import editUserUsername from './editUserUsername.js'
import createChat from './createChat.js'
import openChat from './openChat.js'
import sendMessage from './sendMessage.js'
import getChatMessages from './getChatMessages.js'

const logic = {
authenticateUser,
createChat,
deleteReview,
editUserAvatar,
editUserUsername,
getChatMessages,
getDevUserGames,
getGameById,
getGameReviews,
Expand All @@ -40,6 +41,7 @@ const logic = {
getUserFollowing,
getUserUsername,
makeReview,
openChat,
registerGame,
registerUser,
searchGame,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import 'dotenv/config'
import mongoose from 'mongoose'
import { expect } from 'chai'

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

import { errors } from 'com'

const { NotFoundError, ValidationError } = errors

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

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

it('succeeds on creating new 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]) =>
createChat(user1.id, user2.id)
openChat(user1.id, user2.id)
.then(chatId =>
Chat.findById(chatId).lean()
.then(chat => {
Expand All @@ -32,9 +32,9 @@ describe('createChat', () => {
it('succeds 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]) =>
createChat(user1.id, user2.id)
openChat(user1.id, user2.id)
.then(chatId =>
createChat(user1.id, user2.id)
openChat(user1.id, user2.id)
.then(_chatId => {
expect(_chatId).to.equal(chatId)
})
Expand All @@ -45,7 +45,7 @@ describe('createChat', () => {
it('fails on non exisiting user', () => {
let error
return User.create({ username: 'eden', email: '[email protected]', password: '123123123' })
.then((user) => createChat('66c0ae22930f95c985427ece', user.id))
.then((user) => openChat('66c0ae22930f95c985427ece', user.id))
.catch(_error => error = _error)
.finally(() => {
expect(error).to.be.instanceOf(NotFoundError)
Expand All @@ -57,7 +57,7 @@ describe('createChat', () => {
it('fails on non exisiting targetUser', () => {
let error
return User.create({ username: 'eden', email: '[email protected]', password: '123123123' })
.then((user) => createChat(user.id, '66c0ae22930f95c985427ece'))
.then((user) => openChat(user.id, '66c0ae22930f95c985427ece'))
.catch(_error => error = _error)
.finally(() => {
expect(error).to.be.instanceOf(NotFoundError)
Expand All @@ -69,7 +69,7 @@ describe('createChat', () => {
it('fails on non-string userId', () => {
let error
try {
createChat(123, '66c0ae22930f95c985427ece')
openChat(123, '66c0ae22930f95c985427ece')
} catch (_error) {
error = _error
} finally {
Expand All @@ -81,7 +81,7 @@ describe('createChat', () => {
it('fails on non-string targetUserId', () => {
let error
try {
createChat('66c0ae22930f95c985427ece', 123)
openChat('66c0ae22930f95c985427ece', 123)
} catch (_error) {
error = _error
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dotenv/config'
import mongoose from 'mongoose'

import createChat from './createChat.js'
import openChat from './openChat.js'

mongoose.connect(process.env.MONGODB_URI)
.then(() => createChat('66acb2b1730b0f09da259589', '66acb2b1730b0f09da259589'))
.then(() => openChat('66acb2b1730b0f09da259589', '66acb2b1730b0f09da259589'))
.then(chatId => console.log(chatId))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())

0 comments on commit 3c04792

Please sign in to comment.