Skip to content

Commit

Permalink
feat(core): Create customer history entries for groups
Browse files Browse the repository at this point in the history
Relates to #343
  • Loading branch information
michaelbromley committed May 29, 2020
1 parent 679d4af commit 4620730
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1278,10 +1278,11 @@ export enum HistoryEntryType {
CUSTOMER_REGISTERED = 'CUSTOMER_REGISTERED',
CUSTOMER_VERIFIED = 'CUSTOMER_VERIFIED',
CUSTOMER_DETAIL_UPDATED = 'CUSTOMER_DETAIL_UPDATED',
CUSTOMER_ADDED_TO_GROUP = 'CUSTOMER_ADDED_TO_GROUP',
CUSTOMER_REMOVED_FROM_GROUP = 'CUSTOMER_REMOVED_FROM_GROUP',
CUSTOMER_ADDRESS_CREATED = 'CUSTOMER_ADDRESS_CREATED',
CUSTOMER_ADDRESS_UPDATED = 'CUSTOMER_ADDRESS_UPDATED',
CUSTOMER_ADDRESS_DELETED = 'CUSTOMER_ADDRESS_DELETED',
CUSTOMER_ORDER_PLACED = 'CUSTOMER_ORDER_PLACED',
CUSTOMER_PASSWORD_UPDATED = 'CUSTOMER_PASSWORD_UPDATED',
CUSTOMER_PASSWORD_RESET_REQUESTED = 'CUSTOMER_PASSWORD_RESET_REQUESTED',
CUSTOMER_PASSWORD_RESET_VERIFIED = 'CUSTOMER_PASSWORD_RESET_VERIFIED',
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,10 +919,11 @@ export enum HistoryEntryType {
CUSTOMER_REGISTERED = 'CUSTOMER_REGISTERED',
CUSTOMER_VERIFIED = 'CUSTOMER_VERIFIED',
CUSTOMER_DETAIL_UPDATED = 'CUSTOMER_DETAIL_UPDATED',
CUSTOMER_ADDED_TO_GROUP = 'CUSTOMER_ADDED_TO_GROUP',
CUSTOMER_REMOVED_FROM_GROUP = 'CUSTOMER_REMOVED_FROM_GROUP',
CUSTOMER_ADDRESS_CREATED = 'CUSTOMER_ADDRESS_CREATED',
CUSTOMER_ADDRESS_UPDATED = 'CUSTOMER_ADDRESS_UPDATED',
CUSTOMER_ADDRESS_DELETED = 'CUSTOMER_ADDRESS_DELETED',
CUSTOMER_ORDER_PLACED = 'CUSTOMER_ORDER_PLACED',
CUSTOMER_PASSWORD_UPDATED = 'CUSTOMER_PASSWORD_UPDATED',
CUSTOMER_PASSWORD_RESET_REQUESTED = 'CUSTOMER_PASSWORD_RESET_REQUESTED',
CUSTOMER_PASSWORD_RESET_VERIFIED = 'CUSTOMER_PASSWORD_RESET_VERIFIED',
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1276,10 +1276,11 @@ export enum HistoryEntryType {
CUSTOMER_REGISTERED = 'CUSTOMER_REGISTERED',
CUSTOMER_VERIFIED = 'CUSTOMER_VERIFIED',
CUSTOMER_DETAIL_UPDATED = 'CUSTOMER_DETAIL_UPDATED',
CUSTOMER_ADDED_TO_GROUP = 'CUSTOMER_ADDED_TO_GROUP',
CUSTOMER_REMOVED_FROM_GROUP = 'CUSTOMER_REMOVED_FROM_GROUP',
CUSTOMER_ADDRESS_CREATED = 'CUSTOMER_ADDRESS_CREATED',
CUSTOMER_ADDRESS_UPDATED = 'CUSTOMER_ADDRESS_UPDATED',
CUSTOMER_ADDRESS_DELETED = 'CUSTOMER_ADDRESS_DELETED',
CUSTOMER_ORDER_PLACED = 'CUSTOMER_ORDER_PLACED',
CUSTOMER_PASSWORD_UPDATED = 'CUSTOMER_PASSWORD_UPDATED',
CUSTOMER_PASSWORD_RESET_REQUESTED = 'CUSTOMER_PASSWORD_RESET_REQUESTED',
CUSTOMER_PASSWORD_RESET_VERIFIED = 'CUSTOMER_PASSWORD_RESET_VERIFIED',
Expand Down
91 changes: 90 additions & 1 deletion packages/core/e2e/customer-group.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 {
AccountRegistrationEvent,
EventBus,
Expand All @@ -19,13 +20,15 @@ import {
DeleteCustomerGroup,
GetCustomerGroup,
GetCustomerGroups,
GetCustomerHistory,
GetCustomerList,
GetCustomerWithGroups,
HistoryEntryType,
RemoveCustomersFromGroup,
UpdateCustomerGroup,
} from './graphql/generated-e2e-admin-types';
import { DeletionResult } from './graphql/generated-e2e-shop-types';
import { GET_CUSTOMER_LIST } from './graphql/shared-definitions';
import { GET_CUSTOMER_HISTORY, GET_CUSTOMER_LIST } from './graphql/shared-definitions';
import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
import { sortById } from './utils/test-order-utils';

Expand Down Expand Up @@ -67,6 +70,27 @@ describe('CustomerGroup resolver', () => {
]);
});

it('history entry for CUSTOMER_ADDED_TO_GROUP after group created', async () => {
const { customer } = await adminClient.query<GetCustomerHistory.Query, GetCustomerHistory.Variables>(
GET_CUSTOMER_HISTORY,
{
id: customers[0].id,
options: {
skip: 1,
},
},
);

expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
{
type: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP,
data: {
groupName: 'group 1',
},
},
]);
});

