Feat: refactor routes.js express entrypoint config

Feat: express config script refactor

Feat: add state and settingsCache files
This commit is contained in:
Cesar 2021-01-08 16:08:31 +00:00 committed by Josh Harvey
parent c3f8f98c26
commit 85235eaa13
22 changed files with 807 additions and 1 deletions

View file

@ -0,0 +1,95 @@
const express = require('express')
const router = express.Router()
const semver = require('semver')
const _ = require('lodash/fp')
const compliance = require('../compliance')
const complianceTriggers = require('../compliance-triggers')
const configManager = require('../new-config-manager')
const customers = require('../customers')
function httpError (msg, code) {
const err = new Error(msg)
err.name = 'HTTPError'
err.code = code || 500
return err
}
function updateCustomer (req, res, next) {
const id = req.params.id
const machineVersion = req.query.version
const txId = req.query.txId
const patch = req.body
const triggers = configManager.getTriggers(req.settings.config)
const compatTriggers = complianceTriggers.getBackwardsCompatibleTriggers(triggers)
customers.getById(id)
.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))
})
.then(customer => res.status(200).json({ customer }))
.catch(next)
}
function triggerSanctions (req, res, next) {
const id = req.params.id
customers.getById(id)
.then(customer => {
if (!customer) { throw httpError('Not Found', 404) }
return compliance.validationPatch(req.deviceId, true, customer)
.then(patch => customers.update(id, patch))
})
.then(customer => res.status(200).json({ customer }))
.catch(next)
}
function triggerBlock (req, res, next) {
const id = req.params.id
customers.update(id, { authorizedOverride: 'blocked' })
.then(customer => res.status(200).json({ customer }))
.catch(next)
}
function triggerSuspend (req, res, next) {
const id = req.params.id
const triggerId = req.body.triggerId
const triggers = configManager.getTriggers(req.settings.config)
const getSuspendDays = _.compose(_.get('suspensionDays'), _.find(_.matches({ id: triggerId })))
const days = triggerId === 'no-ff-camera' ? 1 : getSuspendDays(triggers)
const date = new Date()
date.setDate(date.getDate() + days);
customers.update(id, { suspendedUntil: date })
.then(customer => res.status(200).json({ customer }))
.catch(next)
}
router.patch('/:id', updateCustomer)
router.patch('/:id/sanctions', triggerSanctions)
router.patch('/:id/block', triggerBlock)
router.patch('/:id/suspend', triggerSuspend)
module.exports = router

View file

@ -0,0 +1,63 @@
const express = require('express')
const router = express.Router()
const logger = require('../logger')
const newSettingsLoader = require('../new-settings-loader')
const poller = require('../poller')
const settingsCache = require('../middlewares/settingsCache')
const state = require('../middlewares/state')
router.get('/pid', (req, res) => {
const deviceId = req.query.device_id
const pidRec = state.pids[deviceId]
res.json(pidRec)
})
router.post('/reboot', (req, res) => {
const deviceId = req.query.device_id
const pid = state.pids[deviceId] && state.pids[deviceId].pid
if (!deviceId || !pid) {
return res.sendStatus(400)
}
state.reboots[deviceId] = pid
res.sendStatus(200)
})
router.post('/shutdown', (req, res) => {
const deviceId = req.query.device_id
const pid = state.pids[deviceId] && state.pids[deviceId].pid
if (!deviceId || !pid) {
return res.sendStatus(400)
}
state.shutdowns[deviceId] = pid
res.sendStatus(200)
})
router.post('/restartServices', (req, res) => {
const deviceId = req.query.device_id
const pid = state.pids[deviceId] && state.pids[deviceId].pid
if (!deviceId || !pid) {
return res.sendStatus(400)
}
state.restartServicesMap[deviceId] = pid
res.sendStatus(200)
})
router.post('/dbChange', (req, res, next) => {
settingsCache.clearCache()
return newSettingsLoader.loadLatest()
.then(poller.reload)
.then(() => logger.info('Config reloaded'))
.catch(err => {
logger.error(err)
res.sendStatus(500)
})
})
module.exports = router

35
lib/routes/logsRoutes.js Normal file
View file

@ -0,0 +1,35 @@
const express = require('express')
const router = express.Router()
const _ = require('lodash/fp')
const state = require('../middlewares/state')
const logs = require('../logs')
const THROTTLE_LOGS_QUERY = 30 * 1000
function getLastSeen (req, res, next) {
const deviceId = req.deviceId
const timestamp = Date.now()
const shouldTrigger = !state.canGetLastSeenMap[deviceId] ||
timestamp - state.canGetLastSeenMap[deviceId] >= THROTTLE_LOGS_QUERY
if (shouldTrigger) {
state.canGetLastSeenMap[deviceId] = timestamp
return logs.getLastSeen(deviceId)
.then(r => res.json(r))
.catch(next)
}
return res.status(408).json({})
}
function updateLogs (req, res, next) {
return logs.update(req.deviceId, req.body.logs)
.then(status => res.json({ success: status }))
.catch(next)
}
router.get('/', getLastSeen)
router.post('/', updateLogs)
module.exports = router

