This is an API written in Nodejs (ExpressJS) and uses MONGODB as its database. In the following, we explain how to use
curl
to send HTTP request to the API. You might also use Postman and
set the commands accordingly.
To start the server we need to follow these steps:
- install dependencies with
npm install
, - add the first admin with
ADMIN_EMAIL="<FIRST_ADMIN_EMAIL>" \
ADMIN_PASSWORD="<FIRST_ADMIN_PASSWORD>" \
MONGODB_URI="<MONGODB_URI>" \
npm run create-first-admin
- feed contract options with
MONGODB_URI="<MONGODB_URI>" npm run feed-options
This will feed the database with all options exported in const/options.js
.
- run
dev
server using the database URI<MONGODB_URI>
, with
MONGODB_URI="<MONGODB_URI>" npm run dev
This command will run concurrently
- the ExpressJS server,
- the script
scripts/updateContractsStatus.js
that updates the contracts status on regular basis (currently every 1 min).
Note: Make sure to put <MONGODB_URI>
within the quotes.
{
"email": {
"type": String,
"required": true
},
"password": {
"type": String,
"required": true
},
"isAdmin": {
"type": Boolean,
"default": false
},
"createdAt": {
"type": Date,
"default": Date.now
}
}
{
"title": {
"type": String,
"required": true
},
"description": {
"type": String,
"required": true
}
}
{
"startAt": {
"type": Date,
"require": true
},
"options": {
"type": [{ "type": mongoose.Schema.Types.ObjectId, "ref": "Option" }],
"required": true
},
"clients": {
"type": [{ "type": mongoose.Schema.Types.ObjectId, "ref": "User" }],
"required": true
},
"status": {
"type": String,
"default": PENDING,
"enum": [PENDING, ACTIVE, FINISHED]
},
"finishAt": {
"type": Date
},
"createdAt": {
"type": Date,
"default": Date.now
}
}
curl -X POST \
--header "Content-Type:application/json" \
-d '{"email": "<EMAIL>", "password": "<PASSWORD>"}' \
localhost:3000/api/user/signup
curl -X GET localhost:3000/api/user/gentoken?email=<EMAIL>&password=<PASSWORD>
It returns a JSON response containing the token:
{token: ....}
Note: You might need to use these tokens in protected endpoints (should be used as <TOKEN>
or <ADMIN_TOKEN>
in the following)
curl -X POST \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <ADMIN_TOKEN>" \
-d '{"email": "<EMAIL>", "password": "<PASSWORD>"}' \
localhost:3000/api/user/addadmin
curl -X GET \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <ADMIN_TOKEN>" \
localhost:3000/api/user
curl -X GET \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <TOKEN>" \
localhost:3000/api/option
curl -X GET \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <TOKEN>" \
localhost:3000/api/option?optionId=<OPTION_ID>
curl -X POST \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <ADMIN_TOKEN>" \
-d '{"title": "<OPTION_TITLE>", "description": "<OPTION_DESC>"}' \
localhost:3000/api/option
Note: All dates should be in format 'dd/mm/yyyy' (e.g.'01/01/2021')
curl -X GET \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <TOKEN>" \
localhost:3000/api/contract
curl -X GET \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <TOKEN>" \
localhost:3000/api/contract?contractId=<CONTRACT_ID>
curl -X POST \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <ADMIN_TOKEN>" \
-d '{"startAt": "<START_AT>", "options": <LIST_OF_OPTIONS_IDS>, "clients": <LIST_OF_CLIENTS_IDS>}' \
localhost:3000/api/contract
curl -X POST \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <TOKEN>" \
-d '{"finishAt": "<FINISH_AT>", "contractId": "<CONTRACT_ID>"}' \
localhost:3000/api/contract/finish
- All passwords are encrypted, using
bcryptjs
, then registered in database.