Skip to content

An API that handles money transfers between user accounts.

License

Notifications You must be signed in to change notification settings

erick-tmr/tk-bank-api

Repository files navigation

TK-Bank-API

Contributing

To contribute with this project please refer to Contribution Guidelines.

Introduction

An API that handles money transfers between user accounts.

Features

  • Authentication enpoint that returns a JWT (JSON Web Token).
  • Authorization through JWT.
  • Transfer endpoint that manages the transfers of funds between two accounts (source and destination).
  • Account endpoint that returns a specific account according to the passed id.

Running locally

This project uses RVM to manage Ruby and Rails/Gems.

You must have PostgreSQL installed and running.

If you already have RVM installed with its wrappers and bash script it should be straight foward, just run:

cd ./ (to the repository folder)

rvm install ruby 2.6.3 (if needed)

gem install bundler -v 2.0.2

bundle install

rails db:create

rails db:migrate

rails db:seed

rails s

Specifications

This is a Restful API, so it follows it conventions for routes.

Errors

Error responses will follow this format:

{
  "errors": "[]:string"
}

Currency

The primary currency is BRL (Brazilian real).

Include options

Using query params it is possible to specify what relationship to include in the response.

Example:

curl -X GET \
  http://localhost:3000/accounts/:account_id/?include_user=true \
  -H 'Authorization: Bearer :JWT'

Authentication Endpoint

Authenticate an user using its email. Returns a JWT with a lifespan of 24hrs.

POST /auth/login

curl -X POST \
  http://localhost:3000/auth/login \
  -H 'Content-Type: application/json' \
  -d '{
    "user": {
      "email": "[email protected]"
    }
  }'

Body format:

{
  "user": {
    "email": "string"
  }
}

Response format:

{
  "token": "string:JWT"
}

Protected Endpoints

All the endpoints listed bellow are protected, so it requires the Authorization header set with a valid JWT.

Accounts Endpoint

Returns a account.

GET /accounts/:account_id

curl -X GET \
  http://localhost:3000/accounts/1 \
  -H 'Authorization: Bearer :JWT'

Response format:

{
  "account": {
    "id": "number",
    "balance_cents": "number",
    "balance": "formated_balance_value:string"
  }
}

Optional includes

  • User

Response format:

{
  "account": {
    "id": "number",
    "balance_cents": "number",
    "balance":"formated_currency_value:string",
    "user": {
      "id": "number",
      "email": "string",
      "first_name": "string",
      "last_name": "string"
    }
  }
}

Transfers Endpoint

Create a Transfer and proccess the funds in destination and source accounts. Returns the created transfer with the accounts related included.

POST /transfers

curl -X POST \
  http://localhost:3000/transfers \
  -H 'Authorization: Bearer :JWT' \
  -H 'Content-Type: application/json' \
  -d '{
    "transfer": {
      "value": "10.00",
      "destination_id": "1",
      "source_id": "2"
    }
  }'

Body format:

  • Value format: '00.00'
    • where the decimals are separated with dot ('.').
{
  "transfer": {
    "value": ":value_format:string",
    "destination_id": "number",
    "source_id": "number"
  }
}

Response format:

{
  "transfer": {
    "id": "number",
    "initial_balance_cents": "number",
    "value_cents": "number",
    "initial_balance": "formated_currency_value:string",
    "value": "formated_currency_value:string",
    "destination": {
      "id": "number",
      "balance_cents": "number",
      "balance": "formated_currency_value:string"
    },
    "source": {
      "id": "number",
      "balance_cents": "number",
      "balance": "formated_currency_value:string"
    }
  }
}

Tests

This project uses Rspec as it's testing tool.

To run the entire test suite, run:

cd ./ (to the repository folder)

rspec

You should see something like this:

CreateTransfer
  .call
    with inexistent destination_id
      does not create the transfer
      fails
      returns inexistent destination account message
    with correct arguments
      debits the source account
      creates the transfer
      sets the correct columns on transfer
      succeeds
      returns the created transfer
      returns no erros
      credits the destination account
    with inexistent destination_id and source_id
      fails
      returns inexistent message for both
      does not create the transfer
    with inexistent source_id
      fails
      returns inexistent source account message
      does not create the transfer
    with incorrect formatted value
      returns incorrect value message
      fails
      does not create the transfer

If the text from the output is green 💚 means that the test passed and a red 🔴 text means that it failed.

License

Licensed under the MIT license, see the separate LICENSE file.

About

An API that handles money transfers between user accounts.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published