View file

@ -0,0 +1,11 @@
const express = require('express')
const router = express.Router()
const ca = require('../middlewares/ca')
const pair = require('../middlewares/pair')
const populateDeviceId = require('../middlewares/populateDeviceId')
router.post('/pair', populateDeviceId, pair)
router.get('/ca', ca)
module.exports = router

View file

@ -0,0 +1,73 @@
const express = require('express')
const router = express.Router()
const semver = require('semver')
const _ = require('lodash/fp')
const compliance = require('../compliance')
const complianceTriggers = require('../compliance-triggers')
const configManager = require('../new-config-manager')
const customers = require('../customers')
const plugins = require('../plugins')
const Tx = require('../tx')
function addOrUpdateCustomer (req) {
const customerData = req.body
const machineVersion = req.query.version
const triggers = configManager.getTriggers(req.settings.config)
const compatTriggers = complianceTriggers.getBackwardsCompatibleTriggers(triggers)
const maxDaysThreshold = complianceTriggers.maxDaysThreshold(triggers)
return customers.get(customerData.phone)
.then(customer => {
if (customer) return customer
return customers.add(req.body)
})
.then(customer => {
// BACKWARDS_COMPATIBILITY 7.5
// machines before 7.5 expect customer with sanctions result
const isOlderMachineVersion = !machineVersion || semver.lt(machineVersion, '7.5.0-beta.0')
const shouldRunOfacCompat = !compatTriggers.sanctions && isOlderMachineVersion
if (!shouldRunOfacCompat) return customer
return compliance.validationPatch(req.deviceId, !!compatTriggers.sanctions, customer)
.then(patch => {
if (_.isEmpty(patch)) return customer
return customers.update(customer.id, patch)
})
}).then(customer => {
return Tx.customerHistory(customer.id, maxDaysThreshold)
.then(result => {
customer.txHistory = result
return customer
})
})
}
function httpError (msg, code) {
const err = new Error(msg)
err.name = 'HTTPError'
err.code = code || 500
return err
}
function getCustomerWithPhoneCode (req, res, next) {
const pi = plugins(req.settings, req.deviceId)
const phone = req.body.phone
return pi.getPhoneCode(phone)
.then(code => {
return addOrUpdateCustomer(req)
.then(customer => res.status(200).json({ code, customer }))
})
.catch(err => {
if (err.name === 'BadNumberError') throw httpError('Bad number', 401)
throw err
})
.catch(next)
}
router.post('/', getCustomerWithPhoneCode)
module.exports = router

112
lib/routes/pollingRoutes.js Normal file
View file

@ -0,0 +1,112 @@
const express = require('express')
const router = express.Router()
const _ = require('lodash/fp')
const complianceTriggers = require('../compliance-triggers')
const configManager = require('../new-config-manager')
const plugins = require('../plugins')
const semver = require('semver')
const state = require('../middlewares/state')
const version = require('../../package.json').version
function checkHasLightning (settings) {
return configManager.getWalletSettings('BTC', settings.config).layer2 !== 'no-layer2'
}
function createTerms (terms) {
if (!terms.active || !terms.text) return null
return {
active: terms.active,
title: terms.title,
text: nmd(terms.text),
accept: terms.acceptButtonText,
cancel: terms.cancelButtonText
}
}
function poll (req, res, next) {
const machineVersion = req.query.version
const machineModel = req.query.model
const deviceId = req.deviceId
const deviceTime = req.deviceTime
const serialNumber = req.query.sn
const pid = req.query.pid
const settings = req.settings
const localeConfig = configManager.getLocale(deviceId, settings.config)
const pi = plugins(settings, deviceId)
const hasLightning = checkHasLightning(settings)
const triggers = configManager.getTriggers(settings.config)
const operatorInfo = configManager.getOperatorInfo(settings.config)
const cashOutConfig = configManager.getCashOut(deviceId, settings.config)
const receipt = configManager.getReceipt(settings.config)
state.pids[deviceId] = { pid, ts: Date.now() }
return pi.pollQueries(serialNumber, deviceTime, req.query, machineVersion, machineModel)
.then(results => {
const cassettes = results.cassettes
const reboot = pid && state.reboots[deviceId] && state.reboots[deviceId] === pid
const shutdown = pid && state.shutdowns[deviceId] && state.shutdowns[deviceId] === pid
const restartServices = pid && state.restartServicesMap[deviceId] && state.restartServicesMap[deviceId] === pid
const langs = localeConfig.languages
const locale = {
fiatCode: localeConfig.fiatCurrency,
localeInfo: {
primaryLocale: langs[0],
primaryLocales: langs,
country: localeConfig.country
}
}
const response = {
error: null,
locale,
version,
receiptPrintingActive: receipt.active,
cassettes,
twoWayMode: cashOutConfig.active,
zeroConfLimit: cashOutConfig.zeroConfLimit,
reboot,
shutdown,
restartServices,
hasLightning,
receipt,
operatorInfo,
triggers
}
// BACKWARDS_COMPATIBILITY 7.5
// machines before 7.5 expect old compliance
if (!machineVersion || semver.lt(machineVersion, '7.5.0-beta.0')) {
const compatTriggers = complianceTriggers.getBackwardsCompatibleTriggers(triggers)
response.smsVerificationActive = !!compatTriggers.sms
response.smsVerificationThreshold = compatTriggers.sms
response.idCardDataVerificationActive = !!compatTriggers.idCardData
response.idCardDataVerificationThreshold = compatTriggers.idCardData
response.idCardPhotoVerificationActive = !!compatTriggers.idCardPhoto
response.idCardPhotoVerificationThreshold = compatTriggers.idCardPhoto
response.sanctionsVerificationActive = !!compatTriggers.sancations
response.sanctionsVerificationThreshold = compatTriggers.sancations
response.frontCameraVerificationActive = !!compatTriggers.facephoto
response.frontCameraVerificationThreshold = compatTriggers.facephoto
}
// BACKWARDS_COMPATIBILITY 7.4.9
// machines before 7.4.9 expect t&c on poll
if (!machineVersion || semver.lt(machineVersion, '7.4.9')) {
response.terms = config.termsScreenActive && config.termsScreenText ? createTerms(config) : null
}
return res.json(_.assign(response, results))
})
.catch(next)
}
router.get('/', poll)
module.exports = router

