Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#166075487 Connect loan model to postgres database #34

Merged
merged 14 commits into from
May 19, 2019

Conversation

chidimo
Copy link
Owner

@chidimo chidimo commented May 19, 2019

What does this PR do?

Connect loan model to postgres database

Description of Task to be completed?

  1. Make use of methods defined in /models/Model.js to send SQL queries using pg.
  2. Use async/await to gather the results of such queries and return it.
  3. Create helper functions in /controllers/helpers/LoansController to handle repetitive tasks.
  4. Use psql with node's exec to populate and clear out the test database tables

How should this be manually tested?

  1. Clone the repo
  2. cd into the root folder on a cmd shell and run yarn install. PowerShell is fine also.
  3. Setup a PostgreSQL DB.
  4. Define a .env file in the root of the project and supply values for the following parameters
    JWT_SECRET=<json-web-token-secret>
    PGPASSWORD=<postgres-db-password>
    PGHOST=<postgres-db-host>
    PGUSER=<postgres-db-user>
    PGPORT=<postgres-db-port>
  5. Create a PostgreSQL database called testdb.
  6. Create the database tables listed below.
  7. Now run the tests with yarn test
CREATE TABLE users (
	id SERIAL PRIMARY KEY,
	email VARCHAR NOT NULL UNIQUE,
    password VARCHAR NOT NULL,
    firstname VARCHAR DEFAULT '',
    lastname VARCHAR DEFAULT '',
    phone VARCHAR DEFAULT '',
    address jsonb DEFAULT '{"home": "", "office": ""}',
    status VARCHAR DEFAULT 'unverified',
    isAdmin BOOLEAN DEFAULT false
);

CREATE TABLE IF NOT EXISTS loans (
	id SERIAL PRIMARY KEY,
	userid INT NOT NULL,
    createdon TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    status VARCHAR DEFAULT 'pending',
    repaid BOOLEAN DEFAULT false,
    amount FLOAT NOT NULL,
    tenor INT NOT NULL,
    interest FLOAT NOT NULL,
    balance FLOAT NOT NULL,
    paymentinstallment FLOAT NOT NULL
);

CREATE TABLE IF NOT EXISTS repayments (
    id SERIAL PRIMARY KEY,
    loanid INT NOT NULL,
    adminid INT NOT NULL,
    createdon TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    amount FLOAT NOT NULL
)
  1. Now run command yarn test

Any background context you want to provide?

Be sure to setup the database as instructed above

What are the relevant pivotal tracker stories?

166075487

Screenshots (if appropriate)

Nil

Questions:

Nil

chidimo added 14 commits May 18, 2019 19:03
On pages with the profile icon, the hidden submenu creates an extra
space to the right of the screen. This commit corrects this
by changing the submenu width for smaller devices

[Finishes #166104469]
- pass the where statement from the query caller.
This ensures that the where statement can be constructed in any manner
we wish for cases where there may be AND
- This pattern also allows passing LIMIT if we like to
- Create method for insert with return id
- Create method for in-situ incrementation of a value
- Create helper function to hold repetitive tasks
- check if loan exists
- add loan to database and return the id
- update loan status
- update loan balance
- get loan by given id
- get loan repayment history
- add repayment to database and return the id
- get repayment by id

Add adminid field to repayment to capture the admin to collect the loan

Refactor controller to perform read and write operations on backend db
- Create helper functions for modularity
- function check user existence
- function to add user to db
- function to get user by id or email
- use psql to load data before test
- use psql to clear database after test
- each test file loads and clears data each time it runs
@chidimo chidimo merged commit 35ad089 into develop May 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant