Commit 27bcf17c authored by rlacko's avatar rlacko 💬
Browse files

Merge branch 'feature/kszkep-23-refactoring' into 'dev'

Resolve "Átfaktorálás"

See merge request !18
parents e34c9cfa 0f8f83d3
Pipeline #4729 passed with stages
in 3 minutes and 21 seconds
......@@ -7,9 +7,9 @@
},
"runOnChangeOnly": false,
"watch": ["src/**/*.js"],
"watch": ["src/**/*"],
"env": {
"NODE_ENV": "development"
},
"ext": "js,json"
"ext": "js,json,yml"
}
......@@ -4,6 +4,40 @@
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@apidevtools/json-schema-ref-parser": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/@apidevtools/json-schema-ref-parser/-/json-schema-ref-parser-8.0.0.tgz",
"integrity": "sha512-n4YBtwQhdpLto1BaUCyAeflizmIbaloGShsPyRtFf5qdFJxfssj+GgLavczgKJFa3Bq+3St2CKcpRJdjtB4EBw==",
"requires": {
"@jsdevtools/ono": "^7.1.0",
"call-me-maybe": "^1.0.1",
"js-yaml": "^3.13.1"
}
},
"@apidevtools/openapi-schemas": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/@apidevtools/openapi-schemas/-/openapi-schemas-2.0.4.tgz",
"integrity": "sha512-ob5c4UiaMYkb24pNhvfSABShAwpREvUGCkqjiz/BX9gKZ32y/S22M+ALIHftTAuv9KsFVSpVdIDzi9ZzFh5TCA=="
},
"@apidevtools/swagger-methods": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/@apidevtools/swagger-methods/-/swagger-methods-3.0.2.tgz",
"integrity": "sha512-QAkD5kK2b1WfjDS/UQn/qQkbwF31uqRjPTrsCs5ZG9BQGAkjwvqGFjjPqAuzac/IYzpPtRzjCP1WrTuAIjMrXg=="
},
"@apidevtools/swagger-parser": {
"version": "9.0.1",
"resolved": "https://registry.npmjs.org/@apidevtools/swagger-parser/-/swagger-parser-9.0.1.tgz",
"integrity": "sha512-Irqybg4dQrcHhZcxJc/UM4vO7Ksoj1Id5e+K94XUOzllqX1n47HEA50EKiXTCQbykxuJ4cYGIivjx/MRSTC5OA==",
"requires": {
"@apidevtools/json-schema-ref-parser": "^8.0.0",
"@apidevtools/openapi-schemas": "^2.0.2",
"@apidevtools/swagger-methods": "^3.0.0",
"@jsdevtools/ono": "^7.1.0",
"call-me-maybe": "^1.0.1",
"openapi-types": "^1.3.5",
"z-schema": "^4.2.2"
}
},
"@babel/code-frame": {
"version": "7.10.4",
"resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.10.4.tgz",
......@@ -627,6 +661,11 @@
"chalk": "^4.0.0"
}
},
"@jsdevtools/ono": {
"version": "7.1.3",
"resolved": "https://registry.npmjs.org/@jsdevtools/ono/-/ono-7.1.3.tgz",
"integrity": "sha512-4JQNk+3mVzK3xh2rqd6RB4J46qUR19azEHBneZyTZM+c456qOrbbM/5xcR8huNCCcbVt7+UmizG6GuUvPvKUYg=="
},
"@sindresorhus/is": {
"version": "0.14.0",
"resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz",
......@@ -1043,7 +1082,6 @@
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
"integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
"dev": true,
"requires": {
"sprintf-js": "~1.0.2"
}
......@@ -1209,8 +1247,7 @@
"balanced-match": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
"dev": true
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
},
"base": {
"version": "0.11.2",
......@@ -1427,7 +1464,6 @@
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
"dev": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
......@@ -1522,6 +1558,11 @@
}
}
},
"call-me-maybe": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/call-me-maybe/-/call-me-maybe-1.0.1.tgz",
"integrity": "sha1-JtII6onje1y95gJQoV8DHBak1ms="
},
"callsites": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz",
......@@ -1755,6 +1796,11 @@
"delayed-stream": "~1.0.0"
}
},
"commander": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-5.0.0.tgz",
"integrity": "sha512-JrDGPAKjMGSP1G0DUoaceEJ3DZgAfr/q6X7FVk4+U5KxUSKviYGM2k6zWkfyyBHy5rAtzgYJFa1ro2O9PtoxwQ=="
},
"commondir": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz",
......@@ -1770,8 +1816,7 @@
"concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
"dev": true
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
},
"configstore": {
"version": "5.0.1",
......@@ -2091,7 +2136,6 @@
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz",
"integrity": "sha512-yS+Q5i3hBf7GBkd4KG8a7eBNNWNGLTaEwwYWUijIYM7zrlYDM0BFXHjjPWlWZ1Rg7UaddZeIDmi9jF3HmqiQ2w==",
"dev": true,
"requires": {
"esutils": "^2.0.2"
}
......@@ -2513,8 +2557,7 @@
"esprima": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
"dev": true
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A=="
},
"esquery": {
"version": "1.3.1",
......@@ -2551,8 +2594,7 @@
"esutils": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz",
"integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==",
"dev": true
"integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g=="
},
"etag": {
"version": "1.8.1",
......@@ -3036,8 +3078,7 @@
"fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
"dev": true
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8="
},
"fsevents": {
"version": "2.1.3",
......@@ -3104,7 +3145,6 @@
"version": "7.1.6",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz",
"integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==",
"dev": true,
"requires": {
"fs.realpath": "^1.0.0",
"inflight": "^1.0.4",
......@@ -3373,7 +3413,6 @@
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
"integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
"dev": true,
"requires": {
"once": "^1.3.0",
"wrappy": "1"
......@@ -4269,7 +4308,6 @@
"version": "3.14.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.0.tgz",
"integrity": "sha512-/4IbIeHcD9VMHFqDR/gQ7EdZdLimOvW2DdcxFjdyyZ9NsbS+ccrXqVWDtab/lRl5AlUqmpBx8EhPaWR+OtY17A==",
"dev": true,
"requires": {
"argparse": "^1.0.7",
"esprima": "^4.0.0"
......@@ -4477,6 +4515,16 @@
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz",
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ=="
},
"lodash.get": {
"version": "4.4.2",
"resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz",
"integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk="
},
"lodash.isequal": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz",
"integrity": "sha1-QVxEePK8wwEgwizhDtMib30+GOA="
},
"lodash.sortby": {
"version": "4.7.0",
"resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz",
......@@ -4601,7 +4649,6 @@
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"dev": true,
"requires": {
"brace-expansion": "^1.1.7"
}
......@@ -4998,7 +5045,6 @@
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
"dev": true,
"requires": {
"wrappy": "1"
}
......@@ -5012,6 +5058,11 @@
"mimic-fn": "^2.1.0"
}
},
"openapi-types": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/openapi-types/-/openapi-types-1.3.5.tgz",
"integrity": "sha512-11oi4zYorsgvg5yBarZplAqbpev5HkuVNPlZaPTknPDzAynq+lnJdXAmruGWP0s+dNYZS7bjM+xrTpJw7184Fg=="
},
"optionator": {
"version": "0.9.1",
"resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.1.tgz",
......@@ -5173,8 +5224,7 @@
"path-is-absolute": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
"dev": true
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18="
},
"path-key": {
"version": "3.1.1",
......@@ -6248,8 +6298,7 @@
"sprintf-js": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
"dev": true
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw="
},
"sshpk": {
"version": "1.16.1",
......@@ -6486,6 +6535,50 @@
}
}
},
"swagger-jsdoc": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/swagger-jsdoc/-/swagger-jsdoc-4.0.0.tgz",
"integrity": "sha512-wHrmRvE/OQa3d387YIrRNPvsPwxkJc0tAYeCVa359gUIKPjC4ReduFhqq/+4erLUS79kY1T5Fv0hE0SV/PgBig==",
"requires": {
"commander": "5.0.0",
"doctrine": "3.0.0",
"glob": "7.1.6",
"js-yaml": "3.13.1",
"swagger-parser": "9.0.1"
},
"dependencies": {
"js-yaml": {
"version": "3.13.1",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz",
"integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==",
"requires": {
"argparse": "^1.0.7",
"esprima": "^4.0.0"
}
}
}
},
"swagger-parser": {
"version": "9.0.1",
"resolved": "https://registry.npmjs.org/swagger-parser/-/swagger-parser-9.0.1.tgz",
"integrity": "sha512-oxOHUaeNetO9ChhTJm2fD+48DbGbLD09ZEOwPOWEqcW8J6zmjWxutXtSuOiXsoRgDWvORYlImbwM21Pn+EiuvQ==",
"requires": {
"@apidevtools/swagger-parser": "9.0.1"
}
},
"swagger-ui-dist": {
"version": "3.30.2",
"resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.30.2.tgz",
"integrity": "sha512-hAu/ig5N8i0trXXbrC7rwbXV4DhpEAsZhYXDs1305OjmDgjGC0thINbb0197idy3Pp+B6w7u426SUM43GAP7qw=="
},
"swagger-ui-express": {
"version": "4.1.4",
"resolved": "https://registry.npmjs.org/swagger-ui-express/-/swagger-ui-express-4.1.4.tgz",
"integrity": "sha512-Ea96ecpC+Iq9GUqkeD/LFR32xSs8gYqmTW1gXCuKg81c26WV6ZC2FsBSPVExQP6WkyUuz5HEiR0sEv/HCC343g==",
"requires": {
"swagger-ui-dist": "^3.18.1"
}
},
"symbol-tree": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.4.tgz",
......@@ -7158,8 +7251,7 @@
"wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
"dev": true
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
},
"write": {
"version": "1.0.3",
......@@ -7288,6 +7380,30 @@
"dev": true
}
}
},
"z-schema": {
"version": "4.2.3",
"resolved": "https://registry.npmjs.org/z-schema/-/z-schema-4.2.3.tgz",
"integrity": "sha512-zkvK/9TC6p38IwcrbnT3ul9in1UX4cm1y/VZSs4GHKIiDCrlafc+YQBgQBUdDXLAoZHf2qvQ7gJJOo6yT1LH6A==",
"requires": {
"commander": "^2.7.1",
"lodash.get": "^4.4.2",
"lodash.isequal": "^4.5.0",
"validator": "^12.0.0"
},
"dependencies": {
"commander": {
"version": "2.20.3",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz",
"integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==",
"optional": true
},
"validator": {
"version": "12.2.0",
"resolved": "https://registry.npmjs.org/validator/-/validator-12.2.0.tgz",
"integrity": "sha512-jJfE/DW6tIK1Ek8nCfNFqt8Wb3nzMoAbocBF6/Icgg1ZFSBpObdnwVY2jQj6qUqzhx5jc71fpvBWyLGO7Xl+nQ=="
}
}
}
}
}
......@@ -28,6 +28,8 @@
"morgan": "^1.10.0",
"passport": "^0.4.1",
"passport-oauth2": "^1.5.0",
"swagger-jsdoc": "^4.0.0",
"swagger-ui-express": "^4.1.4",
"validator": "^13.1.1"
},
"devDependencies": {
......
const { crudControllers } = require('../../utils/crud')
const { Activity } = require('./activityModel')
const { User } = require('../user/userModel')
const { Attendance } = require('../attendance/attendanceModel')
const { Comment } = require('../comment/commentModel')
const { omit, pick } = require('lodash')
// Config
const pickedKeys = [
'_id',
'title',
......@@ -12,13 +12,14 @@ const pickedKeys = [
'date',
'type',
'createdAt',
'updatedAt',
'attendance',
'comment',
'comments',
]
exports.default = crudControllers(Activity, pickedKeys)
// Controller
exports.default.createOne = async (req, res) => {
module.exports.createOne = async function createOne(req, res) {
try {
// Invalid Date provided
if (!req.body.date || Date.parse(req.body.date) < new Date().getTime())
......@@ -56,25 +57,10 @@ exports.default.createOne = async (req, res) => {
.exec()
}
await activity
.populate({
path: 'comment',
select: '_id creator text createdAt',
})
.populate({
path: 'attendance',
select: '_id user state',
})
.execPopulate()
// conver to JS Object to overwrite .creator
let objActivity = activity.toObject()
objActivity.creator = objActivity._creator
res
return res
.status(200)
.json({
data: pick(objActivity, pickedKeys),
data: pick(activity, pickedKeys),
})
.end()
} catch (err) {
......@@ -91,76 +77,80 @@ exports.default.createOne = async (req, res) => {
}
}
exports.default.getOne = async (req, res) => {
module.exports.getOne = async function getOne(req, res) {
try {
const activity = await Activity.findOne({ _id: req.params.id })
.populate({
path: 'comment',
select: '_id creator text createdAt',
})
.populate({
path: 'attendance',
select: '_id user state',
})
.select('-__v')
.lean()
.exec()
if (!activity)
return res.status(404).json({ messages: ['No such activity.'] })
return res.status(200).json({ data: pick(activity, pickedKeys) })
if (req.user.role == 'mentor')
return res.status(200).json({
data: pick(activity, pickedKeys),
})
return res.status(200).json({
data: pick(activity, [
'_id',
'title',
'description',
'date',
'type',
'createdAt',
'updatedAt',
]),
})
} catch (err) {
if (err.name == 'ValidationError') {
// Throwed by Mongoose
let messages = []
for (field in err.errors) {
messages.push(err.errors[field].message)
}
return res.status(422).json({ messages })
}
console.error(err)
return res.status(500).json({ message: err.message })
}
}
exports.default.getMany = async (req, res) => {
module.exports.getMany = async function getMany(req, res) {
try {
const activity = await Activity.find()
.populate({
path: 'comment',
select: '_id creator text createdAt',
})
.populate({
path: 'attendance',
select: '_id user state',
})
.select('-__v')
.lean()
.exec()
const activity = await Activity.find().select('-__v').lean().exec()
if (!activity)
return res.status(404).json({ message: 'Activity not found!' })
res.status(200).json({
data: activity.map(function pickKeys(doc) {
return pick(doc, pickedKeys)
return pick(populatedActivity, pickedKeys)
}),
})
} catch (err) {
if (err.name == 'ValidationError') {
// Throwed by Mongoose
let messages = []
for (field in err.errors) {
messages.push(err.errors[field].message)
}
return res.status(422).json({ messages })
}
console.error(err)
res.status(500).json({ message: err.message })
}
}
exports.default.removeOne = async (req, res) => {
module.exports.removeOne = async function removeOne(req, res) {
try {
const activity = await Activity.findByIdAndDelete({
_id: req.params.id,
})
.populate({
path: 'comment',
select: '_id creator text createdAt',
})
.select('-__v')
.lean()
.exec()
if (!activity) {
if (!activity)
return res.status(404).json({ message: 'Activity not found!' })
}
await Attendance.deleteMany({
_id: activity.attendance.map(function getAttendanceIds(element) {
......@@ -169,30 +159,33 @@ exports.default.removeOne = async (req, res) => {
})
await Comment.deleteMany({
_id: activity.comment.map(function getCommentIds(element) {
_id: activity.comments.map(function getCommentIds(element) {
return element._id
}),
})
return res.status(200).json({ data: pick(activity, pickedKeys) })
} catch (err) {
if (err.name == 'ValidationError') {
// Throwed by Mongoose
let messages = []
for (field in err.errors) {
messages.push(err.errors[field].message)
}
return res.status(422).json({ messages })
}
console.error(err)
return res.status(500).json({ message: err.message })
}
}
exports.default.updateOne = async (req, res) => {
module.exports.updateOne = async function updateOne(req, res) {
try {
const activity = await Activity.findOneAndUpdate(
{ _id: req.params.id },
omit(req.body, ['attendance', 'comment']),
omit(req.body, ['attendance', 'comments']),
{ new: true }
)
.populate({
path: 'comment',
select: '_id creator text createdAt',
})
.select('-__v')
.lean()
.exec()
......@@ -201,6 +194,14 @@ exports.default.updateOne = async (req, res) => {
res.status(200).json({ data: pick(activity, pickedKeys) })
} catch (err) {
if (err.name == 'ValidationError') {
// Throwed by Mongoose
let messages = []
for (field in err.errors) {
messages.push(err.errors[field].message)
}
return res.status(422).json({ messages })
}
console.error(err)
res.status(500).json({ message: err.message })
}
......
openapi: '3.0.2'
info:
title: 'Activity Endpoint'
version: '1.0'
paths:
/activity:
get:
tags:
- 'Activity'
summary: 'Get a List of comments'
description: 'Have to be logged in for this.'
operationId: 'getAllActivity'
responses:
'200':
description: OK
content:
application/json:
schema:
type: 'array'
items:
$ref: '#/components/schemas/Activity'
post:
tags:
- 'Activity'
summary: 'Create a activity'
description: 'Only mentors can create activity.'
operationId: 'createActivity'
requestBody:
content:
application/json: