@exode-team/ofd-uz
Version:
Node.js module for interacting with OFD tax system
95 lines (94 loc) • 3.67 kB
JavaScript
;
/**
* OFDClient
*
* @author: exode <hello@exode.ru>
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationError = void 0;
exports.validateQRPayment = validateQRPayment;
exports.validateReceipt = validateReceipt;
const lodash_1 = __importDefault(require("lodash"));
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = 'ValidationError';
}
}
exports.ValidationError = ValidationError;
function validateQRPayment(payment) {
if (!payment.PaymentId || payment.PaymentId.length > 36) {
throw new ValidationError('Invalid PaymentId');
}
if (!lodash_1.default.isFinite(+payment.PaidSum) || payment.PaidSum <= 0) {
throw new ValidationError('Invalid PaidSum');
}
if (!payment.DateTime.match(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/)) {
throw new ValidationError('Invalid DateTime format');
}
if (!payment.TIN || !/^\d{9}$/.test(payment.TIN)) {
throw new ValidationError('Invalid TIN');
}
if (payment.PINFL && !/^\d{14}$/.test(payment.PINFL)) {
throw new ValidationError('Invalid PINFL');
}
if (!payment.PhoneNumber || !/^998\d{9}$/.test(payment.PhoneNumber)) {
throw new ValidationError('Invalid PhoneNumber');
}
}
function validateReceipt(receipt) {
if (!lodash_1.default.isFinite(+receipt.ReceiptSeq) || receipt.ReceiptSeq <= 0) {
throw new ValidationError('Invalid ReceiptSeq');
}
if (![0, 1].includes(receipt.IsRefund)) {
throw new ValidationError('Invalid IsRefund value');
}
if (!Array.isArray(receipt.Items) || receipt.Items.length === 0) {
throw new ValidationError('Receipt must contain at least one item');
}
receipt.Items.forEach(validateItem);
if (!lodash_1.default.isFinite(+receipt.ReceivedCash) || receipt.ReceivedCash < 0) {
throw new ValidationError('Invalid ReceivedCash value');
}
if (lodash_1.default.isFinite(+receipt.ReceivedCard) || receipt.ReceivedCard < 0) {
throw new ValidationError('Invalid ReceivedCard value');
}
if (!receipt.Time.match(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/)) {
throw new ValidationError('Invalid Time format');
}
}
function validateItem(item) {
if (!item.Name || item.Name.length > 63) {
throw new ValidationError('Invalid item Name');
}
if (item.Barcode && !/^\d{13}$/.test(item.Barcode)) {
throw new ValidationError('Invalid Barcode format');
}
if (!item.SPIC || !/^\d{17}$/.test(item.SPIC)) {
throw new ValidationError('Invalid SPIC');
}
if (!item.PackageCode || item.PackageCode.length > 20) {
throw new ValidationError('Invalid PackageCode');
}
if (![0, 1, 2].includes(item.OwnerType)) {
throw new ValidationError('Invalid OwnerType');
}
if (!lodash_1.default.isFinite(+item.GoodPrice) || item.GoodPrice <= 0) {
throw new ValidationError('Invalid GoodPrice');
}
if (!lodash_1.default.isFinite(+item.Price) || item.Price <= 0) {
throw new ValidationError('Invalid Price');
}
if (!lodash_1.default.isFinite(+item.Amount) || item.Amount <= 0) {
throw new ValidationError('Invalid Amount');
}
if (!lodash_1.default.isFinite(+item.VAT) || item.VAT < 0) {
throw new ValidationError('Invalid VAT');
}
if (!lodash_1.default.isFinite(+item.VATPercent) || item.VATPercent < 0) {
throw new ValidationError('Invalid VATPercent');
}
}