Просмотр исходного кода

refactor(core): Add relations to the getFulfillmentsLinesForOrderLine method (#2314)

Alexis Vigoureux 2 лет назад
Родитель
Сommit
2eb4b232a4
1 измененных файлов с 17 добавлено и 11 удалено
  1. 17 11
      packages/core/src/service/services/fulfillment.service.ts

+ 17 - 11
packages/core/src/service/services/fulfillment.service.ts

@@ -3,9 +3,10 @@ import { ConfigurableOperationInput, OrderLineInput } from '@vendure/common/lib/
 import { ID } from '@vendure/common/lib/shared-types';
 import { isObject } from '@vendure/common/lib/shared-utils';
 import { unique } from '@vendure/common/lib/unique';
-import { In } from 'typeorm';
+import { In, Not } from 'typeorm';
 
 import { RequestContext } from '../../api/common/request-context';
+import { RelationPaths } from '../../api/index';
 import {
     CreateFulfillmentError,
     FulfillmentStateTransitionError,
@@ -127,16 +128,21 @@ export class FulfillmentService {
             .then(fulfillment => fulfillment.lines);
     }
 
-    async getFulfillmentsLinesForOrderLine(ctx: RequestContext, orderLineId: ID): Promise<FulfillmentLine[]> {
-        const fulfillmentLines = await this.connection
-            .getRepository(ctx, FulfillmentLine)
-            .createQueryBuilder('fulfillmentLine')
-            .leftJoin('fulfillmentLine.fulfillment', 'fulfillment')
-            .where('fulfillmentLine.orderLineId = :orderLineId', { orderLineId })
-            .andWhere('fulfillment.state != :cancelledState', { cancelledState: 'Cancelled' })
-            .getMany();
-
-        return fulfillmentLines;
+    async getFulfillmentsLinesForOrderLine(
+        ctx: RequestContext,
+        orderLineId: ID,
+        relations: RelationPaths<FulfillmentLine> = [],
+    ): Promise<FulfillmentLine[]> {
+        const defaultRelations = ['fulfillment'];
+        return this.connection.getRepository(ctx, FulfillmentLine).find({
+            relations: Array.from(new Set([...defaultRelations, ...relations])),
+            where: {
+                fulfillment: {
+                    state: Not('Cancelled'),
+                },
+                orderLineId,
+            },
+        });
     }
 
     /**