@crowdin/app-project-module 0.15.1 → 0.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,10 +11,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const storage_1 = require("../storage");
13
13
  const util_1 = require("../util");
14
+ const connection_1 = require("../util/connection");
14
15
  function handle(config) {
15
16
  return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
16
17
  (0, util_1.log)('Recieved integration logout request', config.logger);
17
18
  yield (0, storage_1.deleteIntegrationCredentials)(req.crowdinContext.clientId);
19
+ (0, connection_1.clearCache)(req.crowdinContext.crowdinId);
18
20
  res.status(204).end();
19
21
  }), config.onError);
20
22
  }
@@ -5,6 +5,7 @@ export declare function prepareCrowdinClient(config: Config, credentials: Crowdi
5
5
  token: string;
6
6
  }>;
7
7
  export declare function prepareIntegrationCredentials(config: Config, integration: IntegrationLogic, integrationCredentials: IntegrationCredentials): Promise<any>;
8
+ export declare function clearCache(organization: string): void;
8
9
  export declare function checkSubscription(config: Config, token: string, organization: string, accountType: AccountType): Promise<{
9
10
  expired: boolean;
10
11
  subscribeLink?: string;
@@ -31,7 +31,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.checkSubscription = exports.prepareIntegrationCredentials = exports.prepareCrowdinClient = void 0;
34
+ exports.checkSubscription = exports.clearCache = exports.prepareIntegrationCredentials = exports.prepareCrowdinClient = void 0;
35
35
  const crowdin_api_client_1 = __importDefault(require("@crowdin/crowdin-api-client"));
36
36
  const crowdinAppFunctions = __importStar(require("@crowdin/crowdin-apps-functions"));
37
37
  const axios_1 = __importDefault(require("axios"));
@@ -112,18 +112,27 @@ function prepareIntegrationCredentials(config, integration, integrationCredentia
112
112
  }
113
113
  exports.prepareIntegrationCredentials = prepareIntegrationCredentials;
114
114
  const subscriptionCache = {};
115
- function addToCache(organization, validUntil, cachingSeconds, subscribeLink) {
115
+ function addToCache(organization, appIdenfifier, validUntil, cachingSeconds, subscribeLink) {
116
116
  if (!cachingSeconds) {
117
117
  return;
118
118
  }
119
+ const orgCache = subscriptionCache[organization] || {};
119
120
  const now = new Date();
120
121
  now.setSeconds(now.getSeconds() + cachingSeconds);
121
- subscriptionCache[organization] = {
122
+ orgCache[appIdenfifier] = {
122
123
  cacheValidUntil: now,
123
124
  validUntil,
124
125
  subscribeLink,
125
126
  };
127
+ subscriptionCache[organization] = orgCache;
126
128
  }
129
+ function getFromCache(organization, appIdenfifier) {
130
+ return (subscriptionCache[organization] || {})[appIdenfifier];
131
+ }
132
+ function clearCache(organization) {
133
+ delete subscriptionCache[organization];
134
+ }
135
+ exports.clearCache = clearCache;
127
136
  function checkSubscription(config, token, organization, accountType) {
128
137
  return __awaiter(this, void 0, void 0, function* () {
129
138
  if (!config.pricing || config.pricing.planType === 'free') {
@@ -139,8 +148,10 @@ function checkSubscription(config, token, organization, accountType) {
139
148
  days = config.pricing.trialCrowdin || config.pricing.trial || defaultSubscriptionPlan;
140
149
  }
141
150
  (0, _1.log)(`Checking subscription plan. Subscriptino plan ${days} days`, config.logger);
142
- if (subscriptionCache[organization]) {
143
- const { cacheValidUntil, validUntil, subscribeLink } = subscriptionCache[organization];
151
+ const appIdentifier = config.identifier;
152
+ const cacheEntry = getFromCache(organization, appIdentifier);
153
+ if (cacheEntry) {
154
+ const { cacheValidUntil, validUntil, subscribeLink } = cacheEntry;
144
155
  if (cacheValidUntil.getTime() > Date.now()) {
145
156
  (0, _1.log)(`Loaded data from cache. Subscription is vali until ${validUntil.toISOString()}`, config.logger);
146
157
  const expired = new Date(validUntil).getTime() < Date.now();
@@ -149,7 +160,6 @@ function checkSubscription(config, token, organization, accountType) {
149
160
  }
150
161
  }
151
162
  try {
152
- const appIdentifier = config.identifier;
153
163
  const subscription = yield crowdinAppFunctions.getSubscription({
154
164
  appIdentifier,
155
165
  organization: accountType === models_1.AccountType.ENTERPRISE ? organization : undefined,
@@ -158,7 +168,7 @@ function checkSubscription(config, token, organization, accountType) {
158
168
  (0, _1.log)(`Recieved subscription info. ${JSON.stringify(subscription)}`, config.logger);
159
169
  const expired = new Date(subscription.expires).getTime() < Date.now();
160
170
  (0, _1.log)(`expired ${expired}`, config.logger);
161
- addToCache(organization, new Date(subscription.expires), config.pricing.cachingSeconds);
171
+ addToCache(organization, appIdentifier, new Date(subscription.expires), config.pricing.cachingSeconds);
162
172
  return { expired };
163
173
  }
164
174
  catch (e) {
@@ -169,7 +179,7 @@ function checkSubscription(config, token, organization, accountType) {
169
179
  date.setDate(date.getDate() + days);
170
180
  const expired = date.getTime() < Date.now();
171
181
  (0, _1.log)(`expired ${expired}`, config.logger);
172
- addToCache(organization, new Date(date), config.pricing.cachingSeconds, subscribeLink);
182
+ addToCache(organization, appIdentifier, new Date(date), config.pricing.cachingSeconds, subscribeLink);
173
183
  return { expired, subscribeLink };
174
184
  }
175
185
  if (config.onError) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "0.15.1",
3
+ "version": "0.15.2",
4
4
  "description": "Module that generates for you all common endpoints for serving standalone Crowdin App",
5
5
  "main": "out/index.js",
6
6
  "types": "out/index.d.ts",