From cd40ce36790a60ad7ba727f15ed63baf51e88180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Mart=C3=ADn?= Date: Thu, 25 Sep 2025 20:57:28 +0200 Subject: [PATCH 1/2] Bugfix/enable save button after editing tags in create or update activity dialog (#5561) * Enable save button after editing tags in create or update activity dialog * Update changelog --- CHANGELOG.md | 8 +++ ...ate-or-update-activity-dialog.component.ts | 4 -- .../create-or-update-activity-dialog.html | 3 +- .../tags-selector/tags-selector.component.ts | 59 +++++++++++++++++-- 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4caf3625..f9b2fa815 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added `settings` to the `Access` model +### Changed + +- Extended the tags selector component to support form control + +### Fixed + +- Fixed an issue where the save button was not enabled after editing tags in the create or update activity dialog + ## 2.201.0 - 2025-09-24 ### Added diff --git a/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts b/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts index fde4f20fa..33698acfb 100644 --- a/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts +++ b/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts @@ -573,10 +573,6 @@ export class GfCreateOrUpdateActivityDialog implements OnDestroy { } } - public onTagsChanged(tags: Tag[]) { - this.activityForm.get('tags').setValue(tags); - } - public ngOnDestroy() { this.unsubscribeSubject.next(); this.unsubscribeSubject.complete(); diff --git a/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.html b/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.html index d7f466743..d6c14cfdc 100644 --- a/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.html +++ b/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.html @@ -322,10 +322,9 @@
diff --git a/libs/ui/src/lib/tags-selector/tags-selector.component.ts b/libs/ui/src/lib/tags-selector/tags-selector.component.ts index 50faab651..05a4b3e7a 100644 --- a/libs/ui/src/lib/tags-selector/tags-selector.component.ts +++ b/libs/ui/src/lib/tags-selector/tags-selector.component.ts @@ -14,7 +14,13 @@ import { signal, ViewChild } from '@angular/core'; -import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms'; +import { + ControlValueAccessor, + FormControl, + FormsModule, + NG_VALUE_ACCESSOR, + ReactiveFormsModule +} from '@angular/forms'; import { MatAutocompleteModule, MatAutocompleteSelectedEvent @@ -40,12 +46,21 @@ import { BehaviorSubject, Subject, takeUntil } from 'rxjs'; MatInputModule, ReactiveFormsModule ], + providers: [ + { + multi: true, + provide: NG_VALUE_ACCESSOR, + useExisting: GfTagsSelectorComponent + } + ], schemas: [CUSTOM_ELEMENTS_SCHEMA], selector: 'gf-tags-selector', styleUrls: ['./tags-selector.component.scss'], templateUrl: 'tags-selector.component.html' }) -export class GfTagsSelectorComponent implements OnInit, OnChanges, OnDestroy { +export class GfTagsSelectorComponent + implements ControlValueAccessor, OnChanges, OnDestroy, OnInit +{ @Input() hasPermissionToCreateTag = false; @Input() readonly = false; @Input() tags: Tag[]; @@ -99,7 +114,10 @@ export class GfTagsSelectorComponent implements OnInit, OnChanges, OnDestroy { return [...(tags ?? []), tag]; }); - this.tagsChanged.emit(this.tagsSelected()); + const newTags = this.tagsSelected(); + this.tagsChanged.emit(newTags); + this.onChange(newTags); + this.onTouched(); this.tagInput.nativeElement.value = ''; this.tagInputControl.setValue(undefined); } @@ -111,7 +129,31 @@ export class GfTagsSelectorComponent implements OnInit, OnChanges, OnDestroy { }); }); - this.tagsChanged.emit(this.tagsSelected()); + const newTags = this.tagsSelected(); + this.tagsChanged.emit(newTags); + this.onChange(newTags); + this.onTouched(); + this.updateFilters(); + } + + public registerOnChange(fn: (value: Tag[]) => void) { + this.onChange = fn; + } + + public registerOnTouched(fn: () => void) { + this.onTouched = fn; + } + + public setDisabledState(isDisabled: boolean) { + if (isDisabled) { + this.tagInputControl.disable(); + } else { + this.tagInputControl.enable(); + } + } + + public writeValue(value: Tag[]) { + this.tagsSelected.set(value || []); this.updateFilters(); } @@ -133,6 +175,15 @@ export class GfTagsSelectorComponent implements OnInit, OnChanges, OnDestroy { }); } + // eslint-disable-next-line @typescript-eslint/no-unused-vars + private onChange = (_value: Tag[]): void => { + // ControlValueAccessor onChange callback + }; + + private onTouched = (): void => { + // ControlValueAccessor onTouched callback + }; + private updateFilters() { this.filteredOptions.next(this.filterTags()); } From ef5661d81c0393f56abe8aa3f108e00eda06aabe Mon Sep 17 00:00:00 2001 From: Kenrick Tandrian <60643640+KenTandrian@users.noreply.github.com> Date: Sat, 27 Sep 2025 00:34:00 +0700 Subject: [PATCH 2/2] Bugfix/investment calculation when selling all units of holding (#5509) * Fix investment calculation when selling all units of holding * Update changelog --------- Co-authored-by: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> --- CHANGELOG.md | 1 + .../calculator/portfolio-calculator.ts | 8 +- ...folio-calculator-msft-buy-and-sell.spec.ts | 146 ++++++++++++++++++ 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index f9b2fa815..17f0d57e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed an issue where the save button was not enabled after editing tags in the create or update activity dialog +- Fixed an issue in the investment calculation when selling all units of a holding ## 2.201.0 - 2025-09-24 diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts index e4d9cdfe8..8a8606003 100644 --- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts @@ -922,7 +922,7 @@ export abstract class PortfolioCalculator { if (oldAccumulatedSymbol) { let investment = oldAccumulatedSymbol.investment; - const newQuantity = quantity + let newQuantity = quantity .mul(factor) .plus(oldAccumulatedSymbol.quantity); @@ -948,6 +948,12 @@ export abstract class PortfolioCalculator { } } + if (newQuantity.abs().lt(Number.EPSILON)) { + // Reset to zero if quantity is (almost) zero to avoid rounding issues + investment = new Big(0); + newQuantity = new Big(0); + } + currentTransactionPointItem = { currency, dataSource, diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts new file mode 100644 index 000000000..bb976564a --- /dev/null +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts @@ -0,0 +1,146 @@ +import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface'; +import { + activityDummyData, + symbolProfileDummyData, + userDummyData +} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; +import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; +import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; +import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; +import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; +import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; +import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; +import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; +import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; +import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; +import { parseDate } from '@ghostfolio/common/helper'; +import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; + +jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { + return { + CurrentRateService: jest.fn().mockImplementation(() => { + return CurrentRateServiceMock; + }) + }; +}); + +jest.mock( + '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', + () => { + return { + // eslint-disable-next-line @typescript-eslint/naming-convention + PortfolioSnapshotService: jest.fn().mockImplementation(() => { + return PortfolioSnapshotServiceMock; + }) + }; + } +); + +jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { + return { + // eslint-disable-next-line @typescript-eslint/naming-convention + RedisCacheService: jest.fn().mockImplementation(() => { + return RedisCacheServiceMock; + }) + }; +}); + +describe('PortfolioCalculator', () => { + let configurationService: ConfigurationService; + let currentRateService: CurrentRateService; + let exchangeRateDataService: ExchangeRateDataService; + let portfolioCalculatorFactory: PortfolioCalculatorFactory; + let portfolioSnapshotService: PortfolioSnapshotService; + let redisCacheService: RedisCacheService; + + beforeEach(() => { + configurationService = new ConfigurationService(); + currentRateService = new CurrentRateService(null, null, null, null); + exchangeRateDataService = new ExchangeRateDataService( + null, + null, + null, + null + ); + portfolioSnapshotService = new PortfolioSnapshotService(null); + redisCacheService = new RedisCacheService(null, null); + portfolioCalculatorFactory = new PortfolioCalculatorFactory( + configurationService, + currentRateService, + exchangeRateDataService, + portfolioSnapshotService, + redisCacheService + ); + }); + + describe('get transaction point', () => { + it('with MSFT buy and sell with fractional quantities (multiples of 1/3)', () => { + jest.useFakeTimers().setSystemTime(parseDate('2024-04-01').getTime()); + + const activities: Activity[] = [ + { + ...activityDummyData, + date: new Date('2024-03-08'), + feeInAssetProfileCurrency: 0, + quantity: 0.3333333333333333, + SymbolProfile: { + ...symbolProfileDummyData, + currency: 'USD', + dataSource: 'YAHOO', + name: 'Microsoft Inc.', + symbol: 'MSFT' + }, + type: 'BUY', + unitPriceInAssetProfileCurrency: 408 + }, + { + ...activityDummyData, + date: new Date('2024-03-13'), + quantity: 0.6666666666666666, + feeInAssetProfileCurrency: 0, + SymbolProfile: { + ...symbolProfileDummyData, + currency: 'USD', + dataSource: 'YAHOO', + name: 'Microsoft Inc.', + symbol: 'MSFT' + }, + type: 'BUY', + unitPriceInAssetProfileCurrency: 400 + }, + { + ...activityDummyData, + date: new Date('2024-03-14'), + quantity: 1, + feeInAssetProfileCurrency: 0, + SymbolProfile: { + ...symbolProfileDummyData, + currency: 'USD', + dataSource: 'YAHOO', + name: 'Microsoft Inc.', + symbol: 'MSFT' + }, + type: 'SELL', + unitPriceInAssetProfileCurrency: 411 + } + ]; + + const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ + activities, + calculationType: PerformanceCalculationType.ROAI, + currency: 'USD', + userId: userDummyData.id + }); + + const transactionPoints = portfolioCalculator.getTransactionPoints(); + const lastTransactionPoint = + transactionPoints[transactionPoints.length - 1]; + const position = lastTransactionPoint.items.find( + (item) => item.symbol === 'MSFT' + ); + + expect(position.investment.toNumber()).toBe(0); + expect(position.quantity.toNumber()).toBe(0); + }); + }); +});