diff --git a/staff/marti-herms/project/V-HUB/core/logic/makeReview.js b/staff/marti-herms/project/V-HUB/core/logic/makeReview.js index a7558d5d1..87bcf780c 100644 --- a/staff/marti-herms/project/V-HUB/core/logic/makeReview.js +++ b/staff/marti-herms/project/V-HUB/core/logic/makeReview.js @@ -4,7 +4,7 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors -export default (userId, gameId, comment, rate = -1) => { +export default (userId, gameId, comment, rate = 0) => { validate.string(userId, 'userId') validate.string(gameId, 'gameId') validate.string(comment, 'comment') diff --git a/staff/marti-herms/project/V-HUB/core/logic/makeReview.spec.js b/staff/marti-herms/project/V-HUB/core/logic/makeReview.spec.js index 6975b01af..b2f8df8b2 100644 --- a/staff/marti-herms/project/V-HUB/core/logic/makeReview.spec.js +++ b/staff/marti-herms/project/V-HUB/core/logic/makeReview.spec.js @@ -28,7 +28,7 @@ describe('makeReview', () => { ) .then(review => { expect(review.comment).to.equal('great game') - expect(review.rate).to.equal(-1) + expect(review.rate).to.equal(0) }) ) diff --git a/staff/marti-herms/project/V-HUB/core/logic/searchUser.spec.js b/staff/marti-herms/project/V-HUB/core/logic/searchUser.spec.js index f996cee9d..fb81640bf 100644 --- a/staff/marti-herms/project/V-HUB/core/logic/searchUser.spec.js +++ b/staff/marti-herms/project/V-HUB/core/logic/searchUser.spec.js @@ -3,16 +3,12 @@ import mongoose from 'mongoose' import { expect } from 'chai' import searchUser from './searchUser.js' -import { User, Game } from '../data/models.js' +import { User } from '../data/models.js' import { errors } from 'com' const { ValidationError, NotFoundError } = errors -const img = 'https://store-images.s-microsoft.com/image/apps.54354.13510798882606697.7a42c472-75d7-487e-9538-ebb5ce1657e6.372723d8-dd1a-450a-9fed-d420e7705e4e?mode=scale&q=90&h=300&w=200' - -const link = 'https://www.microsoft.com/en-us/p/candy-crush-saga/9nblggh18846?activetab=pivot:overviewtab' - describe('searchUser', () => { before(() => mongoose.connect(process.env.MONGODB_URI)) diff --git a/staff/marti-herms/project/V-HUB/core/logic/toggleAddGame.js b/staff/marti-herms/project/V-HUB/core/logic/toggleAddGame.js index 55a49c954..137c12e94 100644 --- a/staff/marti-herms/project/V-HUB/core/logic/toggleAddGame.js +++ b/staff/marti-herms/project/V-HUB/core/logic/toggleAddGame.js @@ -26,4 +26,5 @@ export default (userId, gameId) => { .catch(error => { throw new SystemError(error.message) }) }) }) + .then(() => { }) } \ No newline at end of file diff --git a/staff/marti-herms/project/V-HUB/core/logic/toggleFavGame.js b/staff/marti-herms/project/V-HUB/core/logic/toggleFavGame.js index 7d115c026..a0406f791 100644 --- a/staff/marti-herms/project/V-HUB/core/logic/toggleFavGame.js +++ b/staff/marti-herms/project/V-HUB/core/logic/toggleFavGame.js @@ -26,4 +26,5 @@ export default (userId, gameId) => { .catch(error => { throw new SystemError(error.message) }) }) }) + .then(() => { }) } \ No newline at end of file diff --git a/staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.js b/staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.js new file mode 100644 index 000000000..aab2d842d --- /dev/null +++ b/staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.js @@ -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(() => { }) +} \ No newline at end of file diff --git a/staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.spec.js b/staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.spec.js new file mode 100644 index 000000000..04eed4bbb --- /dev/null +++ b/staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.spec.js @@ -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: 'mono@loco.com', password: '123123123' }) + .then(user => + User.create({ username: 'eden', email: 'marti@herms.com', 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: 'mono@loco.com', password: '123123123' }) + .then(user => + User.create({ username: 'eden', email: 'marti@herms.com', 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: 'mono@loco.com', 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()) +}) \ No newline at end of file diff --git a/staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.test.js b/staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.test.js new file mode 100644 index 000000000..28410ddf7 --- /dev/null +++ b/staff/marti-herms/project/V-HUB/core/logic/toggleFollowUser.test.js @@ -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()) \ No newline at end of file