Commit d6252d2b4879509811efe1784d5f733ffd72073f

Authored by Adhidarma Hadiwinoto
1 parent 116165417f
Exists in master

select suppliers

Showing 3 changed files with 95 additions and 47 deletions Side-by-side Diff

... ... @@ -32,10 +32,10 @@ function initMongoClient() {
32 32 if (!config.mongodb || !config.mongodb.url) {
33 33 return;
34 34 }
35   -
  35 +
36 36 try {
37 37 var url = config.mongodb.url;
38   -
  38 +
39 39 mongoClient.connect(url, function(err, db) {
40 40 if (err) {
41 41 console.log('Failed to connect to mongodb');
... ... @@ -47,7 +47,7 @@ function initMongoClient() {
47 47 }
48 48 catch(err) {
49 49 console.log('Exception when connecting to mongodb');
50   - }
  50 + }
51 51 }
52 52 initMongoClient();
53 53  
... ... @@ -85,11 +85,11 @@ function authNeeded(req, res, next) {
85 85 function createPaginatorObject(total, perpage, path, qs) {
86 86 var pageCount = Math.ceil(total / perpage);
87 87 var pages = [];
88   -
  88 +
89 89 var newQs = querystring.parse(querystring.stringify(qs));
90   -
91   -
92   -
  90 +
  91 +
  92 +
93 93 for (var i = 0; i < pageCount; i++) {
94 94  
95 95 var pageNumber = i + 1;
... ... @@ -97,11 +97,11 @@ function createPaginatorObject(total, perpage, path, qs) {
97 97 pages[i] = {
98 98 'number': pageNumber,
99 99 }
100   -
  100 +
101 101 if (pageNumber == qs.page) {
102 102 pages[i].active = true;
103 103 }
104   -
  104 +
105 105 newQs.page = pageNumber;
106 106 pages[i].link = path + '?' + querystring.stringify(newQs);
107 107  
... ... @@ -111,20 +111,20 @@ function createPaginatorObject(total, perpage, path, qs) {
111 111  
112 112 function pageHome(req, res, next) {
113 113 var limitPerPage = 50;
114   -
  114 +
115 115 var page = 1;
116 116 if (req.query.page) {
117 117 page = req.query.page;
118 118 } else {
119 119 req.query.page = page;
120 120 }
121   -
  121 +
122 122 var skip = (page - 1) * limitPerPage;
123 123  
124 124 var trxdate = '';
125 125 if (req.query.trxdate) {
126 126 trxdate = req.query.trxdate;
127   - }
  127 + }
128 128 else if (req.body.trxdate) {
129 129 trxdate = req.body.trxdate;
130 130 } else {
... ... @@ -132,68 +132,69 @@ function pageHome(req, res, next) {
132 132 res.redirect('/?trxdate=' + trxdate);
133 133 return;
134 134 }
135   -
  135 +
136 136 var conditions = {
137 137 ts_date: trxdate.trim()
138 138 };
139   -
  139 +
140 140 if (req.query.destination) {
141 141 conditions.destination = req.query.destination.trim();
142 142 }
143   -
  143 +
144 144 if (req.query.product) {
145 145 conditions.product = req.query.product.trim();
146 146 }
147   -
  147 +
148 148 mongodb.collection('trx').find(conditions).count(function(err, count) {
149   -
  149 +
150 150 if (err) {
151 151 var errorMessage = 'Something wrong when retrieving number of docs on mongo: ' + err;
152 152 console.log(errorMessage);
153 153 res.send(errorMessage);
154 154 return;
155 155 }
156   -
  156 +
157 157 var pagecount = Math.ceil(count / limitPerPage);
158 158 var paginator = createPaginatorObject(count, limitPerPage, req.path, req.query);
159   -
  159 +
160 160 mongodb.collection('trx').find(conditions).sort([['ts', -1]]).skip(skip).limit(limitPerPage).toArray(function (err, docs) {
161   -
  161 +
162 162 if (err) {
163 163 var errorMessage = 'Something wrong when retrieving docs from mongo: ' + err;
164 164 console.log(errorMessage);
165 165 res.send(errorMessage);
166 166 return;
167 167 }
168   -
  168 +
169 169 res.render('index.html', {
170 170 title: 'Transactions',
171 171 conditions: conditions,
172   - trxdate: trxdate,
  172 + trxdate: trxdate,
173 173 trxcount: count,
174 174 pagecount: pagecount,
175 175 limitperpage: limitPerPage,
176 176 paginator: paginator,
177 177 debugmsg: JSON.stringify(paginator),
178   - trxs: docs
  178 + trxs: docs,
  179 + suppliers: ['CJK', 'TRUST']
179 180 });
180   - });
  181 + });
181 182 });
182 183 }
183 184  
184 185 function pageTrxView(req, res, next) {
185   -
  186 +
186 187 mongodb.collection('trx').find({requestId: req.params.id}).limit(1).next(function(err, doc) {
187 188 res.render(
188   - 'trx.view.html',
  189 + 'trx.view.html',
189 190 {
190 191 title: 'Transaction #' + req.params.id,
191 192 trx: doc
192 193 }
193 194 );
194   -
  195 +
195 196 });
196   -
  197 +
197 198 }
198 199  
199 200 function pageSigninForm(req, res, next) {
... ... @@ -201,7 +202,7 @@ function pageSigninForm(req, res, next) {
201 202 res.redirect('/');
202 203 return;
203 204 }
204   -
  205 +
205 206 res.render('signin.html');
206 207 }
207 208  
... ... @@ -212,12 +213,12 @@ function pageSigninPost(req, res, next) {
212 213 res.redirect('/signin');
213 214 return;
214 215 }
215   -
  216 +
216 217 if (!doc) {
217 218 res.redirect('/signin');
218 219 return;
219 220 }
220   -
  221 +
221 222 req.session.username = req.body.username;
222 223 res.redirect('/');
223 224  
... ... @@ -0,0 +1,28 @@
  1 +var mongoClient = require('mongodb').MongoClient;
  2 +var url = "mongodb://r97:pulsa123@172.23.3.61:27017/cjk?authMechanism=SCRAM-SHA-1&authSource=cjk";
  3 +
  4 +mongoClient.connect(url, function(err, db) {
  5 + if (err) {
  6 + console.log('gagal konek ke db');
  7 + return;
  8 + }
  9 + var i = 0;
  10 + db.collection('trx').find({}).each(function (err, doc) {
  11 + if (err) {
  12 + console.log('gagal find each');
  13 + return;
  14 + }
  15 +
  16 + try {
  17 + var ts = doc.ts;
  18 + var ts_date = ts.substring(0, 10);
  19 + console.log(++i + '. ' + ts + ' => ' + ts_date);
  20 +
  21 + db.collection('trx').update({_id: doc._id}, {ts_date: ts_date});
  22 + }
  23 + catch(except) {
  24 + }
  25 +
  26 +
  27 + });
  28 +});
... ... @@ -9,33 +9,52 @@
9 9 <input name="trxdate" type="date" class="form-control" id="inputDate" value="{{ conditions.ts_date }}">
10 10 </div>
11 11 </div>
12   -
  12 +
13 13 <div class="form-group">
14 14 <label for="inputDestination" class="col-sm-2 control-label">Destination</label>
15 15 <div class="col-sm-10">
16 16 <input name="destination" type="text" class="form-control" id="inputDestination" value="{{ conditions.destination }}">
17 17 </div>
18 18 </div>
19   -
  19 +
20 20 <div class="form-group">
21 21 <label for="inputProduct" class="col-sm-2 control-label">Product</label>
22 22 <div class="col-sm-10">
23 23 <input name="product" type="text" class="form-control" id="inputProduct" value="{{ conditions.product }}">
24 24 </div>
25 25 </div>
26   -
  26 +
  27 + <div class="form-group">
  28 + <label for="inputSupplier" class="col-sm-2 control-label">Supplier</label>
  29 + <div class="col-sm-10">
  30 + <select class="form-control" name="supplier" id="inputSupplier">
  31 +
  32 + {% for supplier in suppliers %}
  33 + <option
  34 + {% if supplier == conditions.supplier %}
  35 + selected="selected"
  36 + {% endif %}
  37 + >
  38 + {{ supplier }}
  39 + </option>
  40 + {% endfor %}
  41 +
  42 + </select>
  43 + </div>
  44 + </div>
  45 +
27 46 <div class="form-group">
28 47 <div class="col-sm-offset-2 col-sm-10">
29 48 <input type="submit" class="btn btn-primary">
30 49 </div>
31 50 </div>
32   -
  51 +
33 52 </form>
34 53  
35 54 <br><br>
36 55  
37 56 <table class="table table-striped table-hover">
38   -
  57 +
39 58 <tr>
40 59 <th class="hidden-xs hidden-sm hidden-md">ReqId</th>
41 60 <th>Time</th>
... ... @@ -45,25 +64,25 @@
45 64 <th>RC</th>
46 65 <th class="hidden-xs hidden-sm hidden-md">Message</th>
47 66 </tr>
48   -
  67 +
49 68 {% for trx in trxs %}
50 69 {% if trx.responses %}
51 70 {% set lastResponse = trx.responses|last %}
52 71 {% endif %}
53   -
  72 +
54 73 <tr>
55 74 <td class="hidden-xs hidden-sm hidden-md">
56 75 <a href="/trx/view/{{ trx.requestId }}">{{ trx.requestId }}</a>
57 76 </td>
58   -
  77 +
59 78 <td>{{ trx.ts }}</td>
60   -
  79 +
61 80 <td>
62 81 <a href="/trx/view/{{ trx.requestId }}">{{ trx.destination }}</a>
63 82 </td>
64   -
  83 +
65 84 <td>{{ trx.product }}</td>
66   -
  85 +
67 86 <td class="hidden-xs hidden-sm hidden-md">
68 87 {% if trx.supplier %}
69 88 {{ trx.supplier }}
... ... @@ -71,7 +90,7 @@
71 90 -
72 91 {% endif %}
73 92 </td>
74   -
  93 +
75 94 <td>
76 95 {% if lastResponse %}
77 96 {% if (trx.supplier == "CJK") %}
... ... @@ -85,7 +104,7 @@
85 104 -
86 105 {% endif %}
87 106 </td>
88   -
  107 +
89 108 <td class="hidden-xs hidden-sm hidden-md">
90 109 {% if lastResponse %}
91 110 {% if (trx.supplier == "CJK") %}
... ... @@ -99,11 +118,11 @@
99 118 -
100 119 {% endif %}
101 120 </td>
102   -
103   -
  121 +
  122 +
104 123 </tr>
105 124 {% endfor %}
106   -
  125 +
107 126 </table>
108 127  
109 128 <p>