UNPKG

@crowdin/app-project-module

Version:

Module that generates for you all common endpoints for serving standalone Crowdin App

111 lines (110 loc) 5.74 kB
"use strict"; 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.isAppFree = isAppFree; exports.checkSubscription = checkSubscription; exports.clearCache = clearCache; const types_1 = require("../types"); const crowdin_1 = require("./app-functions/crowdin"); const logger_1 = require("./logger"); const subscriptionCache = {}; function isAppFree(config) { return !config.pricing || config.pricing.planType === 'free'; } function checkSubscription(_a) { return __awaiter(this, arguments, void 0, function* ({ config, token, organization, accountType, }) { var _b, _c, _d, _e, _f, _g, _h; 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_1.getSubscription)({ appIdentifier, organization: accountType === types_1.AccountType.ENTERPRISE ? organization : undefined, token, baseUrl: (_b = config.crowdinUrls) === null || _b === void 0 ? void 0 : _b.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, (_c = config.pricing) === null || _c === void 0 ? void 0 : _c.cachingSeconds); return { expired, daysLeft, type: types_1.SubscriptionInfoType.SUBSCRIPTION }; } catch (e) { if (e instanceof types_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 = ((_d = config.pricing) === null || _d === void 0 ? void 0 : _d.trialEnterprise) || ((_e = config.pricing) === null || _e === void 0 ? void 0 : _e.trial) || defaultSubscriptionPlan; } else { days = ((_f = config.pricing) === null || _f === void 0 ? void 0 : _f.trialCrowdin) || ((_g = config.pricing) === null || _g === void 0 ? void 0 : _g.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, (_h = config.pricing) === null || _h === void 0 ? void 0 : _h.cachingSeconds, subscribeLink); return { expired, subscribeLink, daysLeft, type: types_1.SubscriptionInfoType.TRIAL }; } if (config.onError) { config.onError({ error: e }); } else { console.error(e); } (0, logger_1.log)('Recieved http error from subscription request. Returning expired=false'); return { expired: false }; } }); } function clearCache(organization) { delete subscriptionCache[organization]; } 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 }; }