Browse Source

fix: pass budget currency from ui

pull/7140/head
Yip Shing him 3 weeks ago
parent
commit
3d36e37122
  1. 7
      apps/client/src/app/pages/portfolio/budget/budget-page.component.spec.ts
  2. 12
      apps/client/src/app/pages/portfolio/budget/budget-page.component.ts
  3. 64
      apps/client/src/app/pages/portfolio/budget/create-or-update-budget-dialog/create-or-update-budget-dialog.component.spec.ts
  4. 11
      apps/client/src/app/pages/portfolio/budget/create-or-update-budget-dialog/create-or-update-budget-dialog.component.ts

7
apps/client/src/app/pages/portfolio/budget/budget-page.component.spec.ts

@ -136,6 +136,13 @@ describe('GfBudgetPageComponent', () => {
await fixture.whenStable(); await fixture.whenStable();
expect(dialog.open).toHaveBeenCalled(); 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); expect(dataService.fetchBudgets).toHaveBeenCalledTimes(2);
}); });

12
apps/client/src/app/pages/portfolio/budget/budget-page.component.ts

@ -108,6 +108,7 @@ export class GfBudgetPageComponent implements OnInit {
public onCreateBudget() { public onCreateBudget() {
this.openBudgetDialog({ this.openBudgetDialog({
currency: this.getCurrency(),
month: this.monthControl.value month: this.monthControl.value
}); });
} }
@ -124,11 +125,20 @@ export class GfBudgetPageComponent implements OnInit {
public onUpdateBudget(budget: BudgetResponse) { public onUpdateBudget(budget: BudgetResponse) {
this.openBudgetDialog({ this.openBudgetDialog({
budget, budget,
currency: this.getCurrency(budget.currency),
month: this.monthControl.value 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 this.dialog
.open(GfCreateOrUpdateBudgetDialogComponent, { .open(GfCreateOrUpdateBudgetDialogComponent, {
data, data,

64
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 { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { of } from 'rxjs'; 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', () => { describe('GfCreateOrUpdateBudgetDialogComponent', () => {
let component: GfCreateOrUpdateBudgetDialogComponent; let component: GfCreateOrUpdateBudgetDialogComponent;
@ -17,7 +20,7 @@ describe('GfCreateOrUpdateBudgetDialogComponent', () => {
>; >;
let fixture: ComponentFixture<GfCreateOrUpdateBudgetDialogComponent>; let fixture: ComponentFixture<GfCreateOrUpdateBudgetDialogComponent>;
beforeEach(async () => { const setup = async (dialogData: CreateOrUpdateBudgetDialogData) => {
dataService = { dataService = {
createBudget: jest.fn().mockReturnValue( createBudget: jest.fn().mockReturnValue(
of({ of({
@ -57,10 +60,7 @@ describe('GfCreateOrUpdateBudgetDialogComponent', () => {
}, },
{ {
provide: MAT_DIALOG_DATA, provide: MAT_DIALOG_DATA,
useValue: { useValue: dialogData
budget: undefined,
month: '2026-06'
}
} }
] ]
}).compileComponents(); }).compileComponents();
@ -68,9 +68,15 @@ describe('GfCreateOrUpdateBudgetDialogComponent', () => {
fixture = TestBed.createComponent(GfCreateOrUpdateBudgetDialogComponent); fixture = TestBed.createComponent(GfCreateOrUpdateBudgetDialogComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
fixture.autoDetectChanges(); fixture.autoDetectChanges();
}); };
it('creates a budget and closes with refresh', async () => { it('creates a budget and closes with refresh', async () => {
await setup({
budget: undefined,
currency: 'USD',
month: '2026-06'
});
component.budgetForm.setValue({ component.budgetForm.setValue({
amount: 500, amount: 500,
categoryId: 'food', categoryId: 'food',
@ -83,8 +89,52 @@ describe('GfCreateOrUpdateBudgetDialogComponent', () => {
expect(dataService.createBudget).toHaveBeenCalledWith({ expect(dataService.createBudget).toHaveBeenCalledWith({
amount: 500, amount: 500,
categoryId: 'food', 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' 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 }); expect(dialogRef.close).toHaveBeenCalledWith({ refresh: true });
}); });
}); });

11
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 { export interface CreateOrUpdateBudgetDialogData {
budget?: BudgetResponse; budget?: BudgetResponse;
currency: string;
month: string; month: string;
} }
@ -65,10 +66,16 @@ export class GfCreateOrUpdateBudgetDialogComponent {
return; return;
} }
const budget = this.budgetForm.getRawValue(); const budget = {
...this.budgetForm.getRawValue(),
currency: this.data.currency
};
if (this.data.budget) { if (this.data.budget) {
this.updateBudget(budget); this.updateBudget({
...budget,
id: this.data.budget.id
});
} else { } else {
this.createBudget(budget); this.createBudget(budget);
} }

Loading…
Cancel
Save