Commit ef02d241 authored by rlacko's avatar rlacko 💬
Browse files

get list of users by role and set unique indexes

parent 62fa3a29
......@@ -4,6 +4,7 @@ const GroupsSchema = new mongoose.Schema({
name: {
type: String,
required: true,
index: { unique: true },
},
description: {
type: String,
......@@ -15,8 +16,6 @@ const GroupsSchema = new mongoose.Schema({
},
})
GroupsSchema.index({ name: 1 })
const Groups = mongoose.model('groups', GroupsSchema)
exports.Groups = Groups
......@@ -130,6 +130,15 @@ describe('/user "Mentor" Functionality', () => {
expect(response.statusCode).toBe(422)
expect(response.body.messages).not.toBe(undefined)
})
test(`GET users by role`, async () => {
const user = await User.create(fakeUserJson)
let response = await authSession.get(`${endpointUrl}/role/normal`)
expect(response.body.data.length).toBe(1)
response = await authSession.get(`${endpointUrl}/role/accepted`)
expect(response.body.data.length).toBe(0)
response = await authSession.get(`${endpointUrl}/role/mentor`)
expect(response.body.data.length).toBe(1)
})
})
describe('/user "Accepted" Functionality', () => {
......
......@@ -2,7 +2,7 @@ const { crudControllers, getMany } = require('../../utils/crud')
const { User } = require('./userModel')
const { pick } = require('lodash')
const defaultKeys = [
const pickedKeys = [
'schacc',
'fullName',
'secondaryEmail',
......@@ -33,7 +33,7 @@ module.exports.getOne = async (req, res) => {
try {
// Get Own User
if (req.params.schacc === req.user.schacc && req.user.role !== 'mentor')
return res.status(200).json({ data: pick(req.user, defaultKeys) })
return res.status(200).json({ data: pick(req.user, pickedKeys) })
// Get Other User
if (req.user.role !== 'mentor') return res.status(403).end()
......@@ -47,7 +47,7 @@ module.exports.getOne = async (req, res) => {
return res
.status(200)
.json({ data: pick(user, [...defaultKeys, 'bit', 'presence']) })
.json({ data: pick(user, [...pickedKeys, 'bit', 'presence']) })
} catch (err) {
if (err.name === 'CastError')
// Throwed by Mongoose
......@@ -59,7 +59,18 @@ module.exports.getOne = async (req, res) => {
}
// Overwrite the returned keys
module.exports.getMany = getMany(User, [...defaultKeys, 'bit', 'presence'])
module.exports.getMany = getMany(User, [...pickedKeys, 'bit', 'presence'])
module.exports.getManyByRole = async (req, res) => {
try {
const docs = await User.find({ role: req.params.role }).lean().exec()
res.status(200).json({ data: docs.map((e) => pick(e, pickedKeys)) })
} catch (e) {
console.error(e)
res.status(400).end()
}
}
// Doesn't delete the user just disable it
module.exports.softRemove = async (req, res) => {
......@@ -81,7 +92,7 @@ module.exports.softRemove = async (req, res) => {
return res
.status(200)
.json({ data: pick(user, [...defaultKeys, 'bit', 'presence']) })
.json({ data: pick(user, [...pickedKeys, 'bit', 'presence']) })
} catch (e) {
console.error(e)
return res.status(400).end()
......@@ -112,8 +123,8 @@ module.exports.updateOne = async (req, res) => {
// Pick keys
let returnData
if (req.user.role === 'mentor')
returnData = pick(updatedUser, [...defaultKeys, 'bit', 'presence'])
else returnData = pick(updatedUser, defaultKeys)
returnData = pick(updatedUser, [...pickedKeys, 'bit', 'presence'])
else returnData = pick(updatedUser, pickedKeys)
return res.status(200).json({
data: returnData,
......
......@@ -52,6 +52,22 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/User'
/user/role/{role}:
get:
tags:
- 'User'
summary: 'Get a List of users by role'
description: 'This can only be done by a mentor.'
operationId: 'getAllUserByRole'
responses:
'200':
description: OK
content:
application/json:
schema:
type: 'array'
items:
$ref: '#/components/schemas/User'
/user/schacc/{schacc}:
get:
tags:
......
......@@ -10,7 +10,7 @@ const UserSchema = new mongoose.Schema(
schacc: {
type: String,
required: true,
unique: true,
index: { unique: true },
},
fullName: {
type: String,
......@@ -63,8 +63,6 @@ const UserSchema = new mongoose.Schema(
{ timestamps: true }
)
UserSchema.index({ schacc: 1 })
const User = mongoose.model('user', UserSchema)
exports.User = User
......@@ -7,6 +7,9 @@ const router = Router()
// /api/v1/user
router.route('/').get(isLoggedIn, isMentor, controllers.getMany)
// /api/v1/user/role/:role
router.route('/role/:role').get(isLoggedIn, isMentor, controllers.getManyByRole)
// /api/v1/user/schacc/:schacc
router
.route('/schacc/:schacc')
......
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