File

src/utils/services/validator.service.ts

Index

Methods

Methods

Public isCorrectAmountMoney
isCorrectAmountMoney(role: RoleType, senderAmountMoney: string | number, transactionAmountMoney: string | number)
Parameters :
Name Type Optional
role RoleType No
senderAmountMoney string | number No
transactionAmountMoney string | number No
Returns : boolean
Public isCorrectRecipient
isCorrectRecipient(senderBillId: number, recipientBillId: number)
Parameters :
Name Type Optional
senderBillId number No
recipientBillId number No
Returns : boolean
Public isHigherRole
isHigherRole(role: RoleType)
Parameters :
Name Type Optional
role RoleType No
Returns : boolean
Public isImage
isImage(mimeType: string)
Parameters :
Name Type Optional
mimeType string No
Returns : boolean
import { Injectable } from '@nestjs/common';
import * as _ from 'lodash';
import { RoleType } from 'common/constants';
import {
  AmountMoneyNotEnoughException,
  AttemptMakeTransferToMyselfException,
  BillNotFoundException,
} from 'exceptions';

@Injectable()
export class ValidatorService {
  public isImage(mimeType: string): boolean {
    const imageMimeTypes = ['image/jpeg', 'image/png'];

    return _.includes(imageMimeTypes, mimeType);
  }

  public isCorrectAmountMoney(
    role: RoleType,
    senderAmountMoney: string | number,
    transactionAmountMoney: string | number,
  ): boolean {
    if (transactionAmountMoney <= 0) {
      throw new AmountMoneyNotEnoughException();
    }

    if (this.isHigherRole(role)) {
      return true;
    }

    if (Number(senderAmountMoney) < Number(transactionAmountMoney)) {
      throw new AmountMoneyNotEnoughException();
    }

    return true;
  }

  public isCorrectRecipient(
    senderBillId: number,
    recipientBillId: number,
  ): boolean {
    if (!senderBillId || !recipientBillId) {
      throw new BillNotFoundException();
    }

    if (senderBillId === recipientBillId) {
      throw new AttemptMakeTransferToMyselfException();
    }

    return true;
  }

  public isHigherRole(role: RoleType): boolean {
    if (
      Object.values(RoleType)
        .filter((item) => item !== RoleType.USER)
        .includes(role)
    ) {
      return true;
    }

    return false;
  }
}

result-matching ""

    No results matching ""