Skip to main content
Skip to main content

CustomerService

Provides layer to manipulate customers.

Constructors

constructor

**new CustomerService**(«destructured»)

Parameters

__namedParametersInjectedDependenciesRequired

Properties

__configModule__Record<string, unknown>
__container__anyRequired
__moduleDeclaration__Record<string, unknown>
addressRepository_Repository<Address>Required
customerRepository_Repository<Customer> & &#123; listAndCount: Method listAndCount &#125;Required
eventBusService_EventBusServiceRequired
manager_EntityManagerRequired
transactionManager_undefined | EntityManagerRequired
EventsobjectRequired
Events.CREATEDstringRequired

Default: "customer.created"

Events.PASSWORD_RESETstringRequired

Default: "customer.password_reset"

Events.UPDATEDstringRequired

Default: "customer.updated"

Accessors

activeManager_

Protected get**activeManager_**(): EntityManager

Returns

EntityManager

EntityManagerEntityManagerRequired

Methods

addAddress

**addAddress**(customerId, address): Promise&#60;[Customer](/references/services/classes/Customer) \| [Address](/references/services/classes/Address)&#62;

Parameters

customerIdstringRequired
addressAddressCreatePayloadRequired
Address fields used when creating an address.

Returns

Promise<Customer | Address>

PromisePromise<Customer | Address>Required

atomicPhase_

Protected **atomicPhase_**<TypeParameter TResult, TypeParameter TError>(work, isolationOrErrorHandler?, maybeErrorHandlerOrDontFail?): Promise&#60;TResult&#62;

Wraps some work within a transactional block. If the service already has a transaction manager attached this will be reused, otherwise a new transaction manager is created.

TResultobjectRequired
TErrorobjectRequired

Parameters

work(transactionManager: EntityManager) => Promise<TResult>Required
the transactional work to be done
isolationOrErrorHandlerIsolationLevel | (error: TError) => Promise<void | TResult>
the isolation level to be used for the work.
maybeErrorHandlerOrDontFail(error: TError) => Promise<void | TResult>
Potential error handler

Returns

Promise<TResult>

PromisePromise<TResult>Required
the result of the transactional work

count

**count**(): Promise&#60;number&#62;

Return the total number of documents in database

Returns

Promise<number>

PromisePromise<number>Required
the result of the count operation

create

**create**(customer): Promise&#60;[Customer](/references/services/classes/Customer)&#62;

Creates a customer from an email - customers can have accounts associated, e.g. to login and view order history, etc. If a password is provided the customer will automatically get an account, otherwise the customer is just used to hold details of customers.

Parameters

customerCreateCustomerInputRequired
the customer to create

Returns

Promise<Customer>

PromisePromise<Customer>Required
the result of create

delete

**delete**(customerId): Promise&#60;void \| [Customer](/references/services/classes/Customer)&#62;

Deletes a customer from a given customer id.

Parameters

customerIdstringRequired
the id of the customer to delete. Must be castable as an ObjectId

Returns

Promise<void | Customer>

PromisePromise<void | Customer>Required
the result of the delete operation.

generateResetPasswordToken

**generateResetPasswordToken**(customerId): Promise&#60;string&#62;

Generate a JSON Web token, that will be sent to a customer, that wishes to reset password. The token will be signed with the customer's current password hash as a secret a long side a payload with userId and the expiry time for the token, which is always 15 minutes.

Parameters

customerIdstringRequired
the customer to reset the password for

Returns

Promise<string>

PromisePromise<string>Required
the generated JSON web token

hashPassword_

**hashPassword_**(password): Promise&#60;string&#62;

Hashes a password

Parameters

passwordstringRequired
the value to hash

Returns

Promise<string>

PromisePromise<string>Required
hashed password

list

**list**(selector?, config?): Promise&#60;[Customer](/references/services/classes/Customer)[]&#62;

Parameters

selectorSelector<Customer> & &#123; groups?: string[] ; q?: string &#125;Required
the query object for find

Default: {}

configFindConfig<Customer>Required
the config object containing query settings

Returns

Promise<Customer[]>

PromisePromise<Customer[]>Required
the result of the find operation

listAndCount

**listAndCount**(selector, config?): Promise&#60;[[Customer](/references/services/classes/Customer)[], number]&#62;

