@crowdin/app-project-module 0.35.1 → 0.36.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.
@@ -35,20 +35,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.listenQueueMessage = exports.updateCrowdinFromWebhookRequest = exports.prepareWebhookData = exports.filterSyncFiles = exports.unregisterAllCrowdinWebhooks = exports.unregisterCrowdinWebhooks = exports.updateCrowdinWebhooks = exports.registerCrowdinWebhook = exports.getAllCrowdinProjectWebhooks = exports.getCrowdinProjectWebhook = exports.unregisterWebhooks = exports.registerWebhooks = exports.makeCrowdinWebhookUrl = exports.decodedUrlParam = exports.encodedUrlParam = void 0;
38
+ exports.listenQueueMessage = exports.updateCrowdinFromWebhookRequest = exports.prepareWebhookData = exports.unregisterAllCrowdinWebhooks = exports.unregisterCrowdinWebhooks = exports.updateCrowdinWebhooks = exports.registerCrowdinWebhook = exports.getAllCrowdinProjectWebhooks = exports.getCrowdinProjectWebhook = exports.unregisterWebhooks = exports.registerWebhooks = exports.makeCrowdinWebhookUrl = exports.decodedUrlParam = exports.encodedUrlParam = exports.HookEvents = void 0;
39
39
  const crowdinAppFunctions = __importStar(require("@crowdin/crowdin-apps-functions"));
40
40
  const amqplib_1 = __importDefault(require("amqplib"));
41
41
  const models_1 = require("../models");
42
42
  const storage_1 = require("../storage");
43
43
  const connection_1 = require("./connection");
44
44
  const defaults_1 = require("./defaults");
45
- const file_snapshot_1 = require("./file-snapshot");
46
45
  const index_1 = require("./index");
47
46
  const logger_1 = require("./logger");
