This project was built only for testing purposes, we have some customers who they can register and then login to our application, each customer have a credit number.
we can add/update subscriptions, and customers can buy them. after buying until subscription expire(whitch is 1 month by default ) or customer deactive subscription, each 10 min we create new invoice for customer base on subscription price and decrease that price from it's credit.
customers can see all of their subscriptions and invoices, and we log customers actions in database as well.
we use Js(node.js) and Express.js for this project, PostgreSql as database, and Knex as query builder
- first of all make sure you have node.js and postgreSql installed on your system
node --version
//v16.18.1
which psql
// if Postgres is installed you'll get a response with the path to the location of the Postgres installed
-
use command
npm i
to install dependencies -
go to
src/config
and create a file calleddev.env
and add all of required environment variables. sample file is insrc/config/sample.env
-
use one of below scripts to run project :)
npm run start:prod
npm run start:dev
for calling Apis you can use swagger ui http://localhost:PORT/api-docs
for api documentation or sample curl requests from next section.
here are some sample requests to know exactly how to call Apis
curl --location --request POST 'localhost:3000/customer/register' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "fateme",
"password": "123",
"credit": 20
}'
returned token will expire in 1 hour
curl --location --request POST 'localhost:3000/customer/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "fateme",
"password": "123"
}'
curl --location --request POST 'localhost:3000/subscription/add' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "vm1",
"price":"2"
}'
curl --location --request PATCH 'localhost:3000/subscription/1' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "new vm2",
"price": 6
}'
all request queries(limit, offset) are optional
curl --location --request GET 'localhost:3000/subscription/?limit=4&offset=0' \
curl --location --request GET 'localhost:3000/subscription/buy/1' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXV...'
curl --location --request PATCH 'localhost:3000/subscription/1/deactive' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVC...'
all request queries(limit, offset, isActive) are optional
curl --location --request GET 'localhost:3000/subscription/customer?limit=4&offset=0&isActive=1' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXV...'
all request queries(limit, offset) are optional
curl --location --request GET 'localhost:3000/invoice?limit=2&offset=3' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpX...'
all request queries(limit, offset) are optional
curl --location --request GET 'localhost:3000/invoice/10?limit=10&offset=0' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVC...'