@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
112 lines (111 loc) • 5.85 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearCache = exports.checkSubscription = exports.isAppFree = void 0;
const crowdin_apps_functions_1 = require("@crowdin/crowdin-apps-functions");
const types_1 = require("../types");
const logger_1 = require("./logger");
const subscriptionCache = {};
function isAppFree(config) {
return !config.pricing || config.pricing.planType === 'free';
}
exports.isAppFree = isAppFree;
function checkSubscription({ config, token, organization, accountType, }) {
var _a, _b, _c, _d, _e, _f, _g;
return __awaiter(this, void 0, void 0, function* () {
if (isAppFree(config)) {
return { expired: false };
}
(0, logger_1.log)('Checking subscription plan');
const appIdentifier = config.identifier;
const cacheEntry = getFromCache(organization, appIdentifier);
if (cacheEntry) {
const { cacheValidUntil, validUntil, subscribeLink, type } = cacheEntry;
if (cacheValidUntil.getTime() > Date.now()) {
(0, logger_1.log)(`Loaded data from cache. Subscription is valid until ${validUntil.toISOString()}`);
const { expired, daysLeft } = validateSubscription(new Date(validUntil));
(0, logger_1.log)(`expired ${expired}`);
return { expired, subscribeLink, daysLeft, type };
}
}
try {
const subscription = yield (0, crowdin_apps_functions_1.getSubscription)({
appIdentifier,
organization: accountType === types_1.AccountType.ENTERPRISE ? organization : undefined,
token,
baseUrl: (_a = config.crowdinUrls) === null || _a === void 0 ? void 0 : _a.subscriptionUrl,
});
(0, logger_1.log)(`Recieved subscription info. ${JSON.stringify(subscription)}`);
const { expired, daysLeft } = validateSubscription(new Date(subscription.expires));
(0, logger_1.log)(`expired ${expired}`);
addToCache(organization, appIdentifier, new Date(subscription.expires), types_1.SubscriptionInfoType.SUBSCRIPTION, (_b = config.pricing) === null || _b === void 0 ? void 0 : _b.cachingSeconds);
return { expired, daysLeft, type: types_1.SubscriptionInfoType.SUBSCRIPTION };
}
catch (e) {
if (e instanceof crowdin_apps_functions_1.PaymentRequiredError) {
const { initializedAt, subscribeLink } = e;
(0, logger_1.log)(`Recieved 402 payment error. initializedAt ${initializedAt}`);
//default 2 weeks
const defaultSubscriptionPlan = 14;
let days;
if (accountType === types_1.AccountType.ENTERPRISE) {
days = ((_c = config.pricing) === null || _c === void 0 ? void 0 : _c.trialEnterprise) || ((_d = config.pricing) === null || _d === void 0 ? void 0 : _d.trial) || defaultSubscriptionPlan;
}
else {
days = ((_e = config.pricing) === null || _e === void 0 ? void 0 : _e.trialCrowdin) || ((_f = config.pricing) === null || _f === void 0 ? void 0 : _f.trial) || defaultSubscriptionPlan;
}
(0, logger_1.log)(`Subscriptino trial plan ${days} days`);
const date = new Date(initializedAt);
date.setDate(date.getDate() + days);
const { expired, daysLeft } = validateSubscription(date);
(0, logger_1.log)(`expired ${expired}`);
addToCache(organization, appIdentifier, new Date(date), types_1.SubscriptionInfoType.TRIAL, (_g = config.pricing) === null || _g === void 0 ? void 0 : _g.cachingSeconds, subscribeLink);
return { expired, subscribeLink, daysLeft, type: types_1.SubscriptionInfoType.TRIAL };
}
if (config.onError) {
config.onError(e);
}
else {
console.error(e);
}
(0, logger_1.log)('Recieved http error from subscription request. Returning expired=false');
return { expired: false };
}
});
}
exports.checkSubscription = checkSubscription;
function clearCache(organization) {
delete subscriptionCache[organization];
}
exports.clearCache = clearCache;
function addToCache(organization, appIdentifier, validUntil, type, cachingSeconds, subscribeLink) {
if (!cachingSeconds) {
return;
}
const orgCache = subscriptionCache[organization] || {};
const now = new Date();
now.setSeconds(now.getSeconds() + cachingSeconds);
orgCache[appIdentifier] = {
cacheValidUntil: now,
validUntil,
subscribeLink,
type,
};
subscriptionCache[organization] = orgCache;
}
function getFromCache(organization, appIdentifier) {
return (subscriptionCache[organization] || {})[appIdentifier];
}
function validateSubscription(date) {
const expired = date.getTime() < Date.now();
const daysLeft = Math.round((date.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
return { expired, daysLeft };
}