Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve problem with deleting filtered activities #3394

Merged
merged 11 commits into from
May 12, 2024
19 changes: 14 additions & 5 deletions apps/api/src/app/order/order.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
} from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { AuthGuard } from '@nestjs/passport';
import { Order as OrderModel, Prisma } from '@prisma/client';
import { AssetClass, Order as OrderModel, Prisma } from '@prisma/client';
GerardPolloRebozado marked this conversation as resolved.
Show resolved Hide resolved
import { parseISO } from 'date-fns';
import { StatusCodes, getReasonPhrase } from 'http-status-codes';

Expand All @@ -48,15 +48,24 @@ export class OrderController {
private readonly impersonationService: ImpersonationService,
private readonly orderService: OrderService,
@Inject(REQUEST) private readonly request: RequestWithUser
) {}
) { }
GerardPolloRebozado marked this conversation as resolved.
Show resolved Hide resolved

@Delete()
@HasPermission(permissions.deleteOrder)
@UseGuards(AuthGuard('jwt'), HasPermissionGuard)
public async deleteOrders(): Promise<number> {
return this.orderService.deleteOrders({
userId: this.request.user.id
public async deleteOrders(
@Query('accounts') filterByAccounts?: string,
@Query('assetClasses') filterByAssetClasses?: string,
@Query('tags') filterByTags?: string
): Promise<number> {
const filters = this.apiService.buildFiltersFromQueryParams({
filterByAccounts,
filterByAssetClasses,
filterByTags
});
return this.orderService.deleteOrders({
userId: this.request.user.id,
}, filters);
}

@Delete(':id')
Expand Down
58 changes: 57 additions & 1 deletion apps/api/src/app/order/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,63 @@ export class OrderService {
return order;
}

public async deleteOrders(where: Prisma.OrderWhereInput): Promise<number> {
public async deleteOrders(where: Prisma.OrderWhereInput, filters: Filter[]): Promise<number> {
GerardPolloRebozado marked this conversation as resolved.
Show resolved Hide resolved

const {
GerardPolloRebozado marked this conversation as resolved.
Show resolved Hide resolved
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 };
})
}
};
}

const { count } = await this.prismaService.order.deleteMany({
where
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ export class ActivitiesPageComponent implements OnDestroy, OnInit {

if (confirmation) {
this.dataService
.deleteAllActivities()
.deleteAllOrders({
GerardPolloRebozado marked this conversation as resolved.
Show resolved Hide resolved
filters: this.userService.getFilters()
})
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe({
next: () => {
Expand Down Expand Up @@ -217,9 +219,8 @@ export class ActivitiesPageComponent implements OnDestroy, OnInit {
data.activities
),
contentType: 'text/calendar',
fileName: `ghostfolio-draft${
data.activities.length > 1 ? 's' : ''
}-${format(parseISO(data.meta.date), 'yyyyMMddHHmmss')}.ics`,
fileName: `ghostfolio-draft${data.activities.length > 1 ? 's' : ''
}-${format(parseISO(data.meta.date), 'yyyyMMddHHmmss')}.ics`,
format: 'string'
});
});
Expand Down
9 changes: 6 additions & 3 deletions apps/client/src/app/services/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { map } from 'rxjs/operators';
providedIn: 'root'
})
export class DataService {
public constructor(private http: HttpClient) {}
public constructor(private http: HttpClient) { }
GerardPolloRebozado marked this conversation as resolved.
Show resolved Hide resolved

public buildFiltersAsQueryParams({ filters }: { filters?: Filter[] }) {
let params = new HttpParams();
Expand Down Expand Up @@ -256,8 +256,11 @@ export class DataService {
return this.http.delete<any>(`/api/v1/account-balance/${aId}`);
}

public deleteActivity(aId: string) {
dtslvr marked this conversation as resolved.
Show resolved Hide resolved
return this.http.delete<any>(`/api/v1/order/${aId}`);
public deleteAllOrders({
GerardPolloRebozado marked this conversation as resolved.
Show resolved Hide resolved
filters
}) {
let params = this.buildFiltersAsQueryParams({ filters });
return this.http.delete<any>(`/api/v1/order`, { params });
}

public deleteAllActivities() {
GerardPolloRebozado marked this conversation as resolved.
Show resolved Hide resolved
Expand Down