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:
William Brawner 2022-07-02 13:06:59 -06:00
parent 24c74a2dee
commit bc58d555c9
4 changed files with 64 additions and 12 deletions

View file

@ -32,13 +32,41 @@ export class BudgetDetailsComponent implements OnInit, OnDestroy, Actionable {
{ data: [0, 0], label: 'Expected' }, { data: [0, 0], label: 'Expected' },
{ data: [0, 0], label: 'Actual' }, { data: [0, 0], label: 'Actual' },
]; ];
from: Date
to: Date
constructor( constructor(
private app: AppComponent, private app: AppComponent,
private route: ActivatedRoute, private route: ActivatedRoute,
@Inject(TWIGS_SERVICE) private twigsService: TwigsService, @Inject(TWIGS_SERVICE) private twigsService: TwigsService,
private router: Router, 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() { ngOnInit() {
this.getBudget(); this.getBudget();
@ -87,7 +115,7 @@ export class BudgetDetailsComponent implements OnInit, OnDestroy, Actionable {
getBalance(): void { getBalance(): void {
const id = this.route.snapshot.paramMap.get('id'); 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; this.budgetBalance = balance;
}); });
} }
@ -116,7 +144,7 @@ export class BudgetDetailsComponent implements OnInit, OnDestroy, Actionable {
this.income.push(category); this.income.push(category);
this.expectedIncome += category.amount; this.expectedIncome += category.amount;
} }
this.twigsService.getCategoryBalance(category.id).subscribe( this.twigsService.getCategoryBalance(category.id, this.from, this.to).subscribe(
balance => { balance => {
console.log(balance); console.log(balance);
if (category.expense) { if (category.expense) {
@ -146,7 +174,7 @@ export class BudgetDetailsComponent implements OnInit, OnDestroy, Actionable {
this.router.navigateByUrl(this.router.routerState.snapshot.url + "/edit") this.router.navigateByUrl(this.router.routerState.snapshot.url + "/edit")
} }
getActionLabel(): string { getActionLabel(): string {
return "Edit"; return "Edit";
} }
} }

View file

@ -73,8 +73,20 @@ export class TwigsHttpService implements TwigsService {
return this.budgets; return this.budgets;
} }
getBudgetBalance(id: string): Observable<number> { getBudgetBalance(
return this.http.get<any>(`${this.apiUrl}/transactions/sum?budgetId=${id}`, this.options) 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)); .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); return this.http.get<Category>(`${this.apiUrl}/categories/${id}`, this.options);
} }
getCategoryBalance(id: string): Observable<number> { getCategoryBalance(
return this.http.get<any>(`${this.apiUrl}/transactions/sum?categoryId=${id}`, this.options) 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)); .pipe(map(obj => obj.balance));
} }

View file

@ -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 => { return new Observable(emitter => {
emitter.next(200); emitter.next(200);
emitter.complete() 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 => { return new Observable(emitter => {
emitter.next(20); emitter.next(20);
emitter.complete() emitter.complete()

View file

@ -14,7 +14,7 @@ export interface TwigsService {
// Budgets // Budgets
getBudgets(): Observable<Budget[]>; getBudgets(): Observable<Budget[]>;
getBudget(id: string): Observable<Budget>; getBudget(id: string): Observable<Budget>;
getBudgetBalance(id: string): Observable<number>; getBudgetBalance(id: string, from?: Date, to?: Date): Observable<number>;
createBudget( createBudget(
id: string, id: string,
name: string, name: string,
@ -27,7 +27,7 @@ export interface TwigsService {
// Categories // Categories
getCategories(budgetId?: string, count?: number): Observable<Category[]>; getCategories(budgetId?: string, count?: number): Observable<Category[]>;
getCategory(id: string): 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>; createCategory(id: string, budgetId: string, name: string, description: string, amount: number, isExpense: boolean): Observable<Category>;
updateCategory(id: string, changes: object): Observable<Category>; updateCategory(id: string, changes: object): Observable<Category>;
deleteCategory(id: string): Observable<void>; deleteCategory(id: string): Observable<void>;