Feat: refactor routes.js express entrypoint config
Feat: express config script refactor Feat: add state and settingsCache files
This commit is contained in:
parent
c3f8f98c26
commit
85235eaa13
22 changed files with 807 additions and 1 deletions
70
lib/routes/txRoutes.js
Normal file
70
lib/routes/txRoutes.js
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue