feat: customer auth via email
This commit is contained in:
parent
92a3f16c80
commit
ab304093f3
22 changed files with 252 additions and 27 deletions
|
|
@ -20,6 +20,9 @@ const machineLoader = require('../machine-loader')
|
|||
const { loadLatestConfig } = require('../new-settings-loader')
|
||||
const customInfoRequestQueries = require('../new-admin/services/customInfoRequests')
|
||||
const T = require('../time')
|
||||
const plugins = require('../plugins')
|
||||
const Tx = require('../tx')
|
||||
const loyalty = require('../loyalty')
|
||||
|
||||
function updateCustomerCustomInfoRequest (customerId, patch, req, res) {
|
||||
if (_.isNil(patch.data)) {
|
||||
|
|
@ -185,6 +188,70 @@ function sendSmsReceipt (req, res, next) {
|
|||
})
|
||||
}
|
||||
|
||||
function addOrUpdateCustomer (customerData, config, isEmailAuth) {
|
||||
const triggers = configManager.getTriggers(config)
|
||||
const maxDaysThreshold = complianceTriggers.maxDaysThreshold(triggers)
|
||||
|
||||
const customerKey = isEmailAuth ? customerData.email : customerData.phone
|
||||
const getFunc = isEmailAuth ? customers.getWithEmail : customers.get
|
||||
const addFunction = isEmailAuth ? customers.addWithEmail : customers.add
|
||||
|
||||
return getFunc(customerKey)
|
||||
.then(customer => {
|
||||
if (customer) return customer
|
||||
|
||||
return addFunction(customerData)
|
||||
})
|
||||
.then(customer => customers.getById(customer.id))
|
||||
.then(customer => {
|
||||
return Tx.customerHistory(customer.id, maxDaysThreshold)
|
||||
.then(result => {
|
||||
customer.txHistory = result
|
||||
return customer
|
||||
})
|
||||
})
|
||||
.then(customer => {
|
||||
return loyalty.getCustomerActiveIndividualDiscount(customer.id)
|
||||
.then(discount => ({ ...customer, discount }))
|
||||
})
|
||||
}
|
||||
|
||||
function getOrAddCustomerPhone (req, res, next) {
|
||||
const customerData = req.body
|
||||
|
||||
const pi = plugins(req.settings, req.deviceId)
|
||||
const phone = req.body.phone
|
||||
|
||||
return pi.getPhoneCode(phone)
|
||||
.then(code => {
|
||||
return addOrUpdateCustomer(customerData, req.settings.config, false)
|
||||
.then(customer => respond(req, res, { code, customer }))
|
||||
})
|
||||
.catch(err => {
|
||||
if (err.name === 'BadNumberError') throw httpError('Bad number', 401)
|
||||
throw err
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function getOrAddCustomerEmail (req, res, next) {
|
||||
const customerData = req.body
|
||||
|
||||
const pi = plugins(req.settings, req.deviceId)
|
||||
const email = req.body.email
|
||||
|
||||
return pi.getEmailCode(email)
|
||||
.then(code => {
|
||||
return addOrUpdateCustomer(customerData, req.settings.config, true)
|
||||
.then(customer => respond(req, res, { code, customer }))
|
||||
})
|
||||
.catch(err => {
|
||||
if (err.name === 'BadNumberError') throw httpError('Bad number', 401)
|
||||
throw err
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
router.patch('/:id', updateCustomer)
|
||||
router.patch('/:id/sanctions', triggerSanctions)
|
||||
router.patch('/:id/block', triggerBlock)
|
||||
|
|
@ -192,5 +259,7 @@ router.patch('/:id/suspend', triggerSuspend)
|
|||
router.patch('/:id/photos/idcarddata', updateIdCardData)
|
||||
router.patch('/:id/:txId/photos/customerphoto', updateTxCustomerPhoto)
|
||||
router.post('/:id/smsreceipt', sendSmsReceipt)
|
||||
router.post('/phone_code', getOrAddCustomerPhone)
|
||||
router.post('/email_code', getOrAddCustomerEmail)
|
||||
|
||||
module.exports = router
|
||||
|
|
|
|||
|
|
@ -66,8 +66,19 @@ function getPhoneTx (req, res, next) {
|
|||
return next(httpError('Not Found', 404))
|
||||
}
|
||||
|
||||
function getEmailTx (req, res, next) {
|
||||
if (req.query.email) {
|
||||
return helpers.fetchEmailTx(req.query.email)
|
||||
.then(r => res.json(r))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
return next(httpError('Not Found', 404))
|
||||
}
|
||||
|
||||
router.post('/', postTx)
|
||||
router.get('/:id', getTx)
|
||||
router.get('/', getPhoneTx)
|
||||
router.get('/', getEmailTx)
|
||||
|
||||
module.exports = { postTx, getTx, getPhoneTx, router }
|
||||
module.exports = { postTx, getTx, getPhoneTx, getEmailTx, router }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue