Skip to content

Commit

Permalink
add all models; implemente issue api/app logics routes and test ; cre…
Browse files Browse the repository at this point in the history
…ate component Footer and implement in react router-dom b00tc4mp#257
  • Loading branch information
Yeyusin committed Aug 5, 2024
1 parent 240a233 commit 6a6946e
Show file tree
Hide file tree
Showing 45 changed files with 838 additions and 39 deletions.
94 changes: 92 additions & 2 deletions staff/sergio-ocaña/project/api/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,110 @@ const room = new Schema({
},
cinema: {
type: ObjectId,
ref: 'Cinema'
ref: 'Cinema',
required: true
},
temperature: {
type: String,
required: true,
}
})
const ticket = new Schema({
user: {
type: ObjectId,
ref: 'User',
},
cinema: {
type: ObjectId,
ref: 'Cinema',
required: true
},
room: {
type: ObjectId,
ref: 'Room',
required: true
},
seat: {
type: String,
required: true
}
})
const issue = new Schema({
author: {
type: ObjectId,
ref: 'User',
required: true
},
cinema: {
type: ObjectId,
ref: 'Cinema',
required: true
},
room: {
type: ObjectId,
ref: 'Room',
},
location: {
type: String,
required: true
},
type: {
type: String,
requiered: true,
enum: ['temperature', 'sound', 'film', 'cleaning']
},
description: {
type: String,
requiered: true
},
status: {
type: String,
enum: ['open', 'closed'],
default: 'open',
required: true
},
ticket: {
type: ObjectId,
ref: 'Ticket'
},
date: {
type: Date,
required: true
}
})
const comment = new Schema({
text: {
type: String,
requiered: true
},
issue: {
type: ObjectId,
ref: 'Issue',
requiered: true
},
author: {
type: ObjectId,
ref: 'User',
required: true
},
date: {
type: Date,
required: true
}
})

const Room = model('Room', room)
const Cinema = model('Cinema', cinema)
const User = model('User', user)
const Ticket = model('Ticket', ticket)
const Issue = model('Issue', issue)
const Comment = model('Comment', comment)

export {
Cinema,
User,
Room
Room,
Ticket,
Issue,
Comment
}
185 changes: 177 additions & 8 deletions staff/sergio-ocaña/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ mongoose.connect(MONGO_URL)
const jsonBodyParser = express.json()

server.use(cors())
/* User Routes */

/* UserRoutes */

server.post('/users/customer', jsonBodyParser, (req, res) => {
try {
const { name, birthdate, email, password } = req.body
Expand Down Expand Up @@ -168,11 +170,11 @@ mongoose.connect(MONGO_URL)
status = 401

error = new MatchError(error.message)

}
res.status(status).json({ error: error.constructor.name, message: error.message })
}
})

server.get('/cinemas', (req, res) => {
try {
const { authorization } = req.headers
Expand Down Expand Up @@ -202,7 +204,6 @@ mongoose.connect(MONGO_URL)
status = 401

error = new MatchError(error.message)

}
res.status(status).json({ error: error.constructor.name, message: error.message })
}
Expand Down Expand Up @@ -239,7 +240,6 @@ mongoose.connect(MONGO_URL)
status = 401

error = new MatchError(error.message)

}
res.status(status).json({ error: error.constructor.name, message: error.message })
}
Expand Down Expand Up @@ -275,7 +275,6 @@ mongoose.connect(MONGO_URL)
status = 401

error = new MatchError(error.message)

}
res.status(status).json({ error: error.constructor.name, message: error.message })
}
Expand Down Expand Up @@ -390,7 +389,6 @@ mongoose.connect(MONGO_URL)
}
})


server.get('/rooms/:cinemaId', (req, res) => {
try {
const { authorization } = req.headers
Expand Down Expand Up @@ -458,10 +456,8 @@ mongoose.connect(MONGO_URL)

error = new MatchError(error.message)
}

res.status(status).json({ error: error.constructor.name, message: error.message })
}

})

server.delete('/rooms/:cinemaId/:roomId', (req, res) => {
Expand Down Expand Up @@ -494,12 +490,185 @@ mongoose.connect(MONGO_URL)
status = 401

error = new MatchError(error.message)
}
res.status(status).json({ error: error.constructor.name, message: error.message })
}
})

