Convert fiat totals to BTC and add to total amount
This commit is contained in:
@@ -16,11 +16,9 @@ export default class AddExpenseItemComponent extends Component {
|
|||||||
|
|
||||||
defaultDate = moment().startOf('hour').toDate();
|
defaultDate = moment().startOf('hour').toDate();
|
||||||
|
|
||||||
// TODO fetch/apply exchange rate to (W)BTC
|
|
||||||
currencies = [
|
currencies = [
|
||||||
{ code: 'EUR' },
|
{ code: 'EUR' },
|
||||||
{ code: 'USD' },
|
{ code: 'USD' }
|
||||||
{ code: 'GBP' }
|
|
||||||
];
|
];
|
||||||
|
|
||||||
get isValidAmount () {
|
get isValidAmount () {
|
||||||
|
|||||||
@@ -67,6 +67,19 @@ export default class AddReimbursementComponent extends Component {
|
|||||||
anchor.scrollIntoView();
|
anchor.scrollIntoView();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateTotalAmountFromFiat() {
|
||||||
|
let btcAmount = parseFloat(this.total);
|
||||||
|
|
||||||
|
if (this.exchangeRates.btceur > 0 && this.totalEUR > 0) {
|
||||||
|
btcAmount += (this.totalEUR / this.exchangeRates.btceur);
|
||||||
|
}
|
||||||
|
if (this.exchangeRates.btcusd > 0 && this.totalUSD > 0) {
|
||||||
|
btcAmount += (this.totalUSD / this.exchangeRates.btcusd);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.total = btcAmount.toFixed(8);
|
||||||
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
showExpenseForm () {
|
showExpenseForm () {
|
||||||
this.expenseFormVisible = true;
|
this.expenseFormVisible = true;
|
||||||
@@ -76,12 +89,14 @@ export default class AddReimbursementComponent extends Component {
|
|||||||
@action
|
@action
|
||||||
addExpenseItem (expenseItem) {
|
addExpenseItem (expenseItem) {
|
||||||
this.expenses.pushObject(expenseItem);
|
this.expenses.pushObject(expenseItem);
|
||||||
|
this.updateTotalAmountFromFiat();
|
||||||
this.expenseFormVisible = false;
|
this.expenseFormVisible = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
removeExpenseItem (expenseItem) {
|
removeExpenseItem (expenseItem) {
|
||||||
this.expenses.removeObject(expenseItem);
|
this.expenses.removeObject(expenseItem);
|
||||||
|
this.updateTotalAmountFromFiat();
|
||||||
|
|
||||||
if (this.expenses.length === 0) {
|
if (this.expenses.length === 0) {
|
||||||
this.expenseFormVisible = true;
|
this.expenseFormVisible = true;
|
||||||
|
|||||||
@@ -2,6 +2,19 @@ import Application from '../app';
|
|||||||
import config from '../config/environment';
|
import config from '../config/environment';
|
||||||
import { setApplication } from '@ember/test-helpers';
|
import { setApplication } from '@ember/test-helpers';
|
||||||
import { start } from 'ember-qunit';
|
import { start } from 'ember-qunit';
|
||||||
|
import fetchMock from 'fetch-mock';
|
||||||
|
|
||||||
|
fetchMock.get(`${config.corsProxy}https://www.bitstamp.net/api/v2/ticker/btceur/`, {
|
||||||
|
"high": "9258.43", "last": "9165.14", "timestamp": "1601455909", "bid":
|
||||||
|
"9159.93", "vwap": "9167.57", "volume": "1542.54764854", "low": "9080.20",
|
||||||
|
"ask": "9165.14", "open": "9240.84"
|
||||||
|
});
|
||||||
|
|
||||||
|
fetchMock.get(`${config.corsProxy}https://www.bitstamp.net/api/v2/ticker/btcusd/`, {
|
||||||
|
"high": "10865.00", "last": "10714.62", "timestamp": "1601455914", "bid":
|
||||||
|
"10711.31", "vwap": "10749.70", "volume": "4460.32091975", "low": "10636.66",
|
||||||
|
"ask": "10715.99", "open": "10840.00"
|
||||||
|
});
|
||||||
|
|
||||||
setApplication(Application.create(config.APP));
|
setApplication(Application.create(config.APP));
|
||||||
|
|
||||||
|
|||||||
@@ -39,4 +39,12 @@ module('Unit | Component | add-reimbursement', function(hooks) {
|
|||||||
assert.equal(component.totalEUR, '71');
|
assert.equal(component.totalEUR, '71');
|
||||||
assert.equal(component.totalUSD, '59');
|
assert.equal(component.totalUSD, '59');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('#updateTotalAmountFromFiat', async function(assert) {
|
||||||
|
let component = createComponent('component:add-reimbursement');
|
||||||
|
component.expenses = expenses;
|
||||||
|
await component.exchangeRates.fetchRates();
|
||||||
|
component.updateTotalAmountFromFiat();
|
||||||
|
assert.equal(component.total, '0.01323322', 'converts EUR and USD totals to BTC and rounds to 8 decimals');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,21 +1,5 @@
|
|||||||
import { module, test } from 'qunit';
|
import { module, test } from 'qunit';
|
||||||
import { setupTest } from 'ember-qunit';
|
import { setupTest } from 'ember-qunit';
|
||||||
import config from 'kredits-web/config/environment';
|
|
||||||
import fetchMock from 'fetch-mock';
|
|
||||||
|
|
||||||
const corsProxy = config.corsProxy;
|
|
||||||
|
|
||||||
fetchMock.get(`${corsProxy}https://www.bitstamp.net/api/v2/ticker/btceur/`, {
|
|
||||||
"high": "9258.43", "last": "9165.14", "timestamp": "1601455909", "bid":
|
|
||||||
"9159.93", "vwap": "9167.57", "volume": "1542.54764854", "low": "9080.20",
|
|
||||||
"ask": "9165.14", "open": "9240.84"
|
|
||||||
});
|
|
||||||
|
|
||||||
fetchMock.get(`${corsProxy}https://www.bitstamp.net/api/v2/ticker/btcusd/`, {
|
|
||||||
"high": "10865.00", "last": "10714.62", "timestamp": "1601455914", "bid":
|
|
||||||
"10711.31", "vwap": "10749.70", "volume": "4460.32091975", "low": "10636.66",
|
|
||||||
"ask": "10715.99", "open": "10840.00"
|
|
||||||
});
|
|
||||||
|
|
||||||
module('Unit | Service | exchange-rates', function(hooks) {
|
module('Unit | Service | exchange-rates', function(hooks) {
|
||||||
setupTest(hooks);
|
setupTest(hooks);
|
||||||
|
|||||||
Reference in New Issue
Block a user