router-ussd.js 1009 Bytes
"use strict";

const moment = require('moment');
const express = require('express');

const localDb = require('../local-db');

const router = express.Router();
module.exports = router;

function pageIndex(req, res, next) {
    if (!req.query.input_date) {
        req.query.input_date = moment().format('YYYY-MM-DD');
    }

    res.render(
        'ussd.index.html',
        {
            page_title: 'Histori USSD',
            input_date: req.query.input_date
        }
    )
}

function pageDataTables(req, res, next) {
    const db = localDb.getConnection();

    const created_date = req.query.input_date || moment().format('YYYY-MM-DD');
    const query = "SELECT rowid, * FROM ussd WHERE created_date = ? ORDER BY created DESC, rowid DESC";
    db.all(query, created_date, function (err, rows) {
        if (err) {
            res.end("Ada error: " + err);
            return;
        }
        res.json({data: rows});
    });
}

router.get('/', pageIndex);
router.get('/datatables', pageDataTables);