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.
implement get user chats logic and testing b00tc4mp#84
- Loading branch information
Showing
6 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
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
31 changes: 31 additions & 0 deletions
31
staff/marti-herms/project/G-HUB/core/logic/getUserChats.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,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
83
staff/marti-herms/project/G-HUB/core/logic/getUserChats.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,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
10
staff/marti-herms/project/G-HUB/core/logic/getUserChats.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 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()) |
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
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