From e6ffcacb789eca56f8ac8db70cf0fc9c9833a83c Mon Sep 17 00:00:00 2001 From: Rafael Taranto Date: Wed, 28 May 2025 07:11:37 +0100 Subject: [PATCH] feat: reuse last address option --- packages/server/lib/customers.js | 6 ++++++ packages/server/lib/routes/customerRoutes.js | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/server/lib/customers.js b/packages/server/lib/customers.js index afb687ae..5e768e6d 100644 --- a/packages/server/lib/customers.js +++ b/packages/server/lib/customers.js @@ -1009,6 +1009,11 @@ function addExternalCompliance(customerId, service, id) { return db.none(sql, [customerId, id, service]) } +function getLastUsedAddress(id, cryptoCode) { + const sql = `SELECT to_address FROM cash_in_txs WHERE customer_id=$1 AND crypto_code=$2 AND fiat > 0 ORDER BY created DESC LIMIT 1` + return db.oneOrNone(sql, [id, cryptoCode]).then(it => it?.to_address) +} + module.exports = { add, addWithEmail, @@ -1035,4 +1040,5 @@ module.exports = { updateLastAuthAttempt, addExternalCompliance, checkExternalCompliance, + getLastUsedAddress, } diff --git a/packages/server/lib/routes/customerRoutes.js b/packages/server/lib/routes/customerRoutes.js index 7e387bf0..b4f8aa55 100644 --- a/packages/server/lib/routes/customerRoutes.js +++ b/packages/server/lib/routes/customerRoutes.js @@ -311,7 +311,13 @@ function getExternalComplianceLink(req, res, next) { .then(url => respond(req, res, { url })) } -function addOrUpdateCustomer(customerData, deviceId, config, isEmailAuth) { +function addOrUpdateCustomer( + customerData, + deviceId, + config, + isEmailAuth, + cryptoCode, +) { const triggers = configManager.getTriggers(config) const maxDaysThreshold = complianceTriggers.maxDaysThreshold(triggers) @@ -346,6 +352,14 @@ function addOrUpdateCustomer(customerData, deviceId, config, isEmailAuth) { .getCustomerActiveIndividualDiscount(customer.id) .then(discount => ({ ...customer, discount })) }) + .then(customer => { + if (!cryptoCode) return customer + return customers + .getLastUsedAddress(customer.id, cryptoCode) + .then(lastUsedAddress => { + return { ...customer, lastUsedAddress } + }) + }) } function getOrAddCustomerPhone(req, res, next) { @@ -354,6 +368,7 @@ function getOrAddCustomerPhone(req, res, next) { const pi = plugins(req.settings, deviceId) const phone = req.body.phone + const cryptoCode = req.query.cryptoCode return pi .getPhoneCode(phone) @@ -363,6 +378,7 @@ function getOrAddCustomerPhone(req, res, next) { deviceId, req.settings.config, false, + cryptoCode, ).then(customer => respond(req, res, { code, customer })) }) .catch(err => { @@ -375,6 +391,7 @@ function getOrAddCustomerPhone(req, res, next) { function getOrAddCustomerEmail(req, res, next) { const deviceId = req.deviceId const customerData = req.body + const cryptoCode = req.query.cryptoCode const pi = plugins(req.settings, req.deviceId) const email = req.body.email @@ -387,6 +404,7 @@ function getOrAddCustomerEmail(req, res, next) { deviceId, req.settings.config, true, + cryptoCode, ).then(customer => respond(req, res, { code, customer })) }) .catch(err => {