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 toggle follow user logic and testing in core b00tc4mp#84
- Loading branch information
Showing
8 changed files
with
144 additions
and
7 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
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
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
30 changes: 30 additions & 0 deletions
30
staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.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,30 @@ | ||
import { User } 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 User.findById(userId).lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) throw new NotFoundError('user not found') | ||
|
||
return User.findById(targetUserId).lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(targetUser => { | ||
if (!targetUser) throw new NotFoundError('targetUser not found') | ||
|
||
if (user.following.some(userObjectId => userObjectId.toString() === targetUserId)) | ||
return User.findByIdAndUpdate(userId, { $pull: { following: targetUser._id } }) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
else | ||
return User.findByIdAndUpdate(userId, { $push: { following: targetUser._id } }) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
}) | ||
}) | ||
.then(() => { }) | ||
} |
99 changes: 99 additions & 0 deletions
99
staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.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,99 @@ | ||
import 'dotenv/config' | ||
import toggleFollowUser from './toggleFollowUser.js' | ||
import mongoose from 'mongoose' | ||
|
||
import { expect } from 'chai' | ||
import { User, Game } from '../data/models.js' | ||
|
||
import { errors } from 'com' | ||
|
||
const { NotFoundError, ValidationError } = errors | ||
|
||
describe('toggleFollowUser', () => { | ||
before(() => mongoose.connect(process.env.MONGODB_URI)) | ||
|
||
beforeEach(() => Promise.all([User.deleteMany(), Game.deleteMany()])) | ||
|
||
it('succeeds on existing user and game and game is in user following', () => { | ||
return User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' }) | ||
.then(user => | ||
User.create({ username: 'eden', email: '[email protected]', password: '123123123' }) | ||
.then(_user => | ||
toggleFollowUser(user.id, _user.id) | ||
.then(() => User.findOne({ username: 'monoloco' })) | ||
.then(user => { | ||
expect(user.following).to.include(_user._id) | ||
}) | ||
) | ||
) | ||
}) | ||
|
||
it('succeeds on existing user and game and game is not in user following', () => { | ||
return User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' }) | ||
.then(user => | ||
User.create({ username: 'eden', email: '[email protected]', password: '123123123' }) | ||
.then(_user => | ||
toggleFollowUser(user.id, _user.id) | ||
.then(() => toggleFollowUser(user.id, _user.id)) | ||
.then(() => User.findOne({ username: 'monoloco' })) | ||
.then(user => { | ||
expect(user.following).to.not.include(_user._id) | ||
expect(user.following.length).to.equal(0) | ||
}) | ||
) | ||
) | ||
}) | ||
|
||
it('fails on non-existing user', () => { | ||
let _error | ||
|
||
return toggleFollowUser('66ba313a881fabd96394b179', '66ba007f874aa7b84ec54491') | ||
.catch(error => _error = error) | ||
.finally(() => { | ||
expect(_error).to.be.instanceOf(NotFoundError) | ||
expect(_error.message).to.equal('user not found') | ||
}) | ||
}) | ||
|
||
it('fails on non-existing targetUser', () => { | ||
let _error | ||
|
||
return User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' }) | ||
.then(user => toggleFollowUser(user.id, '66ba007f874aa7b84ec54491')) | ||
.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 { | ||
toggleFollowUser(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 targetUserId', () => { | ||
let error | ||
|
||
try { | ||
toggleFollowUser('66ba313a881fabd96394b179', 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(), Game.deleteMany()])) | ||
|
||
after(() => mongoose.disconnect()) | ||
}) |
10 changes: 10 additions & 0 deletions
10
staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.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 toggleFollowUser from './toggleFollowUser.js' | ||
|
||
mongoose.connect(process.env.MONGODB_URI) | ||
.then(() => toggleFollowUser('66ba007f874aa7b84ec54491', '66ba313a881fabd96394b179')) | ||
.then(games => console.log(games)) | ||
.catch(error => console.error(error)) | ||
.finally(() => mongoose.disconnect()) |