Add camelize & populate in Customer
This commit is contained in:
parent
3623e41302
commit
e8f41c1427
3 changed files with 64 additions and 27 deletions
|
|
@ -184,7 +184,7 @@ app.get('/api/customer/:id', (req, res, next) => {
|
|||
})
|
||||
|
||||
/**
|
||||
* Endpoint for patching customer's authorized_override status
|
||||
* Endpoint for patching customer's authorizedOverride status
|
||||
*
|
||||
* Possible values: blocked, verified, automatic
|
||||
*
|
||||
|
|
@ -194,10 +194,10 @@ app.get('/api/customer/:id', (req, res, next) => {
|
|||
* @param {function} next Callback
|
||||
*/
|
||||
app.patch('/api/customer/:id', (req, res, next) => {
|
||||
if (!req.query.authorized_override) return res.status(400).send({Error: 'Requires authorized'})
|
||||
if (!req.query.authorizedOverride) return res.status(400).send({Error: 'Requires authorized'})
|
||||
|
||||
return customers.patch(req.params.id, {
|
||||
authorized_override: req.query.authorized_override
|
||||
authorized_override: req.query.authorizedOverride
|
||||
})
|
||||
.then(r => res.send(r))
|
||||
.catch(() => res.status(404).send({Error: 'Not found'}))
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ const _ = require('lodash/fp')
|
|||
const BN = require('./bn')
|
||||
const anonymous = require('../lib/constants').anonymousCustomer
|
||||
const NUM_RESULTS = 20
|
||||
const camelize = require('camelize')
|
||||
|
||||
function add (customer) {
|
||||
const sql = 'insert into customers (id, phone, phone_at) values ($1, $2, now()) returning *'
|
||||
|
|
@ -37,6 +38,9 @@ function patch (id, values) {
|
|||
function getById (id) {
|
||||
const sql = 'select * from customers where id=$1'
|
||||
return db.oneOrNone(sql, [id])
|
||||
.then(customer => {
|
||||
return (customer) ? format(customer) : null
|
||||
})
|
||||
}
|
||||
|
||||
function getDailyVolume (id) {
|
||||
|
|
@ -52,6 +56,38 @@ function getDailyVolume (id) {
|
|||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Format and populate fields
|
||||
* for customer record
|
||||
*
|
||||
* @function format
|
||||
*
|
||||
* @param {object} customer Customer object
|
||||
* @returns {object} Customer camelized & populated with computed fields
|
||||
*/
|
||||
function format (customer) {
|
||||
/**
|
||||
* Populate with status field
|
||||
*
|
||||
*/
|
||||
const status = _.maxBy('value', [{
|
||||
label: 'Phone',
|
||||
value: customer.phone_at
|
||||
}, {
|
||||
label: 'ID card',
|
||||
value: customer.id_card_at
|
||||
}, {
|
||||
label: 'Front facing camera',
|
||||
value: customer.front_facing_cam_at
|
||||
}, {
|
||||
label: 'ID card image',
|
||||
value: customer.id_card_image_at
|
||||
}])
|
||||
customer.status = status.label
|
||||
|
||||
return camelize(customer)
|
||||
}
|
||||
|
||||
/**
|
||||
* Query all customers
|
||||
*
|
||||
|
|
@ -62,20 +98,13 @@ function getDailyVolume (id) {
|
|||
* @returns {array} Array of customers populated with status field
|
||||
*/
|
||||
function batch () {
|
||||
const sql = `select
|
||||
CASE GREATEST(
|
||||
phone_at,
|
||||
id_card_at,
|
||||
front_facing_cam_at,
|
||||
id_card_image_at
|
||||
)
|
||||
WHEN phone_at THEN 'Phone'
|
||||
WHEN id_card_at THEN 'ID card'
|
||||
WHEN front_facing_cam_at THEN 'Front facing camera'
|
||||
WHEN id_card_image_at THEN 'ID card image'
|
||||
END AS status, * from customers
|
||||
const sql = `select * from customers
|
||||
where id != $1
|
||||
order by created desc limit $2`
|
||||
return db.any(sql, [ anonymous.uuid, NUM_RESULTS ])
|
||||
.then(customers => {
|
||||
return _.map(customer => format(customer), customers)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { add, get, batch, getById, patch}
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue