🚨 Announcing Vendure v2 Beta

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[]>;
  async getCustomerGroups(ctx: RequestContext, customerId: ID) => Promise<CustomerGroup[]>;
  async create(ctx: RequestContext, input: CreateCustomerInput, password?: string) => Promise<ErrorResultUnion<CreateCustomerResult, Customer>>;
  async update(ctx: RequestContext, input: UpdateCustomerShopInput & { id: ID }) => Promise<Customer>;
  async update(ctx: RequestContext, input: UpdateCustomerInput) => Promise<ErrorResultUnion<UpdateCustomerResult, Customer>>;
  async update(ctx: RequestContext, input: UpdateCustomerInput | (UpdateCustomerShopInput & { id: ID })) => Promise<ErrorResultUnion<UpdateCustomerResult, Customer>>;
  async registerCustomerAccount(ctx: RequestContext, input: RegisterCustomerInput) => Promise<RegisterCustomerAccountResult | EmailAddressConflictError | PasswordValidationError>;
  async refreshVerificationToken(ctx: RequestContext, emailAddress: string) => Promise<void>;
  async verifyCustomerEmailAddress(ctx: RequestContext, verificationToken: string, password?: string) => Promise<ErrorResultUnion<VerifyCustomerAccountResult, Customer>>;
  async requestPasswordReset(ctx: RequestContext, emailAddress: string) => Promise<void>;
  async resetPassword(ctx: RequestContext, passwordResetToken: string, password: string) => Promise<
        User | PasswordResetTokenExpiredError | PasswordResetTokenInvalidError | PasswordValidationError
    >;
  async requestUpdateEmailAddress(ctx: RequestContext, userId: ID, newEmailAddress: string) => Promise<boolean | EmailAddressConflictError>;
  async updateEmailAddress(ctx: RequestContext, token: string) => Promise<boolean | IdentifierChangeTokenInvalidError | IdentifierChangeTokenExpiredError>;
  async createOrUpdate(ctx: RequestContext, input: Partial<CreateCustomerInput> & { emailAddress: string }, errorOnExistingUser: boolean = false) => Promise<Customer | EmailAddressConflictError>;
  async createAddress(ctx: RequestContext, customerId: ID, input: CreateAddressInput) => Promise<Address>;
  async updateAddress(ctx: RequestContext, input: UpdateAddressInput) => Promise<Address>;
  async deleteAddress(ctx: RequestContext, id: ID) => Promise<boolean>;
  async softDelete(ctx: RequestContext, customerId: ID) => Promise<DeletionResponse>;
  async createAddressesForNewCustomer(ctx: RequestContext, order: Order) => ;
  async addNoteToCustomer(ctx: RequestContext, input: AddNoteToCustomerInput) => Promise<Customer>;
  async updateCustomerNote(ctx: RequestContext, input: UpdateCustomerNoteInput) => Promise<HistoryEntry>;
  async deleteCustomerNote(ctx: RequestContext, id: ID) => Promise<DeletionResponse>;
}

Members

constructor

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

findAll

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

findOne

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

findOneByUserId

method
type:
(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
type:
(ctx: RequestContext, customerId: ID) => Promise<Address[]>
Returns all Address entities associated with the specified Customer.

getCustomerGroups

async method
type:
(ctx: RequestContext, customerId: ID) => Promise<CustomerGroup[]>
Returns a list of all CustomerGroup entities.

create

async method
type:
(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

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

update

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

update

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

registerCustomerAccount

async method
type:
(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

async method
type:
(ctx: RequestContext, emailAddress: string) => Promise<void>
Refreshes a stale email address verification token by generating a new one and publishing a AccountRegistrationEvent.

verifyCustomerEmailAddress

async method
type:
(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

async method
type:
(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

async method
type:
(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

async method
type:
(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

async method
type:
(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

async method
type:
(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

async method
type:
(ctx: RequestContext, customerId: ID, input: CreateAddressInput) => Promise<Address>
Creates a new Address for the given Customer.

updateAddress

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

deleteAddress

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

softDelete

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

createAddressesForNewCustomer

async method
type:
(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

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

updateCustomerNote

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

deleteCustomerNote

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