From b0fa62a9f37663a25d9a54ece76d07d2b63409d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Fri, 6 Aug 2021 18:41:04 +0100 Subject: [PATCH] feat: add discount source column to transaction tables fix: move individual discounts to phone code response instead of poller --- lib/cash-in/cash-in-low.js | 2 +- lib/cash-out/cash-out-helper.js | 2 +- lib/loyalty.js | 17 ++++++++++++++++- lib/plugins.js | 10 +++------- lib/routes/phoneCodeRoutes.js | 8 +++++++- .../1626891847835-add-individual-discounts.js | 5 ++++- 6 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/cash-in/cash-in-low.js b/lib/cash-in/cash-in-low.js index 79e726b5..a3439df3 100644 --- a/lib/cash-in/cash-in-low.js +++ b/lib/cash-in/cash-in-low.js @@ -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), diff --git a/lib/cash-out/cash-out-helper.js b/lib/cash-out/cash-out-helper.js index 0ef9747a..2620c23c 100644 --- a/lib/cash-out/cash-out-helper.js +++ b/lib/cash-out/cash-out-helper.js @@ -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) } diff --git a/lib/loyalty.js b/lib/loyalty.js index 7883b907..9c9a8ce5 100644 --- a/lib/loyalty.js +++ b/lib/loyalty.js @@ -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 } diff --git a/lib/plugins.js b/lib/plugins.js index 4c7dd893..5c70885a 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -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 } }) } diff --git a/lib/routes/phoneCodeRoutes.js b/lib/routes/phoneCodeRoutes.js index 441a3b47..bd03c3c9 100644 --- a/lib/routes/phoneCodeRoutes.js +++ b/lib/routes/phoneCodeRoutes.js @@ -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) { diff --git a/migrations/1626891847835-add-individual-discounts.js b/migrations/1626891847835-add-individual-discounts.js index 8d4002a4..14d7301e 100644 --- a/migrations/1626891847835-add-individual-discounts.js +++ b/migrations/1626891847835-add-individual-discounts.js @@ -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)