@crowdin/app-project-module 0.60.3 → 0.62.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.
@@ -106,6 +106,8 @@ const updateCrowdinTest = ({ appConfig, integrationTestConfig, }) => __awaiter(v
106
106
  },
107
107
  update: updateProgressMock,
108
108
  type: types_1.JobClientType.MANUAL,
109
+ fetchTranslation: jest.fn(),
110
+ translationUploaded: jest.fn(),
109
111
  },
110
112
  });
111
113
  }, 'Fail to run method updateCrowdin()');
@@ -63,6 +63,8 @@ const updateIntegrationTest = ({ appConfig, integrationTestConfig, }) => __await
63
63
  },
64
64
  update: updateProgressMock,
65
65
  type: types_1.JobClientType.MANUAL,
66
+ fetchTranslation: jest.fn(),
67
+ translationUploaded: jest.fn(),
66
68
  },
67
69
  });
68
70
  }, 'Fail to run method updateIntegration()');
@@ -40,6 +40,8 @@ function handle(config, integration) {
40
40
  title: 'Sync files to Crowdin',
41
41
  payload: req.body,
42
42
  res,
43
+ projectId: projectId,
44
+ client: req.crowdinApiClient,
43
45
  jobType: types_1.JobClientType.MANUAL,
44
46
  jobCallback: (job) => __awaiter(this, void 0, void 0, function* () {
45
47
  var _c;
@@ -34,6 +34,8 @@ function handle(config, integration) {
34
34
  title: 'Sync files to ' + config.name,
35
35
  payload: req.body,
36
36
  res,
37
+ projectId: req.crowdinContext.jwtPayload.context.project_id,
38
+ client: req.crowdinApiClient,
37
39
  jobType: types_1.JobClientType.MANUAL,
38
40
  jobCallback: (job) => __awaiter(this, void 0, void 0, function* () {
39
41
  const result = yield integration.updateIntegration({
@@ -33,6 +33,8 @@ function handle(config, integration) {
33
33
  title: 'Save sync settings',
34
34
  payload: req.body,
35
35
  res,
36
+ projectId: req.crowdinContext.jwtPayload.context.project_id,
37
+ client: req.crowdinApiClient,
36
38
  jobType: types_1.JobClientType.MANUAL,
37
39
  jobCallback: () => __awaiter(this, void 0, void 0, function* () {
38
40
  if (Array.isArray(expandIntegrationFolders) && expandIntegrationFolders.length) {
@@ -233,6 +233,8 @@ function filesCron({ config, integration, period, }) {
233
233
  title: `Sync files to ${config.name} [scheduled]`,
234
234
  payload: filesToProcess,
235
235
  jobType: types_2.JobClientType.CRON,
236
+ projectId: projectId,
237
+ client: crowdinClient,
236
238
  jobCallback: (job) => __awaiter(this, void 0, void 0, function* () {
237
239
  yield integration.updateIntegration({
238
240
  projectId,
@@ -283,6 +285,8 @@ function filesCron({ config, integration, period, }) {
283
285
  title: 'Sync files to Crowdin [scheduled]',
284
286
  payload: intFiles,
285
287
  jobType: types_2.JobClientType.CRON,
288
+ projectId: projectId,
289
+ client: crowdinClient,
286
290
  jobCallback: (job) => __awaiter(this, void 0, void 0, function* () {
287
291
  yield integration.updateCrowdin({
288
292
  projectId,
@@ -1,12 +1,15 @@
1
1
  import { JobClient, JobClientType, JobType } from './types';
2
2
  import { Response } from 'express';
3
- export declare function runAsJob({ integrationId, crowdinId, type, title, payload, res, jobType, jobCallback, onError, }: {
3
+ import Crowdin from '@crowdin/crowdin-api-client';
4
+ export declare function runAsJob({ integrationId, crowdinId, type, title, payload, res, projectId, client, jobType, jobCallback, onError, }: {
4
5
  integrationId: string;
5
6
  crowdinId: string;
6
7
  type: JobType;
7
8
  title?: string;
8
9
  payload?: any;
9
10
  res?: Response;
11
+ projectId: number;
12
+ client: Crowdin;
10
13
  jobType: JobClientType;
11
14
  jobCallback: (arg1: JobClient) => Promise<any>;
12
15
  onError?: (e: any) => Promise<void>;
@@ -19,7 +19,7 @@ const blockingJobs = {
19
19
  [types_1.JobType.CROWDIN_SYNC_SETTINGS_SAVE]: [types_1.JobType.CROWDIN_SYNC_SETTINGS_SAVE],
20
20
  [types_1.JobType.INTEGRATION_SYNC_SETTINGS_SAVE]: [types_1.JobType.INTEGRATION_SYNC_SETTINGS_SAVE],
21
21
  };
22
- function runAsJob({ integrationId, crowdinId, type, title, payload, res, jobType, jobCallback, onError, }) {
22
+ function runAsJob({ integrationId, crowdinId, type, title, payload, res, projectId, client, jobType, jobCallback, onError, }) {
23
23
  return __awaiter(this, void 0, void 0, function* () {
24
24
  const storage = (0, storage_1.getStorage)();
25
25
  const activeJobs = yield storage.getActiveJobs({ integrationId, crowdinId });
@@ -66,6 +66,44 @@ function runAsJob({ integrationId, crowdinId, type, title, payload, res, jobType
66
66
  });
67
67
  },
68
68
  type: jobType,
69
+ fetchTranslation: ({ fileId, languageId }) => __awaiter(this, void 0, void 0, function* () {
70
+ const translationCache = yield storage.getTranslationCache({
71
+ integrationId,
72
+ crowdinId,
73
+ fileId,
74
+ languageId,
75
+ });
76
+ if (!translationCache) {
77
+ yield storage.saveTranslationCache({ integrationId, crowdinId, fileId, languageId });
78
+ }
79
+ let translation = null;
80
+ try {
81
+ (0, logger_1.log)(`Receiving translation for file ${fileId} in language ${languageId}`);
82
+ translation = yield client.translationsApi.buildProjectFileTranslation(projectId, fileId, {
83
+ targetLanguageId: languageId,
84
+ }, (translationCache === null || translationCache === void 0 ? void 0 : translationCache.etag) ? translationCache === null || translationCache === void 0 ? void 0 : translationCache.etag : undefined);
85
+ return translation;
86
+ }
87
+ catch (e) {
88
+ (0, logger_1.log)(`Unable to get translation for file ${fileId} in language ${languageId}`);
89
+ }
90
+ return translation;
91
+ }),
92
+ translationUploaded: ({ fileId, languageId, etag }) => __awaiter(this, void 0, void 0, function* () {
93
+ const translationCache = yield storage.getTranslationCache({
94
+ integrationId,
95
+ crowdinId,
96
+ fileId,
97
+ languageId,
98
+ });
99
+ (0, logger_1.log)(`Saving etag translation for file ${fileId} in language ${languageId}`);
100
+ if (!translationCache) {
101
+ yield storage.saveTranslationCache({ integrationId, crowdinId, fileId, languageId, etag });
102
+ }
103
+ else {
104
+ yield storage.updateTranslationCache({ integrationId, crowdinId, fileId, languageId, etag });
105
+ }
106
+ }),
69
107
  };
70
108
  try {
71
109
  const data = yield jobCallback(job);
@@ -1,6 +1,8 @@
1
1
  import Crowdin, { ProjectsGroupsModel } from '@crowdin/crowdin-api-client';
2
2
  import { Config } from '../../../types';
3
3
  import { IntegrationLogic, IntegrationSyncSettings } from '../types';
4
+ import { ResponseObject } from '@crowdin/crowdin-api-client/out/core';
5
+ import { TranslationsModel } from '@crowdin/crowdin-api-client/out/translations';
4
6
  export declare enum JobType {
5
7
  UPDATE_TO_CROWDIN = "updateCrowdin",
6
8
  UPDATE_TO_INTEGRATION = "updateIntegration",
@@ -48,6 +50,8 @@ export type JobClient = {
48
50
  get: () => Promise<Job | undefined>;
49
51
  update: UpdateJobProgress;
50
52
  type: JobClientType;
53
+ translationUploaded: SaveUploadedFileTranslation;
54
+ fetchTranslation: FetchTranslation;
51
55
  };
52
56
  export type UpdateJobProgress = ({ progress, status, info, data, }: Omit<UpdateJobParams, 'id'>) => Promise<{
53
57
  isCanceled: boolean;
@@ -63,3 +67,21 @@ export interface GetAllNewFilesArgs {
63
67
  integrationSettings: any;
64
68
  syncSettings: IntegrationSyncSettings;
65
69
  }
70
+ export type SaveUploadedFileTranslation = ({ fileId, languageId, etag, }: {
71
+ fileId: number;
72
+ languageId: string;
73
+ etag: string;
74
+ }) => Promise<void>;
75
+ export type FetchTranslation = ({ fileId, languageId, }: {
76
+ fileId: number;
77
+ languageId: string;
78
+ }) => Promise<ResponseObject<TranslationsModel.BuildProjectFileTranslationResponse> | null>;
79
+ export interface TranslationCache {
80
+ integrationId: string;
81
+ crowdinId: string;
82
+ fileId: number;
83
+ languageId: string;
84
+ etag?: string;
85
+ }
86
+ export type GetTranslationCacheParams = Pick<TranslationCache, 'integrationId' | 'crowdinId' | 'fileId' | 'languageId'>;
87
+ export type UpdateTranslationCacheParams = Pick<TranslationCache, 'integrationId' | 'crowdinId' | 'fileId' | 'languageId' | 'etag'>;