feat: suspension trigger

This commit is contained in:
Taranto 2020-10-02 16:56:41 +01:00 committed by Josh Harvey
parent 118852a433
commit e0581fc39b
4 changed files with 31 additions and 6 deletions

View file

@ -10,4 +10,8 @@ function hasSanctions (triggers) {
return _.some(_.matches({ requirement: 'sanctions' }))(triggers) return _.some(_.matches({ requirement: 'sanctions' }))(triggers)
} }
module.exports = { getBackwardsCompatibleTriggers, hasSanctions } function maxDaysThreshold (triggers) {
return _.max(_.map('thresholdDays')(triggers))
}
module.exports = { getBackwardsCompatibleTriggers, hasSanctions, maxDaysThreshold }

View file

@ -215,6 +215,7 @@ function addOrUpdateCustomer (req) {
const machineVersion = req.query.version const machineVersion = req.query.version
const triggers = configManager.getTriggers(req.settings.config) const triggers = configManager.getTriggers(req.settings.config)
const compatTriggers = complianceTriggers.getBackwardsCompatibleTriggers(triggers) const compatTriggers = complianceTriggers.getBackwardsCompatibleTriggers(triggers)
const maxDaysThreshold = complianceTriggers.maxDaysThreshold(triggers)
return customers.get(customerData.phone) return customers.get(customerData.phone)
.then(customer => { .then(customer => {
@ -235,8 +236,7 @@ function addOrUpdateCustomer (req) {
return customers.update(customer.id, patch) return customers.update(customer.id, patch)
}) })
}).then(customer => { }).then(customer => {
// TODO new-admin: only get customer history till max needed for triggers return Tx.customerHistory(customer.id, maxDaysThreshold)
return Tx.customerHistory(customer.id)
.then(result => { .then(result => {
customer.txHistory = result customer.txHistory = result
return customer return customer
@ -318,7 +318,12 @@ function triggerBlock (req, res, next) {
function triggerSuspend (req, res, next) { function triggerSuspend (req, res, next) {
const id = req.params.id 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 })) .then(customer => respond(req, res, { customer }))
.catch(next) .catch(next)
} }

View file

@ -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 const sql = ` select txIn.id, txIn.created, txIn.fiat, 'cashIn' as direction from cash_in_txs txIn
where txIn.customer_id = $1 where txIn.customer_id = $1
and txIn.created > now() - interval $2
union union
select txOut.id, txOut.created, txOut.fiat, 'cashOut' as direction from cash_out_txs txOut select txOut.id, txOut.created, txOut.fiat, 'cashOut' as direction from cash_out_txs txOut
where txOut.customer_id = $1 where txOut.customer_id = $1
and txOut.created > now() - interval $2
order by created;` 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} module.exports = {post, cancel, customerHistory}

View file

@ -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()
}