forked from b00tc4mp/eurofirms-bootcamp-202402
-
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.
add comments logic and update delete cinema/issues logic b00tc4mp#257
- Loading branch information
Showing
11 changed files
with
247 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { User, Issue, Comment } from '../data/index.js' | ||
|
||
import { validate, errors } from 'com' | ||
|
||
const { SystemError, MatchError } = errors | ||
|
||
function createComment(userId, issueId, text) { | ||
validate.id(userId) | ||
validate.id(issueId, 'issueId') | ||
validate.text(text) | ||
|
||
return User.findById(userId) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) throw new MatchError('User not found') | ||
|
||
return Issue.findById(issueId) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(issue => { | ||
if (!issue) throw new MatchError('Issue not found') | ||
|
||
if (issue.status === 'closed') throw new MatchError('You can only comments on open issues') | ||
|
||
if (issue.author.toString() !== userId && (user.role !== 'manager' || user.cinema.toString() !== issue.cinema.toString())) throw new MatchError('You only could comment issues that is not created by you if you´re manager of the cinema') | ||
|
||
const newComment = { author: userId, issue: issueId, text, date: new Date() } | ||
|
||
return Comment.create(newComment) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(comment => { }) | ||
}) | ||
}) | ||
} | ||
export default createComment |
14 changes: 14 additions & 0 deletions
14
staff/sergio-ocaña/project/api/logic/createComment.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,14 @@ | ||
import createComment from './createComment.js' | ||
|
||
import mongoose from 'mongoose' | ||
|
||
mongoose.connect('mongodb://localhost:27017/project') | ||
.then(() => { | ||
try { | ||
createComment('669a7e6492f2e1bb5b9cbcf2', '66b1800bb4ebd58a8b4e25aa', 'no funciona porque no os da la gana') | ||
.then(() => console.log('comment created')) | ||
.catch(error => console.error(error.message)) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
}) |
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,42 @@ | ||
import { User, Comment } from '../data/index.js' | ||
|
||
import { validate, errors } from 'com' | ||
|
||
const { SystemError, MatchError } = errors | ||
|
||
function deleteComment(userId, commentId, text) { | ||
validate.id(userId) | ||
validate.id(commentId, 'commentId') | ||
validate.text(text) | ||
|
||
|
||
return User.findById(userId) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) throw new MatchError('User not found') | ||
|
||
return Comment.findById(commentId).populate('issue', 'author cinema status') | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(comment => { | ||
if (!comment) throw new MatchError('comment not found') | ||
|
||
if (!comment.issue) throw new MatchError('issue not found') | ||
|
||
const { issue } = comment | ||
|
||
if (issue.status === 'closed') throw new MatchError('You can only edit comments from open Issues') | ||
|
||
if (issue.author.toString() !== userId && (user.role !== 'manager' || user.cinema.toString() !== comment.issue.cinema.toString())) throw new MatchError('You only could edit comments in issues that is created by you or if you´re manager of the cinema') | ||
|
||
if (comment.author.toString() !== userId) throw new MatchError('You can only edit your comments') | ||
|
||
comment.text = text | ||
|
||
return comment.save() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(comment => { }) | ||
|
||
}) | ||
}) | ||
} | ||
export default deleteComment |
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
44 changes: 44 additions & 0 deletions
44
staff/sergio-ocaña/project/api/logic/retrieveCommentsFromIssue.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,44 @@ | ||
import { User, Issue, Comment } from '../data/index.js' | ||
|
||
import { validate, errors } from 'com' | ||
|
||
const { SystemError, MatchError } = errors | ||
|
||
function retrieveCommentsFromIssue(userId, issueId) { | ||
validate.id(userId) | ||
validate.id(issueId, 'issueId') | ||
|
||
return User.findById(userId) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) throw new MatchError('User not found') | ||
|
||
return Issue.findById(issueId) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(issue => { | ||
if (!issue) throw new MatchError('Cinema not found') | ||
|
||
if (issue.author.toString() !== userId && (user.role !== 'manager' || user.cinema.toString() !== issue.cinema.toString())) throw new MatchError('You only could only see issues that belongs you or if you´re manager of the cinema') | ||
|
||
return Comment.find({ issue: issueId }).select('-__v').sort({ date: 1 }).lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(comments => { | ||
comments.forEach(comment => { | ||
if (comment._id) { | ||
comment.id = comment._id.toString() | ||
|
||
delete comment._id | ||
} | ||
if (typeof comment.issue === 'object') comment.issue = comment.issue.toString() | ||
|
||
if (typeof comment.author === 'object') comment.author = comment.author.toString() | ||
}) | ||
|
||
return comments | ||
}) | ||
|
||
}) | ||
}) | ||
} | ||
|
||
export default retrieveCommentsFromIssue |
14 changes: 14 additions & 0 deletions
14
staff/sergio-ocaña/project/api/logic/retrieveCommentsFromIssue.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,14 @@ | ||
import retrieveCommentsFromIssue from './retrieveCommentsFromIssue.js' | ||
|
||
import mongoose from 'mongoose' | ||
|
||
mongoose.connect('mongodb://localhost:27017/project') | ||
.then(() => { | ||
try { | ||
retrieveCommentsFromIssue('669a7e6492f2e1bb5b9cbcf2', '66b1800bb4ebd58a8b4e25aa') | ||
.then((comments) => console.log('comments retrieved', comments)) | ||
.catch(error => console.error(error.message)) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
}) |
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,42 @@ | ||
import { User, Comment } from '../data/index.js' | ||
|
||
import { validate, errors } from 'com' | ||
|
||
const { SystemError, MatchError } = errors | ||
|
||
function updateComment(userId, commentId, text) { | ||
validate.id(userId) | ||
validate.id(commentId, 'commentId') | ||
validate.text(text) | ||
|
||
|
||
return User.findById(userId) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) throw new MatchError('User not found') | ||
|
||
return Comment.findById(commentId).populate('issue', 'author cinema status') | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(comment => { | ||
if (!comment) throw new MatchError('Comment not found') | ||
|
||
console.log(comment.issue) | ||
|
||
if (!comment.issue) throw new MatchError('Issue not found') | ||
|
||
if (comment.issue.status === 'closed') throw new MatchError('You can only edit comments from open Issues') | ||
|
||
if (comment.issue.author.toString() !== userId && (user.role !== 'manager' || user.cinema.toString() !== comment.issue.cinema.toString())) throw new MatchError('You only could edit comments in issues that is created by you or if you´re manager of the cinema') | ||
|
||
if (comment.author.toString() !== userId) throw new MatchError('You can only edit your comments') | ||
|
||
comment.text = text | ||
|
||
return comment.save() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(comment => { }) | ||
}) | ||
}) | ||
|
||
} | ||
export default updateComment |
14 changes: 14 additions & 0 deletions
14
staff/sergio-ocaña/project/api/logic/updateComment.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,14 @@ | ||
import updateComment from './updateComment.js' | ||
|
||
import mongoose from 'mongoose' | ||
|
||
mongoose.connect('mongodb://localhost:27017/project') | ||
.then(() => { | ||
try { | ||
updateComment('669a7e6492f2e1bb5b9cbcf2', '66b2c0476568a998a6123a5a', 'no funciona porque vagos') | ||
.then(() => console.log('comment updated')) | ||
.catch(error => console.error(error.message)) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
}) |
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