router-sms.js 1018 Bytes
"use strict";

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

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

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

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

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

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

    const created_date = req.query.input_date || moment().format('YYYY-MM-DD');
    const query = "SELECT rowid, * FROM sms 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('/', pageSmsIndex);
router.get('/datatables', pageSmsDataTables);