pricing4ts
Version:
 Pricing4TS is a TypeScript-based toolkit designed to enhance the server-side functionality of a pricing-driven SaaS by enabling the seamless integration of pricing plans into the application logic. T
67 lines (66 loc) • 2.91 kB
JavaScript
;
// deno-lint-ignore-file no-explicit-any
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = v21Tov30Updater;
var version_manager_1 = require("../../version-manager");
function v21Tov30Updater(extractedPricing) {
var nextVersion = (0, version_manager_1.calculateNextVersion)(extractedPricing.syntaxVersion);
extractedPricing.syntaxVersion = nextVersion;
_replaceTimeDrivenAndResponseDrivenLimits(extractedPricing);
_addSubscriptionConstraintsToAddOns(extractedPricing);
_renameContextsInExpressions(extractedPricing);
return extractedPricing;
}
function _replaceTimeDrivenAndResponseDrivenLimits(extractedPricing) {
var usageLimits = extractedPricing.usageLimits;
for (var usageLimitKey in usageLimits) {
var usageLimit = usageLimits[usageLimitKey];
if (usageLimit.type === "TIME_DRIVEN") {
usageLimit.type = "RENEWABLE";
usageLimit.period = {
value: 1,
unit: "MONTH"
};
}
else if (usageLimit.type === "RESPONSE_DRIVEN") {
usageLimit.type = "NON_RENEWABLE";
usageLimit.trackable = false;
}
else if (usageLimit.type === "RENEWABLE" || usageLimit.type === "NON_RENEWABLE") {
continue;
}
else {
throw new Error("Unsupported usage limit type: ".concat(usageLimit.type, ". Valid types are: RENEWABLE, NON_RENEWABLE, TIME_DRIVEN, RESPONSE_DRIVEN."));
}
}
}
function _addSubscriptionConstraintsToAddOns(extractedPricing) {
var addOns = extractedPricing.addOns;
for (var addOnKey in addOns) {
var addOn = addOns[addOnKey];
var isScalable = (addOn.features && Object.keys(addOn.features).length === 0) ||
(addOn.usageLimits && Object.keys(addOn.usageLimits).length === 0) ||
(addOn.usageLimitsExtensions && Object.keys(addOn.usageLimitsExtensions).length > 0);
if (isScalable && !addOn.subscriptionConstraints) {
addOn.subscriptionConstraints = {
minQuantity: 1,
maxQuantity: Infinity,
quantityStep: 1
};
}
}
}
function _renameContextsInExpressions(extractedPricing) {
var features = extractedPricing.features;
for (var featureKey in features) {
var feature = features[featureKey];
if (feature.expression) {
feature.expression = feature.expression.replace("planContext", "pricingContext");
feature.expression = feature.expression.replace("userContext", "subscriptionContext");
}
if (feature.serverExpression) {
feature.serverExpression = feature.serverExpression.replace("planContext", "pricingContext");
feature.serverExpression = feature.serverExpression.replace("userContext", "subscriptionContext");
}
}
}