Browse Source

fix: Applied comments from review

pull/3394/head
gerardPolloRebozado 1 year ago
parent
commit
51b45a672f
  1. 11
      apps/api/src/app/order/order.controller.ts
  2. 73
      apps/api/src/app/order/order.service.ts
  3. 2
      apps/client/src/app/pages/portfolio/activities/activities-page.component.ts
  4. 13
      apps/client/src/app/services/data.service.ts

11
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')

73
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<number> {
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<number> {
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: <string>where.userId
})
new PortfolioChangedEvent({ userId: where.userId as string })
);
return count;
}
public async getLatestOrder({ dataSource, symbol }: UniqueAsset) {
return this.prismaService.order.findFirst({
orderBy: {

2
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))

13
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<any>(`/api/v1/account-balance/${aId}`);
}
public deleteActivity(aId: string) {
return this.http.delete<any>(`/api/v1/order/${aId}`);
}
public deleteAllOrders({
filters
}) {
@ -263,8 +267,11 @@ export class DataService {
return this.http.delete<any>(`/api/v1/order`, { params });
}
public deleteAllActivities() {
return this.http.delete<any>(`/api/v1/order`);
public deleteAllActivities({
filters
}) {
let params = this.buildFiltersAsQueryParams({ filters });
return this.http.delete<any>(`/api/v1/order`, { params });
}
public deleteBenchmark({ dataSource, symbol }: UniqueAsset) {

Loading…
Cancel
Save