it('customerGroups', async () => {
const { customerGroups } = await adminClient.query<
GetCustomerGroups.Query,
Expand Down Expand Up @@ -141,6 +165,50 @@ describe('CustomerGroup resolver', () => {
]);
});

it('history entry for CUSTOMER_ADDED_TO_GROUP not duplicated', async () => {
const { customer } = await adminClient.query<GetCustomerHistory.Query, GetCustomerHistory.Variables>(
GET_CUSTOMER_HISTORY,
{
id: customers[0].id,
options: {
filter: {
type: { eq: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP },
},
},
},
);

expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
{
type: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP,
data: {
groupName: 'group 1',
},
},
]);
});

it('history entry for CUSTOMER_ADDED_TO_GROUP after customer added', async () => {
const { customer } = await adminClient.query<GetCustomerHistory.Query, GetCustomerHistory.Variables>(
GET_CUSTOMER_HISTORY,
{
id: customers[2].id,
options: {
skip: 1,
},
},
);

expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
{
type: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP,
data: {
groupName: 'group 1 updated',
},
},
]);
});

it('customer.groups field resolver', async () => {
const { customer } = await adminClient.query<
GetCustomerWithGroups.Query,
Expand Down Expand Up @@ -180,6 +248,27 @@ describe('CustomerGroup resolver', () => {
]);
});

it('history entry for CUSTOMER_REMOVED_FROM_GROUP', async () => {
const { customer } = await adminClient.query<GetCustomerHistory.Query, GetCustomerHistory.Variables>(
GET_CUSTOMER_HISTORY,
{
id: customers[1].id,
options: {
skip: 2,
},
},
);

expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
{
type: HistoryEntryType.CUSTOMER_REMOVED_FROM_GROUP,
data: {
groupName: 'group 1 updated',
},
},
]);
});

