From 3d36e3712280f4b5cf60ddfb735274ae8e6c6a59 Mon Sep 17 00:00:00 2001 From: Yip Shing him Date: Tue, 23 Jun 2026 21:32:04 +0100 Subject: [PATCH] fix: pass budget currency from ui --- .../budget/budget-page.component.spec.ts | 7 ++ .../portfolio/budget/budget-page.component.ts | 12 +++- ...-or-update-budget-dialog.component.spec.ts | 64 +++++++++++++++++-- ...reate-or-update-budget-dialog.component.ts | 11 +++- 4 files changed, 84 insertions(+), 10 deletions(-) diff --git a/apps/client/src/app/pages/portfolio/budget/budget-page.component.spec.ts b/apps/client/src/app/pages/portfolio/budget/budget-page.component.spec.ts index be6472c48..a3a70a0c4 100644 --- a/apps/client/src/app/pages/portfolio/budget/budget-page.component.spec.ts +++ b/apps/client/src/app/pages/portfolio/budget/budget-page.component.spec.ts @@ -136,6 +136,13 @@ describe('GfBudgetPageComponent', () => { await fixture.whenStable(); expect(dialog.open).toHaveBeenCalled(); + expect(dialog.open).toHaveBeenCalledWith(expect.any(Function), { + data: { + currency: 'USD', + month: fixture.componentInstance.monthControl.value + }, + width: '32rem' + }); expect(dataService.fetchBudgets).toHaveBeenCalledTimes(2); }); diff --git a/apps/client/src/app/pages/portfolio/budget/budget-page.component.ts b/apps/client/src/app/pages/portfolio/budget/budget-page.component.ts index b46a64255..6a33c238c 100644 --- a/apps/client/src/app/pages/portfolio/budget/budget-page.component.ts +++ b/apps/client/src/app/pages/portfolio/budget/budget-page.component.ts @@ -108,6 +108,7 @@ export class GfBudgetPageComponent implements OnInit { public onCreateBudget() { this.openBudgetDialog({ + currency: this.getCurrency(), month: this.monthControl.value }); } @@ -124,11 +125,20 @@ export class GfBudgetPageComponent implements OnInit { public onUpdateBudget(budget: BudgetResponse) { this.openBudgetDialog({ budget, + currency: this.getCurrency(budget.currency), month: this.monthControl.value }); } - private openBudgetDialog(data: { budget?: BudgetResponse; month: string }) { + private getCurrency(fallback = 'USD') { + return this.user?.settings?.baseCurrency ?? fallback; + } + + private openBudgetDialog(data: { + budget?: BudgetResponse; + currency: string; + month: string; + }) { this.dialog .open(GfCreateOrUpdateBudgetDialogComponent, { data, diff --git a/apps/client/src/app/pages/portfolio/budget/create-or-update-budget-dialog/create-or-update-budget-dialog.component.spec.ts b/apps/client/src/app/pages/portfolio/budget/create-or-update-budget-dialog/create-or-update-budget-dialog.component.spec.ts index cec54d4f1..b0a7d5851 100644 --- a/apps/client/src/app/pages/portfolio/budget/create-or-update-budget-dialog/create-or-update-budget-dialog.component.spec.ts +++ b/apps/client/src/app/pages/portfolio/budget/create-or-update-budget-dialog/create-or-update-budget-dialog.component.spec.ts @@ -5,7 +5,10 @@ import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { NoopAnimationsModule } from '@angular/platform-browser/animations'; import { of } from 'rxjs'; -import { GfCreateOrUpdateBudgetDialogComponent } from './create-or-update-budget-dialog.component'; +import { + CreateOrUpdateBudgetDialogData, + GfCreateOrUpdateBudgetDialogComponent +} from './create-or-update-budget-dialog.component'; describe('GfCreateOrUpdateBudgetDialogComponent', () => { let component: GfCreateOrUpdateBudgetDialogComponent; @@ -17,7 +20,7 @@ describe('GfCreateOrUpdateBudgetDialogComponent', () => { >; let fixture: ComponentFixture; - beforeEach(async () => { + const setup = async (dialogData: CreateOrUpdateBudgetDialogData) => { dataService = { createBudget: jest.fn().mockReturnValue( of({ @@ -57,10 +60,7 @@ describe('GfCreateOrUpdateBudgetDialogComponent', () => { }, { provide: MAT_DIALOG_DATA, - useValue: { - budget: undefined, - month: '2026-06' - } + useValue: dialogData } ] }).compileComponents(); @@ -68,9 +68,15 @@ describe('GfCreateOrUpdateBudgetDialogComponent', () => { fixture = TestBed.createComponent(GfCreateOrUpdateBudgetDialogComponent); component = fixture.componentInstance; fixture.autoDetectChanges(); - }); + }; it('creates a budget and closes with refresh', async () => { + await setup({ + budget: undefined, + currency: 'USD', + month: '2026-06' + }); + component.budgetForm.setValue({ amount: 500, categoryId: 'food', @@ -83,8 +89,52 @@ describe('GfCreateOrUpdateBudgetDialogComponent', () => { expect(dataService.createBudget).toHaveBeenCalledWith({ amount: 500, categoryId: 'food', + currency: 'USD', + month: '2026-06' + }); + expect(dialogRef.close).toHaveBeenCalledWith({ refresh: true }); + }); + + it('updates a budget and closes with refresh', async () => { + await setup({ + budget: { + amount: 500, + category: { + id: 'food', + name: 'Food' + }, + categoryId: 'food', + createdAt: new Date('2026-06-01'), + currency: 'USD', + id: 'budget-1', + month: '2026-06', + remaining: 500, + spent: 0, + updatedAt: new Date('2026-06-01') + }, + currency: 'USD', + month: '2026-06' + }); + + component.budgetForm.setValue({ + amount: 650, + categoryId: 'food', month: '2026-06' }); + + component.onSubmit(); + await fixture.whenStable(); + + expect(dataService.updateBudget).toHaveBeenCalledWith({ + budget: { + amount: 650, + categoryId: 'food', + currency: 'USD', + id: 'budget-1', + month: '2026-06' + }, + id: 'budget-1' + }); expect(dialogRef.close).toHaveBeenCalledWith({ refresh: true }); }); }); diff --git a/apps/client/src/app/pages/portfolio/budget/create-or-update-budget-dialog/create-or-update-budget-dialog.component.ts b/apps/client/src/app/pages/portfolio/budget/create-or-update-budget-dialog/create-or-update-budget-dialog.component.ts index b583f0286..0f528b4aa 100644 --- a/apps/client/src/app/pages/portfolio/budget/create-or-update-budget-dialog/create-or-update-budget-dialog.component.ts +++ b/apps/client/src/app/pages/portfolio/budget/create-or-update-budget-dialog/create-or-update-budget-dialog.component.ts @@ -21,6 +21,7 @@ import { MatInputModule } from '@angular/material/input'; export interface CreateOrUpdateBudgetDialogData { budget?: BudgetResponse; + currency: string; month: string; } @@ -65,10 +66,16 @@ export class GfCreateOrUpdateBudgetDialogComponent { return; } - const budget = this.budgetForm.getRawValue(); + const budget = { + ...this.budgetForm.getRawValue(), + currency: this.data.currency + }; if (this.data.budget) { - this.updateBudget(budget); + this.updateBudget({ + ...budget, + id: this.data.budget.id + }); } else { this.createBudget(budget); }