Skip to main content

CustomerService

CustomerService

Contains methods relating to Customer entities.

Signature
class CustomerService {
constructor(connection: TransactionalConnection, configService: ConfigService, userService: UserService, countryService: CountryService, listQueryBuilder: ListQueryBuilder, eventBus: EventBus, historyService: HistoryService, channelService: ChannelService, customFieldRelationService: CustomFieldRelationService, translator: TranslatorService)
findAll(ctx: RequestContext, options: ListQueryOptions<Customer> | undefined, relations: RelationPaths<Customer> = []) => Promise<PaginatedList<Customer>>;
findOne(ctx: RequestContext, id: ID, relations: RelationPaths<Customer> = []) => Promise<Customer | undefined>;
findOneByUserId(ctx: RequestContext, userId: ID, filterOnChannel: = true) => Promise<Customer | undefined>;
findAddressesByCustomerId(ctx: RequestContext, customerId: ID) => Promise<Address[]>;
getCustomerGroups(ctx: RequestContext, customerId: ID) => Promise<CustomerGroup[]>;
create(ctx: RequestContext, input: CreateCustomerInput, password?: string) => Promise<ErrorResultUnion<CreateCustomerResult, Customer>>;
update(ctx: RequestContext, input: UpdateCustomerShopInput & { id: ID }) => Promise<Customer>;
update(ctx: RequestContext, input: UpdateCustomerInput) => Promise<ErrorResultUnion<UpdateCustomerResult, Customer>>;
update(ctx: RequestContext, input: UpdateCustomerInput | (UpdateCustomerShopInput & { id: ID })) => Promise<ErrorResultUnion<UpdateCustomerResult, Customer>>;
registerCustomerAccount(ctx: RequestContext, input: RegisterCustomerInput) => Promise<RegisterCustomerAccountResult | EmailAddressConflictError | PasswordValidationError>;
refreshVerificationToken(ctx: RequestContext, emailAddress: string) => Promise<void>;
verifyCustomerEmailAddress(ctx: RequestContext, verificationToken: string, password?: string) => Promise<ErrorResultUnion<VerifyCustomerAccountResult, Customer>>;
requestPasswordReset(ctx: RequestContext, emailAddress: string) => Promise<void>;
resetPassword(ctx: RequestContext, passwordResetToken: string, password: string) => Promise<
User | PasswordResetTokenExpiredError | PasswordResetTokenInvalidError | PasswordValidationError
>;
requestUpdateEmailAddress(ctx: RequestContext, userId: ID, newEmailAddress: string) => Promise<boolean | EmailAddressConflictError>;
updateEmailAddress(ctx: RequestContext, token: string) => Promise<boolean | IdentifierChangeTokenInvalidError | IdentifierChangeTokenExpiredError>;
createOrUpdate(ctx: RequestContext, input: Partial<CreateCustomerInput> & { emailAddress: string }, errorOnExistingUser: boolean = false) => Promise<Customer | EmailAddressConflictError>;
createAddress(ctx: RequestContext, customerId: ID, input: CreateAddressInput) => Promise<Address>;
updateAddress(ctx: RequestContext, input: UpdateAddressInput) => Promise<Address>;
deleteAddress(ctx: RequestContext, id: ID) => Promise<boolean>;
softDelete(ctx: RequestContext, customerId: ID) => Promise<DeletionResponse>;
createAddressesForNewCustomer(ctx: RequestContext, order: Order) => ;
addNoteToCustomer(ctx: RequestContext, input: AddNoteToCustomerInput) => Promise<Customer>;
updateCustomerNote(ctx: RequestContext, input: UpdateCustomerNoteInput) => Promise<HistoryEntry>;
deleteCustomerNote(ctx: RequestContext, id: ID) => Promise<DeletionResponse>;
}

constructor

method
(connection: TransactionalConnection, configService: ConfigService, userService: UserService, countryService: CountryService, listQueryBuilder: ListQueryBuilder, eventBus: EventBus, historyService: HistoryService, channelService: ChannelService, customFieldRelationService: CustomFieldRelationService, translator: TranslatorService) => CustomerService

findAll

method
(ctx: RequestContext, options: ListQueryOptions<Customer> | undefined, relations: RelationPaths<Customer> = []) => Promise<PaginatedList<Customer>>

findOne

method
(ctx: RequestContext, id: ID, relations: RelationPaths<Customer> = []) => Promise<Customer | undefined>

findOneByUserId

method
(ctx: RequestContext, userId: ID, filterOnChannel: = true) => Promise<Customer | undefined>

