@1amageek/tradable
Version:
Cloud Firestore model framework for TypeScript - Google
82 lines • 3.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderValidator = void 0;
const index_1 = require("./index");
const isUndefined = (value) => {
return (value === null || value === undefined || Number.isNaN(value));
};
class OrderValidator {
constructor(order, orderItem) {
this._Order = order;
this._OrderItem = orderItem;
}
validate(order, items) {
if (isUndefined(order.purchasedBy))
return new index_1.TradableError(index_1.TradableErrorCode.invalidArgument, `[Tradable] Error: validation error, purchasedBy is required`);
if (isUndefined(order.selledBy))
return new index_1.TradableError(index_1.TradableErrorCode.invalidArgument, `[Tradable] Error: validation error, selledBy is required`);
if (isUndefined(order.expirationDate))
return new index_1.TradableError(index_1.TradableErrorCode.invalidArgument, `[Tradable] Error: validation error, expirationDate is required`);
if (isUndefined(order.currency))
return new index_1.TradableError(index_1.TradableErrorCode.invalidArgument, `[Tradable] Error: validation error, currency is required`);
if (isUndefined(order.amount))
return new index_1.TradableError(index_1.TradableErrorCode.invalidArgument, `[Tradable] Error: validation error, amount is required`);
if (!this.validateMinimumAmount(order))
return new index_1.TradableError(index_1.TradableErrorCode.invalidArgument, `[Tradable] Error: validation error, Amount is below the lower limit.`);
try {
if (!this.validateCurrency(order, items))
return new index_1.TradableError(index_1.TradableErrorCode.invalidCurrency, `[Tradable] Error: validation error, Currency of OrderItem does not match Currency of Order.`);
if (!this.validateAmount(order, items))
return new index_1.TradableError(index_1.TradableErrorCode.invalidAmount, `[Tradable] Error: validation error, The sum of OrderItem does not match Amount of Order.`);
const orderItemError = this.validateOrderItem(order, items);
if (orderItemError)
return orderItemError;
}
catch (error) {
if (error instanceof Error) {
return error;
}
return new index_1.TradableError(index_1.TradableErrorCode.internal, `[Tradable] Error: ${String(error)}`);
}
}
validateMinimumAmount(order) {
const currency = order.currency;
const amount = order.amount;
if (0 < amount && amount < index_1.Currency.minimum(currency)) {
return false;
}
return true;
}
// Returns true if there is no problem in the verification
validateCurrency(order, orderItems) {
for (const item of orderItems) {
if (item.currency !== order.currency) {
return false;
}
}
return true;
}
// Returns true if there is no problem in the verification
validateAmount(order, orderItems) {
let totalAmount = 0;
for (const item of orderItems) {
totalAmount += (item.amount * item.quantity);
}
if (totalAmount !== order.amount) {
return false;
}
return true;
}
validateOrderItem(order, orderItems) {
for (const orderItem of orderItems) {
const skuID = orderItem.sku;
if (orderItem.type === index_1.OrderItemType.sku) {
if (!skuID) {
return new index_1.TradableError(index_1.TradableErrorCode.internal, `[Failure] ORDER/${order.id} Order requires skuID.`);
}
}
}
}
}
exports.OrderValidator = OrderValidator;
//# sourceMappingURL=orderValidator.js.map