Enable querying budget and category sums for arbitrary date ranges
This only adds support via query params. Actual UI to use this needs to be built still
This commit is contained in:
parent
24c74a2dee
commit
bc58d555c9
4 changed files with 64 additions and 12 deletions
|
@ -32,13 +32,41 @@ export class BudgetDetailsComponent implements OnInit, OnDestroy, Actionable {
|
|||
{ data: [0, 0], label: 'Expected' },
|
||||
{ data: [0, 0], label: 'Actual' },
|
||||
];
|
||||
from: Date
|
||||
to: Date
|
||||
|
||||
constructor(
|
||||
private app: AppComponent,
|
||||
private route: ActivatedRoute,
|
||||
@Inject(TWIGS_SERVICE) private twigsService: TwigsService,
|
||||
private router: Router,
|
||||
) { }
|
||||
) {
|
||||
let fromStr = this.route.snapshot.queryParamMap.get('from');
|
||||
if (fromStr) {
|
||||
let fromDate = new Date(fromStr);
|
||||
if (!isNaN(fromDate.getTime())) {
|
||||
this.from = fromDate;
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.from) {
|
||||
let date = new Date();
|
||||
date.setHours(0);
|
||||
date.setMinutes(0);
|
||||
date.setSeconds(0);
|
||||
date.setMilliseconds(0);
|
||||
date.setDate(1);
|
||||
this.from = date;
|
||||
}
|
||||
|
||||
let toStr = this.route.snapshot.queryParamMap.get('to');
|
||||
if (toStr) {
|
||||
let toDate = new Date(toStr);
|
||||
if (!isNaN(toDate.getTime())) {
|
||||
this.to = toDate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.getBudget();
|
||||
|
@ -87,7 +115,7 @@ export class BudgetDetailsComponent implements OnInit, OnDestroy, Actionable {
|
|||
|
||||
getBalance(): void {
|
||||
const id = this.route.snapshot.paramMap.get('id');
|
||||
this.twigsService.getBudgetBalance(id).subscribe(balance => {
|
||||
this.twigsService.getBudgetBalance(id, this.from, this.to).subscribe(balance => {
|
||||
this.budgetBalance = balance;
|
||||
});
|
||||
}
|
||||
|
@ -116,7 +144,7 @@ export class BudgetDetailsComponent implements OnInit, OnDestroy, Actionable {
|
|||
this.income.push(category);
|
||||
this.expectedIncome += category.amount;
|
||||
}
|
||||
this.twigsService.getCategoryBalance(category.id).subscribe(
|
||||
this.twigsService.getCategoryBalance(category.id, this.from, this.to).subscribe(
|
||||
balance => {
|
||||
console.log(balance);
|
||||
if (category.expense) {
|
||||
|
|
|
@ -73,8 +73,20 @@ export class TwigsHttpService implements TwigsService {
|
|||
return this.budgets;
|
||||
}
|
||||
|
||||
getBudgetBalance(id: string): Observable<number> {
|
||||
return this.http.get<any>(`${this.apiUrl}/transactions/sum?budgetId=${id}`, this.options)
|
||||
getBudgetBalance(
|
||||
id: string,
|
||||
from?: Date,
|
||||
to?: Date
|
||||
): Observable<number> {
|
||||
let httpParams = new HttpParams();
|
||||
if (from) {
|
||||
httpParams = httpParams.set('from', from.toISOString());
|
||||
}
|
||||
if (to) {
|
||||
httpParams = httpParams.set('to', to.toISOString());
|
||||
}
|
||||
const params = { params: httpParams };
|
||||
return this.http.get<any>(`${this.apiUrl}/transactions/sum?budgetId=${id}`, { ...this.options, ...params })
|
||||
.pipe(map(obj => obj.balance));
|
||||
}
|
||||
|
||||
|
@ -187,8 +199,20 @@ export class TwigsHttpService implements TwigsService {
|
|||
return this.http.get<Category>(`${this.apiUrl}/categories/${id}`, this.options);
|
||||
}
|
||||
|
||||
getCategoryBalance(id: string): Observable<number> {
|
||||
return this.http.get<any>(`${this.apiUrl}/transactions/sum?categoryId=${id}`, this.options)
|
||||
getCategoryBalance(
|
||||
id: string,
|
||||
from?: Date,
|
||||
to?: Date
|
||||
): Observable<number> {
|
||||
let httpParams = new HttpParams();
|
||||
if (from) {
|
||||
httpParams = httpParams.set('from', from.toISOString());
|
||||
}
|
||||
if (to) {
|
||||
httpParams = httpParams.set('to', to.toISOString());
|
||||
}
|
||||
const params = { params: httpParams };
|
||||
return this.http.get<any>(`${this.apiUrl}/transactions/sum?categoryId=${id}`, { ...this.options, ...params })
|
||||
.pipe(map(obj => obj.balance));
|
||||
}
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@ export class TwigsLocalService implements TwigsService {
|
|||
});
|
||||
}
|
||||
|
||||
getBudgetBalance(id: string): Observable<number> {
|
||||
getBudgetBalance(id: string, from?: Date, to?: Date): Observable<number> {
|
||||
return new Observable(emitter => {
|
||||
emitter.next(200);
|
||||
emitter.complete()
|
||||
|
@ -162,7 +162,7 @@ export class TwigsLocalService implements TwigsService {
|
|||
});
|
||||
}
|
||||
|
||||
getCategoryBalance(id: string): Observable<number> {
|
||||
getCategoryBalance(id: string, from?: Date, to?: Date): Observable<number> {
|
||||
return new Observable(emitter => {
|
||||
emitter.next(20);
|
||||
emitter.complete()
|
||||
|
|
|
@ -14,7 +14,7 @@ export interface TwigsService {
|
|||
// Budgets
|
||||
getBudgets(): Observable<Budget[]>;
|
||||
getBudget(id: string): Observable<Budget>;
|
||||
getBudgetBalance(id: string): Observable<number>;
|
||||
getBudgetBalance(id: string, from?: Date, to?: Date): Observable<number>;
|
||||
createBudget(
|
||||
id: string,
|
||||
name: string,
|
||||
|
@ -27,7 +27,7 @@ export interface TwigsService {
|
|||
// Categories
|
||||
getCategories(budgetId?: string, count?: number): Observable<Category[]>;
|
||||
getCategory(id: string): Observable<Category>;
|
||||
getCategoryBalance(id: string): Observable<number>;
|
||||
getCategoryBalance(id: string, from?: Date, to?: Date): Observable<number>;
|
||||
createCategory(id: string, budgetId: string, name: string, description: string, amount: number, isExpense: boolean): Observable<Category>;
|
||||
updateCategory(id: string, changes: object): Observable<Category>;
|
||||
deleteCategory(id: string): Observable<void>;
|
||||
|
|
Loading…
Reference in a new issue