import Joi from "joi";
import { Result, RuleValidator } from "../../domain";

export interface IMoneyDTO {
  amount: number | null;
  scale: number;
  currencycode: string;
}

export interface IMoneyRequestDTO extends IMoneyDTO {}
export interface IMoneyResponseDTO extends IMoneyDTO {}

export function ensureMoneyDTOIsValid(money: IMoneyRequestDTO) {
  const schema = Joi.object({
    amount: Joi.number(),
    scale: Joi.number(),
    currencycode: Joi.string(),
  });

  const result = RuleValidator.validate<IMoneyRequestDTO>(schema, money);

  if (result.isFailure) {
    return Result.fail(result.error);
  }

  return Result.ok(true);
}
