pending-archive.js
2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"use strict";
const moment = require('moment');
const logger = require('komodo-sdk/logger');
const db = require('./local-db').getConnection();
function put(task, cb) {
logger.verbose('PENDING-ARCHIVE: Trying to put trx into table', {trx_id: task.trx_id, destination: task.destination, product: task.product});
const query = `
INSERT INTO pendingtrx (
created,
created_date,
trx_id,
destination,
product
)
VALUES (
?,
?,
?,
?,
?
)
`.trim();
const values = [
moment().format('YYYY-MM-DD HH:mm:ss'),
moment().format('YYYY-MM-DD'),
task.trx_id,
task.destination,
task.remote_product.split(',')[1]
];
db.run(query, values, function(err) {
if (err) {
if (typeof cb === 'function') cb(err);
return;
}
if (typeof cb === 'function') cb(null);
})
}
function get(destination, product, created_date, cb) {
if (typeof cb !== 'function') return;
logger.verbose('PENDING-ARCHIVE: Trying to get trx from table', {destination: destination, product: product, created_date: created_date});
const query = "SELECT trx_id FROM pendingtrx WHERE created_date = ? AND destination = ? AND product = ? ORDER BY created DESC LIMIT 1";
db.get(query, created_date, destination, product, function (err, row) {
if (err) {
logger.warn('PENDING-ARCHIVE: Error retrieving', {destination: destination, product: product, created_date: created_date, err: err});
cb(err);
return;
}
if (!row) {
//logger.warn('PENDING-ARCHIVE: Invalid row (empty) when retrieving', {destination: destination, product: product, created_date: created_date});
cb(null, null);
return;
}
if (!row.trx_id) {
cb(null, null);
return;
}
cb(null, row.trx_id);
})
}
function remove(trx_id, cb) {
logger.verbose('PENDING-ARCHIVE: Delete trx from table', {trx_id: trx_id});
const query = "DELETE FROM pendingtrx WHERE trx_id = ?";
db.run(query, trx_id, function(err) {
if (err) logger.warn('PENDING-ARCHIVE: Error on deleting trx', {trx_id: trx_id, err: err});
});
if (typeof cb === 'function') cb(null);
}
exports.put = put;
exports.get = get;
exports.remove = remove;