store-user.js 1.68 KB
"use strict";

var config;
var db;

function get(filter, cb) {
    let dbConnection = db.connection();
    if (!dbConnection) {
        cb("db is not connected");
        return;
    }

    let query = "SELECT * FROM T_STORE_USER WHERE 1=1";

    let queryWithFilter = db.composeQueryWithFilter(query, filter);

    dbConnection.execute(queryWithFilter.query, queryWithFilter.values, function(err, res) {
        if (err) {
            cb(err, null);
            return;
        }

        let retval = db.createResultObject(res);
        cb(null, retval);
    });
}

function _getByMSISDN(msisdn, cb) {
    let dbConnection = db.connection();
    if (!dbConnection) {
        cb("db is not connected");
        return;
    }

    let query = "SELECT T_STORE_USER.* FROM T_STORE_USER, T_STORE_USER_MSISDN WHERE T_STORE_USER_MSISDN.USER_NAME = T_STORE_USER.USER_NAME";
    let filter = {"T_STORE_USER_MSISDN.MSISDN": msisdn};

    let queryWithFilter = db.composeQueryWithFilter(query, filter);

    dbConnection.execute(queryWithFilter.query, queryWithFilter.values, function(err, res) {
        if (err) {
            cb(err, null);
            return;
        }

        let retval = db.createResultObject(res);

        if (retval && retval.length > 0) {
            cb(null, retval[0]);
        }
        else {
            cb(null, null);
        }
    });
}

function getByMSISDN(req, res, next) {
    _getByMSISDN(req.params.msisdn, function (err, result) {
        if (err) {
            res.send(err.message);
            return;
        }

        res.send(result);
    });
}

function init(options) {
    config = options.config;
    db = options.db;
}

exports.init = init;
exports.getByMSISDN = getByMSISDN;