In this Challenge we create an Users Management API.
The user needs to be an admin to make the list users works.
The route must receive name
and email
inside the request body to be able to create an user.
The route must receive, in the request params, the user id
and exchange this user to admin.
The route must receive, in the request params, the user id
and return the user's info found by the response body.
The route must receive, in the request header, a properties user_id
with the user id
and return a list with all users created.
The id
must be used to validate if the user who is requesting the list is an admin. The list response only must be done if the user is an admin.
For this test, you must create a model with the properties below:
{
"id": "string",
"name": "string",
"admin": "boolean",
"email": "string",
"created_at": "Date",
"updated_at": "Date",
}
The admin
property should be started with false
and the id
should be automatically generated as uuid
.
For this test, the create
method of UsersRepository file needs to receive the user's name and email to create an user from the model.
For this test, the list
method of UsersRepository file return a list of all created users.
For this test, the findById
method of UsersRepository file must recieve the user id
and return an user who has the same id
.
For this test, the findByEmail
method of UsersRepository file must recieve the user email
and return the user who has the same email
.
For this test, the turnAdmin
method of UsersRepository must recieve the whole user object, change the admin
property to true
, update the updated_at
propety and return the updated user.
For this test, the execute
method of CreateUserUseCase file must recieve the user name
and email
to be created, create an user through the repository's create method and return the created user.
For this test, the execute
method of CreateUserUseCase file must not allow an user to be created if exist an user with the same email
.
If exist an user with the same email retun the error message:
throw new Error("Mensagem do erro");
For this test, the execute
method of TurnUserAdminUseCase file must recieve the user id
, call the repository method wich change the user to admin
and return the user after the change.
For this test, the execute
method of TurnUserAdminUseCase file must not allow an user
who does not exist to be changed in admin
.
If the user does not exist return the error message:
throw new Error("Mensagem do erro");
For this test, the execute
method of ShowUserProfileUseCase file must recieve the user id
, call the repository's method wich search an user by id
and return the user found.
For this test, the execute
method of ShowUserProfileUseCase file must not allow a non-existing user to be return.
If the user does not exist return the error message:
throw new Error("Mensagem do erro");
For this test, the execute
method of ListAllUsersUseCase file must recieve the user id
, call the repository's method which return all created users and return this info.
For this test, the execute
method of ListAllUsersUseCase must not allow an user who is not a admin
acess the created users.
If the user is not an admin
, return a message error:
throw new Error("Mensagem do erro");
For this test, the execute
method of ListAllUsersUse Case file must not allow an user does not exist, acess the created users list.
If the user does not exist, return a message error:
throw new Error("Mensagem do erro");
For this test, using the proper useCase, the route must create an user
and return a status 201
with the created user object.
For this test, if an error has occurred in useCase, return a response with status 400
and a json with an object { error: "error message" }
, where the value of the error
property should be the message thrown by the error in useCase.
To catch errors thrown by other files, you can wrap the contents of the controller in a try/catch
.
For this test, using the proper useCase, the route must change an user to admin
and return the user changed int the response body.
For this test, if an error has occurred in useCase, return a response with status 404
and a json with an object { error: "error message" }
, where the value of the error
property should be the message thrown by the error in useCase.
For this test, using the proper useCase, the route must recieve the user id
by route params and return the founded user object in the response body.
For this test, if an error has occurred in useCase, return a response with status 404
and a json with an object { error: "error message" }
, where the value of the error
property should be the message thrown by the error in useCase.
For this test, using the proper useCase, the route must recieve the admin
user id
from user_id
request header and return a user list created.
📌 Should not be able to a non admin user get list of all users and Should not be able to a non existing user get list of all users
For these tests, if an error has occurred in useCase, return a response with status 400
and a json with an object { error: "error message" }
, where the value of the error
property should be the message thrown by the error in useCase.
Implementation of extra features.
For this test, the execute
method of CreateUserUseCase file must not allow an user to be created if the field email
or name
is empty.
If exist an user with the same email retun the error message:
throw new Error("Mensagem do erro");