From 3c0479296fd63ac325444216ee8365d7b1009392 Mon Sep 17 00:00:00 2001 From: Marti Herms Date: Mon, 26 Aug 2024 12:48:49 +0200 Subject: [PATCH] change createChat name to openChat; implement getChatMessages logic and testing #84 --- .../G-HUB/core/logic/getChatMessages.js | 40 +++++++ .../G-HUB/core/logic/getChatMessages.spec.js | 101 ++++++++++++++++++ .../G-HUB/core/logic/getChatMessages.test.js | 10 ++ .../G-HUB/core/logic/getDevUserGames.spec.js | 2 +- .../project/G-HUB/core/logic/index.js | 6 +- .../core/logic/{createChat.js => openChat.js} | 0 .../{createChat.spec.js => openChat.spec.js} | 18 ++-- .../{createChat.test.js => openChat.test.js} | 4 +- 8 files changed, 167 insertions(+), 14 deletions(-) create mode 100644 staff/marti-herms/project/G-HUB/core/logic/getChatMessages.js create mode 100644 staff/marti-herms/project/G-HUB/core/logic/getChatMessages.spec.js create mode 100644 staff/marti-herms/project/G-HUB/core/logic/getChatMessages.test.js rename staff/marti-herms/project/G-HUB/core/logic/{createChat.js => openChat.js} (100%) rename staff/marti-herms/project/G-HUB/core/logic/{createChat.spec.js => openChat.spec.js} (86%) rename staff/marti-herms/project/G-HUB/core/logic/{createChat.test.js => openChat.test.js} (64%) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.js b/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.js new file mode 100644 index 000000000..d597adf02 --- /dev/null +++ b/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.js @@ -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) + }) + }) +} \ No newline at end of file diff --git a/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.spec.js new file mode 100644 index 000000000..a2761b94c --- /dev/null +++ b/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.spec.js @@ -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: 'mono@loco.com', password: '123123123' }), User.create({ username: 'eden', email: 'marti@herms.com', 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: 'mono@loco.com', password: '123123123' }), User.create({ username: 'eden', email: 'marti@herms.com', 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: 'mono@loco.com', 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()) +}) \ No newline at end of file diff --git a/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.test.js b/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.test.js new file mode 100644 index 000000000..1ca47a570 --- /dev/null +++ b/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.test.js @@ -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()) \ No newline at end of file diff --git a/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.spec.js index 59dd8d760..961900ef4 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.spec.js @@ -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: 'mono@loco.com', username: 'monoloco', password: '123123123' }) + return User.create({ username: 'monoloco', email: 'mono@loco.com', password: '123123123' }) .then(user => getDevUserGames(user.id, user.id)) .then(games => { expect(games).to.be.an('array') diff --git a/staff/marti-herms/project/G-HUB/core/logic/index.js b/staff/marti-herms/project/G-HUB/core/logic/index.js index f3dc53874..01a8a6e4b 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/index.js +++ b/staff/marti-herms/project/G-HUB/core/logic/index.js @@ -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, @@ -40,6 +41,7 @@ const logic = { getUserFollowing, getUserUsername, makeReview, + openChat, registerGame, registerUser, searchGame, diff --git a/staff/marti-herms/project/G-HUB/core/logic/createChat.js b/staff/marti-herms/project/G-HUB/core/logic/openChat.js similarity index 100% rename from staff/marti-herms/project/G-HUB/core/logic/createChat.js rename to staff/marti-herms/project/G-HUB/core/logic/openChat.js diff --git a/staff/marti-herms/project/G-HUB/core/logic/createChat.spec.js b/staff/marti-herms/project/G-HUB/core/logic/openChat.spec.js similarity index 86% rename from staff/marti-herms/project/G-HUB/core/logic/createChat.spec.js rename to staff/marti-herms/project/G-HUB/core/logic/openChat.spec.js index 39bcf2114..916e54600 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/createChat.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/openChat.spec.js @@ -2,14 +2,14 @@ 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()])) @@ -17,7 +17,7 @@ describe('createChat', () => { it('succeeds on creating new chat', () => { return Promise.all([User.create({ username: 'monoloco', email: 'mono@loco.com', password: '123123123' }), User.create({ username: 'eden', email: 'marti@herms.com', password: '123123123' })]) .then(([user1, user2]) => - createChat(user1.id, user2.id) + openChat(user1.id, user2.id) .then(chatId => Chat.findById(chatId).lean() .then(chat => { @@ -32,9 +32,9 @@ describe('createChat', () => { it('succeds on returning existing chat', () => { return Promise.all([User.create({ username: 'monoloco', email: 'mono@loco.com', password: '123123123' }), User.create({ username: 'eden', email: 'marti@herms.com', 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) }) @@ -45,7 +45,7 @@ describe('createChat', () => { it('fails on non exisiting user', () => { let error return User.create({ username: 'eden', email: 'marti@herms.com', 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) @@ -57,7 +57,7 @@ describe('createChat', () => { it('fails on non exisiting targetUser', () => { let error return User.create({ username: 'eden', email: 'marti@herms.com', 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) @@ -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 { @@ -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 { diff --git a/staff/marti-herms/project/G-HUB/core/logic/createChat.test.js b/staff/marti-herms/project/G-HUB/core/logic/openChat.test.js similarity index 64% rename from staff/marti-herms/project/G-HUB/core/logic/createChat.test.js rename to staff/marti-herms/project/G-HUB/core/logic/openChat.test.js index a9f2713ff..c8c1d7df6 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/createChat.test.js +++ b/staff/marti-herms/project/G-HUB/core/logic/openChat.test.js @@ -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()) \ No newline at end of file