14
lib/routes/stateRoutes.js Normal file
View file

@ -0,0 +1,14 @@
const express = require('express')
const router = express.Router()
const helpers = require('../route-helpers')
function stateChange (req, res, next) {
helpers.stateChange(req.deviceId, req.deviceTime, req.body)
.then(() => res.status(200).json({}))
.catch(next)
}
router.post('/', stateChange)
module.exports = router

View file

@ -0,0 +1,35 @@
const express = require('express')
const router = express.Router()
const configManager = require('../new-config-manager')
const plugins = require('../plugins')
function createTerms (terms) {
if (!terms.active || !terms.text) return null
return {
active: terms.active,
title: terms.title,
text: nmd(terms.text),
accept: terms.acceptButtonText,
cancel: terms.cancelButtonText
}
}
function getTermsConditions (req, res, next) {
const deviceId = req.deviceId
const settings = req.settings
const terms = configManager.getTermsConditions(settings.config)
const pi = plugins(settings, deviceId)
return pi.fetchCurrentConfigVersion().then(version => {
return res.json({ terms: createTerms(terms), version })
})
.catch(next)
}
router.get('/', getTermsConditions)
module.exports = router

70
lib/routes/txRoutes.js Normal file
View file

@ -0,0 +1,70 @@
const express = require('express')
const router = express.Router()
const _ = require('lodash/fp')
const dbErrorCodes = require('../db-error-codes')
const helpers = require('../route-helpers')
const logger = require('../logger')
const plugins = require('../plugins')
const Tx = require('../tx')
function httpError (msg, code) {
const err = new Error(msg)
err.name = 'HTTPError'
err.code = code || 500
return err
}
function postTx (req, res, next) {
const pi = plugins(req.settings, req.deviceId)
return Tx.post(_.set('deviceId', req.deviceId, req.body), pi)
.then(tx => {
if (tx.errorCode) {
logger.error(tx.error)
throw httpError(tx.error, 500)
}
return res.json(tx)
})
.catch(err => {
// 204 so that l-m can ignore the error
// this is fine because the request is polled and will be retried if needed.
if (err.code === dbErrorCodes.SERIALIZATION_FAILURE) {
logger.warn('Harmless DB conflict, the query will be retried.')
return res.status(204).json({})
}
if (err instanceof E.StaleTxError) return res.status(409).json({ errorType: 'stale' })
if (err instanceof E.RatchetError) return res.status(409).json({ errorType: 'ratchet' })
throw err
})
.catch(next)
}
function getTx (req, res, next) {
if (req.query.status) {
return helpers.fetchStatusTx(req.params.id, req.query.status)
.then(r => res.json(r))
.catch(next)
}
return next(httpError('Not Found', 404))
}
function getPhoneTx (req, res, next) {
if (req.query.phone) {
return helpers.fetchPhoneTx(req.query.phone)
.then(r => res.json(r))
.catch(next)
}
return next(httpError('Not Found', 404))
}
router.post('/', postTx)
router.get('/:id', getTx)
router.get('/', getPhoneTx)
module.exports = router

View file

@ -0,0 +1,15 @@
const express = require('express')
const router = express.Router()
const plugins = require('../plugins')
function verifyTx (req, res, next) {
const pi = plugins(req.settings, req.deviceId)
pi.verifyTransaction(req.body)
.then(idResult => res.status(200).json(idResult))
.catch(next)
}
router.post('/', verifyTx)
module.exports = router

View file

@ -0,0 +1,15 @@
const express = require('express')
const router = express.Router()
const plugins = require('../plugins')
function verifyUser (req, res, next) {
const pi = plugins(req.settings, req.deviceId)
pi.verifyUser(req.body)
.then(idResult => res.status(200).json(idResult))
.catch(next)
}
router.post('/', verifyUser)
module.exports = router