Skip to content

Commit

Permalink
Merge pull request #373 from SchoodEIP/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Exiels authored Dec 4, 2024
2 parents 03c5073 + 5763fdd commit 9c0d814
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/models/desanonym.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const desanonymsSchema = new Schema({
type: mongoose.Types.ObjectId,
ref: 'users'
},
mood: {
type: mongoose.Types.ObjectId,
ref: 'moods'
},
reason: {
type: String,
required: false
Expand Down Expand Up @@ -59,6 +63,7 @@ const Desanonyms = mongoose.model('desanonyms', desanonymsSchema)
const validateDesanonyms = (alert) => {
const schema = Joi.object({
user: Joi.string().required(),
mood: Joi.string().required(),
reason: Joi.string().optional(),
message: Joi.string().optional()
})
Expand Down
2 changes: 1 addition & 1 deletion src/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const validateUser = (user) => {
const schema = Joi.object({
email: Joi.string().required(),
password: Joi.string().required(),
rememberMe: Joi.boolean().required(),
rememberMe: Joi.boolean().required()
})
return schema.validate(user)
}
Expand Down
4 changes: 2 additions & 2 deletions src/routes/adm/classes/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ module.exports = async (req, res) => {
return res.status(400).json({ message: 'Invalid request' })
}

const classToUpdate = await Classes.findOne({ _id: id })
const classToUpdate = await Classes.findById(id)
if (!classToUpdate) return res.status(422).json({ message: 'Class not found' })
const tmp = await Classes.findOne({ name: req.body.name })
const tmp = await Classes.findOne({ name: req.body.name, facility: req.user.facility })
if (tmp) return res.status(422).json({ message: 'This name is already used' })

classToUpdate.name = req.body.name
Expand Down
2 changes: 1 addition & 1 deletion src/routes/shared/desanonym/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = async (req, res) => {
if (req.user.role.levelOfAccess === 0) {
query.user = req.user._id
}
const desanonyms = await Desanonyms.find(query).populate('createdBy').populate('user')
const desanonyms = await Desanonyms.find(query).populate('createdBy').populate('user').populate('mood')

desanonyms.forEach(desanonym => {
if (desanonym.user) {
Expand Down
11 changes: 10 additions & 1 deletion src/routes/shared/desanonym/modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const { default: mongoose } = require('mongoose')
const { Desanonyms, validateModify } = require('../../../models/desanonym')
const { Moods } = require('../../../models/moods')

/**
* Main desanonyms function
Expand All @@ -30,7 +31,7 @@ module.exports = async (req, res) => {
}

if (req.user.role.levelOfAccess === 0) {
if (req.body.reason || res.body.message) {
if (req.body.reason || req.body.message) {
return res.status(400).json({ message: 'Invalid request' })
}
}
Expand All @@ -53,6 +54,14 @@ module.exports = async (req, res) => {
desanonym.message = req.body.message ? req.body.message : desanonym.message
desanonym.status = req.body.status ? req.body.status : desanonym.status

if (req.body.status && req.body.status === 'accepted') {
const mood = await Moods.findById(desanonym.mood)

mood.annonymous = false;

await mood.save();
}

await desanonym.save()

return res.status(200).send()
Expand Down
7 changes: 7 additions & 0 deletions src/routes/shared/desanonym/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { default: mongoose } = require('mongoose')
const { validateDesanonyms, Desanonyms } = require('../../../models/desanonym')
const { Users } = require('../../../models/users')
const { createNotification } = require('../../../services/notification')
const { Moods } = require('../../../models/moods')

/**
* Main desanonyms function
Expand Down Expand Up @@ -36,8 +37,14 @@ module.exports = async (req, res) => {
return res.status(400).json({ message: 'Invalid request' })
}

const mood = await Moods.findById(req.body.mood)
if (!mood) {
return res.status(400).json({ message: 'Invalid request' })
}

const desanonym = new Desanonyms({
user: req.body.user,
mood: req.body.mood,
createdBy: new mongoose.Types.ObjectId(req.user._id),
message: req.body.message ? req.body.message : '',
reason: req.body.reason ? req.body.reason : '',
Expand Down
5 changes: 4 additions & 1 deletion src/routes/shared/reports/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ module.exports = async (req, res) => {
populate: [
{
path: 'participants.user'
},
{
path: 'messages'
}
]
],
})

return res.status(200).json(reports)
Expand Down
2 changes: 1 addition & 1 deletion src/routes/user/chat/newMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @inner
* @namespace newMessage
*/
const { default: mongoose } = require('mongoose')

const { Chats } = require('../../../models/chat')
const { Messages, validateMessages } = require('../../../models/message')
const Logger = require('../../../services/logger')
Expand Down
85 changes: 85 additions & 0 deletions src/routes/user/getDisabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* @memberof module:router~mainRouter~admRouter
* @inner
* @namespace get_disabled
*/
const { Roles } = require('../../models/roles')
const { Users } = require('../../models/users')

const Logger = require('../../services/logger')

/**
* Main get_disabled function
* @name GET /adm/get_disabled
* @function
* @memberof module:router~mainRouter~admRouter~get_disabled
* @inner
* @async
* @param {Object} req
* @param {Object} res
* @returns 400 if invalid requests
* @returns 200 if OK
* @returns 500 if Internal Server Error
*/
module.exports = async (req, res) => {
try {
const isAdmin = req.user.role.levelOfAccess === 3
const roles = (
isAdmin
? await Roles.find({ levelOfAccess: 2 })
: await Roles.find({ name: { $in: ['student', 'teacher'] } })
).map(o => o._id)
if (!roles || roles.length === 0) return res.status(400).json({ message: 'Internal server error: No roles found.' })

const users = await Users.aggregate([
{
$match: {
facility: req.user.facility,
active: false,
role: { $in: roles }
}
},
{
$lookup: {
from: 'roles',
localField: 'role',
foreignField: '_id',
as: 'role'
}
},
{
$unwind: {
path: '$role',
preserveNullAndEmptyArrays: false
}
},

{
$lookup: {
from: 'facilities',
localField: 'facility',
foreignField: '_id',
as: 'facility'
}
},
{
$unwind: {
path: '$facility',
preserveNullAndEmptyArrays: false
}
},
{
$project: {
password: 0,
firstConnexion: 0,
__v: 0
}
}
])

return res.status(200).send(users)
} catch (error) /* istanbul ignore next */ {
Logger.error(error)
return res.status(500).json({ message: 'Internal Server Error' })
}
}
2 changes: 2 additions & 0 deletions src/routes/user/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const tokenCheck = require('./tokenCheck')
const downloadApk = require('./downloadApk')
const { upload10Tmp } = require('../../utils/multer')
const getMoodById = require('./getMoodById')
const getDisabled = require('./getDisabled')

/**
* User router connection
Expand All @@ -50,6 +51,7 @@ router.get('/tokenCheck', auth, tokenCheck)
router.get('/downloadApk', downloadApk)
router.patch('/:id', auth, access(1), updateUser)
router.get('/mood', auth, getMoodById)
router.get('/getDisabled', auth, access(2, false), getDisabled)

// helpNumbers
router.get('/helpNumbers', auth, getAllHelpNumbers)
Expand Down

1 comment on commit 9c0d814

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report for src/

Action wasn't able to generate report within GitHub comment limit. If you're facing this issue, please let me know by commenting under this issue.

Report generated by 🧪jest coverage report action from 9c0d814

Please sign in to comment.