Credit Card Reward Point System - JavaScript Full Stack (MEAN) Application with Unit Testing & End to End Automation Testing + Application logging with Log data analysis, monitoring and visualization using ELK Stack
Tech Stack : MongoDB, Express.js, Angular & Node.js - MEAN Stack + Elasticsearch, Logstash & Kibana - ELK Stack
- Go to backend folder in Command Prompt (CMD)
- Run command "npm i" to install all dependencies
- Run command "nodemon app.js" to start the backend application server
- Backend server will start running on Port 3000 => http://localhost:3000/
- Go to frontend folder in Command Prompt (CMD)
- Run command "npm i" to install all dependencies
- Run command "ng serve --open" to start the frontend application
- Browser will open and frontend application will start running on Port 4200 => http://localhost:4200/
- To check code coverage of unit testing => Run command "npm run coverage"
- To open code coverage report => Go to path "/backend/coverage/lcov-report" & open "index.html" file
- To check code coverage of unit testing => Run command "ng test --code-coverage"
- To open code coverage report => Go to path "frontend/coverage/frontend" & open "index.html" file
Go to "frontend" folder and run command "ng e2e" => Cypress Window will open which will have different testcase - click on it to run
Download Link : https://www.elastic.co/downloads/elasticsearch
Note: Refer "ELK Stack\elasticsearch.yml" file given in this Repo & follow steps mentioned below
- Go to config folder => open elasticsearch.yml file
Add "action.auto_create_index: .monitoring*,.watches,.triggered_watches,.watcher-history*,.ml*" this line to allow automatic index creation
Save and close the file - Go to bin folder => run command "elasticsearch.bat" it will generate password and enrollment token for kibana (copy and save it for future use)
- Go to config folder => open elasticsearch.yml file
Change enabled to false in xpack.security.http.ssl & xpack.security.transport.ssl
Save and close the file - Go to bin folder => run command "elasticsearch.bat"
- Check "http://localhost:9200/" it will ask login credentials => username = elastic & password = elastic password generated which you copied
To reset and get password again => Go to bin folder => run command "elasticsearch-reset-password -u elastic" - Go to bin folder => run command "elasticsearch-reset-password -u kibana_system" => copy and save the kibana password
Download Link : https://www.elastic.co/downloads/kibana
Note: Refer "ELK Stack\kibana.yml" file given in this Repo & follow steps mentioned below
- Go to config folder => open kibana.yml file
Uncomment server.port, server.host, elasticsearch.host, elasticsearch.username & elasticsearch.password
Update the password => the kibana password which you saved previously
Save and close the file - Go to bin folder => run command "kibana.bat"
- Go to "http://localhost:5601" it will ask login credentials => username = elastic & password = elastic password which you copied
Download Link : https://www.elastic.co/downloads/logstash
Note: Refer "ELK Stack\credit-card-reward-calculator-logstash.conf" file given in this Repo & follow steps mentioned below
- Go to config folder => open the credit-card-reward-calculator-logstash.conf file (Use https://grokdebugger.com/ and https://github.com/hpcugent/logstash-patterns/blob/master/files/grok-patterns for GROK Patterns)
Update the password => the elastic password which you saved previously - Go to bin folder => run command "logstash -f .\config\credit-card-reward-calculator-logstash.conf --config.reload.automatic"
On Elastic Search Portal ==>
- Go to Stack Management ==> Index Management ==> find file "logstsh_index_logdata-credit-card-reward-point-system-%{+YYYY.MM.dd}" with today's date => copy name
- Go to Stack Management ==> Data Views ==> Create Data View => In index pattern write "logstsh_index_logdata-credit-card-reward-point-system" & give any name as requried => Click Save data view to kibana
- Go to http://localhost:5601/app/home#/ ==> Click on 'Discover' from the left menu ==> Select the Data View created to check the data which is parsed and will be used for visualization in dashboard
- Go to Dashboard ==> Create Dashboard ==> Click on Create Visualization ==> Top left side in dropdown select the data view name which you created in prev step
- MongoDB shell version: 5.0.6
- Express version: 4.17.2
- Angular CLI version: 13.2.1
- Node version: 16.13.0
- Node Package Manager (npm) version: 8.4.0
- Testing & Code Coverage: Jasmine, Karma, Nyc(Istanbul), Supertest
- End to End Testing: Cypress
- ELK Stack: Elasticsearch v8.5.3, Logstash v8.5.3, Kibana v8.5.3
The Problem Statement : The aim is to create a rewards calculation system that calculates the total monthly reward points earned based on a customer's credit card purchases. More than one reward points calculation rules could apply to a merchant’s transaction(s) and the system should maximize the calculated points for a merchant by considering different priorities or combinations of the rules.
Sample Transactions : Here is a list of transactions for a customer that needs to be considered for your solution.
transactions = {
"T01": {"date": "2021-05-01", "merchant_code" : "sportcheck", "amount_cents": 21000},
"T02": {"date": "2021-05-02", "merchant_code" : "sportcheck", "amount_cents": 8700},
"T03": {"date": "2021-05-03", "merchant_code" : "tim_hortons", "amount_cents": 323},
"T04": {"date": "2021-05-04", "merchant_code" : "tim_hortons", "amount_cents": 1267},
"T05": {"date": "2021-05-05", "merchant_code" : "tim_hortons", "amount_cents": 2116},
"T06": {"date": "2021-05-06", "merchant_code" : "tim_hortons", "amount_cents": 2211},
"T07": {"date": "2021-05-07", "merchant_code" : "subway", "amount_cents": 1853},
"T08": {"date": "2021-05-08", "merchant_code" : "subway", "amount_cents": 2153},
"T09": {"date": "2021-05-09", "merchant_code" : "sportcheck", "amount_cents": 7326},
"T10": {"date": "2021-05-10", "merchant_code" : "tim_hortons", "amount_cents": 1321}
}
The Ask : Assuming that each $1 spend is only counted once, implement a method that takes the customer transactions as an input (Merchant Code -> Purchase Amount) and calculates the total maximum rewards points earned for the month , the maximum reward points applied for each transaction.
Rules :
● Rule 1: 500 points for every $75 spend at Sport Check, $25 spend at Tim Hortons and $25 spend at Subway
● Rule 2: 300 points for every $75 spend at Sport Check and $25 spend at Tim Hortons
● Rule 3: 200 points for every $75 spend at Sport Check
● Rule 4: 150 points for every $25 spend at Sport Check, $10 spend at Tim Hortons and $10 spend at Subway
● Rule 5: 75 points for every $25 spend at Sport Check and $10 spend at Tim Hortons
● Rule 6: 75 point for every $20 spend at Sport Check
● Rule 7: 1 points for every $1 spend for all other purchases (including leftover amount)
Here are a few examples to help understand how to use rules to calculate the reward points. Each example has a set of transactions and rules to use for reward points calculation. These examples may not necessarily be the solution to the main problem.
Transactions :
transactions = {
'T1': {'date': '2021-05-09', 'merchant_code' : 'sportcheck', 'amount_cents': 7326},
'T2': {'date': '2021-05-10', 'merchant_code' : 'tim_hortons', 'amount_cents': 1321}
}
Rules :
● Rule 1: 10 points for every $1 spend at Sport Check
Solution :
● Rule 1: 10 points for every $1 spend at Sport Check => Promotion Applied x 73
Total Points: 730
Transaction Level Points:
● T1 - 730
● T2 - 0
Transactions :
transactions = {
'T1': {'date': '2021-05-09', 'merchant_code' : 'sportcheck', 'amount_cents': 7326},
'T2': {'date': '2021-05-10', 'merchant_code' : 'tim_hortons', 'amount_cents': 1321}
}
Rules :
● Rule 1: 10 points for every $1 spend at Sport Check
● Rule 2: 100 points for every $5 spend at Sport Check
Solution :
● Rule 1: 10 points for every $1 spend at Sport Check => Promotion Applied x 3
● Rule 2: 100 points for every $5 spend at Sport Check => Promotion Applied x 14
Total Points: 1430
Transaction Level Points:
● T1 - 1430
● T2 - 0
Transactions :
transactions = {
'T1': {'date': '2021-05-09', 'merchant_code' : 'sportcheck', 'amount_cents': 7326},
'T2': {'date': '2021-05-10', 'merchant_code' : 'tim_hortons', 'amount_cents': 1321}
}
Rules :
● Rule 1: 100 points for every $5 spend at Sport Check
● Rule 2: 10 points for every $1 spend for all other purchases
Solution :
● Rule 1: 100 points for every $5 spend at Sport Check => Promotion Applied x 14
● Rule 2: 10 points for every $1 spend for all other purchases (including leftover amount) => Promotion Applied x 16
Total Points: 1560
Transaction Level Points:
● T1 - 1430
● T2 - 130
Transactions :
transactions = {
'T1': {'date': '2021-05-09', 'merchant_code' : 'sportcheck', 'amount_cents': 2500},
'T2': {'date': '2021-05-10', 'merchant_code' : 'tim_hortons', 'amount_cents': 1000},
'T3’: {'date': '2021-05-10', 'merchant_code' : 'the_bay', 'amount_cents': 500}
}
Rules :
Same rules as stated in the main problem.
Solution :
● Rule 6: 75 point for every $20 spend at Sport Check => Promotion Applied x 1
● Rule 7: 1 points for every $1 spend for all other purchases (including leftover amount) => Promotion Applied x 20
Total points: 95
Transaction level points:
● T1 - 80 (rule 6 & 7)
● T2 - 10 (rule 7)
● T3 - 5 (rule 7)
Explanation :
Two sets of rules apply to this input
● If you use combination rules 5 and 7, the total points is 80 but that’s not the max total that can be achieved.
● If you use combination rules 6 and 7, the total points is 95 which is the correct solution.