|
|
@@ -6,6 +6,7 @@ import {
|
|
|
CustomerListOptions,
|
|
|
DeletionResponse,
|
|
|
DeletionResult,
|
|
|
+ HistoryEntryType,
|
|
|
MutationAddCustomersToGroupArgs,
|
|
|
MutationRemoveCustomersFromGroupArgs,
|
|
|
UpdateCustomerGroupInput,
|
|
|
@@ -13,6 +14,7 @@ import {
|
|
|
import { ID, PaginatedList } from '@vendure/common/lib/shared-types';
|
|
|
import { Connection } from 'typeorm';
|
|
|
|
|
|
+import { RequestContext } from '../../api/common/request-context';
|
|
|
import { UserInputError } from '../../common/error/errors';
|
|
|
import { assertFound, idsAreEqual } from '../../common/utils';
|
|
|
import { CustomerGroup } from '../../entity/customer-group/customer-group.entity';
|
|
|
@@ -21,11 +23,14 @@ import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-build
|
|
|
import { getEntityOrThrow } from '../helpers/utils/get-entity-or-throw';
|
|
|
import { patchEntity } from '../helpers/utils/patch-entity';
|
|
|
|
|
|
+import { HistoryService } from './history.service';
|
|
|
+
|
|
|
@Injectable()
|
|
|
export class CustomerGroupService {
|
|
|
constructor(
|
|
|
@InjectConnection() private connection: Connection,
|
|
|
private listQueryBuilder: ListQueryBuilder,
|
|
|
+ private historyService: HistoryService,
|
|
|
) {}
|
|
|
|
|
|
findAll(options?: CustomerGroupListOptions): Promise<PaginatedList<CustomerGroup>> {
|
|
|
@@ -48,13 +53,23 @@ export class CustomerGroupService {
|
|
|
.then(([items, totalItems]) => ({ items, totalItems }));
|
|
|
}
|
|
|
|
|
|
- async create(input: CreateCustomerGroupInput): Promise<CustomerGroup> {
|
|
|
+ async create(ctx: RequestContext, input: CreateCustomerGroupInput): Promise<CustomerGroup> {
|
|
|
const customerGroup = new CustomerGroup(input);
|
|
|
|
|
|
const newCustomerGroup = await this.connection.getRepository(CustomerGroup).save(customerGroup);
|
|
|
if (input.customerIds) {
|
|
|
const customers = await this.getCustomersFromIds(input.customerIds);
|
|
|
- customers.forEach((c) => (c.groups = [...(c.groups || []), newCustomerGroup]));
|
|
|
+ for (const customer of customers) {
|
|
|
+ customer.groups = [...(customer.groups || []), newCustomerGroup];
|
|
|
+ await this.historyService.createHistoryEntryForCustomer({
|
|
|
+ ctx,
|
|
|
+ customerId: customer.id,
|
|
|
+ type: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP,
|
|
|
+ data: {
|
|
|
+ groupName: customerGroup.name,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
await this.connection.getRepository(Customer).save(customers);
|
|
|
}
|
|
|
return assertFound(this.findOne(newCustomerGroup.id));
|
|
|
@@ -82,19 +97,34 @@ export class CustomerGroupService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async addCustomersToGroup(input: MutationAddCustomersToGroupArgs): Promise<CustomerGroup> {
|
|
|
+ async addCustomersToGroup(
|
|
|
+ ctx: RequestContext,
|
|
|
+ input: MutationAddCustomersToGroupArgs,
|
|
|
+ ): Promise<CustomerGroup> {
|
|
|
const customers = await this.getCustomersFromIds(input.customerIds);
|
|
|
const group = await getEntityOrThrow(this.connection, CustomerGroup, input.customerGroupId);
|
|
|
for (const customer of customers) {
|
|
|
if (!customer.groups.map((g) => g.id).includes(input.customerGroupId)) {
|
|
|
customer.groups.push(group);
|
|
|
+ await this.historyService.createHistoryEntryForCustomer({
|
|
|
+ ctx,
|
|
|
+ customerId: customer.id,
|
|
|
+ type: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP,
|
|
|
+ data: {
|
|
|
+ groupName: group.name,
|
|
|
+ },
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
await this.connection.getRepository(Customer).save(customers, { reload: false });
|
|
|
return assertFound(this.findOne(group.id));
|
|
|
}
|
|
|
|
|
|
- async removeCustomersFromGroup(input: MutationRemoveCustomersFromGroupArgs): Promise<CustomerGroup> {
|
|
|
+ async removeCustomersFromGroup(
|
|
|
+ ctx: RequestContext,
|
|
|
+ input: MutationRemoveCustomersFromGroupArgs,
|
|
|
+ ): Promise<CustomerGroup> {
|
|
|
const customers = await this.getCustomersFromIds(input.customerIds);
|
|
|
const group = await getEntityOrThrow(this.connection, CustomerGroup, input.customerGroupId);
|
|
|
for (const customer of customers) {
|
|
|
@@ -102,6 +132,14 @@ export class CustomerGroupService {
|
|
|
throw new UserInputError('error.customer-does-not-belong-to-customer-group');
|
|
|
}
|
|
|
customer.groups = customer.groups.filter((g) => !idsAreEqual(g.id, group.id));
|
|
|
+ await this.historyService.createHistoryEntryForCustomer({
|
|
|
+ ctx,
|
|
|
+ customerId: customer.id,
|
|
|
+ type: HistoryEntryType.CUSTOMER_REMOVED_FROM_GROUP,
|
|
|
+ data: {
|
|
|
+ groupName: group.name,
|
|
|
+ },
|
|
|
+ });
|
|
|
}
|
|
|
await this.connection.getRepository(Customer).save(customers, { reload: false });
|
|
|
return assertFound(this.findOne(group.id));
|