Browse Source

feat(core): Record strategy used to register in Customer history

Michael Bromley 5 years ago
parent
commit
5504044eba

+ 35 - 2
packages/core/e2e/authentication-strategy.e2e-spec.ts

@@ -1,3 +1,4 @@
+import { pick } from '@vendure/common/lib/pick';
 import { mergeConfig } from '@vendure/core';
 import { createTestEnvironment } from '@vendure/testing';
 import gql from 'graphql-tag';
@@ -7,8 +8,14 @@ import { initialData } from '../../../e2e-common/e2e-initial-data';
 import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
 
 import { TestAuthenticationStrategy, VALID_AUTH_TOKEN } from './fixtures/test-authentication-strategies';
-import { Authenticate, GetCustomers, Me } from './graphql/generated-e2e-admin-types';
-import { ME } from './graphql/shared-definitions';
+import {
+    Authenticate,
+    GetCustomerHistory,
+    GetCustomers,
+    HistoryEntryType,
+    Me,
+} from './graphql/generated-e2e-admin-types';
+import { GET_CUSTOMER_HISTORY, ME } from './graphql/shared-definitions';
 import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
 
 describe('AuthenticationStrategy', () => {
@@ -39,6 +46,7 @@ describe('AuthenticationStrategy', () => {
             firstName: 'Cixin',
             lastName: 'Liu',
         };
+        let newCustomerId: string;
 
         it(
             'fails with a bad token',
@@ -73,6 +81,31 @@ describe('AuthenticationStrategy', () => {
                 'hayden.zieme12@hotmail.com',
                 userData.email,
             ]);
+            newCustomerId = after.items[1].id;
+        });
+
+        it('creates customer history entry', async () => {
+            const { customer } = await adminClient.query<
+                GetCustomerHistory.Query,
+                GetCustomerHistory.Variables
+            >(GET_CUSTOMER_HISTORY, {
+                id: newCustomerId,
+            });
+
+            expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
+                {
+                    type: HistoryEntryType.CUSTOMER_REGISTERED,
+                    data: {
+                        strategy: 'test_strategy',
+                    },
+                },
+                {
+                    type: HistoryEntryType.CUSTOMER_VERIFIED,
+                    data: {
+                        strategy: 'test_strategy',
+                    },
+                },
+            ]);
         });
 
         it('creates authenticated session', async () => {

+ 9 - 3
packages/core/e2e/shop-auth.e2e-spec.ts

@@ -272,16 +272,22 @@ describe('Shop auth & accounts', () => {
             expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
                 {
                     type: HistoryEntryType.CUSTOMER_REGISTERED,
-                    data: {},
+                    data: {
+                        strategy: 'native',
+                    },
                 },
                 {
                     // second entry because we register twice above
                     type: HistoryEntryType.CUSTOMER_REGISTERED,
-                    data: {},
+                    data: {
+                        strategy: 'native',
+                    },
                 },
                 {
                     type: HistoryEntryType.CUSTOMER_VERIFIED,
-                    data: {},
+                    data: {
+                        strategy: 'native',
+                    },
                 },
             ]);
         });

+ 16 - 5
packages/core/src/service/services/customer.service.ts

@@ -24,6 +24,7 @@ import {
 } from '../../common/error/errors';
 import { ListQueryOptions } from '../../common/types/common-types';
 import { assertFound, idsAreEqual, normalizeEmailAddress } from '../../common/utils';
+import { NATIVE_AUTH_STRATEGY_NAME } from '../../config/auth/native-authentication-strategy';
 import { ConfigService } from '../../config/config.service';
 import { Address } from '../../entity/address/address.entity';
 import { CustomerGroup } from '../../entity/customer-group/customer-group.entity';
@@ -140,7 +141,9 @@ export class CustomerService {
             ctx,
             customerId: createdCustomer.id,
             type: HistoryEntryType.CUSTOMER_REGISTERED,
-            data: {},
+            data: {
+                strategy: NATIVE_AUTH_STRATEGY_NAME,
+            },
         });
 
         if (customer.user?.verified) {
@@ -148,7 +151,9 @@ export class CustomerService {
                 ctx,
                 customerId: createdCustomer.id,
                 type: HistoryEntryType.CUSTOMER_VERIFIED,
-                data: {},
+                data: {
+                    strategy: NATIVE_AUTH_STRATEGY_NAME,
+                },
             });
         }
         return createdCustomer;
@@ -179,7 +184,9 @@ export class CustomerService {
             customerId: customer.id,
             ctx,
             type: HistoryEntryType.CUSTOMER_REGISTERED,
-            data: {},
+            data: {
+                strategy: NATIVE_AUTH_STRATEGY_NAME,
+            },
         });
         if (!user) {
             user = await this.userService.createCustomerUser(input.emailAddress, input.password || undefined);
@@ -195,7 +202,9 @@ export class CustomerService {
                 customerId: customer.id,
                 ctx,
                 type: HistoryEntryType.CUSTOMER_VERIFIED,
-                data: {},
+                data: {
+                    strategy: NATIVE_AUTH_STRATEGY_NAME,
+                },
             });
         }
         return true;
@@ -226,7 +235,9 @@ export class CustomerService {
                 customerId: customer.id,
                 ctx,
                 type: HistoryEntryType.CUSTOMER_VERIFIED,
-                data: {},
+                data: {
+                    strategy: NATIVE_AUTH_STRATEGY_NAME,
+                },
             });
             return this.findOneByUserId(user.id);
         }

+ 6 - 2
packages/core/src/service/services/history.service.ts

@@ -23,8 +23,12 @@ import { getEntityOrThrow } from '../helpers/utils/get-entity-or-throw';
 import { AdministratorService } from './administrator.service';
 
 export type CustomerHistoryEntryData = {
-    [HistoryEntryType.CUSTOMER_REGISTERED]: {};
-    [HistoryEntryType.CUSTOMER_VERIFIED]: {};
+    [HistoryEntryType.CUSTOMER_REGISTERED]: {
+        strategy: string;
+    };
+    [HistoryEntryType.CUSTOMER_VERIFIED]: {
+        strategy: string;
+    };
     [HistoryEntryType.CUSTOMER_DETAIL_UPDATED]: {
         input: UpdateCustomerInput;
     };