forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change createChat name to openChat; implement getChatMessages logic a…
…nd testing b00tc4mp#84
- Loading branch information
Showing
8 changed files
with
167 additions
and
14 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
staff/marti-herms/project/G-HUB/core/logic/getChatMessages.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
101
staff/marti-herms/project/G-HUB/core/logic/getChatMessages.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
staff/marti-herms/project/G-HUB/core/logic/getChatMessages.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 => { | ||
|
@@ -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) | ||
}) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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 { | ||
|
4 changes: 2 additions & 2 deletions
4
...oject/G-HUB/core/logic/createChat.test.js → ...project/G-HUB/core/logic/openChat.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |