Commit 294e544d authored by rlacko's avatar rlacko 💬
Browse files

separate application controller

parent 16e6aa8d
Pipeline #4785 passed with stages
in 2 minutes and 46 seconds
const { Router } = require('express')
const controllers = require('./applicationControllers')
const controllers = require('./controllers')
const { isLoggedIn, isMentor } = require('../../middlewares/auth')
const router = Router()
......@@ -7,15 +7,13 @@ const router = Router()
// /api/v1/application
router
.route('/')
.get(isLoggedIn, isMentor, controllers.default.getMany)
.post(isLoggedIn, controllers.default.createOne)
.get(isLoggedIn, isMentor, controllers.getMany)
.post(isLoggedIn, controllers.createOne)
// /api/v1/application/id/:id
router.route('/id/:id').get(isLoggedIn, controllers.default.getOneByID)
router.route('/id/:id').get(isLoggedIn, controllers.getOneByID)
// /api/v1/application/schacc/:schacc
router
.route('/schacc/:schacc')
.get(isLoggedIn, controllers.default.getOneBySchacc)
router.route('/schacc/:schacc').get(isLoggedIn, controllers.getOneBySchacc)
exports.default = router
const { crudControllers } = require('../../utils/crud')
const { Application } = require('./applicationModel')
const { Groups } = require('../groups/groupsModel')
const { Settings } = require('../settings/settingsModel')
const { pick, merge } = require('lodash')
const { isValidObjectId } = require('mongoose')
exports.default = crudControllers(Application, [
'_id',
'motivation',
'expectation',
'solution',
'groups',
])
const { Application } = require('../applicationModel')
const { Groups } = require('../../groups/groupsModel')
const { Settings } = require('../../settings/settingsModel')
const { pick } = require('lodash')
const { pickedKeys } = require('.')
const findGroupIds = async (group) => {
groupObject = await Groups.findOne({ name: group }).lean().exec()
......@@ -20,7 +11,7 @@ const findGroupIds = async (group) => {
return groupObject._id
}
exports.default.createOne = async (req, res) => {
exports.createOne = async (req, res) => {
try {
// Convert group names to Ids
let groupIds = await Promise.all(
......@@ -37,7 +28,7 @@ exports.default.createOne = async (req, res) => {
creator: req.body.creator,
},
{
upsert: true, // Create new One if there is no match
upsert: true,
returnOriginal: false,
setDefaultsOnInsert: true,
runValidators: true,
......@@ -56,7 +47,7 @@ exports.default.createOne = async (req, res) => {
state: req.body.state,
},
{
upsert: true, // Create new One if there is no match
upsert: true,
returnOriginal: false,
setDefaultsOnInsert: true,
runValidators: true,
......@@ -89,7 +80,7 @@ exports.default.createOne = async (req, res) => {
groups: groupIds,
},
{
upsert: true, // Create new One if there is no match
upsert: true,
returnOriginal: false,
setDefaultsOnInsert: true,
runValidators: true,
......@@ -107,15 +98,7 @@ exports.default.createOne = async (req, res) => {
.exec()
retDoc.creator = retDoc._creator
return res.status(201).json({
data: pick(retDoc, [
'_id',
'creator',
'motivation',
'expectation',
'solution',
'groups',
'state',
]),
data: pick(retDoc, pickedKeys),
})
} catch (err) {
if (err.name == 'ValidationError') {
......@@ -133,112 +116,3 @@ exports.default.createOne = async (req, res) => {
return res.status(400).end()
}
}
exports.default.getMany = async (req, res) => {
try {
const docs = await Application.find()
.populate('groups', ['name'])
.populate('_creator', ['schacc', 'fullName', 'secondaryEmail'])
.lean()
.exec()
res.status(200).json({
data: docs.map((e) => {
e.creator = e._creator
return pick(e, [
'_id',
'creator',
'motivation',
'expectation',
'solution',
'groups',
'state',
])
}),
})
} catch (e) {
console.error(e)
res.status(400).end()
}
}
exports.default.getOneByID = async (req, res) => {
try {
let doc
if (req.params.id === 'own')
doc = Application.findOne({ creator: req.user.schacc })
else doc = Application.findOne({ _id: req.params.id })
doc = await doc
.populate('groups', ['name'])
.populate('_creator', ['schacc', 'fullName', 'secondaryEmail'])
.lean()
.exec()
if (!doc) {
return res.status(404).end()
}
if (doc.creator != req.user.schacc && req.user.role != 'mentor')
return res.status(403).end()
doc.creator = doc._creator
res.status(200).json({
data: pick(doc, [
'_id',
'creator',
'motivation',
'expectation',
'solution',
'groups',
'state',
]),
})
} catch (err) {
if (err.name == 'CastError') {
// Throwed by Mongoose
return res.status(422).json('Invalid ID provided')
} else {
console.error(err)
res.status(400).end()
}
}
}
exports.default.getOneBySchacc = async (req, res) => {
try {
let doc = await Application.findOne({ creator: req.params.schacc })
.populate('groups', ['name'])
.populate('_creator', ['schacc', 'fullName', 'secondaryEmail'])
.lean()
.exec()
if (!doc) {
return res.status(404).end()
}
if (doc.creator != req.user.schacc && req.user.role != 'mentor')
return res.status(403).end()
doc.creator = doc._creator
res.status(200).json({
data: pick(doc, [
'_id',
'creator',
'motivation',
'expectation',
'solution',
'groups',
'state',
]),
})
} catch (err) {
if (err.name == 'CastError') {
// Throwed by Mongoose
return res.status(422).json('Invalid ID provided')
} else {
console.error(err)
res.status(400).end()
}
}
}
const { Application } = require('../applicationModel')
const { pick } = require('lodash')
const { pickedKeys } = require('.')
exports.getMany = async (req, res) => {
try {
const docs = await Application.find()
.populate('groups', ['name'])
.populate('_creator', ['schacc', 'fullName', 'secondaryEmail'])
.lean()
.exec()
res.status(200).json({
data: docs.map((e) => {
e.creator = e._creator
return pick(e, pickedKeys)
}),
})
} catch (e) {
console.error(e)
res.status(400).end()
}
}
const { Application } = require('../applicationModel')
const { pick } = require('lodash')
const { pickedKeys } = require('.')
exports.getOneByID = async (req, res) => {
try {
let doc
if (req.params.id === 'own')
doc = Application.findOne({ creator: req.user.schacc })
else doc = Application.findOne({ _id: req.params.id })
doc = await doc
.populate('groups', ['name'])
.populate('_creator', ['schacc', 'fullName', 'secondaryEmail'])
.lean()
.exec()
if (!doc) {
return res.status(404).end()
}
if (doc.creator != req.user.schacc && req.user.role != 'mentor')
return res.status(403).end()
doc.creator = doc._creator
res.status(200).json({
data: pick(doc, pickedKeys),
})
} catch (err) {
if (err.name == 'CastError') {
// Throwed by Mongoose
return res.status(422).json('Invalid ID provided')
} else {
console.error(err)
res.status(400).end()
}
}
}
const { Application } = require('../applicationModel')
const { pick } = require('lodash')
const { pickedKeys } = require('.')
exports.getOneBySchacc = async (req, res) => {
try {
let doc = await Application.findOne({ creator: req.params.schacc })
.populate('groups', ['name'])
.populate('_creator', ['schacc', 'fullName', 'secondaryEmail'])
.lean()
.exec()
if (!doc) {
return res.status(404).end()
}
if (doc.creator != req.user.schacc && req.user.role != 'mentor')
return res.status(403).end()
doc.creator = doc._creator
res.status(200).json({
data: pick(doc, pickedKeys),
})
} catch (err) {
if (err.name == 'CastError') {
// Throwed by Mongoose
return res.status(422).json('Invalid ID provided')
} else {
console.error(err)
res.status(400).end()
}
}
}
exports.pickedKeys = [
'_id',
'creator',
'motivation',
'expectation',
'solution',
'groups',
'state',
]
exports.createOne = require('./createOne').createOne
exports.getMany = require('./getMany').getMany
exports.getOneByID = require('./getOneByID').getOneByID
exports.getOneBySchacc = require('./getOneBySchacc').getOneBySchacc
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment