Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Refactor order service (#2751)

Thomas Kaul28706d7b268c
diff --git a/apps/api/src/app/export/export.controller.ts b/apps/api/src/app/export/export.controller.ts
index ce02d9835d..51b7bf6327 100644
--- a/apps/api/src/app/export/export.controller.ts
+++ b/apps/api/src/app/export/export.controller.ts
@@ -20,6 +20,7 @@ export class ExportController {
): Promise<Export> {
return this.exportService.export({
activityIds,
+ userCurrency: this.request.user.Settings.settings.baseCurrency,
userId: this.request.user.id
});
}
diff --git a/apps/api/src/app/export/export.service.ts b/apps/api/src/app/export/export.service.ts
index 2134a6520b..031111d7f5 100644
--- a/apps/api/src/app/export/export.service.ts
+++ b/apps/api/src/app/export/export.service.ts
@@ -13,9 +13,11 @@ export class ExportService {
public async export({
activityIds,
+ userCurrency,
userId
}: {
activityIds?: string[];
+ userCurrency: string;
userId: string;
}): Promise<Export> {
const accounts = (
@@ -39,10 +41,13 @@ export class ExportService {
}
);
- let activities = await this.orderService.orders({
- include: { SymbolProfile: true },
- orderBy: { date: 'desc' },
- where: { userId }
+ let { activities } = await this.orderService.getOrders({
+ userCurrency,
+ userId,
+ includeDrafts: true,
+ sortColumn: 'date',
+ sortDirection: 'asc',
+ withExcludedAccounts: true
});
if (activityIds) {
diff --git a/apps/api/src/app/import/import.service.ts b/apps/api/src/app/import/import.service.ts
index f89c577702..d15e8d4375 100644
--- a/apps/api/src/app/import/import.service.ts
+++ b/apps/api/src/app/import/import.service.ts
@@ -236,6 +236,7 @@ export class ImportService {
const activitiesExtendedWithErrors = await this.extendActivitiesWithErrors({
activitiesDto,
+ userCurrency,
userId
});
@@ -459,15 +460,18 @@ export class ImportService {
private async extendActivitiesWithErrors({
activitiesDto,
+ userCurrency,
userId
}: {
activitiesDto: Partial<CreateOrderDto>[];
+ userCurrency: string;
userId: string;
}): Promise<Partial<Activity>[]> {
- const existingActivities = await this.orderService.orders({
- include: { SymbolProfile: true },
- orderBy: { date: 'desc' },
- where: { userId }
+ let { activities: existingActivities } = await this.orderService.getOrders({
+ userCurrency,
+ userId,
+ includeDrafts: true,
+ withExcludedAccounts: true
});
return activitiesDto.map(
diff --git a/apps/api/src/app/order/order.service.ts b/apps/api/src/app/order/order.service.ts
index 574bfdcd27..096aba6f58 100644
--- a/apps/api/src/app/order/order.service.ts
+++ b/apps/api/src/app/order/order.service.ts
@@ -25,7 +25,7 @@ import { endOfToday, isAfter } from 'date-fns';
import { groupBy } from 'lodash';
import { v4 as uuidv4 } from 'uuid';
-import { Activities, Activity } from './interfaces/activities.interface';
+import { Activities } from './interfaces/activities.interface';
@Injectable()
export class OrderService {
@@ -37,34 +37,6 @@ export class OrderService {
private readonly symbolProfileService: SymbolProfileService
) {}
- public async order(
- orderWhereUniqueInput: Prisma.OrderWhereUniqueInput
- ): Promise<Order | null> {
- return this.prismaService.order.findUnique({
- where: orderWhereUniqueInput
- });
- }
-
- public async orders(params: {
- include?: Prisma.OrderInclude;
- skip?: number;
- take?: number;
- cursor?: Prisma.OrderWhereUniqueInput;
- where?: Prisma.OrderWhereInput;
- orderBy?: Prisma.Enumerable<Prisma.OrderOrderByWithRelationInput>;
- }): Promise<OrderWithAccount[]> {
- const { include, skip, take, cursor, where, orderBy } = params;
-
- return this.prismaService.order.findMany({
- cursor,
- include,
- orderBy,
- skip,
- take,
- where
- });
- }
-
public async createOrder(
data: Prisma.OrderCreateInput & {
accountId?: string;
@@ -379,6 +351,14 @@ export class OrderService {
return { activities, count };
}
+ public async order(
+ orderWhereUniqueInput: Prisma.OrderWhereUniqueInput
+ ): Promise<Order | null> {
+ return this.prismaService.order.findUnique({
+ where: orderWhereUniqueInput
+ });
+ }
+
public async updateOrder({
data,
where
@@ -455,4 +435,24 @@ export class OrderService {
where
});
}
+
+ private async orders(params: {
+ include?: Prisma.OrderInclude;
+ skip?: number;
+ take?: number;
+ cursor?: Prisma.OrderWhereUniqueInput;
+ where?: Prisma.OrderWhereInput;
+ orderBy?: Prisma.Enumerable<Prisma.OrderOrderByWithRelationInput>;
+ }): Promise<OrderWithAccount[]> {
+ const { include, skip, take, cursor, where, orderBy } = params;
+
+ return this.prismaService.order.findMany({
+ cursor,
+ include,
+ orderBy,
+ skip,
+ take,
+ where
+ });
+ }
}