From 51b45a672f94523df773d628bcb00d8be708f65e Mon Sep 17 00:00:00 2001 From: gerardPolloRebozado Date: Sat, 11 May 2024 12:32:49 +0200 Subject: [PATCH] fix: Applied comments from review --- apps/api/src/app/order/order.controller.ts | 11 ++- apps/api/src/app/order/order.service.ts | 73 +++---------------- .../activities/activities-page.component.ts | 2 +- apps/client/src/app/services/data.service.ts | 13 +++- 4 files changed, 30 insertions(+), 69 deletions(-) diff --git a/apps/api/src/app/order/order.controller.ts b/apps/api/src/app/order/order.controller.ts index c75c546b3..d8eeb9df6 100644 --- a/apps/api/src/app/order/order.controller.ts +++ b/apps/api/src/app/order/order.controller.ts @@ -48,7 +48,7 @@ export class OrderController { private readonly impersonationService: ImpersonationService, private readonly orderService: OrderService, @Inject(REQUEST) private readonly request: RequestWithUser - ) { } + ) {} @Delete() @HasPermission(permissions.deleteOrder) @@ -63,9 +63,14 @@ export class OrderController { filterByAssetClasses, filterByTags }); - return this.orderService.deleteOrders({ + const where: Prisma.OrderWhereInput = { userId: this.request.user.id, - }, filters); + currency: this.request.user.Settings.settings.baseCurrency, + }; + return this.orderService.deleteOrders({ + filters, + where + }); } @Delete(':id') diff --git a/apps/api/src/app/order/order.service.ts b/apps/api/src/app/order/order.service.ts index 164eda3c2..549a6aade 100644 --- a/apps/api/src/app/order/order.service.ts +++ b/apps/api/src/app/order/order.service.ts @@ -40,7 +40,7 @@ export class OrderService { private readonly exchangeRateDataService: ExchangeRateDataService, private readonly prismaService: PrismaService, private readonly symbolProfileService: SymbolProfileService - ) {} + ) { } public async createOrder( data: Prisma.OrderCreateInput & { @@ -194,77 +194,26 @@ export class OrderService { return order; } - public async deleteOrders(where: Prisma.OrderWhereInput, filters: Filter[]): Promise { - - const { - ACCOUNT: filtersByAccount, - ASSET_CLASS: filtersByAssetClass, - TAG: filtersByTag - } = groupBy(filters, (filter) => { - return filter.type; - }); - - if (filtersByAccount?.length > 0) { - where.accountId = { - in: filtersByAccount.map(({ id }) => { - return id; - }) - }; - } - - if (filtersByAssetClass?.length > 0) { - where.SymbolProfile = { - OR: [ - { - AND: [ - { - OR: filtersByAssetClass.map(({ id }) => { - return { assetClass: AssetClass[id] }; - }) - }, - { - OR: [ - { SymbolProfileOverrides: { is: null } }, - { SymbolProfileOverrides: { assetClass: null } } - ] - } - ] - }, - { - SymbolProfileOverrides: { - OR: filtersByAssetClass.map(({ id }) => { - return { assetClass: AssetClass[id] }; - }) - } - } - ] - }; - } - - if (filtersByTag?.length > 0) { - where.tags = { - some: { - OR: filtersByTag.map(({ id }) => { - return { id }; - }) - } - }; - } - + public async deleteOrders({ filters, where }: { filters?: Filter[], where: Prisma.OrderWhereInput }): Promise { + const userId = where.userId as string; + const userCurrency = where.currency as string; + const { activities } = await this.getOrders({ filters, userId, userCurrency }); + const orderIds = activities.map(order => order.id); const { count } = await this.prismaService.order.deleteMany({ - where + where: { + id: { in: orderIds } + } }); this.eventEmitter.emit( PortfolioChangedEvent.getName(), - new PortfolioChangedEvent({ - userId: where.userId - }) + new PortfolioChangedEvent({ userId: where.userId as string }) ); return count; } + public async getLatestOrder({ dataSource, symbol }: UniqueAsset) { return this.prismaService.order.findFirst({ orderBy: { diff --git a/apps/client/src/app/pages/portfolio/activities/activities-page.component.ts b/apps/client/src/app/pages/portfolio/activities/activities-page.component.ts index 90f422e8c..2c8966549 100644 --- a/apps/client/src/app/pages/portfolio/activities/activities-page.component.ts +++ b/apps/client/src/app/pages/portfolio/activities/activities-page.component.ts @@ -171,7 +171,7 @@ export class ActivitiesPageComponent implements OnDestroy, OnInit { if (confirmation) { this.dataService - .deleteAllOrders({ + .deleteAllActivities({ filters: this.userService.getFilters() }) .pipe(takeUntil(this.unsubscribeSubject)) diff --git a/apps/client/src/app/services/data.service.ts b/apps/client/src/app/services/data.service.ts index 24222966d..ee76f5167 100644 --- a/apps/client/src/app/services/data.service.ts +++ b/apps/client/src/app/services/data.service.ts @@ -55,7 +55,7 @@ import { map } from 'rxjs/operators'; providedIn: 'root' }) export class DataService { - public constructor(private http: HttpClient) { } + public constructor(private http: HttpClient) {} public buildFiltersAsQueryParams({ filters }: { filters?: Filter[] }) { let params = new HttpParams(); @@ -256,6 +256,10 @@ export class DataService { return this.http.delete(`/api/v1/account-balance/${aId}`); } + public deleteActivity(aId: string) { + return this.http.delete(`/api/v1/order/${aId}`); + } + public deleteAllOrders({ filters }) { @@ -263,8 +267,11 @@ export class DataService { return this.http.delete(`/api/v1/order`, { params }); } - public deleteAllActivities() { - return this.http.delete(`/api/v1/order`); + public deleteAllActivities({ + filters + }) { + let params = this.buildFiltersAsQueryParams({ filters }); + return this.http.delete(`/api/v1/order`, { params }); } public deleteBenchmark({ dataSource, symbol }: UniqueAsset) {