diff --git a/lib/respond.js b/lib/respond.js new file mode 100644 index 00000000..502e4ea3 --- /dev/null +++ b/lib/respond.js @@ -0,0 +1,15 @@ +const _ = require('lodash/fp') +const notifier = require('./notifier') + +function respond (req, res, _body, _status) { + const status = _status || 200 + const body = _body || {} + const customer = _.getOr({ sanctions: true }, ['customer'], body) + // sanctions can be null for new customers so we can't use falsy checks + if (customer.sanctions === false) { + notifier.notifyIfActive('compliance', 'sanctionsNotify', customer, req.body.phone) + } + return res.status(status).json(body) +} + +module.exports = respond diff --git a/lib/routes/customerRoutes.js b/lib/routes/customerRoutes.js index a285bcc6..0f5f6902 100644 --- a/lib/routes/customerRoutes.js +++ b/lib/routes/customerRoutes.js @@ -8,6 +8,8 @@ const complianceTriggers = require('../compliance-triggers') const configManager = require('../new-config-manager') const customers = require('../customers') const httpError = require('../route-helpers').httpError +const notifier = require('../notifier') +const respond = require('../respond') function updateCustomer (req, res, next) { const id = req.params.id @@ -37,7 +39,7 @@ function updateCustomer (req, res, next) { .then(newPatch => customers.updateFrontCamera(id, newPatch)) .then(newPatch => customers.update(id, newPatch, null, txId)) }) - .then(customer => res.status(200).json({ customer })) + .then(customer => respond(req, res, { customer })) .catch(next) } @@ -50,7 +52,7 @@ function triggerSanctions (req, res, next) { return compliance.validationPatch(req.deviceId, true, customer) .then(patch => customers.update(id, patch)) }) - .then(customer => res.status(200).json({ customer })) + .then(customer => respond(req, res, { customer })) .catch(next) } @@ -58,7 +60,10 @@ function triggerBlock (req, res, next) { const id = req.params.id customers.update(id, { authorizedOverride: 'blocked' }) - .then(customer => res.status(200).json({ customer })) + .then(customer => { + notifier.notifyIfActive('compliance', 'customerComplianceNotify', customer, req.deviceId, 'BLOCKED') + return respond(req, res, { customer }) + }) .catch(next) } @@ -74,7 +79,10 @@ function triggerSuspend (req, res, next) { const date = new Date() date.setDate(date.getDate() + days) customers.update(id, { suspendedUntil: date }) - .then(customer => res.status(200).json({ customer })) + .then(customer => { + notifier.notifyIfActive('compliance', 'customerComplianceNotify', customer, req.deviceId, 'SUSPENDED', days) + return respond(req, res, { customer }) + }) .catch(next) } diff --git a/lib/routes/phoneCodeRoutes.js b/lib/routes/phoneCodeRoutes.js index 1aed3f14..441a3b47 100644 --- a/lib/routes/phoneCodeRoutes.js +++ b/lib/routes/phoneCodeRoutes.js @@ -10,6 +10,7 @@ const customers = require('../customers') const httpError = require('../route-helpers').httpError const plugins = require('../plugins') const Tx = require('../tx') +const respond = require('../respond') function addOrUpdateCustomer (req) { const customerData = req.body @@ -52,7 +53,7 @@ function getCustomerWithPhoneCode (req, res, next) { return pi.getPhoneCode(phone) .then(code => { return addOrUpdateCustomer(req) - .then(customer => res.status(200).json({ code, customer })) + .then(customer => respond(req, res, { code, customer })) }) .catch(err => { if (err.name === 'BadNumberError') throw httpError('Bad number', 401) diff --git a/lib/routes/stateRoutes.js b/lib/routes/stateRoutes.js index b78c9233..b25b62c0 100644 --- a/lib/routes/stateRoutes.js +++ b/lib/routes/stateRoutes.js @@ -2,10 +2,11 @@ const express = require('express') const router = express.Router() const helpers = require('../route-helpers') +const respond = require('../respond') function stateChange (req, res, next) { helpers.stateChange(req.deviceId, req.deviceTime, req.body) - .then(() => res.status(200).json({})) + .then(() => respond(req, res)) .catch(next) } diff --git a/lib/routes/verifyPromoCodeRoutes.js b/lib/routes/verifyPromoCodeRoutes.js index 0852f8dc..e5740146 100644 --- a/lib/routes/verifyPromoCodeRoutes.js +++ b/lib/routes/verifyPromoCodeRoutes.js @@ -1,23 +1,11 @@ const express = require('express') const router = express.Router() -const _ = require('lodash/fp') const BN = require('../bn') const commissionMath = require('../commission-math') const configManager = require('../new-config-manager') -const notifier = require('../notifier') const promoCodes = require('../promo-codes') - -function respond (req, res, _body, _status) { - const status = _status || 200 - const body = _body || {} - const customer = _.getOr({ sanctions: true }, ['customer'], body) - // sanctions can be null for new customers so we can't use falsy checks - if (customer.sanctions === false) { - notifier.notifyIfActive('compliance', 'sanctionsNotify', customer, req.body.phone) - } - return res.status(status).json(body) -} +const respond = require('../respond') function verifyPromoCode (req, res, next) { promoCodes.getPromoCode(req.body.codeInput) diff --git a/lib/routes/verifyTxRoutes.js b/lib/routes/verifyTxRoutes.js index 95738854..5c5cef3c 100644 --- a/lib/routes/verifyTxRoutes.js +++ b/lib/routes/verifyTxRoutes.js @@ -2,11 +2,12 @@ const express = require('express') const router = express.Router() const plugins = require('../plugins') +const respond = require('../respond') function verifyTx (req, res, next) { const pi = plugins(req.settings, req.deviceId) pi.verifyTransaction(req.body) - .then(idResult => res.status(200).json(idResult)) + .then(idResult => respond(req, res, idResult)) .catch(next) } diff --git a/lib/routes/verifyUserRoutes.js b/lib/routes/verifyUserRoutes.js index 050019a1..abf471b8 100644 --- a/lib/routes/verifyUserRoutes.js +++ b/lib/routes/verifyUserRoutes.js @@ -2,11 +2,12 @@ const express = require('express') const router = express.Router() const plugins = require('../plugins') +const respond = require('../respond') function verifyUser (req, res, next) { const pi = plugins(req.settings, req.deviceId) pi.verifyUser(req.body) - .then(idResult => res.status(200).json(idResult)) + .then(idResult => respond(req, res, idResult)) .catch(next) }