@foxy.io/sdk
Version:
Universal SDK for a full server-side and a limited in-browser access to Foxy hAPI.
58 lines (57 loc) • 2.45 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isNextTransactionDate = void 0;
const getNextTransactionDateConstraints_js_1 = require("./getNextTransactionDateConstraints.js");
const parse_duration_1 = __importDefault(require("parse-duration"));
/**
* Converts YYYY-MM-DD to date.
*
* @param yyyyMmDd Date as YYYY-MM-DD.
* @returns Date object.
*/
function toDate(yyyyMmDd) {
const [yyyy, mm, dd] = yyyyMmDd.split('-').map(v => parseInt(v));
return new Date(yyyy, mm - 1, dd);
}
/**
* Checks if given date (YYYY-MM-DD) can be used as the next transaction
* date for given subscription.
*
* @param opts Subscription, customer portal settings and value.
* @returns True if given date can be used as next transaction date.
*/
function isNextTransactionDate(opts) {
var _a, _b;
const valueAsDate = toDate(opts.value);
const valueAsTime = valueAsDate.getTime();
const constraints = getNextTransactionDateConstraints_js_1.getNextTransactionDateConstraints(opts.subscription, opts.settings.subscriptions.allowNextDateModification);
if (typeof constraints === 'boolean')
return constraints;
if (((_a = constraints.allowedDaysOfMonth) === null || _a === void 0 ? void 0 : _a.includes(valueAsDate.getDate())) === false)
return false;
if (((_b = constraints.allowedDaysOfWeek) === null || _b === void 0 ? void 0 : _b.includes(valueAsDate.getDay())) === false)
return false;
if (constraints.disallowedDates) {
const match = constraints.disallowedDates.find(dateOrRange => {
const [from, to] = dateOrRange.split('..').map(v => toDate(v).getTime());
return valueAsTime === from || (to !== undefined && valueAsTime >= from && valueAsTime <= to);
});
if (match)
return false;
}
if (constraints.min) {
const duration = parse_duration_1.default(constraints.min);
if (duration !== null && Date.now() + duration >= valueAsTime)
return false;
}
if (constraints.max) {
const duration = parse_duration_1.default(constraints.max);
if (duration !== null && Date.now() + duration <= valueAsTime)
return false;
}
return true;
}
exports.isNextTransactionDate = isNextTransactionDate;