add sanctions_logs table

This commit is contained in:
Josh Harvey 2018-05-07 12:20:22 +03:00
parent d3b7e7c2ad
commit 5ba164e566
3 changed files with 85 additions and 41 deletions

View file

@ -1,54 +1,75 @@
const _ = require('lodash/fp')
const uuid = require('uuid')
const logger = require('./logger')
const db = require('./db')
const ofac = require('./ofac/index')
function matchOfac (customer) {
// Probably because we haven't asked for ID yet
if (!_.isPlainObject(customer.idCardData)) {
return true
}
function logSanctionsMatch (deviceId, customer, sanctionsId, alias) {
const sql = `insert into sanctions_logs
(id, device_id, sanctioned_id, sanctioned_alias_id, sanctioned_alias_full_name, customer_id)
values
($1, $2, $3, $4, $5, $6)`
const nameParts = {
firstName: customer.idCardData.firstName,
lastName: customer.idCardData.lastName
}
if (_.some(_.isNil, _.values(nameParts))) {
logger.error(new Error(`Insufficient idCardData while matching OFAC for: ${customer.id}`))
return true
}
const birthDate = customer.idCardData.dateOfBirth
if (_.isNil(birthDate)) {
logger.error(new Error(`No birth date while matching OFAC for: ${customer.id}`))
return true
}
const options = {
threshold: 0.85,
fullNameThreshold: 0.95,
debug: false
}
const results = ofac.match(nameParts, birthDate, options)
return !_.isEmpty(results)
return db.none(sql, [uuid.v4(), deviceId, sanctionsId, alias.id, alias.fullName, customer.id])
}
function validateOfac (customer) {
function logSanctionsMatches (deviceId, customer, results) {
const logAlias = resultId => alias => logSanctionsMatch(deviceId, customer, resultId, alias)
const logResult = result => _.map(logAlias(result.id), result.aliases)
return Promise.all(_.flatMap(logResult, results))
}
function matchOfac (deviceId, customer) {
return Promise.resolve()
.then(() => {
// Probably because we haven't asked for ID yet
if (!_.isPlainObject(customer.idCardData)) {
return true
}
const nameParts = {
firstName: customer.idCardData.firstName,
lastName: customer.idCardData.lastName
}
if (_.some(_.isNil, _.values(nameParts))) {
logger.error(new Error(`Insufficient idCardData while matching OFAC for: ${customer.id}`))
return true
}
const birthDate = customer.idCardData.dateOfBirth
if (_.isNil(birthDate)) {
logger.error(new Error(`No birth date while matching OFAC for: ${customer.id}`))
return true
}
const options = {
threshold: 0.85,
fullNameThreshold: 0.95,
debug: false
}
const results = ofac.match(nameParts, birthDate, options)
return logSanctionsMatches(deviceId, customer, results)
.then(() => !_.isEmpty(results))
})
}
function validateOfac (deviceId, customer) {
if (customer.sanctionsOverride === 'blocked') return false
if (customer.sanctionsOverride === 'verified') return true
return !matchOfac(customer)
return matchOfac(deviceId, customer)
.then(didMatch => !didMatch)
}
function validationPatch (config, customer) {
return Promise.resolve()
.then(() => {
const ofacValidation = validateOfac(customer)
function validationPatch (deviceId, config, customer) {
return validateOfac(deviceId, customer)
.then(ofacValidation => {
if (_.isNil(customer.sanctions) || customer.sanctions !== ofacValidation) {
return {sanctions: ofacValidation}
}