Skip to content

Commit

Permalink
feat(core): Record strategy used to register in Customer history
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Jun 24, 2020
1 parent c3ed2cd commit 5504044
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
37 changes: 35 additions & 2 deletions packages/core/e2e/authentication-strategy.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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', () => {
Expand Down Expand Up @@ -39,6 +46,7 @@ describe('AuthenticationStrategy', () => {
firstName: 'Cixin',
lastName: 'Liu',
};
let newCustomerId: string;

it(
'fails with a bad token',
Expand Down Expand Up @@ -73,6 +81,31 @@ describe('AuthenticationStrategy', () => {
'[email protected]',
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 () => {
Expand Down
12 changes: 9 additions & 3 deletions packages/core/e2e/shop-auth.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
]);
});
Expand Down
21 changes: 16 additions & 5 deletions packages/core/src/service/services/customer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -140,15 +141,19 @@ export class CustomerService {
ctx,
customerId: createdCustomer.id,
type: HistoryEntryType.CUSTOMER_REGISTERED,
data: {},
data: {
strategy: NATIVE_AUTH_STRATEGY_NAME,
},
});

if (customer.user?.verified) {
await this.historyService.createHistoryEntryForCustomer({
ctx,
customerId: createdCustomer.id,
type: HistoryEntryType.CUSTOMER_VERIFIED,
data: {},
data: {
strategy: NATIVE_AUTH_STRATEGY_NAME,
},
});
}
return createdCustomer;
Expand Down Expand Up @@ -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);
Expand All @@ -195,7 +202,9 @@ export class CustomerService {
customerId: customer.id,
ctx,
type: HistoryEntryType.CUSTOMER_VERIFIED,
data: {},
data: {
strategy: NATIVE_AUTH_STRATEGY_NAME,
},
});
}
return true;
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/service/services/history.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit 5504044

Please sign in to comment.