feat: add discount source column to transaction tables

fix: move individual discounts to phone code response instead of poller
This commit is contained in:
Sérgio Salgado 2021-08-06 18:41:04 +01:00 committed by Josh Harvey
parent e837ee3e7a
commit b0fa62a9f3
6 changed files with 32 additions and 12 deletions

View file

@ -8,7 +8,7 @@ const E = require('../error')
const PENDING_INTERVAL_MS = 60 * T.minutes
const massageFields = ['direction', 'cryptoNetwork', 'bills', 'blacklisted', 'addressReuse']
const massageFields = ['direction', 'cryptoNetwork', 'bills', 'blacklisted', 'addressReuse', 'promoCodeApplied']
const massageUpdateFields = _.concat(massageFields, 'cryptoAtoms')
const massage = _.flow(_.omit(massageFields),

View file

@ -57,7 +57,7 @@ function addDbBills (tx) {
function toDb (tx) {
const massager = _.flow(convertBigNumFields, addDbBills,
_.omit(['direction', 'bills']), _.mapKeys(convertField))
_.omit(['direction', 'bills', 'promoCodeApplied']), _.mapKeys(convertField))
return massager(tx)
}

View file

@ -29,7 +29,7 @@ function getNumberOfAvailablePromoCodes () {
}
function getAvailableIndividualDiscounts () {
const sql = `SELECT * from individual_discounts WHERE soft_deleted=false`
const sql = `SELECT * FROM individual_discounts WHERE soft_deleted=false`
return db.any(sql).then(res => _.map(it => ({
id: it.id,
customerId: it.customer_id,
@ -37,6 +37,20 @@ function getAvailableIndividualDiscounts () {
}), res))
}
function getCustomerIndividualDiscounts (customerId) {
const sql = `SELECT * FROM individual_discounts WHERE customer_id=$1 LIMIT 1`
return db.oneOrNone(sql, [customerId]).then(res => {
if (!_.isNil(res)) {
return {
id: res.id,
customerId: res.customer_id,
discount: res.discount
}
}
return res
})
}
function createIndividualDiscount (customerId, discount) {
const sql = `INSERT INTO individual_discounts (id, customer_id, discount) VALUES ($1, $2, $3)`
return db.none(sql, [uuid.v4(), customerId, discount])
@ -54,6 +68,7 @@ module.exports = {
deletePromoCode,
getNumberOfAvailablePromoCodes,
getAvailableIndividualDiscounts,
getCustomerIndividualDiscounts,
createIndividualDiscount,
deleteIndividualDiscount
}

View file

@ -230,7 +230,6 @@ function plugins (settings, deviceId) {
const pingPromise = recordPing(deviceTime, machineVersion, machineModel)
const currentConfigVersionPromise = fetchCurrentConfigVersion()
const currentAvailablePromoCodes = loyalty.getNumberOfAvailablePromoCodes()
const currentAvailableIndividualDiscounts = loyalty.getAvailableIndividualDiscounts()
const timezoneObj = { utcOffset: timezone[0], dstOffset: timezone[1] }
const promises = [
@ -242,8 +241,7 @@ function plugins (settings, deviceId) {
tickerPromises,
balancePromises,
testnetPromises,
currentAvailablePromoCodes,
currentAvailableIndividualDiscounts
currentAvailablePromoCodes
)
return Promise.all(promises)
@ -257,8 +255,7 @@ function plugins (settings, deviceId) {
const testNets = arr.slice(2 * cryptoCodesCount + 4, arr.length - 2)
const coinParams = _.zip(cryptoCodes, testNets)
const coinsWithoutRate = _.map(mapCoinSettings, coinParams)
const areThereAvailablePromoCodes = arr[arr.length - 2] > 0
const individualDiscounts = arr[arr.length - 1]
const areThereAvailablePromoCodes = arr[arr.length - 1] > 0
return {
cassettes,
@ -267,8 +264,7 @@ function plugins (settings, deviceId) {
coins: _.zipWith(_.assign, coinsWithoutRate, tickers),
configVersion,
areThereAvailablePromoCodes,
timezone: tz,
individualDiscounts
timezone: tz
}
})
}

View file

@ -11,6 +11,7 @@ const httpError = require('../route-helpers').httpError
const plugins = require('../plugins')
const Tx = require('../tx')
const respond = require('../respond')
const loyalty = require('../loyalty')
function addOrUpdateCustomer (req) {
const customerData = req.body
@ -37,13 +38,18 @@ function addOrUpdateCustomer (req) {
if (_.isEmpty(patch)) return customer
return customers.update(customer.id, patch)
})
}).then(customer => {
})
.then(customer => {
return Tx.customerHistory(customer.id, maxDaysThreshold)
.then(result => {
customer.txHistory = result
return customer
})
})
.then(customer => {
return loyalty.getCustomerIndividualDiscounts(customer.id)
.then(discount => ({ ...customer, discount }))
})
}
function getCustomerWithPhoneCode (req, res, next) {

View file

@ -8,7 +8,10 @@ exports.up = function (next) {
discount SMALLINT NOT NULL,
soft_deleted BOOLEAN DEFAULT false
)`,
`CREATE UNIQUE INDEX uq_individual_discount ON individual_discounts (customer_id) WHERE NOT soft_deleted`
`CREATE UNIQUE INDEX uq_individual_discount ON individual_discounts (customer_id) WHERE NOT soft_deleted`,
`CREATE TYPE discount_source AS ENUM('individualDiscount', 'promoCode')`,
`ALTER TABLE cash_in_txs ADD COLUMN discount_source discount_source`,
`ALTER TABLE cash_out_txs ADD COLUMN discount_source discount_source`
]
db.multi(sql, next)