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 createChat logic and testing b00tc4mp#84
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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(), Chat.findOne({ participants: { $all: [userId, targetUserId] } }).lean()]) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(([user, targetUser, chat]) => { | ||
if (!user) throw new NotFoundError('user not found') | ||
if (!targetUser) throw new NotFoundError('targetUser not found') | ||
if (chat) return chat._id.toString() | ||
|
||
return Chat.create({ participants: [user._id, targetUser._id] }) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(chat => chat._id.toString()) | ||
}) | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
staff/marti-herms/project/G-HUB/core/logic/createChat.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,96 @@ | ||
import 'dotenv/config' | ||
import mongoose from 'mongoose' | ||
import { expect } from 'chai' | ||
|
||
import createChat from './createChat.js' | ||
import { User, Chat } from '../data/models.js' | ||
|
||
import { errors } from 'com' | ||
|
||
const { NotFoundError, ValidationError } = errors | ||
|
||
describe('createChat', () => { | ||
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) | ||
.then(chatId => | ||
Chat.findById(chatId).lean() | ||
.then(chat => { | ||
expect(chat._id.toString()).to.equal(chatId) | ||
expect(chat.participants[0].toString()).to.equal(user1._id.toString()) | ||
expect(chat.participants[1].toString()).to.equal(user2._id.toString()) | ||
}) | ||
) | ||
) | ||
}) | ||
|
||
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) | ||
.then(chatId => | ||
createChat(user1.id, user2.id) | ||
.then(_chatId => { | ||
expect(_chatId).to.equal(chatId) | ||
}) | ||
) | ||
) | ||
}) | ||
|
||
it('fails on non exisiting user', () => { | ||
let error | ||
return User.create({ username: 'eden', email: '[email protected]', password: '123123123' }) | ||
.then((user) => createChat('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) => createChat(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 { | ||
createChat(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 { | ||
createChat('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/createChat.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 createChat from './createChat.js' | ||
|
||
mongoose.connect(process.env.MONGODB_URI) | ||
.then(() => createChat('66acb2b1730b0f09da259589', '66acb2b1730b0f09da259589')) | ||
.then(chatId => console.log(chatId)) | ||
.catch(error => console.error(error)) | ||
.finally(() => mongoose.disconnect()) |