فهرست منبع

feat(core): Add setOrderBillingAddress mutation to Shop API

Relates to #372
Michael Bromley 5 سال پیش
والد
کامیت
83347b27ac

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

@@ -1336,7 +1336,10 @@ export type Mutation = {
     /** Removes the given coupon code from the active Order */
     removeCouponCode?: Maybe<Order>;
     transitionOrderToState?: Maybe<Order>;
+    /** Sets the shipping address for this order */
     setOrderShippingAddress?: Maybe<Order>;
+    /** Sets the billing address for this order */
+    setOrderBillingAddress?: Maybe<Order>;
     setOrderShippingMethod?: Maybe<Order>;
     addPaymentToOrder?: Maybe<Order>;
     setCustomerForOrder?: Maybe<Order>;
@@ -1412,6 +1415,10 @@ export type MutationSetOrderShippingAddressArgs = {
     input: CreateAddressInput;
 };
 
+export type MutationSetOrderBillingAddressArgs = {
+    input: CreateAddressInput;
+};
+
 export type MutationSetOrderShippingMethodArgs = {
     shippingMethodId: Scalars['ID'];
 };

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

@@ -1336,7 +1336,10 @@ export type Mutation = {
     /** Removes the given coupon code from the active Order */
     removeCouponCode?: Maybe<Order>;
     transitionOrderToState?: Maybe<Order>;
+    /** Sets the shipping address for this order */
     setOrderShippingAddress?: Maybe<Order>;
+    /** Sets the billing address for this order */
+    setOrderBillingAddress?: Maybe<Order>;
     setOrderShippingMethod?: Maybe<Order>;
     addPaymentToOrder?: Maybe<Order>;
     setCustomerForOrder?: Maybe<Order>;
@@ -1412,6 +1415,10 @@ export type MutationSetOrderShippingAddressArgs = {
     input: CreateAddressInput;
 };
 
+export type MutationSetOrderBillingAddressArgs = {
+    input: CreateAddressInput;
+};
+
 export type MutationSetOrderShippingMethodArgs = {
     shippingMethodId: Scalars['ID'];
 };
@@ -2587,6 +2594,31 @@ export type SetShippingAddressMutation = { __typename?: 'Mutation' } & {
     >;
 };
 
+export type SetBillingAddressMutationVariables = {
+    input: CreateAddressInput;
+};
+
+export type SetBillingAddressMutation = { __typename?: 'Mutation' } & {
+    setOrderBillingAddress?: Maybe<
+        { __typename?: 'Order' } & {
+            billingAddress?: Maybe<
+                { __typename?: 'OrderAddress' } & Pick<
+                    OrderAddress,
+                    | 'fullName'
+                    | 'company'
+                    | 'streetLine1'
+                    | 'streetLine2'
+                    | 'city'
+                    | 'province'
+                    | 'postalCode'
+                    | 'country'
+                    | 'phoneNumber'
+                >
+            >;
+        }
+    >;
+};
+
 export type AddPaymentToOrderMutationVariables = {
     input: PaymentInput;
 };
@@ -2885,6 +2917,15 @@ export namespace SetShippingAddress {
     >;
 }
 
+export namespace SetBillingAddress {
+    export type Variables = SetBillingAddressMutationVariables;
+    export type Mutation = SetBillingAddressMutation;
+    export type SetOrderBillingAddress = NonNullable<SetBillingAddressMutation['setOrderBillingAddress']>;
+    export type BillingAddress = NonNullable<
+        NonNullable<SetBillingAddressMutation['setOrderBillingAddress']>['billingAddress']
+    >;
+}
+
 export namespace AddPaymentToOrder {
     export type Variables = AddPaymentToOrderMutationVariables;
     export type Mutation = AddPaymentToOrderMutation;

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

@@ -324,6 +324,24 @@ export const SET_SHIPPING_ADDRESS = gql`
     }
 `;
 
+export const SET_BILLING_ADDRESS = gql`
+    mutation SetBillingAddress($input: CreateAddressInput!) {
+        setOrderBillingAddress(input: $input) {
+            billingAddress {
+                fullName
+                company
+                streetLine1
+                streetLine2
+                city
+                province
+                postalCode
+                country
+                phoneNumber
+            }
+        }
+    }
+`;
+
 export const ADD_PAYMENT = gql`
     mutation AddPaymentToOrder($input: PaymentInput!) {
         addPaymentToOrder(input: $input) {

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

@@ -32,6 +32,7 @@ import {
     GetOrderByCode,
     GetShippingMethods,
     RemoveItemFromOrder,
+    SetBillingAddress,
     SetCustomerForOrder,
     SetShippingAddress,
     SetShippingMethod,
@@ -57,6 +58,7 @@ import {
     GET_NEXT_STATES,
     GET_ORDER_BY_CODE,
     REMOVE_ITEM_FROM_ORDER,
+    SET_BILLING_ADDRESS,
     SET_CUSTOMER,
     SET_SHIPPING_ADDRESS,
     SET_SHIPPING_METHOD,
@@ -382,6 +384,38 @@ describe('Shop orders', () => {
             });
         });
 
+        it('setOrderBillingAddress sets billing address', async () => {
+            const address: CreateAddressInput = {
+                fullName: 'name',
+                company: 'company',
+                streetLine1: '12 the street',
+                streetLine2: null,
+                city: 'foo',
+                province: 'bar',
+                postalCode: '123456',
+                countryCode: 'US',
+                phoneNumber: '4444444',
+            };
+            const { setOrderBillingAddress } = await shopClient.query<
+                SetBillingAddress.Mutation,
+                SetBillingAddress.Variables
+            >(SET_BILLING_ADDRESS, {
+                input: address,
+            });
+
+            expect(setOrderBillingAddress!.billingAddress).toEqual({
+                fullName: 'name',
+                company: 'company',
+                streetLine1: '12 the street',
+                streetLine2: null,
+                city: 'foo',
+                province: 'bar',
+                postalCode: '123456',
+                country: 'United States of America',
+                phoneNumber: '4444444',
+            });
+        });
+
         it('customer default Addresses are not updated before payment', async () => {
             const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
             const { customer } = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(

+ 18 - 1
packages/core/src/api/resolvers/shop/shop-order.resolver.ts

@@ -6,6 +6,7 @@ import {
     MutationApplyCouponCodeArgs,
     MutationRemoveOrderLineArgs,
     MutationSetCustomerForOrderArgs,
+    MutationSetOrderBillingAddressArgs,
     MutationSetOrderShippingAddressArgs,
     MutationSetOrderShippingMethodArgs,
     MutationTransitionOrderToStateArgs,
@@ -55,7 +56,7 @@ export class ShopOrderResolver {
                 skip: 0,
                 take: 99999,
             })
-            .then(data => data.items);
+            .then((data) => data.items);
     }
 
     @Query()
@@ -136,6 +137,22 @@ export class ShopOrderResolver {
         }
     }
 
+    @Mutation()
+    @Allow(Permission.Owner)
+    async setOrderBillingAddress(
+        @Ctx() ctx: RequestContext,
+        @Args() args: MutationSetOrderBillingAddressArgs,
+    ): Promise<Order | undefined> {
+        if (ctx.authorizedAsOwnerOnly) {
+            const sessionOrder = await this.getOrderFromContext(ctx);
+            if (sessionOrder) {
+                return this.orderService.setBillingAddress(ctx, sessionOrder.id, args.input);
+            } else {
+                return;
+            }
+        }
+    }
+
     @Query()
     @Allow(Permission.Owner)
     async eligibleShippingMethods(@Ctx() ctx: RequestContext): Promise<ShippingMethodQuote[]> {

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

@@ -27,7 +27,11 @@ type Mutation {
     "Removes the given coupon code from the active Order"
     removeCouponCode(couponCode: String!): Order
     transitionOrderToState(state: String!): Order
+    "Sets the shipping address for this order"
     setOrderShippingAddress(input: CreateAddressInput!): Order
+    "Sets the billing address for this order"
+    setOrderBillingAddress(input: CreateAddressInput!): Order
+    "Sets the shipping method by id, which can be obtained with the `eligibleShippingMethods` query"
     setOrderShippingMethod(shippingMethodId: ID!): Order
     addPaymentToOrder(input: PaymentInput!): Order
     setCustomerForOrder(input: CreateCustomerInput!): Order

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

@@ -371,6 +371,13 @@ export class OrderService {
         return this.connection.getRepository(Order).save(order);
     }
 
+    async setBillingAddress(ctx: RequestContext, orderId: ID, input: CreateAddressInput): Promise<Order> {
+        const order = await this.getOrderOrThrow(ctx, orderId);
+        const country = await this.countryService.findOneByCode(ctx, input.countryCode);
+        order.billingAddress = { ...input, countryCode: input.countryCode, country: country.name };
+        return this.connection.getRepository(Order).save(order);
+    }
+
     async getEligibleShippingMethods(ctx: RequestContext, orderId: ID): Promise<ShippingMethodQuote[]> {
         const order = await this.getOrderOrThrow(ctx, orderId);
         const eligibleMethods = await this.shippingCalculator.getEligibleShippingMethods(ctx, order);

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
schema-shop.json


برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است