@crowdin/app-project-module 1.0.2 → 1.2.0

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.
package/out/index.js CHANGED
@@ -142,8 +142,8 @@ function addCrowdinEndpoints(app, clientConfig) {
142
142
  if (!config.disableGlobalErrorHandling) {
143
143
  handleUncaughtErrors();
144
144
  }
145
- storage.initialize(config);
146
145
  logger.initialize(config);
146
+ storage.initialize(config);
147
147
  cron.initialize(config);
148
148
  // Middleware to ensure D1 migration before handling requests
149
149
  app.use((req, res, next) => __awaiter(this, void 0, void 0, function* () {
@@ -313,7 +313,10 @@ function addFormSchema({ app, config }) {
313
313
  optional: false,
314
314
  checkSubscriptionExpiration: true,
315
315
  moduleKey: moduleConfigWithForm.key,
316
- }), (0, credentials_masker_1.postRequestCredentialsMasker)(moduleConfigWithForm), (0, form_data_save_1.default)(config));
316
+ }), (0, credentials_masker_1.postRequestCredentialsMasker)({
317
+ secret: config.clientSecret,
318
+ moduleConfig: moduleConfigWithForm,
319
+ }), (0, form_data_save_1.default)(config));
317
320
  }
318
321
  }
319
322
  }
@@ -24,7 +24,10 @@ function autoCredentialsMaskerMiddleware(config) {
24
24
  }
25
25
  if (requestMethod === 'post' && moduleConfigWithForm.formPostDataUrl) {
26
26
  if (requestPath === moduleConfigWithForm.formPostDataUrl) {
27
- return (0, credentials_masker_1.postRequestCredentialsMasker)(moduleConfigWithForm)(req, res, next);
27
+ return (0, credentials_masker_1.postRequestCredentialsMasker)({
28
+ secret: config.clientSecret,
29
+ moduleConfig: moduleConfigWithForm,
30
+ })(req, res, next);
28
31
  }
29
32
  }
30
33
  }
@@ -28,6 +28,8 @@ function handle(integration) {
28
28
  const parsedPaginationData = paginationData ? JSON.parse(paginationData) : undefined;
29
29
  const result = yield integration.getIntegrationFiles({
30
30
  credentials: req.integrationCredentials,
31
+ client: req.crowdinApiClient,
32
+ projectId: req.crowdinContext.jwtPayload.context.project_id,
31
33
  settings,
32
34
  parentId,
33
35
  search,
@@ -18,7 +18,7 @@ const types_1 = require("../util/types");
18
18
  const types_2 = require("../types");
19
19
  function handle(config, integration) {
20
20
  return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
21
- const { files, provider, expandIntegrationFolders, hasLoadMoreElements } = req.body;
21
+ const { files, provider, expandIntegrationFolders, hasLoadMoreElements, folderIds } = req.body;
22
22
  if (req.isApiCall) {
23
23
  if (!files || !provider) {
24
24
  return res.status(400).json({
@@ -77,9 +77,12 @@ function handle(config, integration) {
77
77
  }
78
78
  const settings = req.integrationSettings || {};
79
79
  const syncFiles = (0, files_1.prepareSyncFiles)(files);
80
+ const hasSyncedFolders = provider === types_2.Provider.INTEGRATION
81
+ ? (0, files_1.hasFolders)(syncFiles)
82
+ : Array.isArray(folderIds) && folderIds.length > 0;
80
83
  const needsSnapshot = !integration.webhooks &&
81
84
  ((_a = integration.syncNewElements) === null || _a === void 0 ? void 0 : _a[provider]) &&
82
- (settings[`new-${provider}-files`] || (0, files_1.hasFolders)(syncFiles));
85
+ (settings[`new-${provider}-files`] || hasSyncedFolders);
83
86
  if (needsSnapshot) {
84
87
  yield (0, snapshot_1.createOrUpdateFileSnapshot)(config, integration, req, provider);
85
88
  }
@@ -62,6 +62,8 @@ export interface IntegrationLogic extends ModuleKey {
62
62
  */
63
63
  getIntegrationFiles: (options: {
64
64
  credentials: any;
65
+ client?: Crowdin;
66
+ projectId?: number;
65
67
  settings?: any;
66
68
  parentId?: any;
67
69
  search?: any;
@@ -162,7 +162,7 @@ function filesCron({ config, integration, period, }) {
162
162
  }
163
163
  exports.filesCron = filesCron;
164
164
  function processSyncSettings({ config, integration, period, syncSettings, }) {
165
- var _a, _b, _c;
165
+ var _a, _b, _c, _d;
166
166
  return __awaiter(this, void 0, void 0, function* () {
167
167
  let projectData;
168
168
  let crowdinClient;
@@ -234,13 +234,30 @@ function processSyncSettings({ config, integration, period, syncSettings, }) {
234
234
  const rootFolder = yield (0, defaults_1.getRootFolder)(config, integration, crowdinClient, projectId);
235
235
  const credentials = yield (0, connection_1.prepareIntegrationCredentials)(config, integration, integrationCredentials);
236
236
  let currentFileSnapshot = [];
237
- const needsSnapshotForNewFiles = !integration.webhooks &&
237
+ let needsSnapshotForNewFiles = !integration.webhooks &&
238
238
  ((_a = integration.syncNewElements) === null || _a === void 0 ? void 0 : _a[syncSettings.provider]) &&
239
239
  (intConfig[`new-${syncSettings.provider}-files`] || (0, files_1.hasFolders)(files));
240
+ if (!needsSnapshotForNewFiles &&
241
+ !integration.webhooks &&
242
+ ((_b = integration.syncNewElements) === null || _b === void 0 ? void 0 : _b[syncSettings.provider]) &&
243
+ syncSettings.provider === types_1.Provider.CROWDIN) {
244
+ currentFileSnapshot = yield (0, snapshot_1.getCrowdinSnapshot)(config, integration, crowdinClient, projectData.id, intConfig);
245
+ const syncedFileIds = Object.keys(files);
246
+ const hasSyncedFolders = currentFileSnapshot.some((file) => !('type' in file) && syncedFileIds.includes(String(file.id)));
247
+ if (hasSyncedFolders) {
248
+ needsSnapshotForNewFiles = true;
249
+ }
250
+ }
240
251
  const needsIntegrationSnapshot = needsSnapshotForNewFiles && syncSettings.provider !== types_1.Provider.CROWDIN;
241
- const needsCrowdinSnapshot = needsSnapshotForNewFiles && syncSettings.provider === types_1.Provider.CROWDIN;
252
+ const needsCrowdinSnapshot = needsSnapshotForNewFiles && syncSettings.provider === types_1.Provider.CROWDIN && currentFileSnapshot.length === 0;
242
253
  if (needsIntegrationSnapshot) {
243
- currentFileSnapshot = yield (0, snapshot_1.getIntegrationSnapshot)(integration, credentials, intConfig);
254
+ currentFileSnapshot = yield (0, snapshot_1.getIntegrationSnapshot)({
255
+ integration,
256
+ integrationCredentials: credentials,
257
+ integrationSettings: intConfig,
258
+ client: crowdinClient,
259
+ projectId: projectData.id,
260
+ });
244
261
  }
245
262
  else if (needsCrowdinSnapshot) {
246
263
  currentFileSnapshot = yield (0, snapshot_1.getCrowdinSnapshot)(config, integration, crowdinClient, projectData.id, intConfig);
@@ -376,11 +393,13 @@ function processSyncSettings({ config, integration, period, syncSettings, }) {
376
393
  (0, logger_1.log)(`Executing updateCrowdin task for files cron job with period [${period}] for project ${projectId}. Files ${intFiles.length}`);
377
394
  const credentials = yield (0, connection_1.prepareIntegrationCredentials)(config, integration, integrationCredentials);
378
395
  if (integration.filterByPathIntegrationFiles) {
379
- const includePatterns = (_b = intConfig === null || intConfig === void 0 ? void 0 : intConfig.includeByFilePath) === null || _b === void 0 ? void 0 : _b.split('\n').filter(Boolean);
380
- const excludePatterns = (_c = intConfig === null || intConfig === void 0 ? void 0 : intConfig.excludeByFilePath) === null || _c === void 0 ? void 0 : _c.split('\n').filter(Boolean);
396
+ const includePatterns = (_c = intConfig === null || intConfig === void 0 ? void 0 : intConfig.includeByFilePath) === null || _c === void 0 ? void 0 : _c.split('\n').filter(Boolean);
397
+ const excludePatterns = (_d = intConfig === null || intConfig === void 0 ? void 0 : intConfig.excludeByFilePath) === null || _d === void 0 ? void 0 : _d.split('\n').filter(Boolean);
381
398
  try {
382
399
  const fullTreeResult = yield integration.getIntegrationFiles({
383
400
  credentials,
401
+ client: crowdinClient,
402
+ projectId: projectId,
384
403
  settings: intConfig,
385
404
  });
386
405
  const fullTree = (0, files_1.isExtendedResultType)(fullTreeResult) ? fullTreeResult.data || [] : fullTreeResult;
@@ -421,7 +440,13 @@ function processSyncSettings({ config, integration, period, syncSettings, }) {
421
440
  if (Object.keys(newFiles).length) {
422
441
  const newSyncSettingsFields = allIntFiles.map((file) => (Object.assign(Object.assign({}, file), { schedule: true, sync: false })));
423
442
  yield (0, storage_1.getStorage)().updateSyncSettings(JSON.stringify(newSyncSettingsFields), syncSettings.integrationId, syncSettings.crowdinId, 'schedule', syncSettings.provider);
424
- const currentFileSnapshot = yield (0, snapshot_1.getIntegrationSnapshot)(integration, credentials, intConfig);
443
+ const currentFileSnapshot = yield (0, snapshot_1.getIntegrationSnapshot)({
444
+ integration,
445
+ integrationCredentials: credentials,
446
+ integrationSettings: intConfig,
447
+ client: crowdinClient,
448
+ projectId,
449
+ });
425
450
  yield (0, storage_1.getStorage)().updateFilesSnapshot(JSON.stringify(currentFileSnapshot), syncSettings.integrationId, syncSettings.crowdinId, syncSettings.provider);
426
451
  }
427
452
  (0, logger_1.log)(`updateCrowdin task for files cron job with period [${period}] for project ${projectId} completed`);
@@ -54,6 +54,8 @@ function expandFilesTree(nodes, req, integration, job) {
54
54
  try {
55
55
  integrationData = yield integration.getIntegrationFiles({
56
56
  credentials: req.integrationCredentials,
57
+ client: req.crowdinApiClient,
58
+ projectId: req.crowdinContext.jwtPayload.context.project_id,
57
59
  settings: req.integrationSettings,
58
60
  parentId: id,
59
61
  });
@@ -164,6 +166,8 @@ function expandFilesTreeWithPagination(nodes, req, integration, job) {
164
166
  }
165
167
  const result = yield integration.getIntegrationFiles({
166
168
  credentials: req.integrationCredentials,
169
+ client: req.crowdinApiClient,
170
+ projectId: req.crowdinContext.jwtPayload.context.project_id,
167
171
  settings: req.integrationSettings,
168
172
  parentId: folder.id,
169
173
  search: '',
@@ -2,5 +2,11 @@ import Crowdin from '@crowdin/crowdin-api-client';
2
2
  import { Config } from '../../../types';
3
3
  import { IntegrationLogic, IntegrationRequest, Provider, TreeItem } from '../types';
4
4
  export declare function getCrowdinSnapshot(config: Config, integration: IntegrationLogic, crowdinApiClient: Crowdin, projectId: number, integrationSettings: any): Promise<TreeItem[]>;
5
- export declare function getIntegrationSnapshot(integration: IntegrationLogic, integrationCredentials: any, integrationSettings: any): Promise<TreeItem[]>;
5
+ export declare function getIntegrationSnapshot({ integration, integrationCredentials, integrationSettings, client, projectId, }: {
6
+ integration: IntegrationLogic;
7
+ integrationCredentials: any;
8
+ integrationSettings: any;
9
+ client?: Crowdin;
10
+ projectId?: number;
11
+ }): Promise<TreeItem[]>;
6
12
  export declare function createOrUpdateFileSnapshot(config: Config, integration: IntegrationLogic, req: IntegrationRequest, provider: Provider): Promise<void>;
@@ -40,12 +40,14 @@ function getTreeItems(integrationData) {
40
40
  }
41
41
  return files;
42
42
  }
43
- function getOneLevelFetchingFiles(integration, integrationCredentials, integrationSettings, parentFiles) {
43
+ function getOneLevelFetchingFiles(integration, integrationCredentials, integrationSettings, parentFiles, client, projectId) {
44
44
  return __awaiter(this, void 0, void 0, function* () {
45
45
  for (const file of parentFiles) {
46
46
  if (!file.type) {
47
47
  let childs = yield integration.getIntegrationFiles({
48
48
  credentials: integrationCredentials,
49
+ client,
50
+ projectId,
49
51
  settings: integrationSettings,
50
52
  parentId: file.id,
51
53
  search: '',
@@ -53,7 +55,7 @@ function getOneLevelFetchingFiles(integration, integrationCredentials, integrati
53
55
  });
54
56
  childs = getTreeItems(childs);
55
57
  if (childs.length > 0) {
56
- const childFiles = yield getOneLevelFetchingFiles(integration, integrationCredentials, integrationSettings, childs);
58
+ const childFiles = yield getOneLevelFetchingFiles(integration, integrationCredentials, integrationSettings, childs, client, projectId);
57
59
  parentFiles = [...parentFiles, ...childFiles];
58
60
  }
59
61
  }
@@ -61,20 +63,22 @@ function getOneLevelFetchingFiles(integration, integrationCredentials, integrati
61
63
  return parentFiles;
62
64
  });
63
65
  }
64
- function getIntegrationSnapshot(integration, integrationCredentials, integrationSettings) {
66
+ function getIntegrationSnapshot({ integration, integrationCredentials, integrationSettings, client, projectId, }) {
65
67
  var _a;
66
68
  return __awaiter(this, void 0, void 0, function* () {
67
69
  let files = [];
68
70
  let integrationData = [];
69
71
  integrationData = yield integration.getIntegrationFiles({
70
72
  credentials: integrationCredentials,
73
+ client,
74
+ projectId,
71
75
  settings: integrationSettings,
72
76
  search: '',
73
77
  page: 0,
74
78
  });
75
79
  files = getTreeItems(integrationData);
76
80
  if (integration.integrationOneLevelFetching) {
77
- files = yield getOneLevelFetchingFiles(integration, integrationCredentials, integrationSettings, files);
81
+ files = yield getOneLevelFetchingFiles(integration, integrationCredentials, integrationSettings, files, client, projectId);
78
82
  }
79
83
  if ((integrationSettings === null || integrationSettings === void 0 ? void 0 : integrationSettings.skipIntegrationNodesToggle) === true ||
80
84
  ((integrationSettings === null || integrationSettings === void 0 ? void 0 : integrationSettings.skipIntegrationNodesToggle) === null && ((_a = integration.skipIntegrationNodesToggle) === null || _a === void 0 ? void 0 : _a.value))) {
@@ -94,7 +98,13 @@ function createOrUpdateFileSnapshot(config, integration, req, provider) {
94
98
  files = yield getCrowdinSnapshot(config, integration, req.crowdinApiClient, req.crowdinContext.jwtPayload.context.project_id, req.integrationSettings);
95
99
  }
96
100
  if (provider === types_1.Provider.INTEGRATION) {
97
- files = yield getIntegrationSnapshot(integration, req.integrationCredentials, req.integrationSettings);
101
+ files = yield getIntegrationSnapshot({
102
+ integration,
103
+ integrationCredentials: req.integrationCredentials,
104
+ integrationSettings: req.integrationSettings,
105
+ client: req.crowdinApiClient,
106
+ projectId: req.crowdinContext.jwtPayload.context.project_id,
107
+ });
98
108
  }
99
109
  if (existingSnapshot) {
100
110
  req.logInfo(`Updating file snapshot for provider ${provider} ${JSON.stringify(files, null, 2)}`);