fix: resolve full sanction checking flow in the backend
This commit is contained in:
parent
30a43869ec
commit
cec02c52ba
5 changed files with 55 additions and 49 deletions
|
|
@ -1,37 +1,12 @@
|
||||||
const _ = require('lodash/fp')
|
const sanctions = require('../../../sanctions')
|
||||||
const logger = require('../../../logger')
|
const authentication = require('../modules/userManagement')
|
||||||
const ofac = require('../../../ofac')
|
|
||||||
const T = require('../../../time')
|
|
||||||
|
|
||||||
const sanctionStatus = {
|
|
||||||
loaded: false,
|
|
||||||
timestamp: null
|
|
||||||
}
|
|
||||||
|
|
||||||
const loadOrUpdateSanctions = () => {
|
|
||||||
if (!sanctionStatus.loaded || (sanctionStatus.timestamp && Date.now() > sanctionStatus.timestamp + T.day)) {
|
|
||||||
logger.info('No sanction lists loaded. Loading sanctions...')
|
|
||||||
return ofac.load()
|
|
||||||
.then(() => {
|
|
||||||
logger.info('OFAC sanction list loaded!')
|
|
||||||
sanctionStatus.loaded = true
|
|
||||||
sanctionStatus.timestamp = Date.now()
|
|
||||||
})
|
|
||||||
.catch(e => {
|
|
||||||
logger.error('Couldn\'t load OFAC sanction list!')
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.resolve()
|
|
||||||
}
|
|
||||||
|
|
||||||
const resolvers = {
|
const resolvers = {
|
||||||
Query: {
|
Query: {
|
||||||
checkAgainstSanctions: (...[, { firstName, lastName, birthdate }]) => loadOrUpdateSanctions()
|
checkAgainstSanctions: (...[, { customerId }, context]) => {
|
||||||
.then(() => {
|
const token = authentication.getToken(context)
|
||||||
const ofacMatches = ofac.match({ firstName, lastName }, birthdate, { threshold: 0.85, fullNameThreshold: 0.95, debug: false })
|
return sanctions.checkByUser(customerId, token)
|
||||||
return { ofacSanctioned: _.size(ofacMatches) > 0 }
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ const typeDef = gql`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Query {
|
type Query {
|
||||||
checkAgainstSanctions(firstName: String, lastName: String, birthdate: String): SanctionMatches @auth
|
checkAgainstSanctions(customerId: ID): SanctionMatches @auth
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
|
||||||
44
lib/sanctions.js
Normal file
44
lib/sanctions.js
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
const _ = require('lodash/fp')
|
||||||
|
const ofac = require('./ofac')
|
||||||
|
const T = require('./time')
|
||||||
|
const logger = require('./logger')
|
||||||
|
const customers = require('./customers')
|
||||||
|
|
||||||
|
const sanctionStatus = {
|
||||||
|
loaded: false,
|
||||||
|
timestamp: null
|
||||||
|
}
|
||||||
|
|
||||||
|
const loadOrUpdateSanctions = () => {
|
||||||
|
if (!sanctionStatus.loaded || (sanctionStatus.timestamp && Date.now() > sanctionStatus.timestamp + T.day)) {
|
||||||
|
logger.info('No sanction lists loaded. Loading sanctions...')
|
||||||
|
return ofac.load()
|
||||||
|
.then(() => {
|
||||||
|
logger.info('OFAC sanction list loaded!')
|
||||||
|
sanctionStatus.loaded = true
|
||||||
|
sanctionStatus.timestamp = Date.now()
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
logger.error('Couldn\'t load OFAC sanction list!')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve()
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkByUser = (customerId, userToken) => {
|
||||||
|
return Promise.all([loadOrUpdateSanctions(), customers.getCustomerById(customerId)])
|
||||||
|
.then(([, customer]) => {
|
||||||
|
const { firstName, lastName, dateOfBirth } = customer?.idCardData
|
||||||
|
const birthdate = _.replace(/-/g, '')(dateOfBirth)
|
||||||
|
const ofacMatches = ofac.match({ firstName, lastName }, birthdate, { threshold: 0.85, fullNameThreshold: 0.95, debug: false })
|
||||||
|
const isOfacSanctioned = _.size(ofacMatches) > 0
|
||||||
|
customers.updateCustomer(customerId, { sanctions: !isOfacSanctioned }, userToken)
|
||||||
|
|
||||||
|
return { ofacSanctioned: isOfacSanctioned }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
checkByUser
|
||||||
|
}
|
||||||
|
|
@ -176,9 +176,7 @@ const CustomerData = ({
|
||||||
checkAgainstSanctions: () =>
|
checkAgainstSanctions: () =>
|
||||||
checkAgainstSanctions({
|
checkAgainstSanctions({
|
||||||
variables: {
|
variables: {
|
||||||
firstName: initialValues.idCardData.firstName,
|
customerId: R.path(['id'])(customer)
|
||||||
lastName: initialValues.idCardData.lastName,
|
|
||||||
birthdate: R.replace(/-/g, '')(initialValues.idCardData.dateOfBirth)
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
initialValues: initialValues.idCardData,
|
initialValues: initialValues.idCardData,
|
||||||
|
|
|
||||||
|
|
@ -293,16 +293,8 @@ const GET_ACTIVE_CUSTOM_REQUESTS = gql`
|
||||||
`
|
`
|
||||||
|
|
||||||
const CHECK_AGAINST_SANCTIONS = gql`
|
const CHECK_AGAINST_SANCTIONS = gql`
|
||||||
query checkAgainstSanctions(
|
query checkAgainstSanctions($customerId: ID) {
|
||||||
$firstName: String
|
checkAgainstSanctions(customerId: $customerId) {
|
||||||
$lastName: String
|
|
||||||
$birthdate: String
|
|
||||||
) {
|
|
||||||
checkAgainstSanctions(
|
|
||||||
firstName: $firstName
|
|
||||||
lastName: $lastName
|
|
||||||
birthdate: $birthdate
|
|
||||||
) {
|
|
||||||
ofacSanctioned
|
ofacSanctioned
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -417,10 +409,7 @@ const CustomerProfile = memo(() => {
|
||||||
})
|
})
|
||||||
|
|
||||||
const [checkAgainstSanctions] = useLazyQuery(CHECK_AGAINST_SANCTIONS, {
|
const [checkAgainstSanctions] = useLazyQuery(CHECK_AGAINST_SANCTIONS, {
|
||||||
onCompleted: ({ checkAgainstSanctions: { ofacSanctioned } }) =>
|
onCompleted: () => getCustomer()
|
||||||
updateCustomer({
|
|
||||||
sanctions: !ofacSanctioned
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const updateCustomer = it =>
|
const updateCustomer = it =>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue