fix: migrate accounts and default cash-out commissions
This commit is contained in:
parent
58b74a6425
commit
b1a2c20e87
2 changed files with 15 additions and 9 deletions
|
|
@ -81,6 +81,11 @@ function migrateCommissions (config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const { global, scoped } = getConfigFields(_.keys(codes), 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(
|
const machineAndCryptoScoped = scoped.filter(
|
||||||
f => f.scope.machine !== GLOBAL_SCOPE.machine && f.scope.crypto.length === 1
|
f => f.scope.machine !== GLOBAL_SCOPE.machine && f.scope.crypto.length === 1
|
||||||
|
|
@ -112,7 +117,7 @@ function migrateCommissions (config) {
|
||||||
const allCommissionsOverrides = withCryptoScoped.concat(filteredMachineScoped)
|
const allCommissionsOverrides = withCryptoScoped.concat(filteredMachineScoped)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
..._.fromPairs(global.map(f => [`commissions_${codes[f.code]}`, f.value])),
|
..._.fromPairs(globalWithDefaults.map(f => [`commissions_${codes[f.code]}`, f.value])),
|
||||||
...(allCommissionsOverrides.length > 0 && {
|
...(allCommissionsOverrides.length > 0 && {
|
||||||
commissions_overrides: allCommissionsOverrides.map(s => ({
|
commissions_overrides: allCommissionsOverrides.map(s => ({
|
||||||
..._.fromPairs(s.values.map(f => [codes[f.code], f.value])),
|
..._.fromPairs(s.values.map(f => [codes[f.code], f.value])),
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,35 @@
|
||||||
const db = require('./db')
|
const db = require('./db')
|
||||||
const settingsLoader = require('../lib/admin/settings-loader')
|
|
||||||
const machineLoader = require('../lib/machine-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 { migrate } = require('../lib/config-migration')
|
||||||
|
|
||||||
|
const OLD_SETTINGS_LOADER_SCHEMA_VERSION = 1
|
||||||
|
|
||||||
module.exports.up = function (next) {
|
module.exports.up = function (next) {
|
||||||
function migrateConfig(settings) {
|
function migrateConfig (settings) {
|
||||||
const newSettings = migrate(settings.config, settings.accounts)
|
const newSettings = migrate(settings.config, settings.accounts)
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
saveConfig(newSettings.config),
|
saveConfig(newSettings.config),
|
||||||
saveAccounts(newSettings.accounts)
|
saveAccounts(newSettings.accounts)
|
||||||
])
|
])
|
||||||
.then(() => next())
|
.then(() => next())
|
||||||
}
|
}
|
||||||
|
|
||||||
settingsLoader.loadLatest(false)
|
loadLatest(OLD_SETTINGS_LOADER_SCHEMA_VERSION)
|
||||||
.then(async settings => ({
|
.then(async settings => ({
|
||||||
settings,
|
settings,
|
||||||
machines: await machineLoader.getMachineNames(settings.config)
|
machines: await machineLoader.getMachineNames(settings.config)
|
||||||
}))
|
}))
|
||||||
.then(({ settings, machines }) => {
|
.then(({ settings, machines }) => {
|
||||||
const sql = machines
|
const sql = machines
|
||||||
? machines.map(m => `update devices set name = '${m.name}' where device_id = '${m.deviceId}'`)
|
? machines.map(m => `update devices set name = '${m.name}' where device_id = '${m.deviceId}'`)
|
||||||
: []
|
: []
|
||||||
return db.multi(sql, () => migrateConfig(settings))
|
return db.multi(sql, () => migrateConfig(settings))
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
if (err.message = 'lamassu-server is not configured')
|
if (err.message === 'lamassu-server is not configured') {
|
||||||
next()
|
next()
|
||||||
|
}
|
||||||
console.log(err.message)
|
console.log(err.message)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue