Przeglądaj źródła

fix(core): Resolve OrderItem.fulfillment

Fixes #1381
Michael Bromley 4 lat temu
rodzic
commit
6a9efe9fff

+ 1 - 1
packages/admin-ui/src/lib/core/src/common/generated-types.ts

@@ -4058,7 +4058,7 @@ export type Query = {
   assets: AssetList;
   channel?: Maybe<Channel>;
   channels: Array<Channel>;
-  /** Get a Collection either by id or slug. If neither id nor slug is speicified, an error will result. */
+  /** Get a Collection either by id or slug. If neither id nor slug is specified, an error will result. */
   collection?: Maybe<Collection>;
   collectionFilters: Array<ConfigurableOperationDefinition>;
   collections: CollectionList;

Plik diff jest za duży
+ 489 - 475
packages/asset-server-plugin/e2e/graphql/generated-e2e-asset-server-plugin-types.ts


+ 2 - 2
packages/common/src/generated-shop-types.ts

@@ -2595,7 +2595,7 @@ export type Query = {
     activeCustomer?: Maybe<Customer>;
     /**
      * The active Order. Will be `null` until an Order is created via `addItemToOrder`. Once an Order reaches the
-     * state of `PaymentApproved` or `PaymentSettled`, then that Order is no longer considered "active" and this
+     * state of `PaymentAuthorized` or `PaymentSettled`, then that Order is no longer considered "active" and this
      * query will once again return `null`.
      */
     activeOrder?: Maybe<Order>;
@@ -2603,7 +2603,7 @@ export type Query = {
     availableCountries: Array<Country>;
     /** A list of Collections available to the shop */
     collections: CollectionList;
-    /** Returns a Collection either by its id or slug. If neither 'id' nor 'slug' is speicified, an error will result. */
+    /** Returns a Collection either by its id or slug. If neither 'id' nor 'slug' is specified, an error will result. */
     collection?: Maybe<Collection>;
     /** Returns a list of eligible shipping methods based on the current active Order */
     eligibleShippingMethods: Array<ShippingMethodQuote>;

+ 1 - 1
packages/common/src/generated-types.ts

@@ -3995,7 +3995,7 @@ export type Query = {
   channel?: Maybe<Channel>;
   activeChannel: Channel;
   collections: CollectionList;
-  /** Get a Collection either by id or slug. If neither id nor slug is speicified, an error will result. */
+  /** Get a Collection either by id or slug. If neither id nor slug is specified, an error will result. */
   collection?: Maybe<Collection>;
   collectionFilters: Array<ConfigurableOperationDefinition>;
   countries: CountryList;

Plik diff jest za duży
+ 489 - 475
packages/core/e2e/graphql/generated-e2e-admin-types.ts


+ 95 - 2
packages/core/e2e/graphql/generated-e2e-shop-types.ts

@@ -2505,7 +2505,7 @@ export type Query = {
     activeCustomer?: Maybe<Customer>;
     /**
      * The active Order. Will be `null` until an Order is created via `addItemToOrder`. Once an Order reaches the
-     * state of `PaymentApproved` or `PaymentSettled`, then that Order is no longer considered "active" and this
+     * state of `PaymentAuthorized` or `PaymentSettled`, then that Order is no longer considered "active" and this
      * query will once again return `null`.
      */
     activeOrder?: Maybe<Order>;
@@ -2513,7 +2513,7 @@ export type Query = {
     availableCountries: Array<Country>;
     /** A list of Collections available to the shop */
     collections: CollectionList;
-    /** Returns a Collection either by its id or slug. If neither 'id' nor 'slug' is speicified, an error will result. */
+    /** Returns a Collection either by its id or slug. If neither 'id' nor 'slug' is specified, an error will result. */
     collection?: Maybe<Collection>;
     /** Returns a list of eligible shipping methods based on the current active Order */
     eligibleShippingMethods: Array<ShippingMethodQuote>;
@@ -3439,6 +3439,32 @@ export type GetOrderByCodeWithPaymentsQueryVariables = Exact<{
 
 export type GetOrderByCodeWithPaymentsQuery = { orderByCode?: Maybe<TestOrderWithPaymentsFragment> };
 
+export type GetActiveCustomerOrderWithItemFulfillmentsQueryVariables = Exact<{
+    code: Scalars['String'];
+}>;
+
+export type GetActiveCustomerOrderWithItemFulfillmentsQuery = {
+    activeCustomer?: Maybe<{
+        orders: Pick<OrderList, 'totalItems'> & {
+            items: Array<
+                Pick<Order, 'id' | 'code' | 'state'> & {
+                    lines: Array<
+                        Pick<OrderLine, 'id'> & {
+                            items: Array<
+                                Pick<OrderItem, 'id'> & {
+                                    fulfillment?: Maybe<
+                                        Pick<Fulfillment, 'id' | 'state' | 'method' | 'trackingCode'>
+                                    >;
+                                }
+                            >;
+                        }
+                    >;
+                }
+            >;
+        };
+    }>;
+};
+
 export type GetNextOrderStatesQueryVariables = Exact<{ [key: string]: never }>;
 
 export type GetNextOrderStatesQuery = Pick<Query, 'nextOrderStates'>;
@@ -3958,6 +3984,73 @@ export namespace GetOrderByCodeWithPayments {
     export type OrderByCode = NonNullable<GetOrderByCodeWithPaymentsQuery['orderByCode']>;
 }
 
+export namespace GetActiveCustomerOrderWithItemFulfillments {
+    export type Variables = GetActiveCustomerOrderWithItemFulfillmentsQueryVariables;
+    export type Query = GetActiveCustomerOrderWithItemFulfillmentsQuery;
+    export type ActiveCustomer = NonNullable<
+        GetActiveCustomerOrderWithItemFulfillmentsQuery['activeCustomer']
+    >;
+    export type Orders = NonNullable<
+        NonNullable<GetActiveCustomerOrderWithItemFulfillmentsQuery['activeCustomer']>['orders']
+    >;
+    export type Items = NonNullable<
+        NonNullable<
+            NonNullable<
+                NonNullable<GetActiveCustomerOrderWithItemFulfillmentsQuery['activeCustomer']>['orders']
+            >['items']
+        >[number]
+    >;
+    export type Lines = NonNullable<
+        NonNullable<
+            NonNullable<
+                NonNullable<
+                    NonNullable<
+                        NonNullable<
+                            GetActiveCustomerOrderWithItemFulfillmentsQuery['activeCustomer']
+                        >['orders']
+                    >['items']
+                >[number]
+            >['lines']
+        >[number]
+    >;
+    export type _Items = NonNullable<
+        NonNullable<
+            NonNullable<
+                NonNullable<
+                    NonNullable<
+                        NonNullable<
+                            NonNullable<
+                                NonNullable<
+                                    GetActiveCustomerOrderWithItemFulfillmentsQuery['activeCustomer']
+                                >['orders']
+                            >['items']
+                        >[number]
+                    >['lines']
+                >[number]
+            >['items']
+        >[number]
+    >;
+    export type Fulfillment = NonNullable<
+        NonNullable<
+            NonNullable<
+                NonNullable<
+                    NonNullable<
+                        NonNullable<
+                            NonNullable<
+                                NonNullable<
+                                    NonNullable<
+                                        GetActiveCustomerOrderWithItemFulfillmentsQuery['activeCustomer']
+                                    >['orders']
+                                >['items']
+                            >[number]
+                        >['lines']
+                    >[number]
+                >['items']
+            >[number]
+        >['fulfillment']
+    >;
+}
+
 export namespace GetNextOrderStates {
     export type Variables = GetNextOrderStatesQueryVariables;
     export type Query = GetNextOrderStatesQuery;

+ 29 - 0
packages/core/e2e/graphql/shop-definitions.ts

@@ -613,6 +613,35 @@ export const GET_ORDER_BY_CODE_WITH_PAYMENTS = gql`
     ${TEST_ORDER_WITH_PAYMENTS_FRAGMENT}
 `;
 
+export const GET_ACTIVE_ORDER_CUSTOMER_WITH_ITEM_FULFILLMENTS = gql`
+    query GetActiveCustomerOrderWithItemFulfillments {
+        activeCustomer {
+            orders(
+                options: { skip: 0, take: 5, sort: { createdAt: DESC }, filter: { active: { eq: false } } }
+            ) {
+                totalItems
+                items {
+                    id
+                    code
+                    state
+                    lines {
+                        id
+                        items {
+                            id
+                            fulfillment {
+                                id
+                                state
+                                method
+                                trackingCode
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+`;
+
 export const GET_NEXT_STATES = gql`
     query GetNextOrderStates {
         nextOrderStates

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

@@ -70,6 +70,7 @@ import {
     AddPaymentToOrder,
     ApplyCouponCode,
     DeletionResult,
+    GetActiveCustomerOrderWithItemFulfillments,
     GetActiveCustomerWithOrdersProductSlug,
     GetActiveOrder,
     GetOrderByCodeWithPayments,
@@ -102,6 +103,7 @@ import {
     APPLY_COUPON_CODE,
     GET_ACTIVE_CUSTOMER_WITH_ORDERS_PRODUCT_SLUG,
     GET_ACTIVE_ORDER,
+    GET_ACTIVE_ORDER_CUSTOMER_WITH_ITEM_FULFILLMENTS,
     GET_ORDER_BY_CODE_WITH_PAYMENTS,
     SET_SHIPPING_ADDRESS,
     SET_SHIPPING_METHOD,
@@ -983,6 +985,18 @@ describe('Orders resolver', () => {
             expect(order!.fulfillments![1].orderItems).toEqual([{ id: 'T_4' }, { id: 'T_5' }, { id: 'T_6' }]);
             expect(order!.fulfillments![2].orderItems).toEqual([{ id: 'T_4' }, { id: 'T_5' }, { id: 'T_6' }]);
         });
+
+        it('order.line.items.fulfillment resolver', async () => {
+            const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
+                id: orderId,
+            });
+            const { activeCustomer } = await shopClient.query<
+                GetActiveCustomerOrderWithItemFulfillments.Query,
+                GetActiveCustomerOrderWithItemFulfillments.Variables
+            >(GET_ACTIVE_ORDER_CUSTOMER_WITH_ITEM_FULFILLMENTS);
+            const firstCustomerOrder = activeCustomer!.orders.items[0]!;
+            expect(firstCustomerOrder.lines[0].items[0].fulfillment).not.toBeNull();
+        });
     });
 
     describe('cancellation by orderId', () => {

+ 2 - 0
packages/core/src/api/api-internal-modules.ts

@@ -52,6 +52,7 @@ import {
 } from './resolvers/entity/fulfillment-entity.resolver';
 import { JobEntityResolver } from './resolvers/entity/job-entity.resolver';
 import { OrderAdminEntityResolver, OrderEntityResolver } from './resolvers/entity/order-entity.resolver';
+import { OrderItemEntityResolver } from './resolvers/entity/order-item-entity.resolver';
 import { OrderLineEntityResolver } from './resolvers/entity/order-line-entity.resolver';
 import {
     PaymentAdminEntityResolver,
@@ -123,6 +124,7 @@ export const entityResolvers = [
     FacetValueEntityResolver,
     FulfillmentEntityResolver,
     OrderEntityResolver,
+    OrderItemEntityResolver,
     OrderLineEntityResolver,
     PaymentEntityResolver,
     ProductEntityResolver,

+ 22 - 0
packages/core/src/api/resolvers/entity/order-item-entity.resolver.ts

@@ -0,0 +1,22 @@
+import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
+
+import { Fulfillment, OrderItem } from '../../../entity';
+import { FulfillmentService } from '../../../service';
+import { RequestContext } from '../../common/request-context';
+import { Ctx } from '../../decorators/request-context.decorator';
+
+@Resolver('OrderItem')
+export class OrderItemEntityResolver {
+    constructor(private fulfillmentService: FulfillmentService) {}
+
+    @ResolveField()
+    async fulfillment(
+        @Ctx() ctx: RequestContext,
+        @Parent() orderItem: OrderItem,
+    ): Promise<Fulfillment | undefined> {
+        if (orderItem.fulfillment) {
+            return orderItem.fulfillment;
+        }
+        return this.fulfillmentService.getFulfillmentByOrderItemId(ctx, orderItem.id);
+    }
+}

+ 14 - 0
packages/core/src/service/services/fulfillment.service.ts

@@ -115,6 +115,20 @@ export class FulfillmentService {
         return fulfillment.orderItems;
     }
 
+    /**
+     * @description
+     * Returns the Fulfillment for the given OrderItem (if one exists).
+     */
+    async getFulfillmentByOrderItemId(
+        ctx: RequestContext,
+        orderItemId: ID,
+    ): Promise<Fulfillment | undefined> {
+        const orderItem = await this.connection
+            .getRepository(ctx, OrderItem)
+            .findOne(orderItemId, { relations: ['fulfillments'] });
+        return orderItem?.fulfillment;
+    }
+
     /**
      * @description
      * Transitions the specified Fulfillment to a new state and upon successful transition

Plik diff jest za duży
+ 489 - 475
packages/elasticsearch-plugin/e2e/graphql/generated-e2e-elasticsearch-plugin-types.ts


Plik diff jest za duży
+ 489 - 475
packages/payments-plugin/e2e/graphql/generated-admin-types.ts


+ 2 - 2
packages/payments-plugin/e2e/graphql/generated-shop-types.ts

@@ -2505,7 +2505,7 @@ export type Query = {
     activeCustomer?: Maybe<Customer>;
     /**
      * The active Order. Will be `null` until an Order is created via `addItemToOrder`. Once an Order reaches the
-     * state of `PaymentApproved` or `PaymentSettled`, then that Order is no longer considered "active" and this
+     * state of `PaymentAuthorized` or `PaymentSettled`, then that Order is no longer considered "active" and this
      * query will once again return `null`.
      */
     activeOrder?: Maybe<Order>;
@@ -2513,7 +2513,7 @@ export type Query = {
     availableCountries: Array<Country>;
     /** A list of Collections available to the shop */
     collections: CollectionList;
-    /** Returns a Collection either by its id or slug. If neither 'id' nor 'slug' is speicified, an error will result. */
+    /** Returns a Collection either by its id or slug. If neither 'id' nor 'slug' is specified, an error will result. */
     collection?: Maybe<Collection>;
     /** Returns a list of eligible shipping methods based on the current active Order */
     eligibleShippingMethods: Array<ShippingMethodQuote>;

Plik diff jest za duży
+ 0 - 0
schema-admin.json


Plik diff jest za duży
+ 0 - 0
schema-shop.json


Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików