From bc58d555c98edaea2c5ca3eb0c5741cefb878c26 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sat, 2 Jul 2022 13:06:59 -0600 Subject: [PATCH] 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 --- .../budget-details.component.ts | 36 ++++++++++++++++--- src/app/shared/twigs.http.service.ts | 32 ++++++++++++++--- src/app/shared/twigs.local.service.ts | 4 +-- src/app/shared/twigs.service.ts | 4 +-- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/app/budgets/budget-details/budget-details.component.ts b/src/app/budgets/budget-details/budget-details.component.ts index 06574c6..6987c14 100644 --- a/src/app/budgets/budget-details/budget-details.component.ts +++ b/src/app/budgets/budget-details/budget-details.component.ts @@ -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) { @@ -146,7 +174,7 @@ export class BudgetDetailsComponent implements OnInit, OnDestroy, Actionable { this.router.navigateByUrl(this.router.routerState.snapshot.url + "/edit") } - getActionLabel(): string { + getActionLabel(): string { return "Edit"; } } diff --git a/src/app/shared/twigs.http.service.ts b/src/app/shared/twigs.http.service.ts index 7e0d475..c2d0c65 100644 --- a/src/app/shared/twigs.http.service.ts +++ b/src/app/shared/twigs.http.service.ts @@ -73,8 +73,20 @@ export class TwigsHttpService implements TwigsService { return this.budgets; } - getBudgetBalance(id: string): Observable { - return this.http.get(`${this.apiUrl}/transactions/sum?budgetId=${id}`, this.options) + getBudgetBalance( + id: string, + from?: Date, + to?: Date + ): Observable { + 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(`${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(`${this.apiUrl}/categories/${id}`, this.options); } - getCategoryBalance(id: string): Observable { - return this.http.get(`${this.apiUrl}/transactions/sum?categoryId=${id}`, this.options) + getCategoryBalance( + id: string, + from?: Date, + to?: Date + ): Observable { + 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(`${this.apiUrl}/transactions/sum?categoryId=${id}`, { ...this.options, ...params }) .pipe(map(obj => obj.balance)); } diff --git a/src/app/shared/twigs.local.service.ts b/src/app/shared/twigs.local.service.ts index 2fdd913..67fe3a7 100644 --- a/src/app/shared/twigs.local.service.ts +++ b/src/app/shared/twigs.local.service.ts @@ -66,7 +66,7 @@ export class TwigsLocalService implements TwigsService { }); } - getBudgetBalance(id: string): Observable { + getBudgetBalance(id: string, from?: Date, to?: Date): Observable { return new Observable(emitter => { emitter.next(200); emitter.complete() @@ -162,7 +162,7 @@ export class TwigsLocalService implements TwigsService { }); } - getCategoryBalance(id: string): Observable { + getCategoryBalance(id: string, from?: Date, to?: Date): Observable { return new Observable(emitter => { emitter.next(20); emitter.complete() diff --git a/src/app/shared/twigs.service.ts b/src/app/shared/twigs.service.ts index 68d182c..a581f3a 100644 --- a/src/app/shared/twigs.service.ts +++ b/src/app/shared/twigs.service.ts @@ -14,7 +14,7 @@ export interface TwigsService { // Budgets getBudgets(): Observable; getBudget(id: string): Observable; - getBudgetBalance(id: string): Observable; + getBudgetBalance(id: string, from?: Date, to?: Date): Observable; createBudget( id: string, name: string, @@ -27,7 +27,7 @@ export interface TwigsService { // Categories getCategories(budgetId?: string, count?: number): Observable; getCategory(id: string): Observable; - getCategoryBalance(id: string): Observable; + getCategoryBalance(id: string, from?: Date, to?: Date): Observable; createCategory(id: string, budgetId: string, name: string, description: string, amount: number, isExpense: boolean): Observable; updateCategory(id: string, changes: object): Observable; deleteCategory(id: string): Observable;