voucher-validator
Version:
This lib provide validator to check business rules for a voucher
107 lines • 4.42 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Validator = void 0;
const calculate_percentage_1 = __importDefault(require("../utils/calculate-percentage"));
const fixed_value_1 = __importDefault(require("../utils/fixed-value"));
const voucher_validator_1 = __importDefault(require("./voucher-validator"));
const get_discount_1 = __importDefault(require("../utils/get-discount"));
const total_to_cents_1 = __importDefault(require("../utils/total-to-cents"));
const cents_to_total_1 = __importDefault(require("../utils/cents-to-total"));
const voucher_specification_1 = require("../specification/voucher.specification");
const voucher_specification_2 = require("../specification/voucher.specification");
const voucher_specification_3 = require("../specification/voucher.specification");
const get_value_1 = __importDefault(require("../utils/get-value"));
class Validator {
/**
* @description calculate total to pay applying the discount
* @param total Product price total
* @param voucher Object
* @returns total to pay with discount
*/
CalculateTotalWithDiscount(total, voucher) {
const discount = (0, get_discount_1.default)(voucher);
return (0, fixed_value_1.default)((0, get_value_1.default)(total) - (0, calculate_percentage_1.default)((0, get_value_1.default)(total), discount));
}
/**
* @description check voucher start date and end date with current date
* @param voucher Object
* @returns true if current date is greater than start date and less than end date
*/
IsOnRangeDate(voucher) {
return new voucher_specification_1.IsOnRangeDateSpecification().isSatisfiedBy(voucher);
}
/**
* @description check if voucher has a document
* @param document string
* @param voucher Object
* @returns true if voucher has document and false if not
*/
HasDocument(document, voucher) {
return new voucher_specification_3.IsValidDocumentSpecification(document).isSatisfiedBy(voucher);
}
/**
* @description check if voucher has a product
* @param product string
* @param voucher Object
* @returns true if voucher has product and false if not
*/
HasProduct(product, voucher) {
return new voucher_specification_2.IsValidProductSpecification(product).isSatisfiedBy(voucher);
}
/**
* @description check if voucher has a convenio
* @param convenio string or number
* @param voucher Object
* @returns true if voucher has convenio and false if not
*/
HasConvenio(convenio, voucher) {
return String(convenio) === String(voucher.idConvenio);
}
/**
* @description check if voucher has a client
* @param cliente string or number
* @param voucher Object
* @returns true if voucher has cliente and false if not
*/
HasCliente(cliente, voucher) {
return String(cliente) === String(voucher.idCliente);
}
/**
* @description calculate total of discount applied
* @param total Product price total
* @param voucher Object
* @returns total of discount
*/
GetDiscount(total, voucher) {
const totalToPay = this.CalculateTotalWithDiscount((0, get_value_1.default)(total), voucher);
return (0, fixed_value_1.default)((0, cents_to_total_1.default)((0, total_to_cents_1.default)(total) - (0, total_to_cents_1.default)(totalToPay)));
}
/**
* @description compare a code string with voucher code
* @param voucher Voucher
* @param code string
* @returns true if voucher code is equal or case is different
*/
Compare(voucher, code) {
return voucher.codigoVoucher === code;
}
/**
* @description validate all voucher properties
* @param props Props
* @returns true if is valid voucher and false case not
* @access IsUsedVoucher
* @access IsOnRangeDate
* @access IsNominal and UserDocument Match
* @access IsValidProduct
* @access IsVoucherDeleted
*/
Validate(props) {
return new voucher_validator_1.default(props).isSatisfiedBy(props.voucher);
}
}
exports.Validator = Validator;
exports.default = Validator;
//# sourceMappingURL=validator.js.map