Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
KSZK
DevTeam
kszkepzes
Backend
Commits
8a218b81
Commit
8a218b81
authored
Aug 02, 2020
by
rlacko
💬
Browse files
Groups documentation and use Name instead of ID
parent
30fe0922
Pipeline
#4704
failed with stages
in 1 minute and 46 seconds
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/resources/groups/__tests__/groupsFuncTest.js
View file @
8a218b81
...
...
@@ -13,7 +13,7 @@ const fakeGroupsJson = {
}
const
defaultKeys
=
{
_id
:
tru
e
,
_id
:
fals
e
,
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
'
,
})
...
...
src/resources/groups/groupsControllers.js
View file @
8a218b81
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
()
...
...
src/resources/groups/groupsDocs.yml
0 → 100644
View file @
8a218b81
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
src/resources/groups/groupsModel.js
View file @
8a218b81
...
...
@@ -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
)
...
...
src/resources/groups/groupsRouter.js
View file @
8a218b81
...
...
@@ -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
src/resources/user/userDocs.yml
View file @
8a218b81
...
...
@@ -144,3 +144,4 @@ components:
-
fullName
-
secondaryEmail
-
role
-
valid
src/resources/user/userModel.js
View file @
8a218b81
...
...
@@ -57,6 +57,7 @@ const UserSchema = new mongoose.Schema(
valid
:
{
type
:
Boolean
,
default
:
true
,
required
:
true
,
},
},
{
timestamps
:
true
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment