@crowdin/app-project-module 0.74.0 → 0.76.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.
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  /* eslint-disable no-unused-expressions */
3
+ /* eslint-disable @typescript-eslint/camelcase */
3
4
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
5
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
6
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -9,19 +10,119 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
10
  step((generator = generator.apply(thisArg, _arguments || [])).next());
10
11
  });
11
12
  };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
12
16
  Object.defineProperty(exports, "__esModule", { value: true });
13
17
  exports.PostgreStorage = void 0;
14
18
  const pg_1 = require("pg");
15
19
  const uuid_1 = require("uuid");
16
- const types_1 = require("../modules/integration/util/types");
20
+ const types_1 = require("../types");
21
+ const types_2 = require("../modules/integration/util/types");
17
22
  const util_1 = require("../util");
23
+ const fs_1 = __importDefault(require("fs"));
24
+ const path_1 = require("path");
18
25
  class PostgreStorage {
19
- constructor(config) {
26
+ constructor(config, directoryPath) {
20
27
  this.dbPromise = new Promise((res, rej) => {
21
28
  this._res = res;
22
29
  this._rej = rej;
23
30
  });
31
+ this.tables = {
32
+ crowdin_credentials: `(
33
+ id varchar primary key,
34
+ app_secret varchar,
35
+ domain varchar,
36
+ user_id varchar,
37
+ agent_id varchar,
38
+ organization_id varchar,
39
+ base_url varchar,
40
+ access_token varchar not null,
41
+ refresh_token varchar not null,
42
+ expire varchar not null,
43
+ type varchar not null
44
+ )`,
45
+ integration_credentials: `(
46
+ id varchar primary key,
47
+ credentials varchar,
48
+ crowdin_id varchar not null,
49
+ managers varchar
50
+ )`,
51
+ sync_settings: `(
52
+ id serial primary key,
53
+ files varchar,
54
+ integration_id varchar not null,
55
+ crowdin_id varchar not null,
56
+ type varchar not null,
57
+ provider varchar not null
58
+ )`,
59
+ app_metadata: `(
60
+ id varchar primary key,
61
+ data varchar,
62
+ crowdin_id varchar
63
+ )`,
64
+ files_snapshot: `(
65
+ id serial primary key,
66
+ integration_id varchar not null,
67
+ crowdin_id varchar not null,
68
+ files varchar,
69
+ provider varchar not null
70
+ )`,
71
+ webhooks: `(
72
+ id serial primary key,
73
+ file_id varchar not null,
74
+ integration_id varchar not null,
75
+ crowdin_id varchar not null,
76
+ provider varchar not null
77
+ )`,
78
+ user_errors: `(
79
+ id serial primary key,
80
+ action varchar not null,
81
+ message varchar not null,
82
+ data varchar,
83
+ created_at varchar not null,
84
+ crowdin_id varchar not null,
85
+ integration_id varchar
86
+ )`,
87
+ integration_settings: `(
88
+ id serial primary key,
89
+ integration_id varchar not null,
90
+ crowdin_id varchar not null,
91
+ config varchar
92
+ )`,
93
+ job: `(
94
+ id varchar not null primary key,
95
+ integration_id varchar not null,
96
+ crowdin_id varchar not null,
97
+ type varchar not null,
98
+ title varchar null,
99
+ progress int default 0,
100
+ status varchar default '${types_2.JobStatus.CREATED}',
101
+ payload varchar null,
102
+ info varchar null,
103
+ data varchar null,
104
+ attempt int default 0,
105
+ created_at varchar not null,
106
+ updated_at varchar null,
107
+ finished_at varchar null
108
+ )`,
109
+ translation_file_cache: `(
110
+ id serial primary key,
111
+ integration_id varchar not null,
112
+ crowdin_id varchar not null,
113
+ file_id int not null,
114
+ language_id varchar not null,
115
+ etag varchar
116
+ )`,
117
+ unsynced_files: `(
118
+ id serial primary key,
119
+ integration_id varchar not null,
120
+ crowdin_id varchar not null,
121
+ files varchar
122
+ )`,
123
+ };
24
124
  this.config = config;
125
+ this.directoryPath = directoryPath;
25
126
  }
26
127
  executeQuery(command) {
27
128
  return __awaiter(this, void 0, void 0, function* () {
@@ -48,7 +149,10 @@ class PostgreStorage {
48
149
  migrate() {
49
150
  return __awaiter(this, void 0, void 0, function* () {
50
151
  try {
51
- yield this.executeQuery(this.addTables);
152
+ if (this.directoryPath && fs_1.default.existsSync(this.directoryPath)) {
153
+ yield this.migrateFromSqlite(this.directoryPath);
154
+ }
155
+ yield this.executeQuery((client) => this.addTables(client));
52
156
  this._res && this._res();
53
157
  // TODO: temporary code
54
158
  yield this.executeQuery((client) => this.alterTables(client));
@@ -64,6 +168,7 @@ class PostgreStorage {
64
168
  yield this.addColumns(client, ['crowdin_id'], 'app_metadata');
65
169
  yield this.addColumns(client, ['agent_id'], 'crowdin_credentials');
66
170
  yield this.addColumn(client, 'attempt', 'job', 'int default 0');
171
+ yield this.addColumn(client, 'managers', 'integration_credentials', 'varchar NULL');
67
172
  });
68
173
  }
69
174
  addColumns(client, newColumns, tableName) {
@@ -91,121 +196,9 @@ class PostgreStorage {
91
196
  }
92
197
  addTables(client) {
93
198
  return __awaiter(this, void 0, void 0, function* () {
94
- yield client.query(`
95
- create table if not exists crowdin_credentials
96
- (
97
- id varchar primary key,
98
- app_secret varchar,
99
- domain varchar,
100
- user_id varchar,
101
- agent_id varchar,
102
- organization_id varchar,
103
- base_url varchar,
104
- access_token varchar not null,
105
- refresh_token varchar not null,
106
- expire varchar not null,
107
- type varchar not null
108
- )
109
- `);
110
- yield client.query(`
111
- create table if not exists integration_credentials
112
- (
113
- id varchar primary key,
114
- credentials varchar,
115
- crowdin_id varchar not null,
116
- managers varchar
117
- )
118
- `);
119
- yield client.query(`
120
- create table if not exists sync_settings
121
- (
122
- id serial primary key,
123
- files varchar,
124
- integration_id varchar not null,
125
- crowdin_id varchar not null,
126
- type varchar not null,
127
- provider varchar not null
128
- )
129
- `);
130
- yield client.query(`
131
- create table if not exists app_metadata
132
- (
133
- id varchar primary key,
134
- data varchar,
135
- crowdin_id varchar
136
- )
137
- `);
138
- yield client.query(`
139
- create table if not exists files_snapshot
140
- (
141
- id serial primary key,
142
- integration_id varchar not null,
143
- crowdin_id varchar not null,
144
- files varchar,
145
- provider varchar not null
146
- )
147
- `);
148
- yield client.query(`
149
- create table if not exists webhooks
150
- (
151
- id serial primary key,
152
- file_id varchar not null,
153
- integration_id varchar not null,
154
- crowdin_id varchar not null,
155
- provider varchar not null
156
- )
157
- `);
158
- yield client.query(`
159
- create table if not exists user_errors
160
- (
161
- id serial primary key,
162
- action varchar not null,
163
- message varchar not null,
164
- data varchar,
165
- created_at varchar not null,
166
- crowdin_id varchar not null,
167
- integration_id varchar
168
- )
169
- `);
170
- yield client.query(`
171
- create table if not exists integration_settings
172
- (
173
- id serial primary key,
174
- integration_id varchar not null,
175
- crowdin_id varchar not null,
176
- config varchar
177
- )
178
- `);
179
- yield client.query(`
180
- create table if not exists job
181
- (
182
- id varchar not null primary key,
183
- integration_id varchar not null,
184
- crowdin_id varchar not null,
185
- type varchar not null,
186
- title varchar null,
187
- progress int default 0,
188
- status varchar default '${types_1.JobStatus.CREATED}',
189
- payload varchar null,
190
- info varchar null,
191
- data varchar null,
192
- attempt int default 0,
193
- created_at varchar not null,
194
- updated_at varchar null,
195
- finished_at varchar null
196
- )
197
- `);
198
- yield client.query(`
199
- create table if not exists translation_file_cache
200
- (
201
- id serial primary key,
202
- integration_id varchar not null,
203
- crowdin_id varchar not null,
204
- file_id int not null,
205
- language_id varchar not null,
206
- etag varchar
207
- )
208
- `);
199
+ for (const [tableName, tableSchema] of Object.entries(this.tables)) {
200
+ yield client.query(`create table if not exists ${tableName} ${tableSchema}`);
201
+ }
209
202
  });
210
203
  }
211
204
  saveCrowdinCredentials(credentials) {
@@ -275,6 +268,7 @@ class PostgreStorage {
275
268
  yield client.query('DELETE FROM integration_settings WHERE crowdin_id = $1', [id]);
276
269
  yield client.query('DELETE FROM job WHERE crowdin_id = $1', [id]);
277
270
  yield client.query('DELETE FROM translation_file_cache WHERE crowdin_id = $1', [id]);
271
+ yield client.query('DELETE FROM unsynced_files WHERE crowdin_id = $1', [id]);
278
272
  }));
279
273
  });
280
274
  }
@@ -327,6 +321,7 @@ class PostgreStorage {
327
321
  yield client.query('DELETE FROM files_snapshot where integration_id = $1', [id]);
328
322
  yield client.query('DELETE FROM webhooks where integration_id = $1', [id]);
329
323
  yield client.query('DELETE FROM job where integration_id = $1', [id]);
324
+ yield client.query('DELETE FROM unsynced_files where integration_id = $1', [id]);
330
325
  }));
331
326
  });
332
327
  }
