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();
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);
});

12
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,

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 { 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<GfCreateOrUpdateBudgetDialogComponent>;
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 });
});
});

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 {
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);
}

Loading…
Cancel
Save