File

src/modules/user/services/user-auth.service.ts

Index

Methods

Constructor

constructor(_userAuthRepository: UserAuthRepository, _userRepostiory: UserRepository, _userService: UserService, _userConfigService: UserConfigService)
Parameters :
Name Type Optional
_userAuthRepository UserAuthRepository No
_userRepostiory UserRepository No
_userService UserService No
_userConfigService UserConfigService No

Methods

Private Async _createPinCode
_createPinCode()
Returns : Promise<number>
Private _generatePinCode
_generatePinCode()
Returns : number
Private Async _updateLastFailedLoggedDate
_updateLastFailedLoggedDate(userAuth: UserAuthEntity)
Parameters :
Name Type Optional
userAuth UserAuthEntity No
Returns : Promise<UpdateResult>
Private Async _updateLastSuccessfulLoggedDate
_updateLastSuccessfulLoggedDate(userAuth: UserAuthEntity, lastPresentLoggedDate?: Date)
Parameters :
Name Type Optional
userAuth UserAuthEntity No
lastPresentLoggedDate Date Yes
Returns : Promise<UpdateResult>
Public Async createUserAuth
createUserAuth(createdUser)
Parameters :
Name Optional
createdUser No
Public Async findUserAuth
findUserAuth(options: Partial)
Parameters :
Name Type Optional
options Partial<literal type> No
Public Async updateLastLoggedDate
updateLastLoggedDate(user: UserEntity, isSuccessiveLogged: boolean)
Decorators :
@Transactional()
Parameters :
Name Type Optional
user UserEntity No
isSuccessiveLogged boolean No
Public Async updateLastLogoutDate
updateLastLogoutDate(userAuth: UserAuthEntity)
Parameters :
Name Type Optional
userAuth UserAuthEntity No
Returns : Promise<UpdateResult>
Public Async updatePassword
updatePassword(userAuth: UserAuthEntity, password: string)
Parameters :
Name Type Optional
userAuth UserAuthEntity No
password string No
Returns : Promise<UpdateResult>
Public Async updateRole
updateRole(userAuth: UserAuthEntity, role: RoleType)
Parameters :
Name Type Optional
userAuth UserAuthEntity No
role RoleType No
Returns : Promise<UpdateResult>
import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { RoleType } from 'common/constants';
import {
  CreateFailedException,
  PinCodeGenerationIncorrectException,
} from 'exceptions';
import { UserAuthEntity, UserEntity } from 'modules/user/entities';
import { UserAuthRepository, UserRepository } from 'modules/user/repositories';
import { UserService } from 'modules/user/services';
import { UtilsService } from 'utils/services';
import { UpdateResult } from 'typeorm';
import { Transactional } from 'typeorm-transactional-cls-hooked';
import { UserConfigService } from './user-config.service';

@Injectable()
export class UserAuthService {
  constructor(
    private readonly _userAuthRepository: UserAuthRepository,
    private readonly _userRepostiory: UserRepository,
    @Inject(forwardRef(() => UserService))
    private readonly _userService: UserService,
    private readonly _userConfigService: UserConfigService,
  ) {}

  @Transactional()
  public async updateLastLoggedDate(
    user: UserEntity,
    isSuccessiveLogged: boolean,
  ): Promise<UserEntity> {
    const {
      userAuth,
      userConfig,
      userAuth: { lastSuccessfulLoggedDate },
      userConfig: { lastPresentLoggedDate },
    } = user;
    if (!isSuccessiveLogged) {
      await this._updateLastFailedLoggedDate(userAuth);
    } else if (isSuccessiveLogged && !lastSuccessfulLoggedDate) {
      await Promise.all([
        this._updateLastSuccessfulLoggedDate(userAuth),
        this._userConfigService.updateLastPresentLoggedDate(userConfig),
      ]);
    } else {
      await Promise.all([
        this._updateLastSuccessfulLoggedDate(userAuth, lastPresentLoggedDate),
        this._userConfigService.updateLastPresentLoggedDate(userConfig),
      ]);
    }

    return this._userService.getUser({ uuid: user.uuid });
  }

  public async updateLastLogoutDate(
    userAuth: UserAuthEntity,
  ): Promise<UpdateResult> {
    const queryBuilder = this._userAuthRepository.createQueryBuilder(
      'userAuth',
    );

    return queryBuilder
      .update()
      .set({ lastLogoutDate: new Date() })
      .where('id = :id', { id: userAuth.id })
      .execute();
  }

  public async updateRole(
    userAuth: UserAuthEntity,
    role: RoleType,
  ): Promise<UpdateResult> {
    const queryBuilder = this._userAuthRepository.createQueryBuilder(
      'userAuth',
    );

    return queryBuilder
      .update()
      .set({ role })
      .where('id = :id', { id: userAuth.id })
      .execute();
  }

  public async updatePassword(
    userAuth: UserAuthEntity,
    password: string,
  ): Promise<UpdateResult> {
    const queryBuilder = this._userAuthRepository.createQueryBuilder(
      'userAuth',
    );

    return queryBuilder
      .update()
      .set({ password })
      .where('id = :id', { id: userAuth.id })
      .execute();
  }

  public async findUserAuth(
    options: Partial<{ email: string; pinCode: number; role: RoleType }>,
  ): Promise<UserEntity | undefined> {
    const queryBuilder = this._userRepostiory.createQueryBuilder('user');

    queryBuilder
      .leftJoinAndSelect('user.userAuth', 'userAuth')
      .leftJoinAndSelect('user.userConfig', 'userConfig')

    if (options.email) {
      queryBuilder.orWhere('user.email = :email', {
        email: options.email,
      });
    }
    
    if (options.role) {
      queryBuilder.orWhere('userAuth.role = :role', { role: options.role });
    }

    return queryBuilder.getOne();
  }

  public async createUserAuth(createdUser): Promise<UserAuthEntity[]> {
    // const pinCode = await this._createPinCode();
    const auth = this._userAuthRepository.create({ ...createdUser });
  
    try {
      return this._userAuthRepository.save(auth);
    } catch (error) {
      throw new CreateFailedException(error);
    }
  }

  private async _createPinCode(): Promise<number> {
    const pinCode = this._generatePinCode();
    const user = await this.findUserAuth({ pinCode });

    try {
      return user ? await this._createPinCode() : pinCode;
    } catch (error) {
      throw new PinCodeGenerationIncorrectException(error);
    }
  }

  private _generatePinCode(): number {
    return UtilsService.generateRandomInteger(1, 10e5 - 1);
  }

  private async _updateLastFailedLoggedDate(
    userAuth: UserAuthEntity,
  ): Promise<UpdateResult> {
    const queryBuilder = this._userAuthRepository.createQueryBuilder(
      'userAuth',
    );

    return queryBuilder
      .update()
      .set({ lastFailedLoggedDate: new Date() })
      .where('id = :id', { id: userAuth.id })
      .execute();
  }

  private async _updateLastSuccessfulLoggedDate(
    userAuth: UserAuthEntity,
    lastPresentLoggedDate?: Date,
  ): Promise<UpdateResult> {
    const queryBuilder = this._userAuthRepository.createQueryBuilder(
      'userAuth',
    );

    return queryBuilder
      .update()
      .set({ lastSuccessfulLoggedDate: lastPresentLoggedDate ?? new Date() })
      .where('id = :id', { id: userAuth.id })
      .execute();
  }
}

result-matching ""

    No results matching ""