@@ -340,6 +335,7 @@ class PostgreStorage {
340
335
  yield client.query('DELETE FROM webhooks where crowdin_id = $1', [crowdinId]);
341
336
  yield client.query('DELETE FROM user_errors where crowdin_id = $1', [crowdinId]);
342
337
  yield client.query('DELETE FROM job where crowdin_id = $1', [crowdinId]);
338
+ yield client.query('DELETE FROM unsynced_files where crowdin_id = $1', [crowdinId]);
343
339
  }));
344
340
  });
345
341
  }
@@ -588,7 +584,7 @@ class PostgreStorage {
588
584
  if (status) {
589
585
  updateFields.push('status = $' + updateParams.length.toString());
590
586
  updateParams.push(status);
591
- if ((!progress || progress <= 100) && [types_1.JobStatus.FAILED, types_1.JobStatus.CANCELED].includes(status)) {
587
+ if ((!progress || progress <= 100) && [types_2.JobStatus.FAILED, types_2.JobStatus.CANCELED].includes(status)) {
592
588
  updateFields.push('finished_at = $' + updateParams.length.toString());
593
589
  updateParams.push(Date.now().toString());
594
590
  }
@@ -657,7 +653,7 @@ class PostgreStorage {
657
653
  title, info, data, attempt, created_at as createdAt, updated_at as updatedAt, finished_at as finishedAt
658
654
  FROM job
659
655
  WHERE status IN ($1, $2) AND finished_at is NULL
660
- `, [types_1.JobStatus.IN_PROGRESS, types_1.JobStatus.CREATED]);
656
+ `, [types_2.JobStatus.IN_PROGRESS, types_2.JobStatus.CREATED]);
661
657
  return (res === null || res === void 0 ? void 0 : res.rows) || [];
662
658
  }));
