@foxy.io/sdk
Version:
Universal SDK for a full server-side and a limited in-browser access to Foxy hAPI.
59 lines (58 loc) • 2.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNextTransactionDateConstraints = void 0;
const getTimeFromFrequency_js_1 = require("./getTimeFromFrequency.js");
const jsonata_1 = __importDefault(require("jsonata"));
/**
* Finds which next transaction date modification rules are applicable to
* the given subscription and merges them together.
*
* @param data Subscription to generate constraints for.
* @param rules Next date modification config from the customer portal settings.
*
* @returns
* Returns true if all modifications are allowed, false if next date can't
* be changed by the customer, object with constraints in any other case.
*/
function getNextTransactionDateConstraints(data, rules) {
if (typeof rules === 'boolean')
return rules;
const combinedRule = rules
.filter(rule => Boolean(jsonata_1.default(rule.jsonataQuery).evaluate(data)))
.reduce((result, rule) => {
var _a, _b, _c, _d, _e;
if (rule.min) {
const currentTime = result.min ? getTimeFromFrequency_js_1.getTimeFromFrequency(result.min) : Infinity;
const proposedTime = getTimeFromFrequency_js_1.getTimeFromFrequency(rule.min);
if (proposedTime < currentTime)
result.min = rule.min;
}
if (rule.max) {
const currentTime = result.max ? getTimeFromFrequency_js_1.getTimeFromFrequency(result.max) : -Infinity;
const proposedTime = getTimeFromFrequency_js_1.getTimeFromFrequency(rule.max);
if (proposedTime > currentTime)
result.max = rule.max;
}
if (((_a = rule.allowedDays) === null || _a === void 0 ? void 0 : _a.type) === 'day') {
const previousSet = (_b = result.allowedDaysOfWeek) !== null && _b !== void 0 ? _b : [];
const expandedSet = [...previousSet, ...rule.allowedDays.days];
result.allowedDaysOfWeek = Array.from(new Set(expandedSet));
}
if (((_c = rule.allowedDays) === null || _c === void 0 ? void 0 : _c.type) === 'month') {
const previousSet = (_d = result.allowedDaysOfMonth) !== null && _d !== void 0 ? _d : [];
const expandedSet = [...previousSet, ...rule.allowedDays.days];
result.allowedDaysOfMonth = Array.from(new Set(expandedSet));
}
if (rule.disallowedDates) {
const previousSet = (_e = result.disallowedDates) !== null && _e !== void 0 ? _e : [];
const expandedSet = [...previousSet, ...rule.disallowedDates];
result.disallowedDates = Array.from(new Set(expandedSet));
}
return result;
}, {});
return Object.keys(combinedRule).length === 0 ? false : combinedRule;
}
exports.getNextTransactionDateConstraints = getNextTransactionDateConstraints;