Browse Source

fix(core): Do not error when querying fulfillment on empty order

Fixes #639
Michael Bromley 5 years ago
parent
commit
b0c04577f4

+ 19 - 0
packages/core/e2e/order.e2e-spec.ts

@@ -54,6 +54,7 @@ import {
 } from './graphql/generated-e2e-admin-types';
 import {
     AddItemToOrder,
+    ApplyCouponCode,
     DeletionResult,
     GetActiveOrder,
     GetOrderByCodeWithPayments,
@@ -76,6 +77,7 @@ import {
 } from './graphql/shared-definitions';
 import {
     ADD_ITEM_TO_ORDER,
+    APPLY_COUPON_CODE,
     GET_ACTIVE_ORDER,
     GET_ORDER_BY_CODE_WITH_PAYMENTS,
 } from './graphql/shop-definitions';
@@ -340,6 +342,23 @@ describe('Orders resolver', () => {
         let f2Id: string;
         let f3Id: string;
 
+        // https://github.com/vendure-ecommerce/vendure/issues/639
+        it('returns fulfillments for Order with no lines', async () => {
+            // Apply a coupon code just to create an active order with no OrderLines
+            await shopClient.query<ApplyCouponCode.Mutation, ApplyCouponCode.Variables>(APPLY_COUPON_CODE, {
+                couponCode: 'TEST',
+            });
+            const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
+            const { order } = await adminClient.query<
+                GetOrderFulfillments.Query,
+                GetOrderFulfillments.Variables
+            >(GET_ORDER_FULFILLMENTS, {
+                id: activeOrder!.id,
+            });
+
+            expect(order?.fulfillments).toEqual([]);
+        });
+
         it('return error result if lines is empty', async () => {
             const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
                 id: orderId,

+ 1 - 1
packages/core/src/service/services/order.service.ts

@@ -906,7 +906,7 @@ export class OrderService {
 
     async getOrderFulfillments(ctx: RequestContext, order: Order): Promise<Fulfillment[]> {
         let lines: OrderLine[];
-        if (order.lines?.[0].items?.[0]?.fulfillments !== undefined) {
+        if (order.lines?.[0]?.items?.[0]?.fulfillments !== undefined) {
             lines = order.lines;
         } else {
             lines = await this.connection.getRepository(ctx, OrderLine).find({