Skip to content

Commit

Permalink
fix(currency): recalc tips on rate change (#3137)
Browse files Browse the repository at this point in the history
Fixes #3136
  • Loading branch information
sogehige committed Jan 1, 2020
1 parent 2fdc156 commit a449826
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/bot/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import { isMainThread } from './cluster';
import * as constants from './constants';
import { settings, shared, ui } from './decorators';
import { error, info, warning } from './helpers/log';
import { getRepository } from 'typeorm';
import { UserTip } from './database/entity/user';
import { onLoad } from './decorators/on';

class Currency extends Core {
mainCurrencyLoaded = false;

@settings('currency')
@ui({
type: 'selector',
Expand Down Expand Up @@ -66,6 +71,11 @@ class Currency extends Core {
}
}

@onLoad('mainCurrency')
setMainCurrencyLoaded() {
this.mainCurrencyLoaded = true;
}

public async updateRates() {
clearTimeout(this.timeouts.updateRates);

Expand All @@ -84,6 +94,20 @@ class Currency extends Core {
this.rates[code] = Number((rate.replace(',', '.') / count).toFixed(3));
}
info(chalk.yellow('CURRENCY:') + ' fetched rates');

await new Promise((resolve) => {
const wait = () => {
if (this.mainCurrencyLoaded) {
resolve();
} else {
setTimeout(() => wait(), 10);
}
};
wait();
});
for (const tip of await getRepository(UserTip).find({ where: { currency: this.mainCurrency }})) {
getRepository(UserTip).update({ id: tip.id }, { sortAmount: this.exchange(Number(tip.amount), tip.currency, 'EUR') });
}
} catch (e) {
error(e.stack);
refresh = constants.SECOND;
Expand Down

0 comments on commit a449826

Please sign in to comment.