|
|
@@ -14,6 +14,7 @@ import {
|
|
|
DeletionResponse,
|
|
|
DeletionResult,
|
|
|
HistoryEntryType,
|
|
|
+ OrderAddress,
|
|
|
UpdateAddressInput,
|
|
|
UpdateCustomerInput,
|
|
|
UpdateCustomerNoteInput,
|
|
|
@@ -46,6 +47,7 @@ import { Channel } from '../../entity/channel/channel.entity';
|
|
|
import { CustomerGroup } from '../../entity/customer-group/customer-group.entity';
|
|
|
import { Customer } from '../../entity/customer/customer.entity';
|
|
|
import { HistoryEntry } from '../../entity/history-entry/history-entry.entity';
|
|
|
+import { Order } from '../../entity/index';
|
|
|
import { User } from '../../entity/user/user.entity';
|
|
|
import { EventBus } from '../../event-bus/event-bus';
|
|
|
import { AccountRegistrationEvent } from '../../event-bus/events/account-registration-event';
|
|
|
@@ -790,6 +792,57 @@ export class CustomerService {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @description
|
|
|
+ * If the Customer associated with the given Order does not yet have any Addresses,
|
|
|
+ * this method will create new Address(es) based on the Order's shipping & billing
|
|
|
+ * addresses.
|
|
|
+ */
|
|
|
+ async createAddressesForNewCustomer(ctx: RequestContext, order: Order) {
|
|
|
+ if (!order.customer) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const addresses = await this.findAddressesByCustomerId(ctx, order.customer.id);
|
|
|
+ // If the Customer has no addresses yet, use the shipping/billing address data
|
|
|
+ // to populate the initial default Address.
|
|
|
+ if (addresses.length === 0 && order.shippingAddress?.country) {
|
|
|
+ const shippingAddress = order.shippingAddress;
|
|
|
+ const billingAddress = order.billingAddress;
|
|
|
+ const hasSeparateBillingAddress =
|
|
|
+ billingAddress?.streetLine1 && !this.addressesAreEqual(shippingAddress, billingAddress);
|
|
|
+ if (shippingAddress.streetLine1) {
|
|
|
+ await this.createAddress(ctx, order.customer.id, {
|
|
|
+ ...shippingAddress,
|
|
|
+ company: shippingAddress.company || '',
|
|
|
+ streetLine1: shippingAddress.streetLine1 || '',
|
|
|
+ streetLine2: shippingAddress.streetLine2 || '',
|
|
|
+ countryCode: shippingAddress.countryCode || '',
|
|
|
+ defaultBillingAddress: !hasSeparateBillingAddress,
|
|
|
+ defaultShippingAddress: true,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (hasSeparateBillingAddress) {
|
|
|
+ await this.createAddress(ctx, order.customer.id, {
|
|
|
+ ...billingAddress,
|
|
|
+ company: billingAddress.company || '',
|
|
|
+ streetLine1: billingAddress.streetLine1 || '',
|
|
|
+ streetLine2: billingAddress.streetLine2 || '',
|
|
|
+ countryCode: billingAddress.countryCode || '',
|
|
|
+ defaultBillingAddress: true,
|
|
|
+ defaultShippingAddress: false,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private addressesAreEqual(address1: OrderAddress, address2: OrderAddress): boolean {
|
|
|
+ return (
|
|
|
+ address1.streetLine1 === address2.streetLine1 &&
|
|
|
+ address1.streetLine2 === address2.streetLine2 &&
|
|
|
+ address1.postalCode === address2.postalCode
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
async addNoteToCustomer(ctx: RequestContext, input: AddNoteToCustomerInput): Promise<Customer> {
|
|
|
const customer = await this.connection.getEntityOrThrow(ctx, Customer, input.id, {
|
|
|
channelId: ctx.channelId,
|