diff --git a/lib/compliance-triggers.js b/lib/compliance-triggers.js index d6fc088d..cc9eeaee 100644 --- a/lib/compliance-triggers.js +++ b/lib/compliance-triggers.js @@ -10,4 +10,8 @@ function hasSanctions (triggers) { return _.some(_.matches({ requirement: 'sanctions' }))(triggers) } -module.exports = { getBackwardsCompatibleTriggers, hasSanctions } \ No newline at end of file +function maxDaysThreshold (triggers) { + return _.max(_.map('thresholdDays')(triggers)) +} + +module.exports = { getBackwardsCompatibleTriggers, hasSanctions, maxDaysThreshold } \ No newline at end of file diff --git a/lib/routes.js b/lib/routes.js index 6b033900..1b3df340 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -215,6 +215,7 @@ function addOrUpdateCustomer (req) { const machineVersion = req.query.version const triggers = configManager.getTriggers(req.settings.config) const compatTriggers = complianceTriggers.getBackwardsCompatibleTriggers(triggers) + const maxDaysThreshold = complianceTriggers.maxDaysThreshold(triggers) return customers.get(customerData.phone) .then(customer => { @@ -235,8 +236,7 @@ function addOrUpdateCustomer (req) { return customers.update(customer.id, patch) }) }).then(customer => { - // TODO new-admin: only get customer history till max needed for triggers - return Tx.customerHistory(customer.id) + return Tx.customerHistory(customer.id, maxDaysThreshold) .then(result => { customer.txHistory = result return customer @@ -318,7 +318,12 @@ function triggerBlock (req, res, next) { function triggerSuspend (req, res, next) { const id = req.params.id - customers.update(id, { authorizedOverride: 'blocked' }) + const triggers = configManager.getTriggers(req.settings.config) + const trigger = _.find(_.matches({ id: req.body.triggerId }))(triggers) + + const date = new Date() + date.setDate(date.getDate() + trigger.suspensionDays); + customers.update(id, { suspendedUntil: date }) .then(customer => respond(req, res, { customer })) .catch(next) } diff --git a/lib/tx.js b/lib/tx.js index 96acf2f5..bbb90ba6 100644 --- a/lib/tx.js +++ b/lib/tx.js @@ -64,15 +64,18 @@ function cancel (txId) { }) } -function customerHistory (customerId) { +function customerHistory (customerId, thresholdDays) { const sql = ` select txIn.id, txIn.created, txIn.fiat, 'cashIn' as direction from cash_in_txs txIn where txIn.customer_id = $1 + and txIn.created > now() - interval $2 union select txOut.id, txOut.created, txOut.fiat, 'cashOut' as direction from cash_out_txs txOut where txOut.customer_id = $1 + and txOut.created > now() - interval $2 order by created;` - return db.any(sql, [customerId]) + const days = _.isNil(thresholdDays) ? 0 : thresholdDays + return db.any(sql, [customerId, `${days} days`]) } module.exports = {post, cancel, customerHistory} diff --git a/migrations/1601649726488-user-suspend.js b/migrations/1601649726488-user-suspend.js new file mode 100644 index 00000000..01313ed6 --- /dev/null +++ b/migrations/1601649726488-user-suspend.js @@ -0,0 +1,13 @@ +const db = require('./db') + +exports.up = function (next) { + var sql = [ + "ALTER TABLE customers ADD COLUMN suspended_until timestamptz" + ] + + db.multi(sql, next) +} + +exports.down = function (next) { + next() +}