MIT React code style: prettier

SaverLife

This repo contains a Node.js backend which is part of a financial tracker app built for the non-profit organization SaverLife. The app is also comprised of a React/Redux frontend application (repo), a Data Science backend (repo). DS API deployed using AWS Elastic Beanstalk here.

Contributors

Aurelio Arcabascio Manuel De La Mora Juan Madero Flores Evan Aspaas

Endpoints

Most of the endpoints are used to enable our frontend application's test users to reach the data science API by retreiving a some information that the frontend doesn't have access to, but which the DS API is expecting. Specifically, this backend's database retrieves a bank account number that has been paired up with each test user then passes along the API request to the DS API, and returns the response returned by the DS API to the frontend to display.

All routes to this backend have the base url: https://saverlife-a-api.herokuapp.com/ and has the DS API's base url as an environment variable: http://saverlife-a.eba-atdfhqrp.us-east-1.elasticbeanstalk.com/

GET /data/dashboard/:user_id

Returns basic bank account information such as transactions and account type, which is displayed on the user's dashboard page on the frontend side. The url must include a user id which is the email that the user logs into the frontend site with (using Okta) as a parameter.

POST /data/current_month_spending/:user_id

Returns the stringlified JSON for a Plotly library chart that displays the user's current month's spending broken down by categories. The url must include a user id which is the email that the user logs into the frontend site with (using Okta) as a parameter. The incoming request's body must include an array of the existing categories which are provided by the future_budget endpoint shown below and therefore should be called after calling the future_budget endpoint:

{ "categories": ["Food", "Shopping", "Transportation", "Utilities", "Misc."] }

POST /data/future_budget

Returns the user's recommended level of spending for each spending category which applies to them. The incoming request's body must include a user id which is the email that the user logs into the frontend site with (using Okta) and the user's monthly savings goal. Both this and the current_monthly_spending endpoint described above retrieve the information necessary to display the budget tracker on the My Budget page of the frontend application.

{ "user_id": "llama003@maildrop.cc", "monthly_savings_goal": 100 }

POST /data/spending

Returns the stringlified JSON for a Plotly library chart that displays the user's previous spending broken down by categories for the last week, month, or year. The incoming request's body must include a user id which is the email that the user logs into the frontend site with (using Okta), the graph type which can be 'bar' or 'pie' (both can be found displayed on the Past Spending page of the frontend application, and a time period which can be week, month or year.

{ "user_ID": "llama003@maildrop.cc", "graph_type": "pie", "time_period": "month" }

POST /data/moneyflow

Returns the stringlified JSON for a Plotly library chart that displays the user's daily net income. The incoming request's body must include a user id which is the email that the user logs into the frontend site with (using Okta) and a time period which can be week, month or year.

{ "user_ID": "llama002@maildrop.cc", "time_period": "week" }

Basic node API

Maintainability Test Coverage

The following examples can be found in this project template.

  • CRUD routes for a single resource
  • A Knex model providing CRUD methods for DB operations
  • Some route tests with mocks on the database calls
  • Okta authentication verification middleware
  • eslint setup and prettier formating.
  • Jest coverage setup
  • Inline Swagger docs with a live route at /api-docs
  • Github workflow config setup to run linting, tests and upload coverage to code climate
  • docker-compose file for spinning up postgresql db. (Win10 Home requires WSL)

Requirements

All Labs Engineering Standards must be followed.

API doc

All routes can be viewed in the [/api-docs route](https:// /api-docs/) of your deploy (or locally).

Swagger docs are created using open api v3 notations. The docs are found inline on the router files in api/**/*Router.js and use the yaml notation format. The root of the docs is in config/jsdoc.js using the json format.

The following libraries have been used to build and serve the swagger docs live.

Getting Started

Enviornment Variables

  • PORT - API port (optional, but helpful with FE running as well)
  • DS_API_URL - URL to a data science api. (eg. https://ds-bw-test.herokuapp.com/)
  • DS_API_TOKEN - authorization header token for data science api (eg. SUPERSECRET)
  • DATABASE_URL - connection string for postgres database
  • OKTA_URL_ISSUER - The complete issuer URL for verifying okta access tokens. https://example.okta.com/oauth2/default
  • OKTA_CLIENT_ID - the okta client ID.

See .env.sample for example values

Setup postgres

There are 3 options to get postgresql installed locally [Choose one]:

  1. Use docker. Install for your platform
    • run: docker-compose up -d to start up the postgresql database and pgadmin.
    • Open a browser to pgadmin and you should see the Dev server already defined.
    • If you need to start over you will need to delete the folder $ rm -rf ./data/pg as this is where all of the server data is stored.
      • if the database api-dev was not created then start over.
  2. Download and install postgresql directly from the main site
    • make note of the port, username and password you use to setup the database.
    • Connect your client to the server manually using the values previously mentioned
    • You will need to create a database manually using a client.
    • Make sure to update the DATABASE_URL connection string with the values for username/password, databasename and server port (if not 5432).
  3. Setup a free account at ElephantSQL
    • Sign up for a free Tiney Turtle plan
    • copy the URL to the DATABASE_URL .env variable
    • make sure to add ?ssl=true to the end of this url

Setup the application

  • For Labs projects, clone the repo. Otherwise you can create a new repo using this as a template.

    • note please be sure to set your remote for this repo to point to your Labs Team Repository.

    • Alternatively you can clone this repo then remove the git folder to initialize a new repo

      > git clone --depth=1 --branch=main git@github.com:Lambda-School-Labs/labs-api-starter.git NEW-REPO-NAME
      > rm -rf ./NEW-REPO-NAME/.git
  • run: npm install to download all dependencies.

  • run: cp .env.sample .env and update the enviornment variables to match your local setup.

  • run: npm run knex migrate:latest to create the starting schema.

  • run: npm run knex seed:run to populate your db with some data.

  • run: npm run tests to confirm all is setup and tests pass.

  • run: npm run watch:dev to start nodemon in local dev enviornment.

Make sure to update the details of the app name, description and version in the package.json and config/jsdoc.js files.

Contributing

ESLint and prettier

ESLint and prettier are already configured with Lambda Labs standards and ready to go. These must be ran from the CLI prior to commiting code in the following ways:

  • npm run lint to view all purposed fixes.
  • npm run lint:fix to apply fixes to eslint issues.
  • npm run format to apply the standards defined by eslint/prettier config.

Alternatively you can install plugins for your editor of choice.