Returns the Customer entity associated with the given userId, if one exists. Setting filterOnChannel to true will limit the results to Customers which are assigned to the current active Channel only.

findAddressesByCustomerId

method
(ctx: RequestContext, customerId: ID) => Promise<Address[]>

Returns all Address entities associated with the specified Customer.

getCustomerGroups

method
(ctx: RequestContext, customerId: ID) => Promise<CustomerGroup[]>

Returns a list of all CustomerGroup entities.

create

method
(ctx: RequestContext, input: CreateCustomerInput, password?: string) => Promise<ErrorResultUnion<CreateCustomerResult, Customer>>

Creates a new Customer, including creation of a new User with the special customer Role.

If the password argument is specified, the Customer will be immediately verified. If not, then an AccountRegistrationEvent is published, so that the customer can have their email address verified and set their password in a later step using the verifyCustomerEmailAddress() method.

This method is intended to be used in admin-created Customer flows.

update

method
(ctx: RequestContext, input: UpdateCustomerShopInput & { id: ID }) => Promise<Customer>

update

method
(ctx: RequestContext, input: UpdateCustomerInput) => Promise<ErrorResultUnion<UpdateCustomerResult, Customer>>

update

method
(ctx: RequestContext, input: UpdateCustomerInput | (UpdateCustomerShopInput & { id: ID })) => Promise<ErrorResultUnion<UpdateCustomerResult, Customer>>

registerCustomerAccount

method
(ctx: RequestContext, input: RegisterCustomerInput) => Promise<RegisterCustomerAccountResult | EmailAddressConflictError | PasswordValidationError>

Registers a new Customer account with the NativeAuthenticationStrategy and starts the email verification flow (unless AuthOptions requireVerification is set to false) by publishing an AccountRegistrationEvent.

This method is intended to be used in storefront Customer-creation flows.

refreshVerificationToken

method
(ctx: RequestContext, emailAddress: string) => Promise<void>

Refreshes a stale email address verification token by generating a new one and publishing a AccountRegistrationEvent.

verifyCustomerEmailAddress

method
(ctx: RequestContext, verificationToken: string, password?: string) => Promise<ErrorResultUnion<VerifyCustomerAccountResult, Customer>>

Given a valid verification token which has been published in an AccountRegistrationEvent, this method is used to set the Customer as verified as part of the account registration flow.

requestPasswordReset

method
(ctx: RequestContext, emailAddress: string) => Promise<void>

Publishes a new PasswordResetEvent for the given email address. This event creates a token which can be used in the resetPassword() method.

resetPassword

method
(ctx: RequestContext, passwordResetToken: string, password: string) => Promise< User | PasswordResetTokenExpiredError | PasswordResetTokenInvalidError | PasswordValidationError >

Given a valid password reset token created by a call to the requestPasswordReset() method, this method will change the Customer's password to that given as the password argument.

requestUpdateEmailAddress

method
(ctx: RequestContext, userId: ID, newEmailAddress: string) => Promise<boolean | EmailAddressConflictError>

Publishes a IdentifierChangeRequestEvent for the given User. This event contains a token which is then used in the updateEmailAddress() method to change the email address of the User & Customer.

updateEmailAddress

method
(ctx: RequestContext, token: string) => Promise<boolean | IdentifierChangeTokenInvalidError | IdentifierChangeTokenExpiredError>

Given a valid email update token published in a IdentifierChangeRequestEvent, this method will update the Customer & User email address.

createOrUpdate

method
(ctx: RequestContext, input: Partial<CreateCustomerInput> & { emailAddress: string }, errorOnExistingUser: boolean = false) => Promise<Customer | EmailAddressConflictError>

For guest checkouts, we assume that a matching email address is the same customer.

createAddress

method
(ctx: RequestContext, customerId: ID, input: CreateAddressInput) => Promise<Address>

Creates a new Address for the given Customer.

updateAddress

method
(ctx: RequestContext, input: UpdateAddressInput) => Promise<Address>

deleteAddress

method
(ctx: RequestContext, id: ID) => Promise<boolean>

softDelete

method
(ctx: RequestContext, customerId: ID) => Promise<DeletionResponse>

createAddressesForNewCustomer

method
(ctx: RequestContext, order: Order) =>

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.

addNoteToCustomer

method
(ctx: RequestContext, input: AddNoteToCustomerInput) => Promise<Customer>

updateCustomerNote

method
(ctx: RequestContext, input: UpdateCustomerNoteInput) => Promise<HistoryEntry>

deleteCustomerNote

method
(ctx: RequestContext, id: ID) => Promise<DeletionResponse>