support sanctions compliance

This commit is contained in:
Josh Harvey 2018-05-03 20:18:13 +03:00
parent 80e851fb59
commit d904c8391e
5 changed files with 108 additions and 25 deletions

View file

@ -18,6 +18,7 @@ const Tx = require('./tx')
const E = require('./error')
const customers = require('./customers')
const logs = require('./logs')
const compliance = require('./compliance')
const argv = require('minimist')(process.argv.slice(2))
@ -164,17 +165,33 @@ function verifyTx (req, res, next) {
.catch(next)
}
function addOrUpdateCustomer (req) {
const customerData = req.body
const config = configManager.unscoped(req.settings.config)
return customers.get(customerData.phone)
.then(customer => {
if (customer) return customer
return customers.add(req.body)
})
.then(customer => {
return compliance.validationPatch(config, customer)
.then(patch => {
if (_.isEmpty(patch)) return customer
return customers.update(customer.id, patch)
})
})
}
function getCustomerWithPhoneCode (req, res, next) {
const pi = plugins(req.settings, req.deviceId)
const phone = req.body.phone
return pi.getPhoneCode(phone)
.then(code => {
return customers.get(phone)
.then(customer => {
if (customer) return respond(req, res, {code, customer})
return customers.add(req.body)
.then(customer => respond(req, res, {code, customer}))
})
return addOrUpdateCustomer(req)
.then(customer => respond(req, res, {code, customer}))
})
.catch(err => {
if (err.name === 'BadNumberError') throw httpError('Bad number', 401)
@ -191,9 +208,12 @@ function updateCustomer (req, res, next) {
customers.getById(id)
.then(customer => {
if (!customer) { throw httpError('Not Found', 404) }
return customers.update(id, patch)
const mergedCustomer = _.merge(customer, patch)
return compliance.validationPatch(config, mergedCustomer)
.then(_.merge(patch))
.then(newPatch => customers.update(id, newPatch))
})
.then(customer => customers.validate(config, customer))
.then(customer => respond(req, res, {customer}))
.catch(next)
}
@ -295,9 +315,8 @@ const skip = (req, res) => _.includes(req.path, ['/poll', '/state', '/logs']) &&
const configRequiredRoutes = [
'/poll',
'/event',
'/verify_user',
'/verify_transaction',
'/phone_code',
'/customer',
'/tx'
]