Skip to content

Commit

Permalink
Merge branch 'actioncontrollers' of github.com:JuanPabloLeber/Project…
Browse files Browse the repository at this point in the history
…-Solvion into dev
  • Loading branch information
mutazen committed Jun 16, 2021
2 parents 21a6dcd + 0961a81 commit 5cb8b14
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 27 deletions.
7 changes: 4 additions & 3 deletions api/controllers/action.controllers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const { incidencesModel } = require('../models/incidences.model')

exports.createAction = (req, res) => {
const day = new Date()
const finishD = new Date(0)
incidencesModel
.findById(req.params.incidenceId)
.then(incidence => {
incidence.actions.push({ technicianId: req.params.technicianId, ...req.body })
console.log(incidence)
incidence.actions.push({ technicianId: req.params.technicianId, startDate: day, finishDate: finishD, ...req.body })
incidence.save(err => {
if (err) res.status(500).send(err)
res.status(200).json(incidence)
Expand All @@ -21,13 +24,11 @@ exports.updateAction = (req, res) => {
.findById(req.params.incidenceId)
.then(incidence => {
const action = incidence.actions.id(req.params.actionId)

incidence.actions.id(req.params.actionId).done = req.body.done ?? action.done
incidence.actions.id(req.params.actionId).status = req.body.status ?? action.status
incidence.actions.id(req.params.actionId).startDate = action.startDate
incidence.actions.id(req.params.actionId).finishDate = action.finishDate
incidence.actions.id(req.params.actionId).technicianId = action.technicianId

incidence.save(err => {
if (err) res.status(500).send(err)
res.status(200).json(incidence)
Expand Down
27 changes: 21 additions & 6 deletions api/controllers/clients.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@ const { incidencesModel } = require('../models/incidences.model')

exports.listUserIncidences = (req, res) => {
incidencesModel
.find({ client: { email: req.body.email } })
.find({ 'client.email': req.params.clientEmail })
.then(incidences => {
const incidencesArray = []
incidences.forEach(incidence => {
incidencesArray.push([incidence.status, incidence.actions, incidence.timeToSolve])
})
res.status(200).json(incidencesArray)

if (incidences[0].client.password === req.params.clientPassword) {
console.log(incidences)
const incidencesArray = []
incidences.forEach((incidence, index) => {
incidencesArray[index] = [{ 'status': incidence.status, 'subject': incidence.subject, 'description': incidence.description }]
console.log(incidences[0].actions)
incidences[0].actions.forEach((element1, index1) => {
incidencesArray[index].push({
'done': incidences[0].actions[index1].done,
'status': incidences[0].actions[index1].status,
'startDate': incidences[0].actions[index1].startDate,
'finishDate': incidences[0].actions[index1].finishDate,
})
})
})
res.status(200).json(incidencesArray)
} else {
return res.status(401).json({ error: 'Wrong email or password' })
}
})
.catch(err => {
console.log(err)
Expand Down
67 changes: 62 additions & 5 deletions api/controllers/incidences.controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

const { incidencesModel } = require('../models/incidences.model')

exports.createIncidence = (req, res) => {
const day = new Date()
const isoDay = day.toISOString()
const finishD = new Date(0000,00,00)
const finishD = new Date(0)

console.log(isoDay)
incidencesModel
.create({
subject: req.body.subject,
Expand All @@ -14,7 +13,6 @@ exports.createIncidence = (req, res) => {
priority: ' ',
startDate: day,
finishDate: finishD,
employees: [],
incidenceCategory: req.params.incidenceCategory,
client: {
firstName: req.body.client.firstName,
Expand All @@ -32,4 +30,63 @@ exports.createIncidence = (req, res) => {
console.log(err)
res.status(500).json({ msg: 'Error' })
})
}
}

exports.getIncidence = (req, res) => {
incidencesModel
.findById(req.params.incidenceID)
.populate('incidenceCategory')
.populate('Technician')
.then(incidence => {
res.status(200).json(incidence)
})
.catch(err => {
console.log(err)
res.status(500).json({ msg: 'Error' })
})
}

exports.updateIncidence = (req, res) => {
incidencesModel
.findById(req.params.incidenceID)
.then(incidence => {
const copyIncidence = incidence
incidence.subject = req.body.subject ?? copyIncidence.subject
incidence.description = req.body.description ?? copyIncidence.description
incidence.status = req.body.status ?? copyIncidence.status
incidence.priority = req.body.priority ?? copyIncidence.priority
incidence.startDate = copyIncidence.startDate
incidence.finishDate = req.body.finishDate ?? copyIncidence.finishDate
incidence.Technician = req.body.Technician ?? copyIncidence.Technician
incidence.incidenceCategory = req.params.incidenceCategory ?? copyIncidence.incidenceCategory
incidence.actions = copyIncidence.actions
incidence.client = copyIncidence.client

incidence.save(err => {
if (err) res.status(500).send(err)
res.status(200).json(incidence)
})
})
.catch(err => {
console.log(err)
res.status(500).json({ msg: 'Error' })
})
}

exports.deleteIncidence = (req, res) => {
incidencesModel
.findByIdAndDelete(req.params.incidenceID, (err) => {
if (err) {
return ('There was a problem while deleting')
} else {
return ('Deleting accomplished')
}
})
.then(() => {
res.status(200).json('The incidence was deleted')
})
.catch(err => {
console.log(err)
res.status(500).json({ msg: 'Error' })
})
}
5 changes: 2 additions & 3 deletions api/models/incidences.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ const incidenceSchema = new mongoose.Schema({
},
Technician: {
type: mongoose.Schema.Types.ObjectId,
ref: 'employee',
required: true
ref: 'employee'
},
incidenceCategory: {
type: mongoose.Schema.Types.ObjectId,
Expand All @@ -41,7 +40,7 @@ const incidenceSchema = new mongoose.Schema({
},

actions: [actionSchema],
client: [clientSchema]
client: clientSchema
})

const incidencesModel = mongoose.model('incidences', incidenceSchema)
Expand Down
4 changes: 2 additions & 2 deletions api/models/incidencesCategories.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const mongoose = require('mongoose')
const incidenceCategoriesSchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true
required: true
// unique: true
}
})

Expand Down
5 changes: 3 additions & 2 deletions api/routes/action.router.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const actionRouter = require('express').Router()

const { checkAuth } = require('../../utils')
const {
createAction,
updateAction,
getAction
} = require('../controllers/action.controllers')

actionRouter.get('/incidences/:incidenceId/:actionId', getAction)
actionRouter.post('/incidences/:technicianId/:incidenceId', createAction)
actionRouter.put('/incidences/:incidenceId/:actionId', updateAction)
actionRouter.post('/incidences/:technicianId/:incidenceId', checkAuth, createAction)
actionRouter.put('/incidences/:incidenceId/:actionId', checkAuth, updateAction)

exports.actionRouter = actionRouter
7 changes: 3 additions & 4 deletions api/routes/clients.router.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const clientsRouter = require('express').Router()

const {
listUserIncidences
listUserIncidences
} = require('../controllers/clients.controller')

clientsRouter.get('/', listUserIncidences)
clientsRouter.get('/clientEmail/:clientEmail/clientPassword/:clientPassword', listUserIncidences)


exports.clientsRouter = clientsRouter
exports.clientsRouter = clientsRouter
10 changes: 8 additions & 2 deletions api/routes/incidences.router.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const incidencesRouter = require('express').Router()

const { checkAuth } = require('../../utils')

const {
createIncidence
createIncidence,
getIncidence,
updateIncidence,
deleteIncidence
} = require('../controllers/incidences.controller')

incidencesRouter.get('/:incidenceID', getIncidence)
incidencesRouter.post('/:customerServiceId/:incidenceCategory', createIncidence)
incidencesRouter.put('/:incidenceID', checkAuth, updateIncidence)
incidencesRouter.delete('/incidenceID/:incidenceID', deleteIncidence)

exports.incidencesRouter = incidencesRouter
13 changes: 13 additions & 0 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ exports.checkCustomerServiceOrManager = (req, res, next) => {
})
}

exports.checkCustomerServiceOrManagerOrTechnician = (req, res, next) => {
employeeModel
.findOne({ email: req.body.token.email })
.then(user => {
if (user.rol === 'CustomerService' || user.rol === 'Manager' || user.rol === 'Technician') {
res.locals.user = user
next()
} else {
res.json({ err: 'Token not valid' })
}
})
}

exports.isValidEmail = (email) => {
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return re.test(String(email).toLowerCase())
Expand Down

0 comments on commit 5cb8b14

Please sign in to comment.