"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 = ? "; 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;