feat: add pending manual compliance notifications

This commit is contained in:
siiky 2024-04-10 11:34:36 +01:00
parent 068f68e838
commit 3dbf10183e
4 changed files with 175 additions and 58 deletions

View file

@ -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')
@ -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) {
@ -44,32 +84,34 @@ function updateCustomer (req, res, next) {
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(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)
}