Skip to main content
Skip to main content

UserService

Provides layer to manipulate users.

Constructors

constructor

**new UserService**(«destructured»)

Parameters
__namedParametersUserServicePropsRequired

Properties

__configModule__Record<string, unknown>
__container__anyRequired
__moduleDeclaration__Record<string, unknown>
analyticsConfigService_AnalyticsConfigServiceRequired
eventBus_EventBusServiceRequired
featureFlagRouter_FlagRouterRequired
manager_EntityManagerRequired
transactionManager_undefined | EntityManagerRequired
userRepository_Repository<User>Required
EventsobjectRequired
Events.CREATEDstringRequired

Default: "user.created"

Events.DELETEDstringRequired

Default: "user.deleted"

Events.PASSWORD_RESETstringRequired

Default: "user.password_reset"

Events.UPDATEDstringRequired

Default: "user.updated"

Accessors

activeManager_

Protected activeManager_: [object Object]

Methods

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.

Type Parameters
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

create

**create**(user, password): Promise&#60;[User](/references/services/classes/User)&#62;

Creates a user with username being validated. Fails if email is not a valid format.

Parameters
userCreateUserInputRequired
the user to create
passwordstringRequired
user's password to hash
Returns

Promise<User>

PromisePromise<User>Required
the result of create

delete

**delete**(userId): Promise&#60;void&#62;

Deletes a user from a given user id.

Parameters
userIdstringRequired
the id of the user to delete. Must be castable as an ObjectId
Returns

Promise<void>

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

generateResetPasswordToken

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

Generate a JSON Web token, that will be sent to a user, that wishes to reset password. The token will be signed with the users 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
userIdstringRequired
the id of the user to reset 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;[User](/references/services/classes/User)[]&#62;

Parameters
selectorFilterableUserPropsRequired
the query object for find
configobjectRequired
the configuration object for the query

Default: {}

Returns

Promise<User[]>

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

retrieve

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

Gets a user by id. Throws in case of DB Error and if user was not found.

Parameters
userIdstringRequired
the id of the user to get.
configFindConfig<User>Required
query configs

Default: {}

Returns

Promise<User>

PromisePromise<User>Required
the user document.

retrieveByApiToken

**retrieveByApiToken**(apiToken, relations?): Promise&#60;[User](/references/services/classes/User)&#62;

Gets a user by api token. Throws in case of DB Error and if user was not found.

Parameters
apiTokenstringRequired
the token of the user to get.
relationsstring[]Required
relations to include with the user.

Default: []

Returns

Promise<User>

PromisePromise<User>Required
the user document.

retrieveByEmail

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

Gets a user by email. Throws in case of DB Error and if user was not found.

Parameters
emailstringRequired
the email of the user to get.
configFindConfig<User>Required
query config

Default: {}

Returns

Promise<User>

PromisePromise<User>Required
the user document.

setPassword_

**setPassword_**(userId, password): Promise&#60;[User](/references/services/classes/User)&#62;

Sets a password for a user Fails if no user exists with userId and if the hashing of the new password does not work.

Parameters
userIdstringRequired
the userId to set password for
passwordstringRequired
the old password to set
Returns

Promise<User>

PromisePromise<User>Required
the result of the update operation

shouldRetryTransaction_

Protected **shouldRetryTransaction_**(err): boolean

Parameters
errRecord<string, unknown> | { code: string }Required
Returns

boolean

booleanboolean

update

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

Updates a user.

Parameters
userIdstringRequired
id of the user to update
updateUpdateUserInputRequired
the values to be updated on the user
Returns

Promise<User>

PromisePromise<User>Required
the result of create

withTransaction

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

Parameters
transactionManagerEntityManager
Returns

UserService

UserServiceUserServiceRequired
Was this section helpful?