it('deleteCustomerGroup', async () => {
const { deleteCustomerGroup } = await adminClient.query<
DeleteCustomerGroup.Mutation,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1278,10 +1278,11 @@ export enum HistoryEntryType {
CUSTOMER_REGISTERED = 'CUSTOMER_REGISTERED',
CUSTOMER_VERIFIED = 'CUSTOMER_VERIFIED',
CUSTOMER_DETAIL_UPDATED = 'CUSTOMER_DETAIL_UPDATED',
CUSTOMER_ADDED_TO_GROUP = 'CUSTOMER_ADDED_TO_GROUP',
CUSTOMER_REMOVED_FROM_GROUP = 'CUSTOMER_REMOVED_FROM_GROUP',
CUSTOMER_ADDRESS_CREATED = 'CUSTOMER_ADDRESS_CREATED',
CUSTOMER_ADDRESS_UPDATED = 'CUSTOMER_ADDRESS_UPDATED',
CUSTOMER_ADDRESS_DELETED = 'CUSTOMER_ADDRESS_DELETED',
CUSTOMER_ORDER_PLACED = 'CUSTOMER_ORDER_PLACED',
CUSTOMER_PASSWORD_UPDATED = 'CUSTOMER_PASSWORD_UPDATED',
CUSTOMER_PASSWORD_RESET_REQUESTED = 'CUSTOMER_PASSWORD_RESET_REQUESTED',
CUSTOMER_PASSWORD_RESET_VERIFIED = 'CUSTOMER_PASSWORD_RESET_VERIFIED',
Expand Down
3 changes: 2 additions & 1 deletion packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,10 +919,11 @@ export enum HistoryEntryType {
CUSTOMER_REGISTERED = 'CUSTOMER_REGISTERED',
CUSTOMER_VERIFIED = 'CUSTOMER_VERIFIED',
CUSTOMER_DETAIL_UPDATED = 'CUSTOMER_DETAIL_UPDATED',
CUSTOMER_ADDED_TO_GROUP = 'CUSTOMER_ADDED_TO_GROUP',
CUSTOMER_REMOVED_FROM_GROUP = 'CUSTOMER_REMOVED_FROM_GROUP',
CUSTOMER_ADDRESS_CREATED = 'CUSTOMER_ADDRESS_CREATED',
CUSTOMER_ADDRESS_UPDATED = 'CUSTOMER_ADDRESS_UPDATED',
CUSTOMER_ADDRESS_DELETED = 'CUSTOMER_ADDRESS_DELETED',
CUSTOMER_ORDER_PLACED = 'CUSTOMER_ORDER_PLACED',
CUSTOMER_PASSWORD_UPDATED = 'CUSTOMER_PASSWORD_UPDATED',
CUSTOMER_PASSWORD_RESET_REQUESTED = 'CUSTOMER_PASSWORD_RESET_REQUESTED',
CUSTOMER_PASSWORD_RESET_VERIFIED = 'CUSTOMER_PASSWORD_RESET_VERIFIED',
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 @@ -375,7 +375,7 @@ describe('Shop auth & accounts', () => {
data: {},
},
{
type: HistoryEntryType.CUSTOMER_PASSWORD_RESET_REQUESTED,
type: HistoryEntryType.CUSTOMER_PASSWORD_RESET_VERIFIED,
data: {},
},
]);
Expand Down Expand Up @@ -530,11 +530,17 @@ describe('Shop auth & accounts', () => {
expect(result.customer?.history.items.map(pick(['type', 'data']))).toEqual([
{
type: HistoryEntryType.CUSTOMER_EMAIL_UPDATE_REQUESTED,
data: {},
data: {
newEmailAddress: '[email protected]',
oldEmailAddress: '[email protected]',
},
},
{
type: HistoryEntryType.CUSTOMER_EMAIL_UPDATE_VERIFIED,
data: {},
data: {
newEmailAddress: '[email protected]',
oldEmailAddress: '[email protected]',
},
},
]);
});
Expand Down
32 changes: 27 additions & 5 deletions packages/core/e2e/shop-customer.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ describe('Shop customers', () => {
expect(result.updateCustomer.firstName).toBe('xyz');
});

it('customer history for CUSTOMER_DETAIL_UPDATED', async () => {
const result = await adminClient.query<GetCustomerHistory.Query, GetCustomerHistory.Variables>(
GET_CUSTOMER_HISTORY,
{
id: customer.id,
options: {
// skip populated CUSTOMER_ADDRESS_CREATED entry
skip: 1,
},
},
);

expect(result.customer?.history.items.map(pick(['type', 'data']))).toEqual([
{
type: HistoryEntryType.CUSTOMER_DETAIL_UPDATED,
data: {
input: { firstName: 'xyz', id: 'T_1' },
},
},
]);
});

it('createCustomerAddress works', async () => {
const input: CreateAddressInput = {
streetLine1: '1 Test Street',
Expand All @@ -163,8 +185,8 @@ describe('Shop customers', () => {
{
id: customer.id,
options: {
// skip populated CUSTOMER_ADDRESS_CREATED entry
skip: 1,
// skip populated CUSTOMER_ADDRESS_CREATED, CUSTOMER_DETAIL_UPDATED entries
skip: 2,
},
},
);
Expand Down Expand Up @@ -197,7 +219,7 @@ describe('Shop customers', () => {
it('customer history for CUSTOMER_ADDRESS_UPDATED', async () => {
const result = await adminClient.query<GetCustomerHistory.Query, GetCustomerHistory.Variables>(
GET_CUSTOMER_HISTORY,
{ id: customer.id, options: { skip: 2 } },
{ id: customer.id, options: { skip: 3 } },
);

expect(result.customer?.history.items.map(pick(['type', 'data']))).toEqual([
Expand Down Expand Up @@ -241,7 +263,7 @@ describe('Shop customers', () => {
it('customer history for CUSTOMER_ADDRESS_DELETED', async () => {
const result = await adminClient.query<GetCustomerHistory.Query, GetCustomerHistory.Variables>(
GET_CUSTOMER_HISTORY,
{ id: customer.id, options: { skip: 3 } },
{ id: customer.id, options: { skip: 4 } },
);

expect(result.customer?.history.items.map(pick(['type', 'data']))).toEqual([
Expand Down Expand Up @@ -290,7 +312,7 @@ describe('Shop customers', () => {
it('customer history for CUSTOMER_PASSWORD_UPDATED', async () => {
const result = await adminClient.query<GetCustomerHistory.Query, GetCustomerHistory.Variables>(
GET_CUSTOMER_HISTORY,
{ id: customer.id, options: { skip: 4 } },
{ id: customer.id, options: { skip: 5 } },
);

expect(result.customer?.history.items.map(pick(['type', 'data']))).toEqual([
Expand Down
17 changes: 12 additions & 5 deletions packages/core/src/api/resolvers/admin/customer-group.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ export class CustomerGroupResolver {

@Mutation()
@Allow(Permission.CreateCustomer)
async createCustomerGroup(@Args() args: MutationCreateCustomerGroupArgs): Promise<CustomerGroup> {
return this.customerGroupService.create(args.input);
async createCustomerGroup(
@Ctx() ctx: RequestContext,
@Args() args: MutationCreateCustomerGroupArgs,
): Promise<CustomerGroup> {
return this.customerGroupService.create(ctx, args.input);
}

@Mutation()
Expand All @@ -60,15 +63,19 @@ export class CustomerGroupResolver {

@Mutation()
@Allow(Permission.UpdateCustomer)
async addCustomersToGroup(@Args() args: MutationAddCustomersToGroupArgs): Promise<CustomerGroup> {
return this.customerGroupService.addCustomersToGroup(args);
async addCustomersToGroup(
@Ctx() ctx: RequestContext,
@Args() args: MutationAddCustomersToGroupArgs,
): Promise<CustomerGroup> {
return this.customerGroupService.addCustomersToGroup(ctx, args);
}

@Mutation()
@Allow(Permission.UpdateCustomer)
async removeCustomersFromGroup(
@Ctx() ctx: RequestContext,
@Args() args: MutationRemoveCustomersFromGroupArgs,
): Promise<CustomerGroup> {
return this.customerGroupService.removeCustomersFromGroup(args);
return this.customerGroupService.removeCustomersFromGroup(ctx, args);
}
}
7 changes: 5 additions & 2 deletions packages/core/src/api/resolvers/admin/customer.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ export class CustomerResolver {

@Mutation()
@Allow(Permission.UpdateCustomer)
async updateCustomer(@Args() args: MutationUpdateCustomerArgs): Promise<Customer> {
async updateCustomer(
@Ctx() ctx: RequestContext,
@Args() args: MutationUpdateCustomerArgs,
): Promise<Customer> {
const { input } = args;
return this.customerService.update(input);
return this.customerService.update(ctx, input);
}

@Mutation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ShopCustomerResolver {
@Args() args: MutationUpdateCustomerArgs,
): Promise<Customer> {
const customer = await this.getCustomerForOwner(ctx);
return this.customerService.update({
return this.customerService.update(ctx, {
id: customer.id,
...args.input,
});
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/api/schema/type/history-entry.type.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ enum HistoryEntryType {
CUSTOMER_REGISTERED
CUSTOMER_VERIFIED
CUSTOMER_DETAIL_UPDATED
CUSTOMER_ADDED_TO_GROUP
CUSTOMER_REMOVED_FROM_GROUP
CUSTOMER_ADDRESS_CREATED
CUSTOMER_ADDRESS_UPDATED
CUSTOMER_ADDRESS_DELETED
CUSTOMER_ORDER_PLACED
CUSTOMER_PASSWORD_UPDATED
CUSTOMER_PASSWORD_RESET_REQUESTED
CUSTOMER_PASSWORD_RESET_VERIFIED
Expand Down
Loading

0 comments on commit 4620730

Please sign in to comment.