From 8386fec98a552fa90eb138d0dd8999be9d5b1aba Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 29 Jun 2024 10:53:25 +0200 Subject: [PATCH] Feature/automatic deletion of unused asset profiles (#3525) * Automatic deletion of unused asset profiles * Update changelog --- CHANGELOG.md | 4 ++++ apps/api/src/app/order/order.controller.ts | 1 - apps/api/src/app/order/order.service.ts | 27 ++++++++++++++++++---- apps/api/src/app/user/user.module.ts | 2 ++ apps/api/src/app/user/user.service.ts | 6 +++-- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c52c8589c..8bd423470 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Changed + +- Added support for automatic deletion of unused asset profiles when deleting activities + ### Fixed - Fixed an issue with the all time high in the benchmarks of the markets overview diff --git a/apps/api/src/app/order/order.controller.ts b/apps/api/src/app/order/order.controller.ts index f3709abae..f9190d1eb 100644 --- a/apps/api/src/app/order/order.controller.ts +++ b/apps/api/src/app/order/order.controller.ts @@ -66,7 +66,6 @@ export class OrderController { return this.orderService.deleteOrders({ filters, - userCurrency: this.request.user.Settings.settings.baseCurrency, userId: this.request.user.id }); } diff --git a/apps/api/src/app/order/order.service.ts b/apps/api/src/app/order/order.service.ts index 7a4dd5a4a..a1fa4db6c 100644 --- a/apps/api/src/app/order/order.service.ts +++ b/apps/api/src/app/order/order.service.ts @@ -184,7 +184,15 @@ export class OrderService { where }); - if (['FEE', 'INTEREST', 'ITEM', 'LIABILITY'].includes(order.type)) { + const [symbolProfile] = + await this.symbolProfileService.getSymbolProfilesByIds([ + order.symbolProfileId + ]); + + if ( + ['FEE', 'INTEREST', 'ITEM', 'LIABILITY'].includes(order.type) || + symbolProfile.activitiesCount === 0 + ) { await this.symbolProfileService.deleteById(order.symbolProfileId); } @@ -200,18 +208,16 @@ export class OrderService { public async deleteOrders({ filters, - userCurrency, userId }: { filters?: Filter[]; - userCurrency: string; userId: string; }): Promise { const { activities } = await this.getOrders({ filters, userId, - userCurrency, includeDrafts: true, + userCurrency: undefined, withExcludedAccounts: true }); @@ -225,6 +231,19 @@ export class OrderService { } }); + const symbolProfiles = + await this.symbolProfileService.getSymbolProfilesByIds( + activities.map(({ symbolProfileId }) => { + return symbolProfileId; + }) + ); + + for (const { activitiesCount, id } of symbolProfiles) { + if (activitiesCount === 0) { + await this.symbolProfileService.deleteById(id); + } + } + this.eventEmitter.emit( PortfolioChangedEvent.getName(), new PortfolioChangedEvent({ userId }) diff --git a/apps/api/src/app/user/user.module.ts b/apps/api/src/app/user/user.module.ts index 35f78dc5f..063cfef82 100644 --- a/apps/api/src/app/user/user.module.ts +++ b/apps/api/src/app/user/user.module.ts @@ -1,3 +1,4 @@ +import { OrderModule } from '@ghostfolio/api/app/order/order.module'; import { SubscriptionModule } from '@ghostfolio/api/app/subscription/subscription.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module'; @@ -19,6 +20,7 @@ import { UserService } from './user.service'; secret: process.env.JWT_SECRET_KEY, signOptions: { expiresIn: '30 days' } }), + OrderModule, PrismaModule, PropertyModule, SubscriptionModule, diff --git a/apps/api/src/app/user/user.service.ts b/apps/api/src/app/user/user.service.ts index 51bd4c4db..ce0ec5524 100644 --- a/apps/api/src/app/user/user.service.ts +++ b/apps/api/src/app/user/user.service.ts @@ -1,3 +1,4 @@ +import { OrderService } from '@ghostfolio/api/app/order/order.service'; import { SubscriptionService } from '@ghostfolio/api/app/subscription/subscription.service'; import { environment } from '@ghostfolio/api/environments/environment'; import { PortfolioChangedEvent } from '@ghostfolio/api/events/portfolio-changed.event'; @@ -40,6 +41,7 @@ export class UserService { public constructor( private readonly configurationService: ConfigurationService, private readonly eventEmitter: EventEmitter2, + private readonly orderService: OrderService, private readonly prismaService: PrismaService, private readonly propertyService: PropertyService, private readonly subscriptionService: SubscriptionService, @@ -398,8 +400,8 @@ export class UserService { } catch {} try { - await this.prismaService.order.deleteMany({ - where: { userId: where.id } + await this.orderService.deleteOrders({ + userId: where.id }); } catch {}