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 followers and following getters logic and testing in api b0…
- Loading branch information
Showing
15 changed files
with
338 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useParams } from 'react-router-dom' | ||
|
||
import useContext from '../context' | ||
|
||
import Container from '../library/Container' | ||
|
||
export default function UserList({ userList }) { | ||
const { alert } = useContext() | ||
|
||
const { userId } = useParams() | ||
|
||
return <Container> | ||
|
||
</Container> | ||
} |
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
4 changes: 2 additions & 2 deletions
4
staff/marti-herms/project/V-HUB/app/src/library/NavigationButton.jsx
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,3 +1,3 @@ | ||
export default function Button({ children, ...nextProps }) { | ||
return <button className={'border border-solid border-slate-500 bg-white px-2 rounded active:bg-slate-500'} {...nextProps}>{children}</button> | ||
export default function NavigationButton({ children, ...nextProps }) { | ||
return <button className={'border border-solid border-slate-500 bg-white px-2 rounded active:bg-slate-500 w-[95px]'} {...nextProps}>{children}</button> | ||
} |
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
39 changes: 39 additions & 0 deletions
39
staff/marti-herms/project/V-HUB/core/logic/getUserFollowers.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,39 @@ | ||
import { User } from '../data/models.js' | ||
|
||
import { validate, errors } from 'com' | ||
|
||
const { NotFoundError, SystemError } = 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(user => { | ||
if (!user) throw new NotFoundError('targetUser not found') | ||
|
||
return User.find({ _id: { $in: user.followers } }) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(users => { | ||
const promises = users.map(user => { | ||
user.id = user._id.toString() | ||
|
||
delete user._id | ||
|
||
delete user.password | ||
|
||
return user | ||
}) | ||
|
||
return Promise.all(promises) | ||
}) | ||
}) | ||
}) | ||
} |
96 changes: 96 additions & 0 deletions
96
staff/marti-herms/project/V-HUB/core/logic/getUserFollowers.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 getUserFollowers from './getUserFollowers.js' | ||
import toggleFollowUser from './toggleFollowUser.js' | ||
import mongoose from 'mongoose' | ||
|
||
import { expect } from 'chai' | ||
import { User } from '../data/models.js' | ||
|
||
import { errors } from 'com' | ||
|
||
const { NotFoundError, ValidationError } = errors | ||
|
||
describe('getUserFollowers', () => { | ||
before(() => mongoose.connect(process.env.MONGODB_URI)) | ||
|
||
beforeEach(() => User.deleteMany()) | ||
|
||
it('succeeds on existing user returning all followers', () => { | ||
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(() => getUserFollowers(_user.id, _user.id)) | ||
.then(users => { | ||
expect(users).to.be.an('array') | ||
expect(users[0].id).to.equal(user.id) | ||
}) | ||
) | ||
) | ||
}) | ||
|
||
it('succeeds on existing user and returning empty array ', () => { | ||
return User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' }) | ||
.then(user => | ||
getUserFollowers(user.id, user.id) | ||
.then(users => { | ||
expect(users).to.be.an('array') | ||
expect(users.length).to.equal(0) | ||
}) | ||
) | ||
}) | ||
|
||
it('fails on non-existing user', () => { | ||
let _error | ||
|
||
return getUserFollowers('66ba007f874aa7b84ec54491', '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({ name: 'Mono', surname: 'Loco', email: '[email protected]', username: 'monoloco', password: '123123123' }) | ||
.then(user => getUserFollowers(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 { | ||
getUserFollowers(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 { | ||
getUserFollowers('66ba007f874aa7b84ec54491', 123) | ||
} catch (_error) { | ||
error = _error | ||
} finally { | ||
expect(error).to.be.instanceOf(ValidationError) | ||
expect(error.message).to.equal('targetUserId is not a string') | ||
} | ||
}) | ||
|
||
afterEach(() => User.deleteMany()) | ||
|
||
after(() => mongoose.disconnect()) | ||
}) |
10 changes: 10 additions & 0 deletions
10
staff/marti-herms/project/V-HUB/core/logic/getUserFollowers.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 getUserFollowers from './getUserFollowers.js' | ||
|
||
mongoose.connect(process.env.MONGODB_URI) | ||
.then(() => getUserFollowers('66acb2b1730b0f09da259589', '66acb2b1730b0f09da259589')) | ||
.then(games => console.log(games)) | ||
.catch(error => console.error(error)) | ||
.finally(() => mongoose.disconnect()) |
39 changes: 39 additions & 0 deletions
39
staff/marti-herms/project/V-HUB/core/logic/getUserFollowing.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,39 @@ | ||
import { User } from '../data/models.js' | ||
|
||
import { validate, errors } from 'com' | ||
|
||
const { NotFoundError, SystemError } = 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(user => { | ||
if (!user) throw new NotFoundError('targetUser not found') | ||
|
||
return User.find({ _id: { $in: user.following } }) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(users => { | ||
const promises = users.map(user => { | ||
user.id = user._id.toString() | ||
|
||
delete user._id | ||
|
||
delete user.password | ||
|
||
return user | ||
}) | ||
|
||
return Promise.all(promises) | ||
}) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.