Score Service
Code sections have been written in JavaScript and can be implemented with NodeJS. Of course, you are free to choose your favorite language.
It uses KoaJS library, a NodeJS HTTP framework, and superagent which is a HTTP client request library.
Score handled by the external service
Based on user's accounts and transactions, an external service may compute a score and let Algoan know about it.
After retrieving all user's bank accounts and transactions from Algoan, the service can add a score to the banks user.
Example
Subscribed event: credit_score_required
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');
const request = require('superagent');
const app = new Koa();
const router = new Router();
// This URL is just an example, you can of course create your own one
const resthookUrl = '/v1/resthook/example';
const algoanBaseUrl = 'https://api.algoan.com/v1';
/**
* Definition of your API
*/
async function postResthook(ctx) {
const requestBody = ctx.request.body;
const eventName = requestBody.subscription.eventName;
const payload = requestBody.payload;
if (eventName === 'credit_score_required') {
const banksUserId = payload.banksUserId;
const url = `${BASE_URL}/banks-users/${banksUserId}`;
const headers = { Authorization: `Bearer ${serviceAccountAccessToken}` };
/**
* Get banks accounts and transactions
*/
const accounts = await request
.get(`${url}/accounts`)
.set(headers);
const fullTransactions = [];
for (const account of accounts) {
const transactions = await request
.get(`${url}/accounts/${account.id}/transactions`)
.set(headers);
fullTransactions.push(transactions);
}
/**
* Calculate or get the score
*/
const score = ...
/**
* Send it to Algoan
*/
const reqBody = {
scores: [
{
score: 0.9786,
type: 'CREDIT',
}
],
}
await request.patch(url)
.set(headers)
.send(reqBody);
}
}
app.use(bodyParser());
router.post(resthookUrl, postResthook)
app.use(router.routes());
app.listen(3000);
Score handled by Algoan
When the score is calculated by Algoan, the credit_score_completed
event is emitted to inform the service that it is ready yo be retrieved.
Example
Subscribed event: credit_score_completed
const Koa = require("koa");
const bodyParser = require("koa-bodyparser");
const Router = require("koa-router");
const request = require("superagent");
const app = new Koa();
const router = new Router();
// This URL is just an example, you can of course create your own one
const resthookUrl = "/v1/resthook/example";
const algoanBaseUrl = "https://api.algoan.com/v1";
/**
* Definition of your API
*/
async function postResthook(ctx) {
const requestBody = ctx.request.body;
const eventName = requestBody.subscription.eventName;
const payload = requestBody.payload;
if (eventName === "credit_score_completed") {
const banksUserId = payload.banksUserId;
const url = `${BASE_URL}/banks-users/${banksUserId}`;
const headers = { Authorization: `Bearer ${serviceAccountAccessToken}` };
/**
* Get banks accounts and transactions
*/
const banksUser = await request.get(url).set(headers);
const scores = banksUser.scores;
}
}
app.use(bodyParser());
router.post(resthookUrl, postResthook);
app.use(router.routes());
app.listen(3000);
To retrieve alerts raised by Algoan, the Service can get the banks user exactly as above after receiving the financial_check_completed
event. Alerts are stored in the analysis
array.