fix: migrate accounts and default cash-out commissions

This commit is contained in:
José Oliveira 2021-04-21 16:24:46 +01:00 committed by Rafael Taranto
parent 58b74a6425
commit b1a2c20e87
2 changed files with 15 additions and 9 deletions

View file

@ -81,6 +81,11 @@ function migrateCommissions (config) {
}
const { global, scoped } = getConfigFields(_.keys(codes), config)
const defaultCashOutCommissions = { code: 'cashOutCommission', value: 0, scope: global[0].scope }
const isCashOutDisabled =
_.isEmpty(_.filter(commissionElement => commissionElement.code === 'cashOutCommission', global))
const globalWithDefaults =
isCashOutDisabled ? _.concat(global, defaultCashOutCommissions) : global
const machineAndCryptoScoped = scoped.filter(
f => f.scope.machine !== GLOBAL_SCOPE.machine && f.scope.crypto.length === 1
@ -112,7 +117,7 @@ function migrateCommissions (config) {
const allCommissionsOverrides = withCryptoScoped.concat(filteredMachineScoped)
return {
..._.fromPairs(global.map(f => [`commissions_${codes[f.code]}`, f.value])),
..._.fromPairs(globalWithDefaults.map(f => [`commissions_${codes[f.code]}`, f.value])),
...(allCommissionsOverrides.length > 0 && {
commissions_overrides: allCommissionsOverrides.map(s => ({
..._.fromPairs(s.values.map(f => [codes[f.code], f.value])),

View file

@ -1,34 +1,35 @@
const db = require('./db')
const settingsLoader = require('../lib/admin/settings-loader')
const machineLoader = require('../lib/machine-loader')
const { saveConfig, saveAccounts } = require('../lib/new-settings-loader')
const { saveConfig, saveAccounts, loadLatest } = require('../lib/new-settings-loader')
const { migrate } = require('../lib/config-migration')
const OLD_SETTINGS_LOADER_SCHEMA_VERSION = 1
module.exports.up = function (next) {
function migrateConfig(settings) {
function migrateConfig (settings) {
const newSettings = migrate(settings.config, settings.accounts)
return Promise.all([
saveConfig(newSettings.config),
saveAccounts(newSettings.accounts)
])
.then(() => next())
.then(() => next())
}
settingsLoader.loadLatest(false)
loadLatest(OLD_SETTINGS_LOADER_SCHEMA_VERSION)
.then(async settings => ({
settings,
machines: await machineLoader.getMachineNames(settings.config)
}))
.then(({ settings, machines }) => {
const sql = machines
const sql = machines
? machines.map(m => `update devices set name = '${m.name}' where device_id = '${m.deviceId}'`)
: []
return db.multi(sql, () => migrateConfig(settings))
})
.catch(err => {
if (err.message = 'lamassu-server is not configured')
if (err.message === 'lamassu-server is not configured') {
next()
}
console.log(err.message)
})
}