File

src/modules/app/services/app.service.ts

Index

Properties
Methods

Constructor

constructor(_moduleRef: ModuleRef)
Parameters :
Name Type Optional
_moduleRef ModuleRef No

Methods

Private Async _initAuthorUser
_initAuthorUser()
Returns : Promise<void>
Private Async _initRootUser
_initRootUser()
Returns : Promise<void>
Public Async onModuleInit
onModuleInit()
Returns : Promise<void>

Properties

Private Readonly _configService
Default value : new ConfigService()
Private Readonly _logger
Default value : new Logger(AppService.name)
Private Readonly _moduleOptions
Type : object
Default value : { strict: false }
Private _userAuthService
Type : UserAuthService
Private _userService
Type : UserService
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`);
  }
}

result-matching ""

    No results matching ""