Browse Source

feat(core): Add removeAllOrderLines mutation in Shop API

Closes #430
Jonathan Célio 5 years ago
parent
commit
841e352cc3

+ 1 - 0
docs/content/docs/storefront/shop-api-guide.md

@@ -37,6 +37,7 @@ There are a couple of query parameters which are valid for all GraphQL operation
 * {{< shop-api-operation operation="addItemToOrder" type="mutation" >}} adds an item to the order. The first time it is called will also create a new Order for the session.
 * {{< shop-api-operation operation="adjustOrderLine" type="mutation" >}} is used to adjust the quantity of an OrderLine.
 * {{< shop-api-operation operation="removeOrderLine" type="mutation" >}} removes an OrderLine from the Order.
+* {{< shop-api-operation operation="removeAllOrderLines" type="mutation" >}} removes all OrderLine from the Order.
 * {{< shop-api-operation operation="setCustomerForOrder" type="mutation" >}} specifies the Customer details (required if the customer is not logged in as an authenticated user).
 * {{< shop-api-operation operation="setOrderShippingAddress" type="mutation" >}} sets the shipping address for the Order.
 * {{< shop-api-operation operation="eligibleShippingMethods" type="mutation" >}} returns all available shipping methods based on the customer's shipping address and the contents of the Order.

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

