fix: resolve full sanction checking flow in the backend

This commit is contained in:
Sérgio Salgado 2022-10-08 01:08:29 +01:00 committed by Rafael
parent 30a43869ec
commit cec02c52ba
5 changed files with 55 additions and 49 deletions

View file

@ -1,37 +1,12 @@
const _ = require('lodash/fp')
const logger = require('../../../logger')
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 sanctions = require('../../../sanctions')
const authentication = require('../modules/userManagement')
const resolvers = {
Query: {
checkAgainstSanctions: (...[, { firstName, lastName, birthdate }]) => loadOrUpdateSanctions()
.then(() => {
const ofacMatches = ofac.match({ firstName, lastName }, birthdate, { threshold: 0.85, fullNameThreshold: 0.95, debug: false })
return { ofacSanctioned: _.size(ofacMatches) > 0 }
})
checkAgainstSanctions: (...[, { customerId }, context]) => {
const token = authentication.getToken(context)
return sanctions.checkByUser(customerId, token)
}
}
}

View file

@ -6,7 +6,7 @@ const typeDef = gql`
}
type Query {
checkAgainstSanctions(firstName: String, lastName: String, birthdate: String): SanctionMatches @auth
checkAgainstSanctions(customerId: ID): SanctionMatches @auth
}
`

44
lib/sanctions.js Normal file
View 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
}

View file

@ -176,9 +176,7 @@ const CustomerData = ({
checkAgainstSanctions: () =>
checkAgainstSanctions({
variables: {
firstName: initialValues.idCardData.firstName,
lastName: initialValues.idCardData.lastName,
birthdate: R.replace(/-/g, '')(initialValues.idCardData.dateOfBirth)
customerId: R.path(['id'])(customer)
}
}),
initialValues: initialValues.idCardData,

View file

@ -293,16 +293,8 @@ const GET_ACTIVE_CUSTOM_REQUESTS = gql`
`
const CHECK_AGAINST_SANCTIONS = gql`
query checkAgainstSanctions(
$firstName: String
$lastName: String
$birthdate: String
) {
checkAgainstSanctions(
firstName: $firstName
lastName: $lastName
birthdate: $birthdate
) {
query checkAgainstSanctions($customerId: ID) {
checkAgainstSanctions(customerId: $customerId) {
ofacSanctioned
}
}
@ -417,10 +409,7 @@ const CustomerProfile = memo(() => {
})
const [checkAgainstSanctions] = useLazyQuery(CHECK_AGAINST_SANCTIONS, {
onCompleted: ({ checkAgainstSanctions: { ofacSanctioned } }) =>
updateCustomer({
sanctions: !ofacSanctioned
})
onCompleted: () => getCustomer()
})
const updateCustomer = it =>