Skip to content

Commit

Permalink
Merge pull request #1749 from RafaelTaranto/backport/ofac-button
Browse files Browse the repository at this point in the history
LAM-470 backport: OFAC button
  • Loading branch information
RafaelTaranto authored Nov 28, 2024
2 parents d7562ca + 20eae07 commit 7c92cb2
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function update (id, data, userToken) {
async function updateCustomer (id, data, userToken) {
const formattedData = _.pick(
[
'sanctions',
'authorized_override',
'id_card_photo_override',
'id_card_data_override',
Expand Down
2 changes: 2 additions & 0 deletions lib/new-admin/graphql/resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const machine = require('./machine.resolver')
const notification = require('./notification.resolver')
const pairing = require('./pairing.resolver')
const rates = require('./rates.resolver')
const sanctions = require('./sanctions.resolver')
const scalar = require('./scalar.resolver')
const settings = require('./settings.resolver')
const sms = require('./sms.resolver')
Expand All @@ -37,6 +38,7 @@ const resolvers = [
notification,
pairing,
rates,
sanctions,
scalar,
settings,
sms,
Expand Down
13 changes: 13 additions & 0 deletions lib/new-admin/graphql/resolvers/sanctions.resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const sanctions = require('../../../sanctions')
const authentication = require('../modules/userManagement')

const resolvers = {
Query: {
checkAgainstSanctions: (...[, { customerId }, context]) => {
const token = authentication.getToken(context)
return sanctions.checkByUser(customerId, token)
}
}
}

module.exports = resolvers
2 changes: 2 additions & 0 deletions lib/new-admin/graphql/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const machine = require('./machine.type')
const notification = require('./notification.type')
const pairing = require('./pairing.type')
const rates = require('./rates.type')
const sanctions = require('./sanctions.type')
const scalar = require('./scalar.type')
const settings = require('./settings.type')
const sms = require('./sms.type')
Expand All @@ -37,6 +38,7 @@ const types = [
notification,
pairing,
rates,
sanctions,
scalar,
settings,
sms,
Expand Down
13 changes: 13 additions & 0 deletions lib/new-admin/graphql/types/sanctions.type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { gql } = require('apollo-server-express')

const typeDef = gql`
type SanctionMatches {
ofacSanctioned: Boolean
}
type Query {
checkAgainstSanctions(customerId: ID): SanctionMatches @auth
}
`

module.exports = typeDef
44 changes: 44 additions & 0 deletions lib/sanctions.js
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 11 additions & 2 deletions new-lamassu-admin/src/pages/Customers/CustomerData.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ const CustomerData = ({
authorizeCustomRequest,
updateCustomEntry,
retrieveAdditionalDataDialog,
setRetrieve
setRetrieve,
checkAgainstSanctions
}) => {
const classes = useStyles()
const [listView, setListView] = useState(false)
Expand Down Expand Up @@ -174,6 +175,12 @@ const CustomerData = ({
idCardData: R.merge(idData, formatDates(values))
}),
validationSchema: customerDataSchemas.idCardData,
checkAgainstSanctions: () =>
checkAgainstSanctions({
variables: {
customerId: R.path(['id'])(customer)
}
}),
initialValues: initialValues.idCardData,
isAvailable: !R.isNil(idData),
editable: true
Expand Down Expand Up @@ -463,7 +470,8 @@ const CustomerData = ({
initialValues,
hasImage,
hasAdditionalData,
editable
editable,
checkAgainstSanctions
},
idx
) => {
Expand All @@ -485,6 +493,7 @@ const CustomerData = ({
cancel={cancel}
deleteEditedData={deleteEditedData}
retrieveAdditionalData={retrieveAdditionalData}
checkAgainstSanctions={checkAgainstSanctions}
editable={editable}></EditableCard>
)
}
Expand Down
15 changes: 14 additions & 1 deletion new-lamassu-admin/src/pages/Customers/CustomerProfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery, useMutation } from '@apollo/react-hooks'
import { useQuery, useMutation, useLazyQuery } from '@apollo/react-hooks'
import {
makeStyles,
Breadcrumbs,
Expand Down Expand Up @@ -292,6 +292,14 @@ const GET_ACTIVE_CUSTOM_REQUESTS = gql`
}
`

const CHECK_AGAINST_SANCTIONS = gql`
query checkAgainstSanctions($customerId: ID) {
checkAgainstSanctions(customerId: $customerId) {
ofacSanctioned
}
}
`

const CustomerProfile = memo(() => {
const history = useHistory()

Expand Down Expand Up @@ -400,6 +408,10 @@ const CustomerProfile = memo(() => {
onCompleted: () => getCustomer()
})

const [checkAgainstSanctions] = useLazyQuery(CHECK_AGAINST_SANCTIONS, {
onCompleted: () => getCustomer()
})

const updateCustomer = it =>
setCustomer({
variables: {
Expand Down Expand Up @@ -662,6 +674,7 @@ const CustomerProfile = memo(() => {
authorizeCustomRequest={authorizeCustomRequest}
updateCustomEntry={updateCustomEntry}
setRetrieve={setRetrieve}
checkAgainstSanctions={checkAgainstSanctions}
retrieveAdditionalDataDialog={
<RetrieveDataDialog
onDismissed={() => {
Expand Down
13 changes: 12 additions & 1 deletion new-lamassu-admin/src/pages/Customers/components/EditableCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ const EditableCard = ({
deleteEditedData,
retrieveAdditionalData,
hasAdditionalData = true,
editable
editable,
checkAgainstSanctions
}) => {
const classes = useStyles()

Expand Down Expand Up @@ -277,6 +278,16 @@ const EditableCard = ({
Retrieve API data
</ActionButton>
)}
{checkAgainstSanctions && (
<ActionButton
color="primary"
type="button"
Icon={DataIcon}
InverseIcon={DataReversedIcon}
onClick={() => checkAgainstSanctions()}>
Check against OFAC sanction list
</ActionButton>
)}
</div>
{editable && (
<ActionButton
Expand Down

0 comments on commit 7c92cb2

Please sign in to comment.