src/modules/app/services/app.service.ts
Properties |
|
Methods |
|
constructor(_moduleRef: ModuleRef)
|
||||||
Defined in src/modules/app/services/app.service.ts:14
|
||||||
Parameters :
|
Private Async _initAuthorUser |
_initAuthorUser()
|
Defined in src/modules/app/services/app.service.ts:52
|
Returns :
Promise<void>
|
Private Async _initRootUser |
_initRootUser()
|
Defined in src/modules/app/services/app.service.ts:27
|
Returns :
Promise<void>
|
Public Async onModuleInit |
onModuleInit()
|
Defined in src/modules/app/services/app.service.ts:18
|
Returns :
Promise<void>
|
Private Readonly _configService |
Default value : new ConfigService()
|
Defined in src/modules/app/services/app.service.ts:10
|
Private Readonly _logger |
Default value : new Logger(AppService.name)
|
Defined in src/modules/app/services/app.service.ts:9
|
Private Readonly _moduleOptions |
Type : object
|
Default value : { strict: false }
|
Defined in src/modules/app/services/app.service.ts:11
|
Private _userAuthService |
Type : UserAuthService
|
Defined in src/modules/app/services/app.service.ts:14
|
Private _userService |
Type : UserService
|
Defined in src/modules/app/services/app.service.ts:13
|
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { RoleType } from 'common/constants';
import { UserAuthService, UserService } from 'modules/user/services';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class AppService implements OnModuleInit {
private readonly _logger = new Logger(AppService.name);
private readonly _configService = new ConfigService();
private readonly _moduleOptions = { strict: false };
private _userService: UserService;
private _userAuthService: UserAuthService;
constructor(private readonly _moduleRef: ModuleRef) {}
public async onModuleInit(): Promise<void> {
this._userService = this._moduleRef.get(UserService, this._moduleOptions);
this._userAuthService = this._moduleRef.get(
UserAuthService,
this._moduleOptions,
);
await Promise.all([ this._initAuthorUser(), this._initRootUser()]);
}
private async _initRootUser(): Promise<void> {
const rootEmail = this._configService.get('BANK_ROOT_EMAIL');
const rootPassword = this._configService.get('BANK_ROOT_PASSWORD');
const isExistRootUser = await this._userService.getUser({
email: rootEmail,
});
if (isExistRootUser) {
return;
}
const { userAuth } = await this._userService.createUser({
firstName: 'Bank',
lastName: 'Application',
email: rootEmail,
password: rootPassword,
});
await this._userAuthService.updateRole(userAuth, RoleType.ROOT);
this._logger.log(`Root user have been initiated`);
}
private async _initAuthorUser(): Promise<void> {
const authorEmail = this._configService.get('BANK_AUTHOR_EMAIL');
const authorPassword = this._configService.get('BANK_AUTHOR_PASSWORD');
const authorFirstName = this._configService.get('BANK_AUTHOR_FIRSTNAME');
const authorLastName = this._configService.get('BANK_AUTHOR_LASTNAME');
const isExistAuthorUser = await this._userService.getUser({
email: authorEmail,
});
if (isExistAuthorUser) {
return;
}
const { userAuth } = await this._userService.createUser({
firstName: authorFirstName,
lastName: authorLastName,
email: authorEmail,
password: authorPassword
});
await this._userAuthService.updateRole(userAuth, RoleType.ADMIN);
this._logger.log(`Author user have been initiated`);
}
}