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
102 lines (101 loc) • 4.82 kB
JavaScript
;
// deno-lint-ignore-file no-explicit-any
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.default = v11Tov20Updater;
var pricing_parser_1 = require("../../../../main/utils/pricing-formatting/pricing-parser");
var version_manager_1 = require("../../version-manager");
function v11Tov20Updater(extractedPricing) {
var nextVersion = (0, version_manager_1.calculateNextVersion)(extractedPricing.version);
extractedPricing.version = nextVersion;
_updatePrices(extractedPricing);
_removeHasAnnualPayment(extractedPricing);
return extractedPricing;
}
function _removeHasAnnualPayment(extractedPricing) {
extractedPricing.hasAnnualPayment = undefined;
}
function _updatePrices(extractedPricing) {
if (extractedPricing.plans !== null && extractedPricing.plans !== undefined) {
var pricingPlans = (0, pricing_parser_1.formatObjectToArray)(extractedPricing.plans);
extractedPricing.billing = {};
for (var _i = 0, pricingPlans_1 = pricingPlans; _i < pricingPlans_1.length; _i++) {
var plan = pricingPlans_1[_i];
if (plan.monthlyPrice && plan.annualPrice) {
plan.price = plan.monthlyPrice;
var annualBillingCoef = _computeAnnualBillingCoef(plan.monthlyPrice, plan.annualPrice);
if (Number.isNaN(annualBillingCoef)) {
extractedPricing.billing = __assign({ "monthly": 1 }, extractedPricing.billing);
}
else {
extractedPricing.billing = __assign({ "monthly": 1, "annual": annualBillingCoef }, extractedPricing.billing);
}
}
else if (plan.monthlyPrice === null || plan.monthlyPrice === undefined) {
plan.price = plan.annualPrice;
extractedPricing.billing = __assign({ "annual": 1 }, extractedPricing.billing);
}
else {
plan.price = plan.monthlyPrice;
extractedPricing.billing = __assign({ "monthly": 1 }, extractedPricing.billing);
}
if (plan.price === null || plan.price === undefined) {
throw new Error("Monthly or annual price is required for each plan");
}
plan.monthlyPrice = undefined;
plan.annualPrice = undefined;
}
extractedPricing.plans = pricingPlans;
}
if (extractedPricing.addOns !== null && extractedPricing.addOns !== undefined) {
var pricingAddons = (0, pricing_parser_1.formatObjectToArray)(extractedPricing.addOns);
for (var _a = 0, pricingAddons_1 = pricingAddons; _a < pricingAddons_1.length; _a++) {
var addon = pricingAddons_1[_a];
if (addon.price !== null && addon.price !== undefined)
continue;
if (addon.monthlyPrice === null || addon.monthlyPrice === undefined) {
addon.price = addon.annualPrice;
extractedPricing.billing = __assign({ "annual": 1 }, extractedPricing.billing);
}
else {
addon.price = addon.monthlyPrice;
extractedPricing.billing = __assign({ "monthly": 1 }, extractedPricing.billing);
}
if (addon.price === null || addon.price === undefined) {
throw new Error("Monthly or annual price is required for each addon");
}
addon.monthlyPrice = undefined;
addon.annualPrice = undefined;
}
extractedPricing.addOns = pricingAddons;
}
}
function _computeAnnualBillingCoef(monthlyPrice, annualPrice) {
if (typeof monthlyPrice !== "number" || typeof annualPrice !== "number") {
if (typeof monthlyPrice === "string" && typeof annualPrice === "string") {
return NaN;
}
else {
throw new Error("Monthly and annual prices of plans must be numbers");
}
}
var annualBillingCoef = 0;
if (annualPrice > monthlyPrice) {
console.log("[WARNING UPDATER_v11_v20] Annual price is greater than monthly price. Annual billing coefficient will be calculated considering annualPrice correspond to the annual payment, it is: annualBillingCoef = annualPrice / (monthlyPrice * 12)");
annualBillingCoef = annualPrice / (monthlyPrice * 12);
}
else {
annualBillingCoef = annualPrice / monthlyPrice;
}
return annualBillingCoef;
}