/* IssueRoutes */

server.post('/issues/:cinemaId/:roomId?', jsonBodyParser, (req, res) => {
try {
const { authorization } = req.headers

const token = authorization.slice(7)

const { sub: userId } = jwt.verify(token, JWT_SECRET)

const { cinemaId, roomId } = req.params

const { location, type, description } = req.body

logic.createIssue(userId, cinemaId, location, type, description, roomId)
.then(() => res.status(201).send())
.catch(error => {
let status = 500

if (error instanceof MatchError)
status = 401

res.status(status).json({ error: error.constructor.name, message: error.message })
})

} catch (error) {
let status = 500

if (error instanceof TypeError || error instanceof RangeError || error instanceof ContentError)
status = 400

else if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError) {
status = 401

error = new MatchError(error.message)
}
res.status(status).json({ error: error.constructor.name, message: error.message })
}
})

server.get('/issues/cinema', (req, res) => {
try {
const { authorization } = req.headers

const token = authorization.slice(7)

const { sub: userId } = jwt.verify(token, JWT_SECRET)

logic.retrieveCinemaIssues(userId)
.then(issues => res.json(issues))
.catch(error => {

let status = 500

if (error instanceof MatchError)
status = 404

res.status(status).json({ error: error.constructor.name, message: error.message })
})
} catch (error) {
let status = 500

if (error instanceof TypeError || error instanceof RangeError || error instanceof ContentError)
status = 400
else if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError) {
status = 401

error = new MatchError(error.message)
}
res.status(status).json({ error: error.constructor.name, message: error.message })
}
})

server.get('/issues/user', (req, res) => {
try {
const { authorization } = req.headers

const token = authorization.slice(7)

const { sub: userId } = jwt.verify(token, JWT_SECRET)

logic.retrieveUserIssues(userId)
.then(issues => res.json(issues))
.catch(error => {

let status = 500

if (error instanceof MatchError)
status = 404

res.status(status).json({ error: error.constructor.name, message: error.message })
})
} catch (error) {
let status = 500

if (error instanceof TypeError || error instanceof RangeError || error instanceof ContentError)
status = 400
else if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError) {
status = 401

error = new MatchError(error.message)
}
res.status(status).json({ error: error.constructor.name, message: error.message })
}
})

server.patch('/issues/:issueId', (req, res) => {
try {
const { authorization } = req.headers

const token = authorization.slice(7)

const { sub: userId } = jwt.verify(token, JWT_SECRET)

const { issueId } = req.params

logic.closeIssue(userId, issueId)
.then(() => { res.status(200).send() })
.catch(error => {
let status = 500

if (error instanceof MatchError)
status = 401

res.status(status).json({ error: error.constructor.name, message: error.message })
})
} catch (error) {
let status = 500

if (error instanceof TypeError || error instanceof RangeError || error instanceof ContentError)
status = 400
else if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError) {
status = 401

error = new MatchError(error.message)
}
res.status(status).json({ error: error.constructor.name, message: error.message })
}
})

server.delete('/issues/:issueId', (req, res) => {
try {
const { authorization } = req.headers

const token = authorization.slice(7)

const { sub: userId } = jwt.verify(token, JWT_SECRET)

const { issueId } = req.params

logic.deleteIssue(userId, issueId)
.then(() => res.status(204).send())
.catch(error => {
let status = 500

if (error instanceof MatchError)
status = 401
res.status(status).json({ error: error.constructor.name, message: error.message })
})

} catch (error) {
let status = 500

if (error instanceof TypeError || error instanceof RangeError || error instanceof ContentError)
status = 400

else if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError) {
status = 401

error = new MatchError(error.message)
}
res.status(status).json({ error: error.constructor.name, message: error.message })
}
})

server.listen(PORT, () => console.log(`API started on port ${PORT}`))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function addCinemaToManager(userId, cinemaId) {
.then(user => {
if (!user) throw new MatchError('user not found')

if (user.role !== 'manager') throw new MatchError('Only Managers are allowed could be assigned to a cinema')
if (user.role !== 'manager') throw new MatchError('Only Managers are allowed to be assigned to a cinema')

if (user.cinema) throw new MatchError('You already have a Cinema. If you want to change it, delete it first')

Expand Down
Loading

0 comments on commit 6a6946e

Please sign in to comment.