Add sms/email notifications on transactions (#198)

* Add sms/email notifications on transactions

* Add configuration for transaction notification

* Add notification on provision address error

* Remove unneeded noop from promises
This commit is contained in:
Rafael Taranto 2018-11-06 10:49:12 -02:00 committed by Josh Harvey
parent 272127518d
commit 809bf5a2a9
7 changed files with 155 additions and 5 deletions

View file

@ -18,6 +18,7 @@ const sms = require('./sms')
const email = require('./email')
const cashOutHelper = require('./cash-out/cash-out-helper')
const machineLoader = require('./machine-loader')
const customers = require('./customers')
const coinUtils = require('./coin-utils')
const mapValuesWithKey = _.mapValues.convert({cap: false})
@ -58,6 +59,11 @@ function plugins (settings, deviceId) {
return rates
}
function transactionNotificationsEnabled () {
const config = configManager.unscoped(settings.config)
return config.transactionNotificationsEnabled
}
function notificationsEnabled () {
const config = configManager.unscoped(settings.config)
return config.notificationsEnabled
@ -324,6 +330,73 @@ function plugins (settings, deviceId) {
})
}
function notifyOperator (tx, rec) {
if (!transactionNotificationsEnabled()) return Promise.resolve()
const isCashOut = tx.direction === 'cashOut'
const zeroConf = isZeroConf(tx)
// 0-conf cash-out should only send notification on redemption
if (zeroConf && isCashOut && !rec.isRedemption && !rec.error) return Promise.resolve()
if (!zeroConf && rec.isRedemption) return sendRedemptionMessage(tx.id, rec.error)
const customerPromise = tx.customerId ? customers.getById(tx.customerId) : Promise.resolve({})
return Promise.all([machineLoader.getMachineName(tx.deviceId), customerPromise])
.then(([machineName, customer]) => {
const direction = isCashOut ? 'Cash Out' : 'Cash In'
const crypto = `${coinUtils.toUnit(tx.cryptoAtoms, tx.cryptoCode)} ${tx.cryptoCode}`
const fiat = `${tx.fiat} ${tx.fiatCode}`
const customerName = customer.name || customer.id
const phone = customer.phone ? `- Phone: ${customer.phone}` : ''
let status
if (rec.error) {
status = `Error - ${rec.error}`
} else {
status = !isCashOut ? 'Successful' : !rec.isRedemption
? 'Successful & awaiting redemption' : 'Successful & dispensed'
}
const body = `
- Transaction ID: ${tx.id}
- Status: ${status}
- Machine name: ${machineName}
- ${direction}
- ${fiat}
- ${crypto}
- Customer: ${customerName}
${phone}
`
const subject = `A transaction just happened`
return {
sms: {
body: `${subject} - ${status}`
},
email: {
subject,
body
}
}
})
.then(sendTransactionMessage)
}
function sendRedemptionMessage (txId, error) {
const subject = `Here's an update on transaction ${txId}`
const body = error ? `Error: ${error}` : 'It was just dispensed successfully'
const rec = {
sms: {
body: `${subject} - ${body}`
},
email: { subject, body }
}
return sendTransactionMessage(rec)
}
function pong () {
db.none('insert into server_events (event_type) values ($1)', ['ping'])
.catch(logger.error)
@ -495,6 +568,16 @@ function plugins (settings, deviceId) {
return Promise.all(promises)
}
function sendTransactionMessage (rec) {
const config = configManager.unscoped(settings.config)
let promises = []
if (config.transactionNotificationsEmailEnabled) promises.push(email.sendMessage(settings, rec))
if (config.transactionNotificationsSMSEnabled) promises.push(sms.sendMessage(settings, rec))
return Promise.all(promises)
}
function checkDevicesCashBalances (fiatCode, devices) {
return _.map(device => checkDeviceCashBalances(fiatCode, device), devices)
}
@ -657,7 +740,8 @@ function plugins (settings, deviceId) {
buildAvailableCassettes,
buy,
sell,
notificationsEnabled
notificationsEnabled,
notifyOperator
}
}