Commit 8a218b81 authored by rlacko's avatar rlacko 💬
Browse files

Groups documentation and use Name instead of ID

parent 30fe0922
Pipeline #4704 failed with stages
in 1 minute and 46 seconds
......@@ -13,7 +13,7 @@ const fakeGroupsJson = {
}
const defaultKeys = {
_id: true,
_id: false,
name: true,
description: true,
groupPath: true,
......@@ -33,7 +33,7 @@ describe('/group "Mentor" Functionality', () => {
// GET One
test(`GET one returns with allowed keys`, async () => {
const newGroup = await Groups.create(fakeGroupsJson)
let response = await authSession.get(`${endpointUrl}/${newGroup._id}`)
let response = await authSession.get(`${endpointUrl}/${newGroup.name}`)
expect(response.statusCode).toBe(200)
validateKeys(response.body.data, defaultKeys)
})
......@@ -56,7 +56,7 @@ describe('/group "Mentor" Functionality', () => {
// Delete
test(`Delete returns with allowed keys`, async () => {
const newGroup = await Groups.create(fakeGroupsJson)
let response = await authSession.delete(`${endpointUrl}/${newGroup._id}`)
let response = await authSession.delete(`${endpointUrl}/${newGroup.name}`)
expect(response.statusCode).toBe(200)
validateKeys(response.body.data, defaultKeys)
})
......@@ -64,7 +64,7 @@ describe('/group "Mentor" Functionality', () => {
test(`Update returns with allowed keys`, async () => {
const newGroup = await Groups.create(fakeGroupsJson)
let response = await authSession
.put(`${endpointUrl}/${newGroup._id}`)
.put(`${endpointUrl}/${newGroup.name}`)
.send({
name: 'almafa',
})
......
const { crudControllers } = require('../../utils/crud')
const { crudControllers, createOne } = require('../../utils/crud')
const { Groups } = require('./groupsModel')
const { Application } = require('../application/applicationModel')
const { pick } = require('lodash')
exports.default = crudControllers(Groups, [
'_id',
'name',
'description',
'groupPath',
])
const pickedKeys = ['name', 'description', 'groupPath']
exports.default.removeOne = async (req, res) => {
module.exports = crudControllers(Groups, pickedKeys)
module.exports.getOne = async (req, res) => {
try {
const group = await Groups.findOne({ name: req.params.groupName })
.lean()
.exec()
if (!group) {
return res.status(404).end()
}
res.status(200).json({ data: pick(group, pickedKeys) })
} catch (err) {
if (err.name == 'CastError') {
return res.status(422).json('Invalid ID provided')
} else {
console.error(err)
res.status(400).end()
}
}
}
module.exports.updateOne = async (req, res) => {
try {
const updatedGroup = await Groups.findOneAndUpdate(
{
name: req.params.groupName,
},
req.body,
{ new: true, runValidators: true }
)
.lean()
.exec()
if (!updatedGroup) {
return res.status(404).end()
}
res.status(200).json({ data: pick(updatedGroup, pickedKeys) })
} catch (err) {
if (err.name == 'ValidationError') {
let messages = []
for (field in err.errors) {
messages.push(err.errors[field].message)
}
res.status(422).json({ messages })
} else {
console.error(err)
res.status(400).end()
}
}
}
module.exports.removeOne = async (req, res) => {
try {
const removed = await Groups.findOneAndRemove({
_id: req.params.id,
name: req.params.groupName,
})
if (!removed) {
......@@ -22,11 +70,9 @@ exports.default.removeOne = async (req, res) => {
Application.updateMany({}, { $pull: { groups: { $in: removed._id } } })
return res
.status(200)
.json({
data: pick(removed, ['_id', 'name', 'description', 'groupPath']),
})
return res.status(200).json({
data: pick(removed, pickedKeys),
})
} catch (e) {
console.error(e)
return res.status(400).end()
......
openapi: '3.0.2'
info:
title: 'User Endpoint'
version: '1.0'
paths:
/groups:
get:
tags:
- 'Groups'
summary: 'Get a List of groups'
description: 'Avaliable to anyone.'
operationId: 'getAllGroups'
responses:
'200':
description: OK
content:
application/json:
schema:
type: 'array'
items:
$ref: '#/components/schemas/Groups'
post:
tags:
- 'Groups'
summary: 'Create a group'
description: 'This can only be done by a mentor.'
operationId: 'createGroup'
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Groups'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Groups'
/groups/{id}:
get:
tags:
- 'Groups'
summary: 'Get a group by ID'
description: 'Avaliable to anyone.'
operationId: 'getOneGroup'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Groups'
put:
tags:
- 'Groups'
summary: 'Update a Group by ID'
description: 'This can only be done by a mentor.'
operationId: 'updateOneGroup'
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Groups'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Groups'
delete:
tags:
- 'Groups'
summary: 'Delete Group by ID'
description: 'This can only be done by a mentor.'
operationId: 'deleteOneGroup'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Groups'
components:
schemas:
Groups:
type: object
properties:
name:
type: string
description:
type: string
groupPath:
type: string
required:
- name
- description
- groupPath
......@@ -15,8 +15,7 @@ const GroupsSchema = new mongoose.Schema({
},
})
// Careful with the docs, there are some deprecated ones
// https://mongoosejs.com/docs/guide.html
GroupsSchema.index({ name: 1 })
const Groups = mongoose.model('groups', GroupsSchema)
......
......@@ -5,17 +5,17 @@ const { isLoggedIn, isMentor } = require('../../middlewares/auth')
const router = Router()
// /api/groups
// /api/v1/groups
router
.route('/')
.get(controllers.default.getMany)
.post(isLoggedIn, isMentor, controllers.default.createOne)
.get(controllers.getMany)
.post(isLoggedIn, isMentor, controllers.createOne)
// /api/groups/:id
// /api/v1/groups/:groupName
router
.route('/:id')
.get(controllers.default.getOne)
.put(isLoggedIn, isMentor, controllers.default.updateOne)
.delete(isLoggedIn, isMentor, controllers.default.removeOne)
.route('/:groupName')
.get(controllers.getOne)
.put(isLoggedIn, isMentor, controllers.updateOne)
.delete(isLoggedIn, isMentor, controllers.removeOne)
exports.default = router
......@@ -144,3 +144,4 @@ components:
- fullName
- secondaryEmail
- role
- valid
......@@ -57,6 +57,7 @@ const UserSchema = new mongoose.Schema(
valid: {
type: Boolean,
default: true,
required: true,
},
},
{ timestamps: true }
......
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