From cec02c52ba68118195c39c9ea2e47d2e9730deef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Sat, 8 Oct 2022 01:08:29 +0100 Subject: [PATCH] fix: resolve full sanction checking flow in the backend --- .../graphql/resolvers/sanctions.resolver.js | 37 +++------------- lib/new-admin/graphql/types/sanctions.type.js | 2 +- lib/sanctions.js | 44 +++++++++++++++++++ .../src/pages/Customers/CustomerData.js | 4 +- .../src/pages/Customers/CustomerProfile.js | 17 ++----- 5 files changed, 55 insertions(+), 49 deletions(-) create mode 100644 lib/sanctions.js diff --git a/lib/new-admin/graphql/resolvers/sanctions.resolver.js b/lib/new-admin/graphql/resolvers/sanctions.resolver.js index 95cf2d88..bb1a8862 100644 --- a/lib/new-admin/graphql/resolvers/sanctions.resolver.js +++ b/lib/new-admin/graphql/resolvers/sanctions.resolver.js @@ -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) + } } } diff --git a/lib/new-admin/graphql/types/sanctions.type.js b/lib/new-admin/graphql/types/sanctions.type.js index 76a5e572..b7899da6 100644 --- a/lib/new-admin/graphql/types/sanctions.type.js +++ b/lib/new-admin/graphql/types/sanctions.type.js @@ -6,7 +6,7 @@ const typeDef = gql` } type Query { - checkAgainstSanctions(firstName: String, lastName: String, birthdate: String): SanctionMatches @auth + checkAgainstSanctions(customerId: ID): SanctionMatches @auth } ` diff --git a/lib/sanctions.js b/lib/sanctions.js new file mode 100644 index 00000000..34b1b7fd --- /dev/null +++ b/lib/sanctions.js @@ -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 +} diff --git a/new-lamassu-admin/src/pages/Customers/CustomerData.js b/new-lamassu-admin/src/pages/Customers/CustomerData.js index affe5159..5edbe829 100644 --- a/new-lamassu-admin/src/pages/Customers/CustomerData.js +++ b/new-lamassu-admin/src/pages/Customers/CustomerData.js @@ -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, diff --git a/new-lamassu-admin/src/pages/Customers/CustomerProfile.js b/new-lamassu-admin/src/pages/Customers/CustomerProfile.js index 3c03cb4f..44478022 100644 --- a/new-lamassu-admin/src/pages/Customers/CustomerProfile.js +++ b/new-lamassu-admin/src/pages/Customers/CustomerProfile.js @@ -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 =>