Skip to content

Commit 6073baa

Browse files
committed
Mobile banking account dashboard tutorial
1 parent 3d3cc5f commit 6073baa

File tree

11 files changed

+313
-53
lines changed

11 files changed

+313
-53
lines changed

docs/howtos/solutions/index-solutions.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,24 @@ Learn how to easily build, test and deploy code for common microservice and cach
132132
</div>
133133

134134
</div>
135+
136+
## Mobile Banking
137+
138+
<div class="row">
139+
<div class="col">
140+
<RedisCard
141+
title="Authentication and Session Storage"
142+
description="Mobile Banking Authentication and Session Storage Using Redis"
143+
page="/howtos/solutions/mobile-banking/session-management"
144+
/>
145+
</div>
146+
147+
<div class="col">
148+
<RedisCard
149+
title="Account Dashboard"
150+
description="Mobile Banking Account Dashboard Using Redis"
151+
page="/howtos/solutions/mobile-banking/account-dashboard"
152+
/>
153+
</div>
154+
155+
</div>
57.4 KB
Loading
67 KB
Loading
239 KB
Loading
316 KB
Loading
135 KB
Loading
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
---
2+
id: index-mb-account-dashboard
3+
title: Mobile Banking Account Dashboard Using Redis
4+
sidebar_label: Mobile Banking Account Dashboard Using Redis
5+
slug: /howtos/solutions/mobile-banking/account-dashboard
6+
authors: [prasan, will]
7+
---
8+
9+
import GeneralAdditionalResources from '../common-mb/additional-resources.mdx';
10+
import MobileBankingSourceCode from '../common-mb/source-code-tip.mdx';
11+
import MobileBankingDataSeeding from '../common-mb/data-seeding.mdx';
12+
13+
<MobileBankingSourceCode />
14+
15+
## What is account dashboard for mobile banking?
16+
17+
An account dashboard is a page in a mobile banking app to instantly render account highlights to users. A customer can click on any of the accounts on the dashboard to see the real time account details, such as latest transactions, mortgage amount they have left to pay, checking and savings, etc.
18+
19+
Account dashboard make customer's finances easily visible in one place. It reduces financial complexity for the customer and fosters customer loyalty
20+
21+
![dashboard](./images/dashboard.png)
22+
23+
1. Banks store information in a number of separate databases that support individual banking products
24+
25+
2. Key customer account details (balances, recent transactions) across the banks product portfolio re prefetched into Redis Enterprise using Redis Data Integration
26+
27+
3. Redis Enterprise powers customers' account dashboards, enabling mobile banking users to view balances and other high-priority information immediately upon login
28+
29+
## Why you should use Redis for mobile banking account dashboard?
30+
31+
- **Resilience**: Redis Enterprise provides resilience with 99.999% uptime and Active-Active Geo Distribution to prevent loss of critical user profile data
32+
33+
- **Scalability**: Redis Enterprise provides < 1ms performance at incredibly high scale to ensure apps perform under peak loads
34+
35+
- **JSON Support**: Provides the ability to create and store account information as JSON documents with the < 1ms speed of Redis
36+
37+
- **Querying and Indexing**: Redis Enterprise can quickly identify and store data from multiple different databases and index data to make it readily searchable
38+
39+
:::note
40+
41+
Redis Stack supports the [<u>**JSON**</u>](/howtos/redisjson/) data type and allows you to index and querying JSON and [<u>**more**</u>](https://redis.io/docs/stack/). So your Redis data is not limited to simple key-value stringified data.
42+
43+
:::
44+
45+
## Building account dashboard with Redis
46+
47+
<MobileBankingSourceCode />
48+
49+
Download the above source code and run following command to start the demo application
50+
51+
```sh
52+
docker compose up
53+
```
54+
55+
After docker up & running, open [http://localhost:8080/](http://localhost:8080/) url in browser to view application
56+
57+
### Data seeding
58+
59+
<MobileBankingDataSeeding />
60+
61+
### Balance over time
62+
63+
**Dashboard widget**
64+
65+
![Chart](./images/01-ui-balance-over-time.png)
66+
67+
**API end point**
68+
69+
| | |
70+
| ------------- | --------------------------------- |
71+
| Endpoint | `/transaction/balance` |
72+
| Code Location | `/routers/transaction-router.js` |
73+
| Parameters | none |
74+
| Return value | `[{x: timestamp, y: value}, ...]` |
75+
76+
The balance endpoint leverages **[Redis time series](https://redis.io/docs/stack/timeseries/) feature**, It returns the range of all values from the time series object `balance_ts`. The resulting range is converted to an array of objects with each object containing an `x` property containing the timestamp and a `y` property containing the associated value. This endpoint supplies the time series chart with coordinates to plot a visualization of the balance over time.
77+
78+
```js title="app/routers/transaction-router.js"
79+
const BALANCE_TS = 'balance_ts';
80+
81+
/* fetch all transactions up to an hour ago */
82+
transactionRouter.get('/balance', async (req, res) => {
83+
//time series range
84+
const balance = await redis.ts.range(
85+
BALANCE_TS,
86+
Date.now() - 1000 * 60 * 5,
87+
Date.now(),
88+
);
89+
90+
let balancePayload = balance.map((entry) => {
91+
return {
92+
x: entry.timestamp,
93+
y: entry.value,
94+
};
95+
});
96+
97+
res.send(balancePayload);
98+
});
99+
```
100+
101+
### Biggest spenders
102+
103+
**Dashboard widget**
104+
105+
![Chart](./images/02-ui-biggest-spenders.png)
106+
107+
**API end point**
108+
109+
| | |
110+
| ------------- | -------------------------------- |
111+
| Endpoint | `/transaction//biggestspenders` |
112+
| Code Location | `/routers/transaction-router.js` |
113+
| Parameters | none |
114+
| Return value | `{labels:[...], series:[...] }` |
115+
116+
The biggest spenders endpoint leverages **[Redis sorted sets](https://redis.io/docs/manual/patterns/indexes/) as a secondary index**, It retrieves all members of the sorted set `bigspenders` that have scores greater than zero. The top five or less are returned to provide the UI pie chart with data. The labels array contains the names of the biggest spenders and the series array contains the numeric values associated with each member name.
117+
118+
```js title="app/routers/transaction-router.js"
119+
const SORTED_SET_KEY = 'bigspenders';
120+
121+
/* fetch top 5 biggest spenders */
122+
transactionRouter.get('/biggestspenders', async (req, res) => {
123+
const range = await redis.zRangeByScoreWithScores(
124+
SORTED_SET_KEY,
125+
0,
126+
Infinity,
127+
);
128+
let series = [];
129+
let labels = [];
130+
131+
range.slice(0, 5).forEach((spender) => {
132+
series.push(parseFloat(spender.score.toFixed(2)));
133+
labels.push(spender.value);
134+
});
135+
136+
res.send({ series, labels });
137+
});
138+
```
139+
140+
### Search existing transactions
141+
142+
**Dashboard widget**
143+
144+
![Search transactions](./images/03-ui-search-transactions.png)
145+
146+
**API end point**
147+
148+
| | |
149+
| ---------------- | -------------------------------- |
150+
| Endpoint | `/transaction/search` |
151+
| Code Location | `/routers/transaction-router.js` |
152+
| Query Parameters | term |
153+
| Return value | array of results matching term |
154+
155+
The search endpoint leverages Redis **[Search and Query](https://redis.io/docs/stack/search/)** feature, It receives a `term` query parameter from the UI. A [Redis om Node](https://github.com/redis/redis-om-node) query for the fields `description`, `fromAccountName`, and `accountType` will trigger and return all results.
156+
157+
```js title="app/routers/transaction-router.js"
158+
transactionRouter.get('/search', async (req, res) => {
159+
const term = req.query.term;
160+
161+
let results;
162+
163+
if (term.length >= 3) {
164+
results = await bankRepo
165+
.search()
166+
.where('description')
167+
.matches(term)
168+
.or('fromAccountName')
169+
.matches(term)
170+
.or('transactionType')
171+
.equals(term)
172+
.return.all({ pageSize: 1000 });
173+
}
174+
res.send(results);
175+
});
176+
```
177+
178+
### Get most recent transactions
179+
180+
**Dashboard widget**
181+
182+
![View most recent transactions](./images/04-ui-recent-transactions.png)
183+
184+
**API end point**
185+
186+
| | |
187+
| ------------- | -------------------------------- |
188+
| Endpoint | `/transaction/transactions` |
189+
| Code Location | `/routers/transaction-router.js` |
190+
| Parameters | none |
191+
| Return value | array of results |
192+
193+
Even the transactions endpoint leverages Redis **[Search and Query](https://redis.io/docs/stack/search/)** feature. A [Redis om Node](https://github.com/redis/redis-om-node) query will trigger and return ten most recent transactions.
194+
195+
```js title="app/routers/transaction-router.js"
196+
/* return ten most recent transactions */
197+
transactionRouter.get('/transactions', async (req, res) => {
198+
const transactions = await bankRepo
199+
.search()
200+
.sortBy('transactionDate', 'DESC')
201+
.return.all({ pageSize: 10 });
202+
203+
res.send(transactions.slice(0, 10));
204+
});
205+
```
206+
207+
## Ready to use Redis in account dashboard?
208+
209+
Hopefully, this tutorial has helped you visualize how to use Redis for account dashboard, specifically in the context of mobile banking. For additional resources related to this topic, check out the links below:
210+
211+
### Additional resources
212+
213+
- [Mobile Banking Session Management](/howtos/solutions/mobile-banking/session-management)
214+
215+
<GeneralAdditionalResources />
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
This application leverages **Redis core data structures, JSON, TimeSeries, Search and Query features**. The data seeded is later used to show a searchable transaction overview with realtime updates as well as a personal finance management overview with realtime balance and biggest spenders updates.
2+
3+
On application startup in `app/server.js`, a cron is scheduled to create random bank transactions at regular intervals and seed those transactions in to Redis.
4+
5+
```js title="app/server.js"
6+
//cron job to trigger createBankTransaction() at regular intervals
7+
8+
cron.schedule('*/10 * * * * *', async () => {
9+
const userName = process.env.REDIS_USERNAME;
10+
11+
createBankTransaction(userName);
12+
13+
//...
14+
});
15+
```
16+
17+
The transaction generator creates a randomized banking debit or credit which will reflect on a starting user balance of $100,000.00
18+
19+
The transaction is saved as a JSON document within Redis. The `balanceAfter` value is recorded in a Timeseries with the key `balance_ts`. The transaction is also added to a stream (`transactions`) as an entry. The sorted set `bigspenders` is also updated - the associated **`fromAccountName`** member within the sorted set is incremented by the transaction amount. Note that this amount can be positive or negative.
20+
21+
```js title="app/transactions/transactionsGenerator.js"
22+
let balance = 100000.0;
23+
24+
export const createBankTransaction = async () => {
25+
//to create random bank transaction
26+
let vendorsList = source.source; //app/transactions/transaction_sources.js
27+
const random = Math.floor(Math.random() * 9999999999);
28+
29+
const vendor = vendorsList[random % vendorsList.length]; //random vendor from the list
30+
31+
const amount = createTransactionAmount(vendor.fromAccountName, random);
32+
const transaction = {
33+
id: random * random,
34+
fromAccount: Math.floor((random / 2) * 3).toString(),
35+
fromAccountName: vendor.fromAccountName,
36+
toAccount: '1580783161',
37+
toAccountName: 'bob',
38+
amount: amount,
39+
description: vendor.description,
40+
transactionDate: new Date(),
41+
transactionType: vendor.type,
42+
balanceAfter: balance,
43+
};
44+
45+
//redis json feature
46+
const bankTransaction = await bankTransactionRepository.save(transaction);
47+
console.log('Created bankTransaction!');
48+
// ...
49+
};
50+
51+
const createTransactionAmount = (vendor, random) => {
52+
let amount = createAmount(); //random amount
53+
balance += amount;
54+
balance = parseFloat(balance.toFixed(2));
55+
56+
//redis time series feature
57+
redis.ts.add(BALANCE_TS, '*', balance, { DUPLICATE_POLICY: 'first' });
58+
//redis sorted set as secondary index
59+
redis.zIncrBy(SORTED_SET_KEY, amount * -1, vendor);
60+
61+
return amount;
62+
};
63+
```
64+
65+
Sample `bankTransaction` data view using [RedisInsight](https://redis.com/redis-enterprise/redis-insight/)
66+
67+
![bank transaction data](./images/bank-transaction-data.png)
68+
69+
![bank transaction json](./images/bank-transaction-json.png)
70+
71+
:::tip
72+
Download [<u>**RedisInsight**</u>](https://redis.com/redis-enterprise/redis-insight/) to view your Redis data or to play with raw Redis commands in the workbench. Learn more by reading the [<u>**RedisInsight tutorial**</u>](/explore/redisinsight/)
73+
:::
172 KB
Loading
144 KB
Loading

0 commit comments

Comments
 (0)