This README covers setting up of the application and how to test it out.
Things you may want to cover:
-
Ruby version - 2.5.1
-
Rails version - 6+
The application has two users setup. (a) Customer, (b) Admin.
- Customers can create loans
- Admin approves the loans
- Customer can repay the installments for approved loans
- Once the collected amount equals the disbursed amount, or all the installments are marked paid, the loan is marked fully paid
- The application does not cover the scope of creation of customers or admins, and hence, dummy accounts are already created for testing purposes.
- If the customer pays extra in any installment, then the excess balance is adjusted equally among all the next upcoming installments.
- Loan repayment will always be done for immediate next pending installment, and can not be skipped under any case.
- Loan repayments are always done on the pre-populated payment date in database.
- Download the repo from https://github.com/ajalan065/aspire.git
- Install the gems -
bundle install
- Set up database -
rake db:setup && rake db:migrate && rake db:seed
- Application is set to use.
Models:
- Loan a. Validations check --
- LoanRepayment a. Validations check --
- User a. Assign default role to user --
i. Invalid if term / amount / start date is missing
ii. Creates loans if all the above are present
b. Repayment schedule creation --
i. Creates multiple installments for valid loans
c. Reset installment amount in case of excess payments --
i. Adjusted in all remaining pending installments
i. Invalid if amount / payment date is missing
ii. Create repayments if all the above are present
b. Installments marked paid --
i. Immediate next pending installment is marked paid.
- If amount is equal, then just update status
- If amount is less, then raise error and return without doing anything
- If amount is greater, then adjust the excess balance among remaining pending installments.
i. On user creation, give default role as `customer`.
Controllers:
- LoansController a. #index action --
- LoanRepaymentsController a. #repay action --
- For models, run in terminal
rspec spec/models
- For controllers, run in terminal
rspec spec/controllers
- The application is seeded up with two users:
a. Customer - Email:
[email protected]
-- Password:password
b. Admin - Email:[email protected]
-- Password:password
- The application uses devise for authentication.
- Authenticating user: a. Endpoint: `POST http://localhost:3000/users/sign_in`
- To create loan: a. Endpoint: `POST http://localhost:3000/users/{customer_id}/loans`
- To approve loan: a. Logout from existing session using the below (in case logged in as customer). Endpoint `DELETE http://localhost:3000/users/sign_out`
- To repay the installment: a. Log in as customer using first step of Approve Loan (in case logged in as admin). Endpoint: `POST http://localhost:3000/users/{customer_id}/loans/{id}/repay`
i. When user is not authenticated, return 302 (should be redirecting to signin page from UI perspective)
ii. When user is authenticated
- Should show loans of the user
b. #create action --
i. When user is not authenticated, return 302
ii. When user is authenticated
- If invalid params, then do not create object and return 422
- If valid params, then create loan in `pending` state
c. #approve action --
i. When user is not authenticated, return 302
ii. When user is authenticated
- When user is admin, should be able to approve loans
- When user is not admin, should not be able to approve loans
d. #show action --
i. When user is not authenticated, return 302
ii. When user is authenticated
- When user is admin, can access any loan
- When user is customer, should be able to access only his own loans
i. When user is not authenticated, return 302
ii. When user is authenticated
- Raise Loan Not Found error if no loan found for passed id.
- Raise invalid loan error if loan is not approved or already paid
- When loan is valid
-> If amount is equal, mark the installment paid
-> If amount is less, do not update the status and return with error message
-> If amount is greater, mark the installment paid and adjust the excess balance in next all pending installments equally.
Run rails s
to start the rails server at port 3000
Note: Open Rails console using rails c
and type the below code snippets:
customer_id = User.find_by(email: '[email protected]').id
admin_id = User.find_by(email: '[email protected]').id
The above two ids will be handy in calling APIs from postman.
b. To login as customer, use the below credentials in body param:
{ "user": { "email": "[email protected]", "password": "password" } }
c. To login as admin, user the below credentials:
{ "user": { "email": "[email protected]", "password": "password" } }
b. Body:
{ "loan": { "disbursed_amount": 5000, "term": 2, "start_date": "2024-04-29" } }
c. This will return an object in JSON format. Please note the loan id under the attribute `id`. This will be used in endpoints to approve / repay installments.
b. Login using admin as shown in Step 1(c).
c. For approving loan - Endpoint: `POST http://localhost:3000/users/{customer_id}/loans/{id}/approve`
b. Body:
{ "loan": { "amount": 1000 } }
c. Marks the installment as paid.