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
106 lines (105 loc) • 4.51 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PricingContext = void 0;
var PricingPlanEvaluationError_1 = require("../exceptions/PricingPlanEvaluationError");
var server_1 = require("../server");
/**
* An abstract class from which to create a component that adapts the pricing
* configuration to the application domain
*/
var PricingContext = /** @class */ (function () {
function PricingContext() {
}
/**
* Returns the expiration time of the JWT in milliseconds
*
* @returns JWT expiration time in milliseconds
*/
PricingContext.prototype.getJwtExpiration = function () {
return 86400000;
};
/**
* This method can be used to determine which users are affected
* by the pricing, so a pricing-driven JWT will be only generated
* for them.
*
* @returns A boolean indicating the condition to include or exclude the pricing evaluation context in the JWT.
*/
PricingContext.prototype.userAffectedByPricing = function () {
return true;
};
/**
* This method returns the plan context of the current user, represented by a
* *Record<string, boolean | string | number | PaymentType[]>*. It's used to evaluate the pricing plan.
*
* @return current user's plan context
*/
PricingContext.prototype.getPlanContext = function () {
var _a;
var userPlan = this.getPricing().plans[this.getUserPlan()];
var pricingAddOns = (_a = this.getPricing().addOns) !== null && _a !== void 0 ? _a : {};
var pricingContextToReturn = {
features: __assign({}, userPlan.features),
usageLimits: userPlan.usageLimits ? __assign({}, userPlan.usageLimits) : {},
};
for (var _i = 0, _b = Object.keys(pricingAddOns); _i < _b.length; _i++) {
var addOn = _b[_i];
if (this.getUserAddOns().includes(addOn)) {
if (pricingAddOns[addOn].features) {
for (var _c = 0, _d = Object.keys(pricingAddOns[addOn].features); _c < _d.length; _c++) {
var feature = _d[_c];
pricingContextToReturn.features[feature].value =
pricingAddOns[addOn].features[feature].value;
}
}
if (pricingAddOns[addOn].usageLimits) {
for (var _e = 0, _f = Object.keys(pricingAddOns[addOn].usageLimits); _e < _f.length; _e++) {
var usageLimit = _f[_e];
pricingContextToReturn.usageLimits[usageLimit].value =
pricingAddOns[addOn].usageLimits[usageLimit].value;
}
}
if (pricingAddOns[addOn].usageLimitsExtensions) {
for (var _g = 0, _h = Object.keys(pricingAddOns[addOn].usageLimitsExtensions); _g < _h.length; _g++) {
var usageLimit = _h[_g];
if (pricingContextToReturn.usageLimits[usageLimit].value) {
pricingContextToReturn.usageLimits[usageLimit].value += pricingAddOns[addOn].usageLimitsExtensions[usageLimit].value;
}
else {
pricingContextToReturn.usageLimits[usageLimit].value =
pricingAddOns[addOn].usageLimitsExtensions[usageLimit].value;
}
}
}
}
}
return pricingContextToReturn;
};
/**
* This method returns the PricingManager object that is being used to
* evaluate the pricing plan.
*
* @returns PricingManager object
*/
PricingContext.prototype.getPricing = function () {
try {
return (0, server_1.retrievePricingFromPath)(this.getConfigFilePath());
}
catch (error) {
throw new PricingPlanEvaluationError_1.PricingPlanEvaluationError(error.message);
}
};
return PricingContext;
}());
exports.PricingContext = PricingContext;