Parameters

selectorSelector<Customer> & &#123; groups?: string[] ; q?: string &#125;Required
the query object for find
configFindConfig<Customer>Required
the config object containing query settings

Returns

Promise<[Customer[], number]>

PromisePromise<[Customer[], number]>Required
the result of the find operation

listByEmail

**listByEmail**(email, config?): Promise&#60;[Customer](/references/services/classes/Customer)[]&#62;

Parameters

emailstringRequired
configFindConfig<Customer>Required

Returns

Promise<Customer[]>

PromisePromise<Customer[]>Required

removeAddress

**removeAddress**(customerId, addressId): Promise&#60;void&#62;

Parameters

customerIdstringRequired
addressIdstringRequired

Returns

Promise<void>

PromisePromise<void>Required

retrieve

**retrieve**(customerId, config?): Promise&#60;[Customer](/references/services/classes/Customer)&#62;

Gets a customer by id.

Parameters

customerIdstringRequired
the id of the customer to get.
configFindConfig<Customer>Required
the config object containing query settings

Default: {}

Returns

Promise<Customer>

PromisePromise<Customer>Required
the customer document.

retrieveByEmail

**retrieveByEmail**(email, config?): Promise&#60;[Customer](/references/services/classes/Customer)&#62;

Gets a registered customer by email.

Parameters

emailstringRequired
the email of the customer to get.
configFindConfig<Customer>Required
the config object containing query settings

Default: {}

Returns

Promise<Customer>

PromisePromise<Customer>Required
the customer document.

Deprecated


retrieveByPhone

**retrieveByPhone**(phone, config?): Promise&#60;[Customer](/references/services/classes/Customer)&#62;

Gets a customer by phone.

Parameters

phonestringRequired
the phone of the customer to get.
configFindConfig<Customer>Required
the config object containing query settings

Default: {}

Returns

Promise<Customer>

PromisePromise<Customer>Required
the customer document.

retrieveRegisteredByEmail

**retrieveRegisteredByEmail**(email, config?): Promise&#60;[Customer](/references/services/classes/Customer)&#62;

Parameters

emailstringRequired
configFindConfig<Customer>Required

Default: {}

Returns

Promise<Customer>

PromisePromise<Customer>Required

retrieveUnregisteredByEmail

**retrieveUnregisteredByEmail**(email, config?): Promise&#60;[Customer](/references/services/classes/Customer)&#62;

Parameters

emailstringRequired
configFindConfig<Customer>Required

Default: {}

Returns

Promise<Customer>

PromisePromise<Customer>Required

retrieve_

Private **retrieve_**(selector, config?): Promise&#60;[Customer](/references/services/classes/Customer)&#62;

Parameters

selectorSelector<Customer>Required
configFindConfig<Customer>Required

Default: {}

Returns

Promise<Customer>

PromisePromise<Customer>Required

shouldRetryTransaction_

Protected **shouldRetryTransaction_**(err): boolean

Parameters

errRecord<string, unknown> | &#123; code: string &#125;Required

Returns

boolean

booleanboolean

update

**update**(customerId, update): Promise&#60;[Customer](/references/services/classes/Customer)&#62;

Updates a customer.

Parameters

customerIdstringRequired
the id of the variant. Must be a string that can be casted to an ObjectId
updateUpdateCustomerInputRequired
an object with the update values.

Returns

Promise<Customer>

PromisePromise<Customer>Required
resolves to the update result.

updateAddress

**updateAddress**(customerId, addressId, address): Promise&#60;[Address](/references/services/classes/Address)&#62;

Parameters

customerIdstringRequired
addressIdstringRequired

Returns

Promise<Address>

PromisePromise<Address>Required

updateBillingAddress_

**updateBillingAddress_**(customer, addressOrId): Promise&#60;void&#62;

Updates the customers' billing address.

Parameters

customerCustomerRequired
the Customer to update
addressOrIdundefined | string | DeepPartial<Address>Required
the value to set the billing address to

Returns

Promise<void>

PromisePromise<void>Required
the result of the update operation

withTransaction

**withTransaction**(transactionManager?): [CustomerService](/references/services/classes/CustomerService)

Parameters

transactionManagerEntityManager

Returns

CustomerService

CustomerServiceCustomerServiceRequired
Was this section helpful?