UNPKG

@foxy.io/sdk

Version:

Universal SDK for a full server-side and a limited in-browser access to Foxy hAPI.

52 lines (51 loc) 2.5 kB
import { getTimeFromFrequency } from './getTimeFromFrequency.js'; import jsonata from '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. */ export function getNextTransactionDateConstraints(data, rules) { if (typeof rules === 'boolean') return rules; const combinedRule = rules .filter(rule => Boolean(jsonata(rule.jsonataQuery).evaluate(data))) .reduce((result, rule) => { var _a, _b, _c, _d, _e; if (rule.min) { const currentTime = result.min ? getTimeFromFrequency(result.min) : Infinity; const proposedTime = getTimeFromFrequency(rule.min); if (proposedTime < currentTime) result.min = rule.min; } if (rule.max) { const currentTime = result.max ? getTimeFromFrequency(result.max) : -Infinity; const proposedTime = 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; }