fix: commission overrides calculation

This commit is contained in:
Nikola Ubavic 2022-02-18 16:59:52 +01:00
parent a7ee694169
commit 824922b7ba

View file

@ -45,54 +45,46 @@ const Commissions = ({ name: SCREEN_KEY, id: deviceId }) => {
} }
const getMachineCommissions = () => { const getMachineCommissions = () => {
if (loading || !deviceId || !config) { if (loading || !deviceId || !config) return []
return []
}
const commissions = {}
// first, get general non overridden commissions const overrides = config.overrides
const makeInfo = x => ? R.concat(
(commissions[R.prop('code')(x)] = { R.filter(R.propEq('machine', 'ALL_MACHINES'), config.overrides),
code: x.code, R.filter(R.propEq('machine', deviceId), config.overrides)
name: x.display, )
cashIn: config.cashIn,
cashOut: config.cashOut,
fixedFee: config.fixedFee,
minimumTx: config.minimumTx
})
R.forEach(makeInfo)(data.cryptoCurrencies)
// second, get overrides for all machines
const isId = id => R.propEq('machine', id)
const generalOverrides = config.overrides
? R.filter(isId('ALL_MACHINES'))(config.overrides)
: [] : []
const overrideInfo = o => { return R.map(
commissions[o.cryptoCurrencies[0]].cashIn = o.cashIn coin =>
commissions[o.cryptoCurrencies[0]].cashOut = o.cashOut R.reduce(
commissions[o.cryptoCurrencies[0]].fixedFee = o.fixedFee R.mergeDeepRight,
commissions[o.cryptoCurrencies[0]].minimumTx = o.minimumTx {
} code: coin.code,
R.forEach(overrideInfo)(generalOverrides) name: coin.display,
cashIn: config.cashIn,
// third, get overrides for this machine cashOut: config.cashOut,
const machineOverrides = config.overrides fixedFee: config.fixedFee,
? R.filter(isId(deviceId))(config.overrides) minimumTx: config.minimumTx
: [] },
R.forEach(overrideInfo)(machineOverrides) R.project(
['cashIn', 'cashOut', 'fixedFee', 'minimumTx'],
// in the end, the machine specific overrides overwrite the less general ALL_MACHINE overrides or the general overrides R.filter(
return R.values(commissions) o =>
R.includes(coin.code, o.cryptoCurrencies) ||
R.includes('ALL_COINS', o.cryptoCurrencies),
overrides
)
)
),
data.cryptoCurrencies
)
} }
const machineCommissions = getMachineCommissions()
return ( return (
<EditableTable <EditableTable
name="overrides" name="overrides"
save={saveOverrides} save={saveOverrides}
data={machineCommissions} data={getMachineCommissions()}
elements={overrides(currency)} elements={overrides(currency)}
/> />
) )