@@ -1346,6 +1346,8 @@ export type Mutation = {
     addItemToOrder?: Maybe<Order>;
     /** Remove an OrderLine from the Order */
     removeOrderLine?: Maybe<Order>;
+    /** Remove all OrderLine from the Order */
+    removeAllOrderLines?: Maybe<Order>;
     /**
      * Adjusts an OrderLine. If custom fields are defined on the OrderLine entity, a
      * third argument 'customFields' of type `OrderLineCustomFieldsInput` will be available.

+ 14 - 0
packages/core/e2e/graphql/generated-e2e-shop-types.ts

@@ -1346,6 +1346,8 @@ export type Mutation = {
     addItemToOrder?: Maybe<Order>;
     /** Remove an OrderLine from the Order */
     removeOrderLine?: Maybe<Order>;
+    /** Remove all OrderLine from the Order */
+    removeAllOrderLines?: Maybe<Order>;
     /**
      * Adjusts an OrderLine. If custom fields are defined on the OrderLine entity, a
      * third argument 'customFields' of type `OrderLineCustomFieldsInput` will be available.
@@ -2825,6 +2827,12 @@ export type RemoveCouponCodeMutation = { __typename?: 'Mutation' } & {
     removeCouponCode?: Maybe<{ __typename?: 'Order' } & TestOrderFragmentFragment>;
 };
 
+export type RemoveAllOrderLinesMutationVariables = {};
+
+export type RemoveAllOrderLinesMutation = { __typename?: 'Mutation' } & {
+    removeAllOrderLines?: Maybe<{ __typename?: 'Order' } & TestOrderFragmentFragment>;
+};
+
 type DiscriminateUnion<T, U> = T extends U ? T : never;
 
 type RequireField<T, TNames extends string> = T & { [P in TNames]: (T & { [name: string]: never })[P] };
@@ -3097,3 +3105,9 @@ export namespace RemoveCouponCode {
     export type Mutation = RemoveCouponCodeMutation;
     export type RemoveCouponCode = TestOrderFragmentFragment;
 }
+
+export namespace RemoveAllOrderLines {
+    export type Variables = RemoveAllOrderLinesMutationVariables;
+    export type Mutation = RemoveAllOrderLinesMutation;
+    export type RemoveAllOrderLines = TestOrderFragmentFragment;
+}

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

@@ -426,3 +426,12 @@ export const REMOVE_COUPON_CODE = gql`
     }
     ${TEST_ORDER_FRAGMENT}
 `;
+
+export const REMOVE_ALL_ORDER_LINES = gql`
+    mutation RemoveAllOrderLines {
+        removeAllOrderLines {
+            ...TestOrderFragment
+        }
+    }
+    ${TEST_ORDER_FRAGMENT}
+`;

+ 24 - 0
packages/core/e2e/shop-order.e2e-spec.ts

@@ -32,6 +32,7 @@ import {
     GetNextOrderStates,
     GetOrderByCode,
     GetShippingMethods,
+    RemoveAllOrderLines,
     RemoveItemFromOrder,
     SetBillingAddress,
     SetCustomerForOrder,
@@ -58,6 +59,7 @@ import {
     GET_ELIGIBLE_SHIPPING_METHODS,
     GET_NEXT_STATES,
     GET_ORDER_BY_CODE,
+    REMOVE_ALL_ORDER_LINES,
     REMOVE_ITEM_FROM_ORDER,
     SET_BILLING_ADDRESS,
     SET_CUSTOMER,
@@ -1156,6 +1158,28 @@ describe('Shop orders', () => {
             });
         });
     });
+
+    describe('remove all order lines', () => {
+        beforeAll(async () => {
+            await shopClient.asAnonymousUser();
+            await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
+                productVariantId: 'T_1',
+                quantity: 1,
+            });
+            await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
+                productVariantId: 'T_2',
+                quantity: 3,
+            });
+        });
+        it('should remove all order lines', async () => {
+            const { removeAllOrderLines } = await shopClient.query<
+                RemoveAllOrderLines.Mutation,
+                RemoveAllOrderLines.Variables
+            >(REMOVE_ALL_ORDER_LINES);
+            expect(removeAllOrderLines?.total).toBe(0);
+            expect(removeAllOrderLines?.lines.length).toBe(0);
+        });
+    });
 });
 
 const GET_ORDER_CUSTOM_FIELDS = gql`

+ 7 - 0
packages/core/src/api/resolvers/shop/shop-order.resolver.ts

@@ -258,6 +258,13 @@ export class ShopOrderResolver {
         return this.orderService.removeItemFromOrder(ctx, order.id, args.orderLineId);
     }
 
+    @Mutation()
+    @Allow(Permission.UpdateOrder, Permission.Owner)
+    async removeAllOrderLines(@Ctx() ctx: RequestContext): Promise<Order> {
+        const order = await this.getOrderFromContext(ctx, true);
+        return this.orderService.removeAllItemsFromOrder(ctx, order.id);
+    }
+
     @Mutation()
     @Allow(Permission.UpdateOrder, Permission.Owner)
     async applyCouponCode(

+ 2 - 0
packages/core/src/api/schema/shop-api/shop.api.graphql

@@ -46,6 +46,8 @@ type Mutation {
     addItemToOrder(productVariantId: ID!, quantity: Int!): Order
     "Remove an OrderLine from the Order"
     removeOrderLine(orderLineId: ID!): Order
+    "Remove all OrderLine from the Order"
+    removeAllOrderLines: Order
     "Adjusts an OrderLine. If custom fields are defined on the OrderLine entity, a third argument 'customFields' of type `OrderLineCustomFieldsInput` will be available."
     adjustOrderLine(orderLineId: ID!, quantity: Int): Order
     "Applies the given coupon code to the active Order"

+ 9 - 0
packages/core/src/service/services/order.service.ts

@@ -337,6 +337,15 @@ export class OrderService {
         return updatedOrder;
     }
 
+    async removeAllItemsFromOrder(ctx: RequestContext, orderId: ID): Promise<Order> {
+        const order = await this.getOrderOrThrow(ctx, orderId);
+        this.assertAddingItemsState(order);
+        await this.connection.getRepository(OrderLine).remove(order.lines);
+        order.lines = [];
+        const updatedOrder = await this.applyPriceAdjustments(ctx, order);
+        return updatedOrder;
+    }
+
     async applyCouponCode(ctx: RequestContext, orderId: ID, couponCode: string) {
         const order = await this.getOrderOrThrow(ctx, orderId);
         if (order.couponCodes.includes(couponCode)) {

File diff suppressed because it is too large
+ 0 - 0
schema-admin.json


File diff suppressed because it is too large
+ 0 - 0
schema-shop.json


Some files were not shown because too many files changed in this diff