diff --git "a/staff/sergio-oca\303\261a/project/api/data/index.js" "b/staff/sergio-oca\303\261a/project/api/data/index.js" index cec61efe..e233b7d9 100644 --- "a/staff/sergio-oca\303\261a/project/api/data/index.js" +++ "b/staff/sergio-oca\303\261a/project/api/data/index.js" @@ -45,8 +45,8 @@ const user = new Schema({ enum: ['customer', 'manager'], default: 'customer' } - }) + const room = new Schema({ name: { type: String, @@ -62,6 +62,7 @@ const room = new Schema({ required: true, } }) + const ticket = new Schema({ user: { type: ObjectId, @@ -82,6 +83,7 @@ const ticket = new Schema({ required: true } }) + const issue = new Schema({ author: { type: ObjectId, @@ -125,6 +127,7 @@ const issue = new Schema({ required: true } }) + const comment = new Schema({ text: { type: String, diff --git "a/staff/sergio-oca\303\261a/project/api/logic/createComment.js" "b/staff/sergio-oca\303\261a/project/api/logic/createComment.js" new file mode 100644 index 00000000..92ac1038 --- /dev/null +++ "b/staff/sergio-oca\303\261a/project/api/logic/createComment.js" @@ -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 \ No newline at end of file diff --git "a/staff/sergio-oca\303\261a/project/api/logic/createComment.test.js" "b/staff/sergio-oca\303\261a/project/api/logic/createComment.test.js" new file mode 100644 index 00000000..a81e9661 --- /dev/null +++ "b/staff/sergio-oca\303\261a/project/api/logic/createComment.test.js" @@ -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) + } + }) \ No newline at end of file diff --git "a/staff/sergio-oca\303\261a/project/api/logic/deleteCinema.js" "b/staff/sergio-oca\303\261a/project/api/logic/deleteCinema.js" index d298eb65..8b672e32 100644 --- "a/staff/sergio-oca\303\261a/project/api/logic/deleteCinema.js" +++ "b/staff/sergio-oca\303\261a/project/api/logic/deleteCinema.js" @@ -1,4 +1,4 @@ -import { Cinema, User, Room } from '../data/index.js' +import { Cinema, User, Room, Issue, Comment } from '../data/index.js' import { validate, errors } from 'com' const { MatchError, SystemError } = errors @@ -8,6 +8,8 @@ function deleteCinema(userId, cinemaId) { validate.id(userId) validate.id(cinemaId, 'cinemaId') + // TODO Refactor to promise.all and look forEach deleteComment + return User.findById(userId) .catch(error => { throw new SystemError(error.message) }) .then(user => { @@ -20,17 +22,38 @@ function deleteCinema(userId, cinemaId) { .then(cinema => { if (!cinema) throw new MatchError('Cinema doesn´t exist') - return Cinema.findByIdAndDelete(cinemaId) - .catch(error => { throw SystemError(error.message) }) .then(() => { - return User.updateMany({ cinema: cinemaId }, { $unset: { cinema } }) + return Issue.find({ cinema: cinemaId } .catch(error => { throw SystemError(error.message) }) + .then((issues) => { + const deleteIssuesId = issues.map(issue => issue._id) + + return Cinema.findByIdAndDelete(cinemaId) + .catch(error => { throw SystemError(error.message) }) + .then(() => { + return User.updateMany({ cinema: cinemaId }, { $unset: { cinema } }) + .catch(error => { throw SystemError(error.message) }) + }) + .then(() => { + return Room.deleteMany({ cinema: cinemaId }) + .catch(error => { throw SystemError(error.message) }) + .then(() => { }) + }) + .then(() => { + return Issue.deleteMany({ cinema: cinemaId }) + .catch(error => { throw SystemError(error.message) }) + .then(() => { + deleteIssuesId.forEach(issueId => { + return Comment.deleteMany({ issue: issueId }) + .catch(error => { throw SystemError(error.message) }) + }) + }) + .then(() => { }) + }) + }) + ) }) - .then(() => { - return Room.deleteMany({ cinema: cinemaId }) - .catch(error => { throw SystemError(error.message) }) - .then(() => { }) - }) + }) }) } diff --git "a/staff/sergio-oca\303\261a/project/api/logic/deleteComment.js" "b/staff/sergio-oca\303\261a/project/api/logic/deleteComment.js" new file mode 100644 index 00000000..b1990407 --- /dev/null +++ "b/staff/sergio-oca\303\261a/project/api/logic/deleteComment.js" @@ -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 \ No newline at end of file diff --git "a/staff/sergio-oca\303\261a/project/api/logic/deleteIssue.js" "b/staff/sergio-oca\303\261a/project/api/logic/deleteIssue.js" index ae7df540..b0f01021 100644 --- "a/staff/sergio-oca\303\261a/project/api/logic/deleteIssue.js" +++ "b/staff/sergio-oca\303\261a/project/api/logic/deleteIssue.js" @@ -1,4 +1,4 @@ -import { User, Issue } from '../data/index.js' +import { User, Issue, Comment } from '../data/index.js' import { validate, errors } from 'com' const { MatchError, SystemError } = errors @@ -12,7 +12,7 @@ function deleteIssue(userId, issueId) { .then(user => { if (!user) throw new MatchError('user not found') - return Issue.findById(issueId).lean() + return Issue.findById(issueId) .catch(error => { throw new SystemError(error.message) }) .then(issue => { if (!issue) throw new MatchError('issue not found') @@ -21,7 +21,11 @@ function deleteIssue(userId, issueId) { return Issue.findByIdAndDelete(issueId) .catch(error => { throw new SystemError(error.message) }) - .then(() => { }) + .then(() => { + return Comment.deleteMany({ issue: issueId }) + .catch(error => { throw new SystemError(error.message) }) + .then(() => { }) + }) }) }) } diff --git "a/staff/sergio-oca\303\261a/project/api/logic/retrieveCommentsFromIssue.js" "b/staff/sergio-oca\303\261a/project/api/logic/retrieveCommentsFromIssue.js" new file mode 100644 index 00000000..d7b2809b --- /dev/null +++ "b/staff/sergio-oca\303\261a/project/api/logic/retrieveCommentsFromIssue.js" @@ -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 diff --git "a/staff/sergio-oca\303\261a/project/api/logic/retrieveCommentsFromIssue.test.js" "b/staff/sergio-oca\303\261a/project/api/logic/retrieveCommentsFromIssue.test.js" new file mode 100644 index 00000000..60660286 --- /dev/null +++ "b/staff/sergio-oca\303\261a/project/api/logic/retrieveCommentsFromIssue.test.js" @@ -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) + } + }) \ No newline at end of file diff --git "a/staff/sergio-oca\303\261a/project/api/logic/updateComment.js" "b/staff/sergio-oca\303\261a/project/api/logic/updateComment.js" new file mode 100644 index 00000000..4255e82f --- /dev/null +++ "b/staff/sergio-oca\303\261a/project/api/logic/updateComment.js" @@ -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 \ No newline at end of file diff --git "a/staff/sergio-oca\303\261a/project/api/logic/updateComment.test.js" "b/staff/sergio-oca\303\261a/project/api/logic/updateComment.test.js" new file mode 100644 index 00000000..e02912ff --- /dev/null +++ "b/staff/sergio-oca\303\261a/project/api/logic/updateComment.test.js" @@ -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) + } + }) \ No newline at end of file diff --git "a/staff/sergio-oca\303\261a/project/api/logic/updateRoom.js" "b/staff/sergio-oca\303\261a/project/api/logic/updateRoom.js" index 3f43f797..b76581c7 100644 --- "a/staff/sergio-oca\303\261a/project/api/logic/updateRoom.js" +++ "b/staff/sergio-oca\303\261a/project/api/logic/updateRoom.js" @@ -27,8 +27,6 @@ function updateRoom(userId, cinemaId, roomId, name, temperature) { if (cinemaId !== user.cinema.toString()) throw new MatchError('You can only modify your own cinema') - if (user.role !== 'manager') throw new MatchError('You don t have privileges') - return Room.findById(roomId) .catch(error => { throw new SystemError(error.message) }) .then(room => {