Commit 02d725d2b98a7c1b2bd7a8e43f6fa73a9fa0c848

Authored by Adhidarma Hadiwinoto
1 parent 637269aa5a
Exists in master

sales per CHIP_INFO

Showing 3 changed files with 44 additions and 1 deletions Side-by-side Diff

... ... @@ -41,7 +41,7 @@ function createResultObject(res) {
41 41 let metadataCount = metadata.length;
42 42  
43 43 let retval = [];
44   - let resultCount = res.rows.length
  44 + let resultCount = res.rows.length;
45 45  
46 46 for (let i=0; i < resultCount; i++) {
47 47  
... ... @@ -28,5 +28,6 @@ function checkAuth(req, res, next) {
28 28  
29 29 app.get("/apikey/:apikey/store-user/by-msisdn/:msisdn", checkAuth, storeUser.getByMSISDN);
30 30 app.get("/apikey/:apikey/trans/by-messageid/:messageid", checkAuth, trans.getByMessageid);
  31 +app.get("/apikey/:apikey/sales/last-hour/per-chip-info", checkAuth, trans.getLastHourSalesPerChipInfo);
31 32  
32 33 app.listen(config.listen_port);
... ... @@ -48,6 +48,48 @@ function getByMessageid(req, res, next) {
48 48 });
49 49 }
50 50  
  51 +function _getLastHourSalesPerChipInfo(cb, hour) {
  52 + if (!hour) {
  53 + hour = 1;
  54 + }
  55 +
  56 + let dbConnection = db.connection();
  57 + if (!dbConnection) {
  58 + cb("db is not connected");
  59 + return;
  60 + }
  61 +
  62 + let query = "select CHIP_INFO, COUNT(1) as TRX, sum(PRICE) as AMOUNT, max(TIME_START) as LAST_TS from T_TRANS where TIME_START >= sysdate - :hour / 24 and CHIP_INFO is not null and (TRANS_STAT = '200' or TRANS_STAT = '1200') group by CHIP_INFO";
  63 + dbConnection.execute(query, [hour], function(err, res) {
  64 + if (err) {
  65 + cb(err, null);
  66 + return;
  67 + }
  68 +
  69 + let retval = db.createResultObject(res);
  70 +
  71 + if (retval && retval.length > 0) {
  72 + cb(null, retval);
  73 + }
  74 + else {
  75 + cb(null, null);
  76 + }
  77 + });
  78 +}
  79 +
  80 +function getLastHourSalesPerChipInfo(req, res, next) {
  81 + _getLastHourSalesPerChipInfo(function(err, result) {
  82 + if (err) {
  83 + res.send(err.message);
  84 + return;
  85 + }
  86 +
  87 + res.send(result);
  88 + });
  89 +
  90 +}
  91 +
51 92  
52 93 exports.init = init;
53 94 exports.getByMessageid = getByMessageid
  95 +exports.getLastHourSalesPerChipInfo = getLastHourSalesPerChipInfo;