48
- const HookEvents = {
49
- ALL: ['file.translated', 'file.approved'],
50
- TRANSLATED: ['file.translated'],
51
- APPROVED: ['file.approved'],
47
+ exports.HookEvents = {
48
+ fileAdded: 'file.added',
49
+ fileDeleted: 'file.deleted',
50
+ fileApproved: 'file.approved',
51
+ fileTranslated: 'file.translated',
52
+ translationUpdated: 'translation.updated',
53
+ };
54
+ const HookConditionEvents = {
55
+ ALL: [exports.HookEvents.translationUpdated, exports.HookEvents.fileDeleted],
56
+ TRANSLATED: [exports.HookEvents.fileTranslated, exports.HookEvents.fileDeleted],
57
+ APPROVED: [exports.HookEvents.fileApproved, exports.HookEvents.fileDeleted],
52
58
  };
53
59
  function encodedUrlParam(config, integration, crowdinContext) {
54
60
  var _a;
@@ -75,7 +81,7 @@ exports.makeCrowdinWebhookUrl = makeCrowdinWebhookUrl;
75
81
  function registerWebhooks(config, integration, client, crowdinContext, apiCredentials, appSettings) {
76
82
  var _a, _b;
77
83
  return __awaiter(this, void 0, void 0, function* () {
78
- const isWebhookSync = +appSettings.syncType === models_1.SyncType.WEBHOOKS;
84
+ const isWebhookSync = Number(appSettings.schedule) !== models_1.SyncSchedule.DISABLED;
79
85
  const projectId = crowdinContext.jwtPayload.context.project_id;
80
86
  const urlParam = encodedUrlParam(config, integration, crowdinContext);
81
87
  if ((_a = integration.webhooks) === null || _a === void 0 ? void 0 : _a.crowdinWebhooks) {
@@ -84,9 +90,12 @@ function registerWebhooks(config, integration, client, crowdinContext, apiCreden
84
90
  else {
85
91
  const webhookName = `${config.name} application hook ${crowdinContext.jwtPayload.sub}`;
86
92
  const webhookUrl = makeCrowdinWebhookUrl(config, integration, crowdinContext);
87
- const syncCondition = models_1.SyncCondition[+appSettings.condition];
88
- const events = HookEvents[syncCondition];
93
+ const syncCondition = models_1.SyncCondition[Number(appSettings.condition)];
94
+ const events = HookConditionEvents[syncCondition];
89
95
  const webhook = yield getCrowdinProjectWebhook(config, client, projectId, webhookName);
96
+ if (appSettings['new-crowdin-files']) {
97
+ events.push(exports.HookEvents.fileAdded);
98
+ }
90
99
  if (isWebhookSync && webhook) {
91
100
  yield updateCrowdinWebhooks(config, client, projectId, webhook, events, webhookUrl);
92
101
  }
@@ -210,30 +219,11 @@ function unregisterAllCrowdinWebhooks(config, integration, crowdinId) {
210
219
  });
211
220
  }
212
221
  exports.unregisterAllCrowdinWebhooks = unregisterAllCrowdinWebhooks;
213
- function filterSyncFiles(events, syncFileSettings) {
214
- const files = {};
215
- events.forEach((event) => {
216
- const fileId = event.fileId;
217
- const language = event.language;
218
- if (syncFileSettings[fileId] && syncFileSettings[fileId].includes(language)) {
219
- if (files[fileId]) {
220
- files[fileId] = [...files[fileId], event.language];
221
- }
222
- else {
223
- files[fileId] = [event.language];
224
- }
225
- }
226
- });
227
- return files;
228
- }
229
- exports.filterSyncFiles = filterSyncFiles;
230
222
  function prepareWebhookData(config, integration, webhookUrlParam, provider) {
231
- var _a;
232
223
  return __awaiter(this, void 0, void 0, function* () {
233
224
  let rootFolder = undefined;
234
- let appSettings = null;
235
225
  let syncSettings = null;
236
- let newFiles = [];
226
+ let appSettings = {};
237
227
  const { projectId, crowdinId, clientId } = decodedUrlParam(config, webhookUrlParam);
238
228
  const crowdinCredentials = yield (0, storage_1.getStorage)().getCrowdinCredentials(crowdinId);
239
229
  const crowdinClient = yield (0, connection_1.prepareCrowdinClient)({ config, credentials: crowdinCredentials });
@@ -241,23 +231,10 @@ function prepareWebhookData(config, integration, webhookUrlParam, provider) {
241
231
  const preparedIntegrationCredentials = yield (0, connection_1.prepareIntegrationCredentials)(config, integration, integrationCredentials);
242
232
  if (integrationCredentials === null || integrationCredentials === void 0 ? void 0 : integrationCredentials.config) {
243
233
  appSettings = JSON.parse(integrationCredentials.config);
244
- const isWebhookSync = +appSettings.syncType === models_1.SyncType.WEBHOOKS;
234
+ const isWebhookSync = Number(appSettings.schedule) !== models_1.SyncSchedule.DISABLED;
245
235
  if (isWebhookSync) {
246
236
  syncSettings = (yield (0, storage_1.getStorage)().getSyncSettings(clientId, crowdinId, 'schedule', provider));
247
237
  rootFolder = yield (0, defaults_1.getRootFolder)(config, integration, crowdinClient.client, projectId);
248
- if (((_a = integration.syncNewElements) === null || _a === void 0 ? void 0 : _a[provider]) && appSettings[`new-${provider}-files`]) {
249
- newFiles = yield (0, file_snapshot_1.getAllNewFiles)({
250
- config,
251
- integration,
252
- crowdinApiClient: crowdinClient.client,
253
- crowdinId,
254
- integrationCredentials: preparedIntegrationCredentials,
255
- integrationId: clientId,
256
- projectId,
257
- integrationSettings: appSettings,
258
- syncSettings,
259
- });
260
- }
261
238
  }
262
239
  }
263
240
  return {
@@ -267,7 +244,6 @@ function prepareWebhookData(config, integration, webhookUrlParam, provider) {
267
244
  rootFolder,
268
245
  appSettings,
269
246
  syncSettings,
270
- newFiles,
271
247
  };
272
248
  });
273
249
  }
@@ -277,24 +253,29 @@ function updateCrowdinFromWebhookRequest(args) {
277
253
  return __awaiter(this, void 0, void 0, function* () {
278
254
  const { integration, webhookData, req } = args;
279
255
  let filesToSync = [];
280
- const { projectId, crowdinClient, preparedIntegrationCredentials, rootFolder, appSettings, syncSettings, newFiles, } = webhookData;
256
+ const { projectId, crowdinClient, preparedIntegrationCredentials, rootFolder, appSettings, syncSettings } = webhookData;
281
257
  const syncFiles = (syncSettings === null || syncSettings === void 0 ? void 0 : syncSettings.files) ? JSON.parse(syncSettings.files) : [];
282
258
  if ((_a = integration.webhooks) === null || _a === void 0 ? void 0 : _a.integrationWebhookInterceptor) {
283
259
  filesToSync = yield ((_b = integration.webhooks) === null || _b === void 0 ? void 0 : _b.integrationWebhookInterceptor(projectId, crowdinClient.client, preparedIntegrationCredentials, rootFolder, appSettings, syncSettings, req));
284
260
  }
285
- const allIntFiles = [...filesToSync, ...newFiles].map((file) => (Object.assign({ id: file.id, name: file.name, parentId: file.parent_id || file.parentId,
286
- // eslint-disable-next-line @typescript-eslint/camelcase
287
- parent_id: file.parent_id || file.parentId, nodeType: file.nodeType || file.node_type || '1',
288
- // eslint-disable-next-line @typescript-eslint/camelcase
289
- node_type: file.nodeType || file.node_type || '1' }, (file.type ? { type: file.type } : {}))));
290
- const intFiles = allIntFiles.filter((file) => file.nodeType === '1');
291
- const newSyncSettingsFiels = [...syncFiles, ...newFiles].map((file) => (Object.assign(Object.assign({}, file), { schedule: true, sync: false })));
292
- if (Object.keys(newFiles).length) {
293
- yield (0, storage_1.getStorage)().updateSyncSettings(JSON.stringify(newSyncSettingsFiels), syncSettings.integrationId, syncSettings.crowdinId, 'schedule', syncSettings.provider);
294
- const currentFileSnapshot = yield (0, file_snapshot_1.getIntegrationSnapshot)(integration, preparedIntegrationCredentials, appSettings);
295
- yield (0, storage_1.getStorage)().updateFilesSnapshot(JSON.stringify(currentFileSnapshot), syncSettings.integrationId, syncSettings.crowdinId, syncSettings.provider);
261
+ for (const file of filesToSync) {
262
+ const fileNodeType = file.nodeType || file.node_type || '1';
263
+ if (fileNodeType !== '1') {
264
+ continue;
265
+ }
266
+ const initFile = Object.assign({ id: file.id, name: file.name, sync: false, schedule: true,
267
+ // eslint-disable-next-line @typescript-eslint/camelcase
268
+ parent_id: file.parent_id || file.parentId,
269
+ // eslint-disable-next-line @typescript-eslint/camelcase
270
+ node_type: file.nodeType || file.node_type || '1' }, (file.type ? { type: file.type } : {}));
271
+ if (!syncFiles.find((obj) => obj.id === initFile.id)) {
272
+ yield (0, storage_1.getStorage)().updateSyncSettings(JSON.stringify([...syncFiles, initFile]), syncSettings.integrationId, syncSettings.crowdinId, 'schedule', syncSettings.provider);
273
+ }
274
+ const webhook = yield (0, storage_1.getStorage)().getWebhooks(initFile.id, syncSettings.integrationId, syncSettings.crowdinId, syncSettings.provider);
275
+ if (!webhook) {
276
+ yield (0, storage_1.getStorage)().saveWebhooks(initFile.id, syncSettings.integrationId, syncSettings.crowdinId, syncSettings.provider);
277
+ }
296
278
  }
297
- return yield integration.updateCrowdin(projectId, crowdinClient.client, preparedIntegrationCredentials, intFiles, rootFolder, appSettings);
298
279
  });
299
280
  }
300
281
  exports.updateCrowdinFromWebhookRequest = updateCrowdinFromWebhookRequest;
@@ -334,7 +334,8 @@
334
334
  .then(checkResponse)
335
335
  .then((res) => {
336
336
  project = res;
337
- appComponent.setCrowdinLanguagesData(project.targetLanguages)
337
+ const languagesSorted = project.targetLanguages.sort((a, b) => a.name.localeCompare(b.name));
338
+ appComponent.setCrowdinLanguagesData(languagesSorted)
338
339
  })
339
340
  {{#or withCronSync webhooks}}
340
341
  .then(() => getSyncSettings('crowdin'))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "0.35.1",
3
+ "version": "0.36.0",
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",
@@ -21,7 +21,7 @@
21
21
  "@godaddy/terminus": "^4.12.1",
22
22
  "@types/pg": "^8.10.3",
23
23
  "amqplib": "^0.10.3",
24
- "crypto-js": "^4.1.1",
24
+ "crypto-js": "^4.2.0",
25
25
  "express": "4.18.2",
26
26
  "express-handlebars": "^5.3.5",
27
27
  "mysql2": "^2.3.3",
@@ -36,24 +36,24 @@
36
36
  "@babel/preset-react": "^7.22.15",
37
37
  "@emotion/react": "^11.11.1",
38
38
  "@emotion/styled": "^11.11.0",
39
- "@mui/icons-material": "^5.14.12",
40
- "@mui/material": "^5.14.12",
41
- "@rjsf/core": "^5.13.0",
42
- "@rjsf/mui": "^5.13.0",
43
- "@rjsf/utils": "^5.13.0",
44
- "@rjsf/validator-ajv8": "^5.13.0",
45
- "@rollup/plugin-babel": "^6.0.3",
39
+ "@mui/icons-material": "^5.14.16",
40
+ "@mui/material": "^5.14.16",
41
+ "@rjsf/core": "^5.13.3",
42
+ "@rjsf/mui": "^5.13.3",
43
+ "@rjsf/utils": "^5.13.3",
44
+ "@rjsf/validator-ajv8": "^5.13.3",
45
+ "@rollup/plugin-babel": "^6.0.4",
46
46
  "@rollup/plugin-commonjs": "^24.0.1",
47
47
  "@rollup/plugin-json": "^6.0.0",
48
48
  "@rollup/plugin-node-resolve": "^15.2.1",
49
49
  "@rollup/plugin-replace": "^5.0.2",
50
50
  "@rollup/plugin-terser": "^0.4.3",
51
51
  "@types/amqplib": "^0.10.2",
52
- "@types/crypto-js": "^4.1.2",
52
+ "@types/crypto-js": "^4.1.3",
53
53
  "@types/express": "4.17.18",
54
54
  "@types/express-handlebars": "^5.3.1",
55
55
  "@types/jest": "^29.5.5",
56
- "@types/node": "^16.18.57",
56
+ "@types/node": "^16.18.60",
57
57
  "@types/node-cron": "^3.0.9",
58
58
  "@types/swagger-jsdoc": "^6.0.1",
59
59
  "@typescript-eslint/eslint-plugin": "^2.3.1",