Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release-10.0' into chore/dockerf…
Browse files Browse the repository at this point in the history
…iles-improvement
  • Loading branch information
RafaelTaranto committed Jul 16, 2024
2 parents 62af4c8 + 446ce0b commit 72b5eb5
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 112 deletions.
12 changes: 5 additions & 7 deletions lib/compliance.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ function validateOfac (deviceId, sanctionsActive, customer) {

function validationPatch (deviceId, sanctionsActive, customer) {
return validateOfac(deviceId, sanctionsActive, customer)
.then(ofacValidation => {
if (_.isNil(customer.sanctions) || customer.sanctions !== ofacValidation) {
return {sanctions: ofacValidation}
}

return {}
})
.then(sactions =>
_.isNil(customer.sanctions) || customer.sanctions !== sactions ?
{ sanctions } :
{}
)
}

module.exports = {validationPatch}
12 changes: 5 additions & 7 deletions lib/notifier/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,15 @@ function transactionNotify (tx, rec) {
})
}

function complianceNotify (customer, deviceId, action, period) {
return Promise.all([
settingsLoader.loadLatest(),
queries.getMachineName(deviceId)
])
.then(([settings, machineName]) => {
function complianceNotify (settings, customer, deviceId, action, period) {
return queries.getMachineName(deviceId)
.then(machineName => {
const notifications = configManager.getGlobalNotifications(settings.config)

const msgCore = {
BLOCKED: `was blocked`,
SUSPENDED: `was suspended for ${!!period && period} days`
SUSPENDED: `was suspended for ${!!period && period} days`,
PENDING_COMPLIANCE: `is waiting for your manual approval`,
}

const rec = {
Expand Down
4 changes: 3 additions & 1 deletion lib/notifier/notificationCenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ const customerComplianceNotify = (customer, deviceId, code, days = null) => {
if (days) {
date.setDate(date.getDate() + days)
}
const message = code === 'SUSPENDED' ? `Customer suspended until ${date.toLocaleString()}` : `Customer blocked`
const message = code === 'SUSPENDED' ? `Customer ${customer.phone} suspended until ${date.toLocaleString()}` :
code === 'BLOCKED' ? `Customer ${customer.phone} blocked` :
`Customer ${customer.phone} has pending compliance`

return clearOldCustomerSuspendedNotifications(customer.id, deviceId)
.then(() => queries.getValidNotifications(COMPLIANCE, detailB))
Expand Down
101 changes: 73 additions & 28 deletions lib/routes/customerRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const semver = require('semver')
const _ = require('lodash/fp')
const { zonedTimeToUtc, utcToZonedTime } = require('date-fns-tz/fp')
const { add, intervalToDuration } = require('date-fns/fp')
const uuid = require('uuid')

const sms = require('../sms')
const BN = require('../bn')
Expand All @@ -25,15 +26,54 @@ const Tx = require('../tx')
const loyalty = require('../loyalty')
const logger = require('../logger')

function updateCustomerCustomInfoRequest (customerId, patch, req, res) {
if (_.isNil(patch.data)) {
return customers.getById(customerId)
.then(customer => respond(req, res, { customer }))
function updateCustomerCustomInfoRequest (customerId, patch) {
const promise = _.isNil(patch.data) ?
Promise.resolve(null) :
customInfoRequestQueries.setCustomerDataViaMachine(customerId, patch.infoRequestId, patch)
return promise.then(() => customers.getById(customerId))
}

const createPendingManualComplianceNotifs = (settings, customer, deviceId) => {
const customInfoRequests = _.reduce(
(reqs, req) => _.set(req.info_request_id, req, reqs),
{},
_.get(['customInfoRequestData'], customer)
)

const isPending = field =>
uuid.validate(field) ?
_.get([field, 'override'], customInfoRequests) === 'automatic' :
customer[`${field}At`]
&& (!customer[`${field}OverrideAt`]
|| customer[`${field}OverrideAt`].getTime() < customer[`${field}At`].getTime())

const unnestCustomTriggers = triggersAutomation => {
const customTriggers = _.fromPairs(_.map(({ id, type }) => [id, type], triggersAutomation.custom))
return _.flow(
_.unset('custom'),
_.mapKeys(k => k === 'facephoto' ? 'frontCamera' : k),
_.assign(customTriggers),
)(triggersAutomation)
}

return customInfoRequestQueries.setCustomerDataViaMachine(customerId, patch.infoRequestId, patch)
.then(() => customers.getById(customerId))
.then(customer => respond(req, res, { customer }))
const isManual = v => v === 'Manual'

const hasManualAutomation = triggersAutomation =>
_.any(isManual, _.values(triggersAutomation))

configManager.getTriggersAutomation(customInfoRequestQueries.getCustomInfoRequests(true), settings.config)
.then(triggersAutomation => {
triggersAutomation = unnestCustomTriggers(triggersAutomation)
if (!hasManualAutomation(triggersAutomation)) return

const pendingFields = _.filter(
field => isManual(triggersAutomation[field]) && isPending(field),
_.keys(triggersAutomation)
)

if (!_.isEmpty(pendingFields))
notifier.complianceNotify(settings, customer, deviceId, 'PENDING_COMPLIANCE')
})
}

function updateCustomer (req, res, next) {
Expand All @@ -43,32 +83,35 @@ function updateCustomer (req, res, next) {
const patch = req.body
const triggers = configManager.getTriggers(req.settings.config)
const compatTriggers = complianceTriggers.getBackwardsCompatibleTriggers(triggers)
const deviceId = req.deviceId
const settings = req.settings

if (patch.customRequestPatch) {
return updateCustomerCustomInfoRequest(id, patch.customRequestPatch, req, res).catch(next)
return updateCustomerCustomInfoRequest(id, patch.customRequestPatch)
.then(customer => {
createPendingManualComplianceNotifs(settings, customer, deviceId)
respond(req, res, { customer })
})
.catch(next)
}

// BACKWARDS_COMPATIBILITY 7.5
// machines before 7.5 expect customer with sanctions result
const isOlderMachineVersion = !machineVersion || semver.lt(machineVersion, '7.5.0-beta.0')
customers.getById(id)
.then(customer =>
!customer ? Promise.reject(httpError('Not Found', 404)) :
!isOlderMachineVersion ? {} :
compliance.validationPatch(deviceId, !!compatTriggers.sanctions, _.merge(customer, patch))
)
.then(_.merge(patch))
.then(newPatch => customers.updatePhotoCard(id, newPatch))
.then(newPatch => customers.updateFrontCamera(id, newPatch))
.then(newPatch => customers.update(id, newPatch, null, txId))
.then(customer => {
if (!customer) { throw httpError('Not Found', 404) }

const mergedCustomer = _.merge(customer, patch)

// BACKWARDS_COMPATIBILITY 7.5
// machines before 7.5 expect customer with sanctions result
const isOlderMachineVersion = !machineVersion || semver.lt(machineVersion, '7.5.0-beta.0')

return Promise.resolve({})
.then(emptyObj => {
if (!isOlderMachineVersion) return Promise.resolve(emptyObj)
return compliance.validationPatch(req.deviceId, !!compatTriggers.sanctions, mergedCustomer)
})
.then(_.merge(patch))
.then(newPatch => customers.updatePhotoCard(id, newPatch))
.then(newPatch => customers.updateFrontCamera(id, newPatch))
.then(newPatch => customers.update(id, newPatch, null, txId))
createPendingManualComplianceNotifs(settings, customer, deviceId)
respond(req, res, { customer })
})
.then(customer => respond(req, res, { customer }))
.catch(next)
}

Expand Down Expand Up @@ -100,10 +143,11 @@ function triggerSanctions (req, res, next) {

function triggerBlock (req, res, next) {
const id = req.params.id
const settings = req.settings

customers.update(id, { authorizedOverride: 'blocked' })
.then(customer => {
notifier.complianceNotify(customer, req.deviceId, 'BLOCKED')
notifier.complianceNotify(settings, customer, req.deviceId, 'BLOCKED')
return respond(req, res, { customer })
})
.catch(next)
Expand All @@ -112,6 +156,7 @@ function triggerBlock (req, res, next) {
function triggerSuspend (req, res, next) {
const id = req.params.id
const triggerId = req.body.triggerId
const settings = req.settings

const triggers = configManager.getTriggers(req.settings.config)
const getSuspendDays = _.compose(_.get('suspensionDays'), _.find(_.matches({ id: triggerId })))
Expand All @@ -122,7 +167,7 @@ function triggerSuspend (req, res, next) {

customers.update(id, { suspendedUntil: add(suspensionDuration, new Date()) })
.then(customer => {
notifier.complianceNotify(customer, req.deviceId, 'SUSPENDED', days)
notifier.complianceNotify(settings, customer, req.deviceId, 'SUSPENDED', days)
return respond(req, res, { customer })
})
.catch(next)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,12 @@ const CustomInfoRequestsData = ({ data }) => {
)
}

const getAuthorizedStatus = it => {
return it.approved === null
const getAuthorizedStatus = it =>
it.approved === null
? { label: 'Pending', type: 'neutral' }
: it.approved === false
? { label: 'Rejected', type: 'error' }
: { label: 'Accepted', type: 'success' }
}

return (
<>
Expand Down
Loading

0 comments on commit 72b5eb5

Please sign in to comment.