Explorar el Código

feat(server): Create default Address from Order shipping address

Michael Bromley hace 7 años
padre
commit
971b1ad829
Se han modificado 2 ficheros con 107 adiciones y 3 borrados
  1. 92 2
      server/e2e/order.e2e-spec.ts
  2. 15 1
      server/src/api/resolvers/order.resolver.ts

+ 92 - 2
server/e2e/order.e2e-spec.ts

@@ -1,8 +1,11 @@
 import gql from 'graphql-tag';
 import path from 'path';
 
-import { GET_CUSTOMER_LIST } from '../../admin-ui/src/app/data/definitions/customer-definitions';
-import { CreateAddressInput, GetCustomerList } from '../../shared/generated-types';
+import {
+    GET_CUSTOMER,
+    GET_CUSTOMER_LIST,
+} from '../../admin-ui/src/app/data/definitions/customer-definitions';
+import { CreateAddressInput, GetCustomer, GetCustomerList } from '../../shared/generated-types';
 import { PaymentMethodHandler } from '../src/config/payment-method/payment-method-handler';
 
 import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
@@ -36,6 +39,7 @@ describe('Orders', () => {
     describe('as anonymous user', () => {
         let firstOrderItemId: string;
         let createdCustomerId: string;
+        let orderCode: string;
 
         beforeAll(async () => {
             await client.asAnonymousUser();
@@ -65,6 +69,7 @@ describe('Orders', () => {
             expect(result.addItemToOrder.lines[0].productVariant.id).toBe('T_1');
             expect(result.addItemToOrder.lines[0].id).toBe('T_1');
             firstOrderItemId = result.addItemToOrder.lines[0].id;
+            orderCode = result.addItemToOrder.code;
         });
 
         it(
@@ -219,11 +224,93 @@ describe('Orders', () => {
             expect(customer.id).toBe(createdCustomerId);
         });
 
+        it('setOrderShippingAddress sets shipping address', async () => {
+            const address: CreateAddressInput = {
+                fullName: 'name',
+                company: 'company',
+                streetLine1: '12 the street',
+                streetLine2: 'line 2',
+                city: 'foo',
+                province: 'bar',
+                postalCode: '123456',
+                countryCode: 'US',
+                phoneNumber: '4444444',
+            };
+            const result = await client.query(SET_SHIPPING_ADDRESS, {
+                input: address,
+            });
+
+            expect(result.setOrderShippingAddress.shippingAddress).toEqual({
+                fullName: 'name',
+                company: 'company',
+                streetLine1: '12 the street',
+                streetLine2: 'line 2',
+                city: 'foo',
+                province: 'bar',
+                postalCode: '123456',
+                country: 'United States of America',
+                phoneNumber: '4444444',
+            });
+        });
+
+        it('customer default Addresses are not updated before payment', async () => {
+            const result = await client.query(gql`
+                query {
+                    activeOrder {
+                        customer {
+                            addresses {
+                                id
+                            }
+                        }
+                    }
+                }
+            `);
+
+            expect(result.activeOrder.customer.addresses).toEqual([]);
+        });
+
         it('can transition to ArrangingPayment once Customer has been set', async () => {
             const result = await client.query(TRANSITION_TO_STATE, { state: 'ArrangingPayment' });
 
             expect(result.transitionOrderToState).toEqual({ id: 'T_1', state: 'ArrangingPayment' });
         });
+
+        it('adds a successful payment and transitions Order state', async () => {
+            const result = await client.query(ADD_PAYMENT, {
+                input: {
+                    method: testPaymentMethod.code,
+                    metadata: {},
+                },
+            });
+
+            const payment = result.addPaymentToOrder.payments[0];
+            expect(result.addPaymentToOrder.state).toBe('PaymentSettled');
+            expect(result.addPaymentToOrder.active).toBe(false);
+            expect(result.addPaymentToOrder.payments.length).toBe(1);
+            expect(payment.method).toBe(testPaymentMethod.code);
+            expect(payment.state).toBe('Settled');
+        });
+
+        it('activeOrder is null after payment', async () => {
+            const result = await client.query(GET_ACTIVE_ORDER);
+
+            expect(result.activeOrder).toBeNull();
+        });
+
+        it('customer default Addresses are updated after payment', async () => {
+            await client.asSuperAdmin();
+
+            const result = await client.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
+                id: createdCustomerId,
+            });
+
+            // tslint:disable-next-line:no-non-null-assertion
+            const address = result.customer!.addresses![0];
+            expect(address.streetLine1).toBe('12 the street');
+            expect(address.postalCode).toBe('123456');
+            expect(address.defaultBillingAddress).toBe(true);
+            expect(address.defaultShippingAddress).toBe(true);
+        });
     });
 
     describe('as authenticated user', () => {
@@ -609,6 +696,9 @@ const TEST_ORDER_FRAGMENT = gql`
             code
             description
         }
+        customer {
+            id
+        }
     }
 `;
 

+ 15 - 1
server/src/api/resolvers/order.resolver.ts

@@ -231,7 +231,21 @@ export class OrderResolver {
             const sessionOrder = await this.getOrderFromContext(ctx);
             if (sessionOrder) {
                 const order = await this.orderService.addPaymentToOrder(ctx, sessionOrder.id, args.input);
-
+                if (order.active === false) {
+                    const { customer } = order;
+                    // If the Customer has no addresses yet, use the shipping address data
+                    // to populate the initial default Address.
+                    if (customer && (!customer.addresses || customer.addresses.length === 0)) {
+                        const address = order.shippingAddress;
+                        await this.customerService.createAddress(ctx, customer.id as string, {
+                            ...address,
+                            streetLine1: address.streetLine1 || '',
+                            countryCode: address.countryCode || '',
+                            defaultBillingAddress: true,
+                            defaultShippingAddress: true,
+                        });
+                    }
+                }
                 if (
                     order.active === false &&
                     ctx.session &&