663
659
  });
@@ -708,5 +704,60 @@ class PostgreStorage {
708
704
  `, [etag, integrationId, crowdinId, fileId, languageId]));
709
705
  });
710
706
  }
707
+ migrateFromSqlite(directoryPath) {
708
+ return __awaiter(this, void 0, void 0, function* () {
709
+ const [name, extension] = types_1.storageFiles.DUMP.split('%s');
710
+ const files = fs_1.default
711
+ .readdirSync(directoryPath)
712
+ .filter((file) => file.startsWith(name) && file.endsWith(extension))
713
+ .sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
714
+ for (const file of files) {
715
+ const filePath = (0, path_1.join)(directoryPath, file);
716
+ const sql = fs_1.default.readFileSync(filePath, 'utf8');
717
+ try {
718
+ yield this.executeQuery((client) => client.query(sql));
719
+ fs_1.default.unlinkSync(filePath);
720
+ }
721
+ catch (e) {
722
+ console.error('Error while executing', file);
723
+ console.error(e);
724
+ fs_1.default.renameSync(filePath, filePath.replace('dump_table_', 'error_dump_table_'));
725
+ }
726
+ }
727
+ });
728
+ }
729
+ saveUnsyncedFiles({ integrationId, crowdinId, files }) {
730
+ return __awaiter(this, void 0, void 0, function* () {
731
+ yield this.dbPromise;
732
+ yield this.executeQuery((client) => client.query(`
733
+ INSERT
734
+ INTO unsynced_files(integration_id, crowdin_id, files)
735
+ VALUES ($1, $2, $3,)
736
+ `, [integrationId, crowdinId, files]));
737
+ });
738
+ }
739
+ updateUnsyncedFiles({ integrationId, crowdinId, files }) {
740
+ return __awaiter(this, void 0, void 0, function* () {
741
+ yield this.dbPromise;
742
+ yield this.executeQuery((client) => client.query(`
743
+ UPDATE unsynced_files
744
+ SET files = $1
745
+ WHERE integration_id = $2 AND crowdin_id = $3
746
+ `, [files, integrationId, crowdinId]));
747
+ });
748
+ }
749
+ getUnsyncedFiles({ integrationId, crowdinId }) {
750
+ return __awaiter(this, void 0, void 0, function* () {
751
+ yield this.dbPromise;
752
+ return this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
753
+ const res = yield client.query(`
754
+ SELECT files
755
+ FROM unsynced_files
756
+ WHERE integration_id = $1 AND crowdin_id = $2
757
+ `, [integrationId, crowdinId]);
758
+ return res === null || res === void 0 ? void 0 : res.rows[0];
759
+ }));
760
+ });
761
+ }
711
762
  }
712
763
  exports.PostgreStorage = PostgreStorage;
@@ -1,7 +1,7 @@
1
1
  import { Storage } from '.';
2
2
  import { CrowdinCredentials } from '../types';
3
3
  import { IntegrationConfig, IntegrationCredentials, IntegrationFilesSnapshot, IntegrationSyncSettings, IntegrationWebhooks } from '../modules/integration/types';
4
- import { CreateJobParams, GetActiveJobsParams, GetJobParams, GetFileTranslationCacheByLanguageParams, Job, TranslationCache, UpdateJobParams, UpdateTranslationCacheParams, GetFileTranslationCache } from '../modules/integration/util/types';
4
+ import { CreateJobParams, GetActiveJobsParams, GetJobParams, GetFileTranslationCacheByLanguageParams, Job, TranslationCache, UpdateJobParams, UpdateTranslationCacheParams, GetFileTranslationCache, UnsyncedFiles, GetUnsyncedFiles } from '../modules/integration/util/types';
5
5
  import { UserErrors } from './types';
6
6
  export interface SQLiteStorageConfig {
7
7
  dbFolder: string;
@@ -12,6 +12,19 @@ export declare class SQLiteStorage implements Storage {
12
12
  private _rej?;
13
13
  private dbPromise;
14
14
  private config;
15
+ tables: {
16
+ crowdin_credentials: string;
17
+ integration_credentials: string;
18
+ sync_settings: string;
19
+ app_metadata: string;
20
+ files_snapshot: string;
21
+ webhooks: string;
22
+ user_errors: string;
23
+ integration_settings: string;
24
+ job: string;
25
+ translation_file_cache: string;
26
+ unsynced_files: string;
27
+ };
15
28
  constructor(config: SQLiteStorageConfig);
16
29
  private _run;
17
30
  private run;
@@ -71,4 +84,7 @@ export declare class SQLiteStorage implements Storage {
71
84
  getFileTranslationCache({ integrationId, crowdinId, fileId, }: GetFileTranslationCache): Promise<TranslationCache[] | undefined>;
72
85
  getFileTranslationCacheByLanguage({ integrationId, crowdinId, fileId, languageId, }: GetFileTranslationCacheByLanguageParams): Promise<TranslationCache | undefined>;
73
86
  updateTranslationCache({ integrationId, crowdinId, fileId, languageId, etag, }: UpdateTranslationCacheParams): Promise<void>;
87
+ saveUnsyncedFiles({ integrationId, crowdinId, files }: UnsyncedFiles): Promise<void>;
88
+ updateUnsyncedFiles({ integrationId, crowdinId, files }: UnsyncedFiles): Promise<void>;
89
+ getUnsyncedFiles({ integrationId, crowdinId }: GetUnsyncedFiles): Promise<UnsyncedFiles | undefined>;
74
90
  }