|
|
@@ -18,6 +18,7 @@ import { ListQueryOptions } from '../../common/types/common-types';
|
|
|
import { assertFound, idsAreEqual, normalizeEmailAddress } from '../../common/utils';
|
|
|
import { ConfigService } from '../../config/config.service';
|
|
|
import { Address } from '../../entity/address/address.entity';
|
|
|
+import { CustomerGroup } from '../../entity/customer-group/customer-group.entity';
|
|
|
import { Customer } from '../../entity/customer/customer.entity';
|
|
|
import { User } from '../../entity/user/user.entity';
|
|
|
import { EventBus } from '../../event-bus/event-bus';
|
|
|
@@ -72,14 +73,25 @@ export class CustomerService {
|
|
|
.leftJoinAndSelect('country.translations', 'countryTranslation')
|
|
|
.where('address.customer = :id', { id: customerId })
|
|
|
.getMany()
|
|
|
- .then(addresses => {
|
|
|
- addresses.forEach(address => {
|
|
|
+ .then((addresses) => {
|
|
|
+ addresses.forEach((address) => {
|
|
|
address.country = translateDeep(address.country, ctx.languageCode);
|
|
|
});
|
|
|
return addresses;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ async getCustomerGroups(customerId: ID): Promise<CustomerGroup[]> {
|
|
|
+ const customerWithGroups = await this.connection
|
|
|
+ .getRepository(Customer)
|
|
|
+ .findOne(customerId, { relations: ['groups'] });
|
|
|
+ if (customerWithGroups) {
|
|
|
+ return customerWithGroups.groups;
|
|
|
+ } else {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async create(ctx: RequestContext, input: CreateCustomerInput, password?: string): Promise<Customer> {
|
|
|
input.emailAddress = normalizeEmailAddress(input.emailAddress);
|
|
|
const customer = new Customer(input);
|
|
|
@@ -328,8 +340,8 @@ export class CustomerService {
|
|
|
.findOne(addressId, { relations: ['customer', 'customer.addresses'] });
|
|
|
if (result) {
|
|
|
const customerAddressIds = result.customer.addresses
|
|
|
- .map(a => a.id)
|
|
|
- .filter(id => !idsAreEqual(id, addressId)) as string[];
|
|
|
+ .map((a) => a.id)
|
|
|
+ .filter((id) => !idsAreEqual(id, addressId)) as string[];
|
|
|
|
|
|
if (customerAddressIds.length) {
|
|
|
if (input.defaultBillingAddress === true) {
|
|
|
@@ -362,7 +374,7 @@ export class CustomerService {
|
|
|
const customerAddresses = result.customer.addresses;
|
|
|
if (1 < customerAddresses.length) {
|
|
|
const otherAddresses = customerAddresses
|
|
|
- .filter(address => !idsAreEqual(address.id, addressToDelete.id))
|
|
|
+ .filter((address) => !idsAreEqual(address.id, addressToDelete.id))
|
|
|
.sort((a, b) => (a.id < b.id ? -1 : 1));
|
|
|
if (addressToDelete.defaultShippingAddress) {
|
|
|
otherAddresses[0].defaultShippingAddress = true;
|