Reduce client-side load in calculating category balances
This commit is contained in:
parent
852aa1d6c5
commit
06850c8b8e
4 changed files with 14 additions and 23 deletions
|
@ -119,7 +119,7 @@ export class BudgetDetailsComponent implements OnInit, OnDestroy, Actionable {
|
|||
this.income.push(category);
|
||||
this.expectedIncome += category.amount;
|
||||
}
|
||||
this.getCategoryBalance(category.id).subscribe(
|
||||
this.twigsService.getCategoryBalance(category.id).subscribe(
|
||||
balance => {
|
||||
console.log(balance);
|
||||
if (category.expense) {
|
||||
|
@ -145,28 +145,6 @@ export class BudgetDetailsComponent implements OnInit, OnDestroy, Actionable {
|
|||
});
|
||||
}
|
||||
|
||||
getCategoryBalance(category: number): Observable<number> {
|
||||
return Observable.create(subscriber => {
|
||||
let date = new Date();
|
||||
date.setHours(0);
|
||||
date.setMinutes(0);
|
||||
date.setSeconds(0);
|
||||
date.setDate(1);
|
||||
this.twigsService.getTransactions(this.budget.id, category, null, date).subscribe(transactions => {
|
||||
let balance = 0;
|
||||
for (const transaction of transactions) {
|
||||
if (transaction.expense) {
|
||||
balance -= transaction.amount;
|
||||
} else {
|
||||
balance += transaction.amount;
|
||||
}
|
||||
}
|
||||
subscriber.next(balance);
|
||||
subscriber.complete();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
doAction(): void {
|
||||
this.router.navigateByUrl(this.router.routerState.snapshot.url + "/edit")
|
||||
}
|
||||
|
|
|
@ -173,6 +173,11 @@ export class TwigsHttpService implements TwigsService {
|
|||
return this.http.get<Category>(`${this.apiUrl}/categories/${id}`, this.options);
|
||||
}
|
||||
|
||||
getCategoryBalance(id: number): Observable<number> {
|
||||
return this.http.get<any>(`${this.apiUrl}/categories/${id}/balance`, this.options)
|
||||
.pipe(map(obj => obj.balance));
|
||||
}
|
||||
|
||||
createCategory(budgetId: number, name: string, description: string, amount: number, isExpense: boolean): Observable<Category> {
|
||||
const params = {
|
||||
'title': name,
|
||||
|
|
|
@ -153,6 +153,13 @@ export class TwigsLocalService implements TwigsService {
|
|||
});
|
||||
}
|
||||
|
||||
getCategoryBalance(id: number): Observable<number> {
|
||||
return new Observable(emitter => {
|
||||
emitter.next(20);
|
||||
emitter.complete()
|
||||
})
|
||||
}
|
||||
|
||||
createCategory(budgetId: number, name: string, description: string, amount: number, isExpense: boolean): Observable<Category> {
|
||||
return Observable.create(subscriber => {
|
||||
const category = new Category();
|
||||
|
|
|
@ -25,6 +25,7 @@ export interface TwigsService {
|
|||
// Categories
|
||||
getCategories(budgetId?: number, count?: number): Observable<Category[]>;
|
||||
getCategory(id: number): Observable<Category>;
|
||||
getCategoryBalance(id: number): Observable<number>;
|
||||
createCategory(budgetId: number, name: string, description: string, amount: number, isExpense: boolean): Observable<Category>;
|
||||
updateCategory(budgetId: number, id: number, changes: object): Observable<Category>;
|
||||
deleteCategory(budgetId: number, id: number): Observable<void>;
|
||||
|
|
Loading…
Reference in a new issue