From 049f21252ac23feb3853cb0d5d92d6527000337f Mon Sep 17 00:00:00 2001
From: Adhidarma Hadiwinoto <me@adhisimon.org>
Date: Mon, 3 Sep 2018 11:03:14 +0700
Subject: [PATCH] dashboard-stocks

---
 lib/modem-dashboard/index.js          |  2 ++
 lib/modem-dashboard/router-stocks.js  | 35 +++++++++++++++++++++++++++++++++++
 lib/modem-dashboard/views/stocks.html | 14 ++++++++++++++
 3 files changed, 51 insertions(+)
 create mode 100644 lib/modem-dashboard/router-stocks.js
 create mode 100644 lib/modem-dashboard/views/stocks.html

diff --git a/lib/modem-dashboard/index.js b/lib/modem-dashboard/index.js
index 24f0586..ffdfa1e 100644
--- a/lib/modem-dashboard/index.js
+++ b/lib/modem-dashboard/index.js
@@ -10,6 +10,7 @@ const matrix = require('komodo-sdk/matrix');
 
 const routerSms = require('./router-sms');
 const routerUssd = require('./router-ussd');
+const routerStocks = require('./router-stocks');
 
 const app = express();
 
@@ -57,6 +58,7 @@ app.get('/', function(req, res, next) {
 
 app.use('/sms', routerSms);
 app.use('/ussd', routerUssd);
+app.use('/stocks', routerStocks);
 
 const modem_dashboard_port = (config && config.modem_dashboard && config.modem_dashboard.port) ? config.modem_dashboard.port : 22765;
 app.listen(modem_dashboard_port, function () {
diff --git a/lib/modem-dashboard/router-stocks.js b/lib/modem-dashboard/router-stocks.js
new file mode 100644
index 0000000..5a9b37f
--- /dev/null
+++ b/lib/modem-dashboard/router-stocks.js
@@ -0,0 +1,35 @@
+"use strict";
+
+const express = require('express');
+const router = express.Router();
+module.exports = router;
+
+const matrix = require('komodo-sdk/matrix');
+
+function pageIndex(req, res, next) {
+    let stocks_array;
+
+    if (!matrix || !matrix.stock || (typeof matrix.stock !== 'object')) {
+        stocks_array = null;
+    }
+
+    for (let key in matrix.stock) {
+        if (matrix.stock.hasOwnProperty(key)) {
+            stocks_array.push({
+                name: key,
+                balance: matrix.stock[key];
+            });
+        }
+    }
+
+    res.render(
+        'stocks.html',
+        {
+            page_title: 'Stock'
+            stocks_array: stocks_array
+        }
+    );
+
+}
+
+router.get('/', pageIndex);
diff --git a/lib/modem-dashboard/views/stocks.html b/lib/modem-dashboard/views/stocks.html
new file mode 100644
index 0000000..5c5ad04
--- /dev/null
+++ b/lib/modem-dashboard/views/stocks.html
@@ -0,0 +1,14 @@
+{% extends "template.html" %}
+
+{% block content %}
+
+<table id="stocks" class="table table-hover table-striped">
+    {% for stock in stocks_array %}
+    <tr>
+        <td>{{ stock.name }}</td>
+        <td>{{ stock.balance }}</td>
+    </tr>
+    {% endfor %}
+</table>
+
+{% endblock %}
-- 
1.9.0