Skip to content

Commit

Permalink
add comments logic and update delete cinema/issues logic b00tc4mp#257
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeyusin committed Aug 7, 2024
1 parent 28a4073 commit a942b37
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 15 deletions.
5 changes: 4 additions & 1 deletion staff/sergio-ocaña/project/api/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const user = new Schema({
enum: ['customer', 'manager'],
default: 'customer'
}

})

const room = new Schema({
name: {
type: String,
Expand All @@ -62,6 +62,7 @@ const room = new Schema({
required: true,
}
})

const ticket = new Schema({
user: {
type: ObjectId,
Expand All @@ -82,6 +83,7 @@ const ticket = new Schema({
required: true
}
})

const issue = new Schema({
author: {
type: ObjectId,
Expand Down Expand Up @@ -125,6 +127,7 @@ const issue = new Schema({
required: true
}
})

const comment = new Schema({
text: {
type: String,
Expand Down
34 changes: 34 additions & 0 deletions staff/sergio-ocaña/project/api/logic/createComment.js
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 staff/sergio-ocaña/project/api/logic/createComment.test.js
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)
}
})
41 changes: 32 additions & 9 deletions staff/sergio-ocaña/project/api/logic/deleteCinema.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 => {
Expand All @@ -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(() => { })
})

})
})
}
Expand Down
42 changes: 42 additions & 0 deletions staff/sergio-ocaña/project/api/logic/deleteComment.js
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
10 changes: 7 additions & 3 deletions staff/sergio-ocaña/project/api/logic/deleteIssue.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')
Expand All @@ -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(() => { })
})
})
})
}
Expand Down
44 changes: 44 additions & 0 deletions staff/sergio-ocaña/project/api/logic/retrieveCommentsFromIssue.js
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
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)
}
})
42 changes: 42 additions & 0 deletions staff/sergio-ocaña/project/api/logic/updateComment.js
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 staff/sergio-ocaña/project/api/logic/updateComment.test.js
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)
}
})
2 changes: 0 additions & 2 deletions staff/sergio-ocaña/project/api/logic/updateRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit a942b37

Please sign in to comment.