@qrvey/assets-sharing 0.3.4-dev.2164 → 0.3.4-dev.2179

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.
Files changed (101) hide show
  1. package/dist/cjs/common/common.type.js +3 -0
  2. package/dist/cjs/common/common.type.js.map +1 -0
  3. package/dist/cjs/common/constants.js +56 -0
  4. package/dist/cjs/common/constants.js.map +1 -0
  5. package/dist/cjs/common/persistence/base.js +15 -0
  6. package/dist/cjs/common/persistence/base.js.map +1 -0
  7. package/dist/cjs/common/persistence/poolClient.js +11 -0
  8. package/dist/cjs/common/persistence/poolClient.js.map +1 -0
  9. package/dist/cjs/common/util.js +12 -0
  10. package/dist/cjs/common/util.js.map +1 -0
  11. package/dist/cjs/context.js +22 -0
  12. package/dist/cjs/context.js.map +1 -0
  13. package/dist/cjs/index.js +45 -0
  14. package/dist/cjs/index.js.map +1 -0
  15. package/dist/cjs/quser/entities/types/user.type.js +3 -0
  16. package/dist/cjs/quser/entities/types/user.type.js.map +1 -0
  17. package/dist/cjs/quser/entities/user.js +25 -0
  18. package/dist/cjs/quser/entities/user.js.map +1 -0
  19. package/dist/cjs/quser/implementations/group.model.js +19 -0
  20. package/dist/cjs/quser/implementations/group.model.js.map +1 -0
  21. package/dist/cjs/quser/implementations/group.repository.js +19 -0
  22. package/dist/cjs/quser/implementations/group.repository.js.map +1 -0
  23. package/dist/cjs/quser/implementations/user.model.js +24 -0
  24. package/dist/cjs/quser/implementations/user.model.js.map +1 -0
  25. package/dist/cjs/quser/implementations/user.repository.js +29 -0
  26. package/dist/cjs/quser/implementations/user.repository.js.map +1 -0
  27. package/dist/cjs/quser/interfaces/rolesRepository.interface.js +3 -0
  28. package/dist/cjs/quser/interfaces/rolesRepository.interface.js.map +1 -0
  29. package/dist/cjs/quser/interfaces/userRepository.interface.js +3 -0
  30. package/dist/cjs/quser/interfaces/userRepository.interface.js.map +1 -0
  31. package/dist/cjs/quser/services/fromTokenToUser.js +52 -0
  32. package/dist/cjs/quser/services/fromTokenToUser.js.map +1 -0
  33. package/dist/cjs/quser/services/hasAdminRole.js +48 -0
  34. package/dist/cjs/quser/services/hasAdminRole.js.map +1 -0
  35. package/dist/cjs/sharing/entities/details.js +109 -0
  36. package/dist/cjs/sharing/entities/details.js.map +1 -0
  37. package/dist/cjs/sharing/entities/sharing.js +72 -0
  38. package/dist/cjs/sharing/entities/sharing.js.map +1 -0
  39. package/dist/cjs/sharing/entities/types/details.type.js +3 -0
  40. package/dist/cjs/sharing/entities/types/details.type.js.map +1 -0
  41. package/dist/cjs/sharing/entities/types/orgTreeResult.js +3 -0
  42. package/dist/cjs/sharing/entities/types/orgTreeResult.js.map +1 -0
  43. package/dist/cjs/sharing/entities/types/organization.type.js +3 -0
  44. package/dist/cjs/sharing/entities/types/organization.type.js.map +1 -0
  45. package/dist/cjs/sharing/entities/types/sharing.type.js +3 -0
  46. package/dist/cjs/sharing/entities/types/sharing.type.js.map +1 -0
  47. package/dist/cjs/sharing/implementations/admin.repository.js +47 -0
  48. package/dist/cjs/sharing/implementations/admin.repository.js.map +1 -0
  49. package/dist/cjs/sharing/implementations/details.model.js +93 -0
  50. package/dist/cjs/sharing/implementations/details.model.js.map +1 -0
  51. package/dist/cjs/sharing/implementations/details.repository.js +114 -0
  52. package/dist/cjs/sharing/implementations/details.repository.js.map +1 -0
  53. package/dist/cjs/sharing/implementations/dm.model.js +22 -0
  54. package/dist/cjs/sharing/implementations/dm.model.js.map +1 -0
  55. package/dist/cjs/sharing/implementations/dm.repository.js +20 -0
  56. package/dist/cjs/sharing/implementations/dm.repository.js.map +1 -0
  57. package/dist/cjs/sharing/implementations/dx.model.js +30 -0
  58. package/dist/cjs/sharing/implementations/dx.model.js.map +1 -0
  59. package/dist/cjs/sharing/implementations/dx.repository.js +21 -0
  60. package/dist/cjs/sharing/implementations/dx.repository.js.map +1 -0
  61. package/dist/cjs/sharing/implementations/organization.model.js +31 -0
  62. package/dist/cjs/sharing/implementations/organization.model.js.map +1 -0
  63. package/dist/cjs/sharing/implementations/organization.repository.js +66 -0
  64. package/dist/cjs/sharing/implementations/organization.repository.js.map +1 -0
  65. package/dist/cjs/sharing/implementations/sharing.model.js +35 -0
  66. package/dist/cjs/sharing/implementations/sharing.model.js.map +1 -0
  67. package/dist/cjs/sharing/implementations/sharing.repository.js +31 -0
  68. package/dist/cjs/sharing/implementations/sharing.repository.js.map +1 -0
  69. package/dist/cjs/sharing/interfaces/adminRepository.interface.js +3 -0
  70. package/dist/cjs/sharing/interfaces/adminRepository.interface.js.map +1 -0
  71. package/dist/cjs/sharing/interfaces/dashboardRepository.interface.js +3 -0
  72. package/dist/cjs/sharing/interfaces/dashboardRepository.interface.js.map +1 -0
  73. package/dist/cjs/sharing/interfaces/detailsRepository.interface.js +3 -0
  74. package/dist/cjs/sharing/interfaces/detailsRepository.interface.js.map +1 -0
  75. package/dist/cjs/sharing/interfaces/downloadManagerRepository.interface.js +3 -0
  76. package/dist/cjs/sharing/interfaces/downloadManagerRepository.interface.js.map +1 -0
  77. package/dist/cjs/sharing/interfaces/organizationRepository.interface.js +3 -0
  78. package/dist/cjs/sharing/interfaces/organizationRepository.interface.js.map +1 -0
  79. package/dist/cjs/sharing/interfaces/sharingRepository.interface.js +3 -0
  80. package/dist/cjs/sharing/interfaces/sharingRepository.interface.js.map +1 -0
  81. package/dist/cjs/sharing/services/checkAppPermissions.js +45 -0
  82. package/dist/cjs/sharing/services/checkAppPermissions.js.map +1 -0
  83. package/dist/cjs/sharing/services/checkOrgPermissions.js +32 -0
  84. package/dist/cjs/sharing/services/checkOrgPermissions.js.map +1 -0
  85. package/dist/cjs/sharing/services/checkUserAccessLevel.js +58 -0
  86. package/dist/cjs/sharing/services/checkUserAccessLevel.js.map +1 -0
  87. package/dist/cjs/sharing/services/delete.js +32 -0
  88. package/dist/cjs/sharing/services/delete.js.map +1 -0
  89. package/dist/cjs/sharing/services/getOrgTree.js +26 -0
  90. package/dist/cjs/sharing/services/getOrgTree.js.map +1 -0
  91. package/dist/cjs/sharing/services/list.js +44 -0
  92. package/dist/cjs/sharing/services/list.js.map +1 -0
  93. package/dist/cjs/sharing/services/upsert.js +135 -0
  94. package/dist/cjs/sharing/services/upsert.js.map +1 -0
  95. package/dist/cjs/sharing/services/upsertDefaultSharing.js +48 -0
  96. package/dist/cjs/sharing/services/upsertDefaultSharing.js.map +1 -0
  97. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -0
  98. package/dist/esm/index.mjs +1251 -0
  99. package/dist/esm/index.mjs.map +1 -0
  100. package/dist/types/index.d.ts +217 -0
  101. package/package.json +1 -1
@@ -0,0 +1,1251 @@
1
+ import 'reflect-metadata';
2
+ import { container, injectable, inject } from 'tsyringe';
3
+ import { CrudSchema, CrudService, buildFilter, getDbPool } from '@qrvey/data-persistence';
4
+ import { getAttribute } from '@qrvey/utils';
5
+ import { getId } from '@qrvey/id-generator';
6
+
7
+ var __defProp = Object.defineProperty;
8
+ var __defProps = Object.defineProperties;
9
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
11
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
12
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
13
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
14
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
15
+ var __spreadValues = (a, b) => {
16
+ for (var prop in b || (b = {}))
17
+ if (__hasOwnProp.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ if (__getOwnPropSymbols)
20
+ for (var prop of __getOwnPropSymbols(b)) {
21
+ if (__propIsEnum.call(b, prop))
22
+ __defNormalProp(a, prop, b[prop]);
23
+ }
24
+ return a;
25
+ };
26
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
27
+ var __decorateClass = (decorators, target, key, kind) => {
28
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
29
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
30
+ if (decorator = decorators[i])
31
+ result = (decorator(result)) || result;
32
+ return result;
33
+ };
34
+ var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
35
+
36
+ // src/common/constants.ts
37
+ var ENVIRONMENT = {
38
+ SERVER_PREFIX: process.env.SERVER_PREFIX,
39
+ TABLE_PREFIX: process.env.TABLE_PREFIX,
40
+ DOMAIN: process.env.DOMAIN,
41
+ API_KEY: process.env.API_KEY
42
+ };
43
+ var DATABASE_INFO = {
44
+ DATA_PERSISTENCE_SCHEMA: "admin",
45
+ SERVER_PREFIX: ENVIRONMENT.SERVER_PREFIX,
46
+ TABLE_PREFIX: ENVIRONMENT.TABLE_PREFIX
47
+ };
48
+ var ORGANIZATION_QRVEY = "org:0";
49
+ var ORGANIZATION_ANONYMOUS = "anonymous";
50
+ var ASSET_TYPE = /* @__PURE__ */ ((ASSET_TYPE2) => {
51
+ ASSET_TYPE2["DX"] = "DX";
52
+ ASSET_TYPE2["DM"] = "DM";
53
+ return ASSET_TYPE2;
54
+ })(ASSET_TYPE || {});
55
+ var SHARED_ORIGIN = /* @__PURE__ */ ((SHARED_ORIGIN3) => {
56
+ SHARED_ORIGIN3["INTERNAL"] = "INTERNAL";
57
+ SHARED_ORIGIN3["EXTERNAL"] = "EXTERNAL";
58
+ SHARED_ORIGIN3["UNKNOWN"] = "UNKNOWN";
59
+ return SHARED_ORIGIN3;
60
+ })(SHARED_ORIGIN || {});
61
+ var SHARE_TYPE = /* @__PURE__ */ ((SHARE_TYPE3) => {
62
+ SHARE_TYPE3["USER"] = "USER";
63
+ SHARE_TYPE3["ROLE"] = "ROLE";
64
+ SHARE_TYPE3["ORGANIZATION"] = "ORGANIZATION";
65
+ return SHARE_TYPE3;
66
+ })(SHARE_TYPE || {});
67
+ var ACCESS_LEVEL = /* @__PURE__ */ ((ACCESS_LEVEL3) => {
68
+ ACCESS_LEVEL3[ACCESS_LEVEL3["NONE"] = 0] = "NONE";
69
+ ACCESS_LEVEL3[ACCESS_LEVEL3["CAN_USE"] = 1] = "CAN_USE";
70
+ ACCESS_LEVEL3[ACCESS_LEVEL3["CAN_EDIT"] = 2] = "CAN_EDIT";
71
+ ACCESS_LEVEL3[ACCESS_LEVEL3["ADMIN"] = 4] = "ADMIN";
72
+ return ACCESS_LEVEL3;
73
+ })(ACCESS_LEVEL || {});
74
+ var APP_SHARING_STATUS = /* @__PURE__ */ ((APP_SHARING_STATUS2) => {
75
+ APP_SHARING_STATUS2["PUBLIC"] = "public";
76
+ APP_SHARING_STATUS2["PRIVATE"] = "private";
77
+ return APP_SHARING_STATUS2;
78
+ })(APP_SHARING_STATUS || {});
79
+ var LIMIT_PER_PAGE = 10;
80
+
81
+ // src/quser/implementations/group.model.ts
82
+ var GroupModel = class extends CrudSchema {
83
+ };
84
+ GroupModel.table = {
85
+ name: `${DATABASE_INFO.SERVER_PREFIX}Groups`,
86
+ alias: "qv_groups"
87
+ };
88
+ GroupModel.columns = {
89
+ groupid: {
90
+ type: "string"
91
+ }
92
+ };
93
+ GroupModel.schema = DATABASE_INFO.DATA_PERSISTENCE_SCHEMA;
94
+ var poolClient;
95
+ function getPoolClient() {
96
+ if (!poolClient) poolClient = getDbPool();
97
+ return poolClient;
98
+ }
99
+
100
+ // src/common/persistence/base.ts
101
+ var BaseRepository = class extends CrudService {
102
+ constructor(schema) {
103
+ super(schema, getPoolClient());
104
+ }
105
+ filter(attribute, value, operator) {
106
+ return buildFilter(attribute, value, operator);
107
+ }
108
+ };
109
+
110
+ // src/quser/implementations/group.repository.ts
111
+ var GroupRepository = class extends BaseRepository {
112
+ constructor() {
113
+ super(GroupModel);
114
+ }
115
+ async getOne(identifier) {
116
+ const params = {
117
+ filters: [this.filter("groupid", identifier)]
118
+ };
119
+ const response = await super.findItem(params);
120
+ return response;
121
+ }
122
+ };
123
+ var UserModel = class extends CrudSchema {
124
+ };
125
+ UserModel.table = {
126
+ name: `${DATABASE_INFO.SERVER_PREFIX}Users`,
127
+ alias: "qv_users"
128
+ };
129
+ UserModel.columns = {
130
+ userid: {
131
+ type: "string",
132
+ primary: true,
133
+ columnId: true
134
+ },
135
+ groups: {
136
+ type: "array"
137
+ }
138
+ };
139
+ UserModel.schema = DATABASE_INFO.DATA_PERSISTENCE_SCHEMA;
140
+
141
+ // src/quser/implementations/user.repository.ts
142
+ var UserRepository = class extends BaseRepository {
143
+ constructor() {
144
+ super(UserModel);
145
+ }
146
+ async getOne(identifier) {
147
+ const params = {
148
+ filters: [this.filter("userid", identifier)]
149
+ };
150
+ const response = await super.findItem(params);
151
+ return response;
152
+ }
153
+ async getList(options) {
154
+ const params = {
155
+ filters: [
156
+ this.filter("userid", options.data.identifier),
157
+ this.filter("groups", options.data.role, "CONTAINS")
158
+ ]
159
+ };
160
+ const response = await super.find(params);
161
+ return response;
162
+ }
163
+ };
164
+ var AdminRepository = class {
165
+ async getApplicationInfo(body) {
166
+ var _a;
167
+ const url = `${ENVIRONMENT.DOMAIN}/devapi/v4/user/${body.userId}/app/${body.appId}`;
168
+ const headers = {
169
+ "x-api-key": ENVIRONMENT.API_KEY
170
+ };
171
+ const requestOptions = {
172
+ method: "GET",
173
+ headers
174
+ };
175
+ const response = await fetch(url, requestOptions);
176
+ const data = await response.json();
177
+ return {
178
+ status: response.status,
179
+ data: {
180
+ userId: (_a = data == null ? void 0 : data.userid) != null ? _a : body.userId,
181
+ appId: getAttribute(data, "appId"),
182
+ userEmail: getAttribute(data, "userEmail")
183
+ }
184
+ };
185
+ }
186
+ async getPlatformConfiguration() {
187
+ var _a, _b, _c;
188
+ const url = `${ENVIRONMENT.DOMAIN}/admin/api/v5/customization/features/platform`;
189
+ const headers = {
190
+ "x-api-key": ENVIRONMENT.API_KEY
191
+ };
192
+ const requestOptions = {
193
+ method: "GET",
194
+ headers
195
+ };
196
+ const response = await fetch(url, requestOptions);
197
+ const data = await response.json();
198
+ return {
199
+ status: response.status,
200
+ legacyMode: (_c = (_b = (_a = data == null ? void 0 : data.Item) == null ? void 0 : _a.settings) == null ? void 0 : _b.legacyMode) != null ? _c : true
201
+ };
202
+ }
203
+ };
204
+ var SharingDetailsModel = class extends CrudSchema {
205
+ };
206
+ SharingDetailsModel.table = {
207
+ name: `${DATABASE_INFO.SERVER_PREFIX}_sharing_details`,
208
+ alias: `${DATABASE_INFO.TABLE_PREFIX}_sharing_details`
209
+ };
210
+ SharingDetailsModel.columns = {
211
+ sharing_details_id: { type: "string", primary: true },
212
+ asset_id: { type: "string" },
213
+ access_level: { type: "numeric" },
214
+ org_id: { type: "string" },
215
+ shared_with: { type: "string" },
216
+ shared_type: { type: "string" },
217
+ shared_email: { type: "string" },
218
+ share_origin: { type: "string" },
219
+ created_at: { type: "date" },
220
+ updated_at: { type: "date" },
221
+ deleted_at: { type: "date" }
222
+ };
223
+ SharingDetailsModel.indexes = {
224
+ sharingDetailsOrgId: {
225
+ name: "sharing_details_org_id_index",
226
+ columns: [
227
+ "org_id",
228
+ "asset_id",
229
+ "sharing_details_id",
230
+ "shared_with",
231
+ "shared_type",
232
+ "shared_email",
233
+ "share_origin"
234
+ ]
235
+ },
236
+ sharingDetailsSharedWithAccessLevel: {
237
+ name: "sharing_details_shared_with_access_level_index",
238
+ columns: [
239
+ "shared_with",
240
+ "access_level",
241
+ "asset_id",
242
+ "sharing_details_id",
243
+ "org_id",
244
+ "shared_type",
245
+ "shared_email",
246
+ "share_origin"
247
+ ]
248
+ },
249
+ sharingDetailsAccessLevelSharedWithOrgId: {
250
+ name: "sharing_details_access_level_shared_with_org_id_index",
251
+ columns: [
252
+ "access_level",
253
+ "shared_with",
254
+ "org_id",
255
+ "asset_id",
256
+ "sharing_details_id",
257
+ "shared_type",
258
+ "shared_email",
259
+ "share_origin"
260
+ ]
261
+ },
262
+ sharingDetailsAccessLevelOrgId: {
263
+ name: "sharing_details_access_level_org_id_index",
264
+ columns: [
265
+ "access_level",
266
+ "org_id",
267
+ "asset_id",
268
+ "sharing_details_id",
269
+ "shared_with",
270
+ "shared_type",
271
+ "shared_email",
272
+ "share_origin"
273
+ ]
274
+ },
275
+ sharingDetailsAssetId: {
276
+ name: "sharing_details_asset_id_index",
277
+ columns: [
278
+ "asset_id",
279
+ "sharing_details_id",
280
+ "org_id",
281
+ "shared_with",
282
+ "shared_type",
283
+ "shared_email",
284
+ "share_origin"
285
+ ]
286
+ }
287
+ };
288
+ SharingDetailsModel.schema = DATABASE_INFO.DATA_PERSISTENCE_SCHEMA;
289
+ SharingDetailsModel.usePool = true;
290
+
291
+ // src/sharing/implementations/details.repository.ts
292
+ var SharingDetailsRepository = class extends BaseRepository {
293
+ constructor() {
294
+ super(SharingDetailsModel);
295
+ }
296
+ async getOne(sharingDetailId) {
297
+ const params = {
298
+ filters: [this.filter("sharing_details_id", sharingDetailId)]
299
+ };
300
+ const response = await super.findItem(params);
301
+ return response;
302
+ }
303
+ async getBySharedWith(assetId, sharedWith) {
304
+ const filterList = [
305
+ this.filter("asset_id", assetId),
306
+ this.filter("shared_with", sharedWith)
307
+ ];
308
+ const params = {
309
+ filters: filterList
310
+ };
311
+ const response = await super.findItem(params);
312
+ return response;
313
+ }
314
+ async getList(options) {
315
+ const assetIndex = SharingDetailsModel.indexes.sharingDetailsAssetId;
316
+ const pagination = this.getPagination(options.pagination);
317
+ const params = {
318
+ pagination,
319
+ index: {
320
+ indexName: assetIndex.name,
321
+ columns: assetIndex.columns
322
+ }
323
+ };
324
+ const filterList = [this.filter("asset_id", options.data.assetId)];
325
+ if (options.filters) {
326
+ options.filters.forEach((filter) => {
327
+ filterList.push(this.filter(filter.column, filter.value));
328
+ });
329
+ }
330
+ params.filters = filterList;
331
+ if (options.data.user) {
332
+ const currentFilter = this.getUserFilter(
333
+ options.data.user,
334
+ filterList
335
+ );
336
+ params.filters = currentFilter;
337
+ }
338
+ if (options.sorting) params.sorting = options.sorting;
339
+ const response = await super.find(params);
340
+ return response;
341
+ }
342
+ async create(details) {
343
+ const response = await super.create(details);
344
+ return response;
345
+ }
346
+ async patch(sharingDetailId, details) {
347
+ await super.update(
348
+ [this.filter("sharing_details_id", sharingDetailId)],
349
+ details
350
+ );
351
+ return true;
352
+ }
353
+ async delete(sharingDetailId) {
354
+ await super.remove([
355
+ this.filter("sharing_details_id", sharingDetailId)
356
+ ]);
357
+ return true;
358
+ }
359
+ getPagination(options) {
360
+ var _a, _b;
361
+ return {
362
+ limit: (_a = options == null ? void 0 : options.limit) != null ? _a : LIMIT_PER_PAGE,
363
+ from: (_b = options == null ? void 0 : options.from) != null ? _b : void 0
364
+ };
365
+ }
366
+ getUserFilter(user, currentFilters) {
367
+ const { userId, roles } = user;
368
+ const orgId = user.orgId === "" ? ORGANIZATION_QRVEY : user.orgId;
369
+ const sharing = {
370
+ OR: [
371
+ {
372
+ AND: [
373
+ this.buildFilter("shared_with", userId),
374
+ this.buildFilter("shared_type", "USER"),
375
+ this.buildFilter("org_id", orgId)
376
+ ]
377
+ },
378
+ {
379
+ AND: [
380
+ this.buildFilter("shared_with", orgId),
381
+ this.buildFilter("shared_type", "ORGANIZATION"),
382
+ this.buildFilter("org_id", orgId)
383
+ ]
384
+ }
385
+ ]
386
+ };
387
+ if (roles && (roles == null ? void 0 : roles.length) > 0) {
388
+ sharing.OR.push({
389
+ AND: [
390
+ this.buildFilter("shared_with", roles, "IN"),
391
+ this.buildFilter("shared_type", "ROLE"),
392
+ this.buildFilter("org_id", orgId)
393
+ ]
394
+ });
395
+ }
396
+ const filter = [];
397
+ if (currentFilters) filter.push(...currentFilters);
398
+ filter.push(sharing);
399
+ return { AND: filter };
400
+ }
401
+ };
402
+ var DownloadManagerSchema = class extends CrudSchema {
403
+ };
404
+ DownloadManagerSchema.table = {
405
+ name: `${DATABASE_INFO.SERVER_PREFIX}DownloadManager`,
406
+ alias: `${DATABASE_INFO.TABLE_PREFIX}_download_manager`
407
+ };
408
+ DownloadManagerSchema.columns = {
409
+ downloadManagerId: {
410
+ primary: true,
411
+ type: "String",
412
+ columnId: true
413
+ }
414
+ };
415
+ DownloadManagerSchema.schema = "exports";
416
+ DownloadManagerSchema.usePool = true;
417
+
418
+ // src/sharing/implementations/dm.repository.ts
419
+ var DownloadManagerRepository = class extends BaseRepository {
420
+ constructor() {
421
+ super(DownloadManagerSchema);
422
+ }
423
+ async get(identifier) {
424
+ const filterList = [this.filter("downloadManagerId", identifier)];
425
+ const params = {
426
+ filters: filterList
427
+ };
428
+ const response = await super.findItem(params);
429
+ return response;
430
+ }
431
+ };
432
+ var DashboardSchema = class extends CrudSchema {
433
+ };
434
+ DashboardSchema.table = {
435
+ name: `${DATABASE_INFO.SERVER_PREFIX}_dx_dashboard`,
436
+ alias: `${DATABASE_INFO.TABLE_PREFIX}_dashboard`
437
+ };
438
+ DashboardSchema.columns = {
439
+ ResourceUID: { type: "string", primary: true },
440
+ dashboardId: { type: "string" },
441
+ contentId: { type: "string" },
442
+ userId: { type: "string" },
443
+ ownerId: { type: "string" },
444
+ appId: { type: "string" },
445
+ qvAttributes: { type: "object" }
446
+ };
447
+ DashboardSchema.indexes = {
448
+ ResourceUID: {
449
+ name: "MetadataByResource",
450
+ columns: ["Metadata", "ResourceUID"]
451
+ }
452
+ };
453
+ DashboardSchema.schema = "dashboard_x";
454
+ DashboardSchema.usePool = true;
455
+
456
+ // src/sharing/implementations/dx.repository.ts
457
+ var DashboardRepository = class extends BaseRepository {
458
+ constructor() {
459
+ super(DashboardSchema);
460
+ }
461
+ async get(identifier) {
462
+ const uid = `DASHBOARD=${identifier}&BRANCH=MAIN`;
463
+ const filterList = [this.filter("ResourceUID", uid)];
464
+ const params = {
465
+ filters: filterList
466
+ };
467
+ const response = await super.findItem(params);
468
+ return response;
469
+ }
470
+ };
471
+ var OrganizationModel = class extends CrudSchema {
472
+ };
473
+ OrganizationModel.table = {
474
+ name: "qv_organization",
475
+ alias: `${DATABASE_INFO.TABLE_PREFIX}_organization`
476
+ };
477
+ OrganizationModel.columns = {
478
+ org_id: { type: "string", primary: true },
479
+ name: { type: "string" },
480
+ parent_org_id: { type: "string" },
481
+ org_path: { type: "string" },
482
+ content_privacy: { type: "boolean" },
483
+ effective_content_privacy: { type: "boolean" },
484
+ created_at: { type: "string" },
485
+ updated_at: { type: "string" }
486
+ };
487
+ OrganizationModel.indexes = {
488
+ org_path: { name: "idx_qv_orgs_org_path_gist", columns: ["org_path"] },
489
+ parent_org_id: {
490
+ name: "idx_qv_orgs_parent",
491
+ columns: ["parent_org_id"]
492
+ }
493
+ };
494
+ OrganizationModel.schema = DATABASE_INFO.DATA_PERSISTENCE_SCHEMA;
495
+ OrganizationModel.usePool = true;
496
+
497
+ // src/sharing/implementations/organization.repository.ts
498
+ var OrganizationRepository = class extends BaseRepository {
499
+ constructor() {
500
+ super(OrganizationModel);
501
+ }
502
+ async getOne(orgId) {
503
+ const params = {
504
+ filters: [this.filter("org_id", orgId)]
505
+ };
506
+ const response = await super.findItem(params);
507
+ return response;
508
+ }
509
+ async getOrgTree(orgId, contentSharing) {
510
+ const cte = `WITH target AS (SELECT org_path FROM admin.qv_organization WHERE org_id = '${orgId}')`;
511
+ let selectClause = "";
512
+ let whereClause = `
513
+ FROM admin.qv_organization, target
514
+ WHERE (
515
+ admin.qv_organization.org_path <@ target.org_path
516
+ OR
517
+ admin.qv_organization.org_path @> target.org_path
518
+ )
519
+ AND org_id != '${orgId}'
520
+ `;
521
+ if (contentSharing === void 0) {
522
+ selectClause = `
523
+ SELECT
524
+ COALESCE(ARRAY_AGG(org_id) FILTER (WHERE effective_content_privacy = true), '{}') as db_private,
525
+ COALESCE(ARRAY_AGG(org_id) FILTER (WHERE effective_content_privacy = false), '{}') as db_public
526
+ `;
527
+ } else {
528
+ selectClause = `SELECT COALESCE(ARRAY_AGG(org_id), '{}') as db_specific`;
529
+ whereClause += ` AND effective_content_privacy = ${contentSharing}`;
530
+ }
531
+ const fullSql = `${cte} ${selectClause} ${whereClause};`;
532
+ const result = await super.runRawQuery(fullSql, []);
533
+ const row = result == null ? void 0 : result.rows[0];
534
+ if (!row)
535
+ return {
536
+ privateIds: [],
537
+ publicIds: []
538
+ };
539
+ if (contentSharing === void 0) {
540
+ return {
541
+ privateIds: row.db_private,
542
+ publicIds: row.db_public
543
+ };
544
+ } else {
545
+ return {
546
+ privateIds: contentSharing ? row.db_specific : [],
547
+ publicIds: contentSharing ? [] : row.db_specific
548
+ };
549
+ }
550
+ }
551
+ };
552
+ var SharingModel = class extends CrudSchema {
553
+ };
554
+ SharingModel.table = {
555
+ name: `${DATABASE_INFO.SERVER_PREFIX}_sharing`,
556
+ alias: `${DATABASE_INFO.TABLE_PREFIX}_sharing`
557
+ };
558
+ SharingModel.columns = {
559
+ asset_id: { type: "string", primary: true },
560
+ asset_type: { type: "string" },
561
+ default_content_sharing_enabled: { type: "boolean" },
562
+ default_access_level: { type: "number" },
563
+ created_at: { type: "date" },
564
+ updated_at: { type: "date" },
565
+ deleted_at: { type: "date" },
566
+ qvAttributes: { type: "object" }
567
+ };
568
+ SharingModel.indexes = {
569
+ sharingConfigLookup: {
570
+ name: "sharing_config_lookup_idx",
571
+ columns: ["asset_id", "asset_type"]
572
+ },
573
+ sharingAssetType: {
574
+ name: "sharing_asset_type_index",
575
+ columns: ["asset_type"]
576
+ }
577
+ };
578
+ SharingModel.schema = DATABASE_INFO.DATA_PERSISTENCE_SCHEMA;
579
+ SharingModel.usePool = true;
580
+
581
+ // src/sharing/implementations/sharing.repository.ts
582
+ var SharingRepository = class extends BaseRepository {
583
+ constructor() {
584
+ super(SharingModel);
585
+ }
586
+ async getOne(assetId) {
587
+ const params = {
588
+ filters: [this.filter("asset_id", assetId)]
589
+ };
590
+ const response = await super.findItem(params);
591
+ return response;
592
+ }
593
+ async create(sharing) {
594
+ const response = await super.create(sharing);
595
+ return response;
596
+ }
597
+ async patch(assetId, sharing) {
598
+ await super.update([this.filter("asset_id", assetId)], sharing);
599
+ return true;
600
+ }
601
+ async delete(assetId) {
602
+ await super.remove([this.filter("asset_id", assetId)]);
603
+ return true;
604
+ }
605
+ };
606
+
607
+ // src/context.ts
608
+ container.register(
609
+ "SharingRepository",
610
+ SharingRepository
611
+ );
612
+ container.register(
613
+ "SharingDetailsRepository",
614
+ SharingDetailsRepository
615
+ );
616
+ container.register("UserRepository", UserRepository);
617
+ container.register("RoleRepository", GroupRepository);
618
+ container.register(
619
+ "dxRepository",
620
+ DashboardRepository
621
+ );
622
+ container.register(
623
+ "dmRepository",
624
+ DownloadManagerRepository
625
+ );
626
+ container.register(
627
+ "OrganizationRepository",
628
+ OrganizationRepository
629
+ );
630
+ container.register(
631
+ "AdminRepository",
632
+ AdminRepository
633
+ );
634
+ var Context = container;
635
+
636
+ // src/common/util.ts
637
+ function isComposer(type) {
638
+ return type === "qrveyLogin" /* composer */ || type === "OPENID_CREATORS" /* openIdComposer */;
639
+ }
640
+
641
+ // src/quser/entities/user.ts
642
+ var User = class {
643
+ constructor(params) {
644
+ this.userId = params.userid;
645
+ this.email = params.email;
646
+ this.groups = params.groups;
647
+ this.organization = params.organization;
648
+ }
649
+ get getUserId() {
650
+ return this.userId;
651
+ }
652
+ get getEmail() {
653
+ return this.email;
654
+ }
655
+ get getGroups() {
656
+ return this.groups;
657
+ }
658
+ get getOrganization() {
659
+ return this.organization;
660
+ }
661
+ };
662
+
663
+ // src/quser/services/fromTokenToUser.ts
664
+ var FromTokenToUser = class {
665
+ constructor(userRepository) {
666
+ this.userRepository = userRepository;
667
+ }
668
+ async execute({
669
+ body
670
+ }) {
671
+ var _a;
672
+ const current = await this.userRepository.getOne(
673
+ body.clientId || body.userId
674
+ );
675
+ if (current) {
676
+ const user = new User(current);
677
+ return {
678
+ userId: user.getUserId,
679
+ roles: user.getGroups,
680
+ orgId: (_a = body.orgId) != null ? _a : ORGANIZATION_QRVEY
681
+ };
682
+ }
683
+ const inComposer = isComposer(body.loginType || "");
684
+ if (!inComposer) {
685
+ return {
686
+ userId: body.clientId || "",
687
+ roles: body.roles || [],
688
+ orgId: body.orgId || ""
689
+ };
690
+ }
691
+ return {
692
+ userId: "",
693
+ roles: [],
694
+ orgId: ""
695
+ };
696
+ }
697
+ };
698
+ FromTokenToUser = __decorateClass([
699
+ injectable(),
700
+ __decorateParam(0, inject("UserRepository"))
701
+ ], FromTokenToUser);
702
+ var HasAdminRole = class {
703
+ constructor(userRepository) {
704
+ this.userRepository = userRepository;
705
+ }
706
+ async execute({
707
+ apiKey,
708
+ clientId,
709
+ orgId,
710
+ userId,
711
+ loginType
712
+ } = {}) {
713
+ if (!userId) return false;
714
+ const existingUser = await this.userRepository.getOne(userId);
715
+ const isMasterKey = apiKey && (orgId === void 0 || orgId === ORGANIZATION_QRVEY);
716
+ if (isMasterKey && existingUser && (!clientId || clientId === userId))
717
+ return true;
718
+ const user = await this.userRepository.getList({
719
+ data: { identifier: clientId != null ? clientId : userId, role: "administrator" }
720
+ });
721
+ const isAdmin = user.items.length > 0;
722
+ if (isMasterKey && isAdmin) return true;
723
+ const inComposer = isComposer(loginType || "");
724
+ const isExternal = !clientId && !inComposer || orgId && orgId !== ORGANIZATION_QRVEY;
725
+ if (isExternal) return false;
726
+ if (isAdmin) return true;
727
+ return false;
728
+ }
729
+ };
730
+ HasAdminRole = __decorateClass([
731
+ injectable(),
732
+ __decorateParam(0, inject("UserRepository"))
733
+ ], HasAdminRole);
734
+ var CheckAppPermissions = class {
735
+ constructor(adminRepository) {
736
+ this.adminRepository = adminRepository;
737
+ }
738
+ //TODO Need Test
739
+ async execute({
740
+ appId,
741
+ userId
742
+ }) {
743
+ if (!userId) return { privacy: "private" /* PRIVATE */ };
744
+ const appInfo = await this.adminRepository.getApplicationInfo({
745
+ appId,
746
+ userId
747
+ });
748
+ const platform = await this.adminRepository.getPlatformConfiguration();
749
+ if (appInfo.status !== 200)
750
+ return { privacy: "private" /* PRIVATE */ };
751
+ const hasPublicAccess = platform == null ? void 0 : platform.legacyMode;
752
+ const privacy = hasPublicAccess ? "public" /* PUBLIC */ : "private" /* PRIVATE */;
753
+ return { privacy };
754
+ }
755
+ };
756
+ CheckAppPermissions = __decorateClass([
757
+ injectable(),
758
+ __decorateParam(0, inject("AdminRepository"))
759
+ ], CheckAppPermissions);
760
+ var CheckOrgPermissions = class {
761
+ constructor(organizationRepository) {
762
+ this.organizationRepository = organizationRepository;
763
+ }
764
+ //TODO Need Tes
765
+ async execute({
766
+ orgId
767
+ }) {
768
+ const hasPublicAccess = await this.organizationRepository.getOne(orgId);
769
+ const privacy = (hasPublicAccess == null ? void 0 : hasPublicAccess.effective_content_privacy) ? "private" /* PRIVATE */ : "public" /* PUBLIC */;
770
+ return { privacy };
771
+ }
772
+ };
773
+ CheckOrgPermissions = __decorateClass([
774
+ injectable(),
775
+ __decorateParam(0, inject("OrganizationRepository"))
776
+ ], CheckOrgPermissions);
777
+ var SharingDetail = class _SharingDetail {
778
+ constructor(params) {
779
+ this.sharingDetailsId = params.sharingDetailsId;
780
+ this.assetId = params.assetId;
781
+ this.accessLevel = params.accessLevel;
782
+ this.accessLevelAsText = this.convertAccessLevelToText(
783
+ params.accessLevel
784
+ );
785
+ this.orgId = params.orgId;
786
+ this.sharedWith = params.sharedWith;
787
+ this.sharedType = params.sharedType;
788
+ this.sharedEmail = params.sharedEmail;
789
+ this.sharedOrigin = params.sharedOrigin;
790
+ this.createdAt = params.createdAt;
791
+ this.updatedAt = params.updatedAt;
792
+ this.deletedAt = params.deletedAt;
793
+ }
794
+ static create(params) {
795
+ var _a;
796
+ const now = (/* @__PURE__ */ new Date()).toISOString();
797
+ const obj = __spreadProps(__spreadValues({}, params), {
798
+ sharingDetailsId: (_a = params.sharedId) != null ? _a : getId(),
799
+ createdAt: now,
800
+ updatedAt: now
801
+ });
802
+ return new _SharingDetail(obj);
803
+ }
804
+ static parse(params) {
805
+ return new _SharingDetail({
806
+ sharingDetailsId: params.sharing_details_id,
807
+ assetId: params.asset_id,
808
+ accessLevel: params.access_level,
809
+ orgId: params.org_id,
810
+ sharedWith: params.shared_with,
811
+ sharedType: params.shared_type,
812
+ sharedEmail: params.shared_email,
813
+ sharedOrigin: params.share_origin,
814
+ createdAt: params.created_at,
815
+ updatedAt: params.updated_at,
816
+ deletedAt: params.deleted_at
817
+ });
818
+ }
819
+ update(params) {
820
+ if (params.accessLevel || params.accessLevel === 0)
821
+ this.accessLevel = params.accessLevel;
822
+ if (params.sharedEmail) this.sharedEmail = params.sharedEmail;
823
+ this.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
824
+ }
825
+ toTable() {
826
+ return {
827
+ sharing_details_id: this.sharingDetailsId,
828
+ asset_id: this.assetId,
829
+ access_level: this.accessLevel,
830
+ org_id: this.orgId,
831
+ shared_with: this.sharedWith,
832
+ shared_type: this.sharedType,
833
+ shared_email: this.sharedEmail,
834
+ share_origin: this.sharedOrigin,
835
+ created_at: this.createdAt,
836
+ updated_at: this.updatedAt,
837
+ deleted_at: this.deletedAt
838
+ };
839
+ }
840
+ toPlain() {
841
+ return {
842
+ sharingDetailsId: this.sharingDetailsId,
843
+ assetId: this.assetId,
844
+ accessLevel: this.accessLevel,
845
+ orgId: this.orgId,
846
+ sharedWith: this.sharedWith,
847
+ sharedType: this.sharedType,
848
+ sharedEmail: this.sharedEmail,
849
+ sharedOrigin: this.sharedOrigin,
850
+ createdAt: this.createdAt,
851
+ updatedAt: this.updatedAt,
852
+ deletedAt: this.deletedAt
853
+ };
854
+ }
855
+ toJSON() {
856
+ return {
857
+ sharedId: this.sharingDetailsId,
858
+ assetId: this.assetId,
859
+ accessLevel: this.accessLevel,
860
+ accessLevelAsText: this.accessLevelAsText,
861
+ orgId: this.orgId,
862
+ sharedWith: this.sharedWith,
863
+ sharedType: this.sharedType,
864
+ sharedEmail: this.sharedEmail,
865
+ sharedOrigin: this.sharedOrigin,
866
+ createdAt: this.createdAt,
867
+ updatedAt: this.updatedAt
868
+ };
869
+ }
870
+ get getSharingDetailsId() {
871
+ return this.sharingDetailsId;
872
+ }
873
+ get getAccessLevel() {
874
+ return this.accessLevel;
875
+ }
876
+ get getAccessLevelAsText() {
877
+ return this.accessLevelAsText;
878
+ }
879
+ convertAccessLevelToText(level) {
880
+ var _a;
881
+ return (_a = ACCESS_LEVEL[level]) != null ? _a : "UNKNOWN";
882
+ }
883
+ };
884
+
885
+ // src/sharing/services/checkUserAccessLevel.ts
886
+ var CheckUserAccessLevel = class {
887
+ constructor(detailsRepository) {
888
+ this.detailsRepository = detailsRepository;
889
+ }
890
+ async execute({
891
+ userId,
892
+ assetId,
893
+ clientId,
894
+ loginType,
895
+ roles,
896
+ orgId
897
+ }) {
898
+ const user = await fromTokenToUser({
899
+ body: { userId, clientId, roles, orgId, loginType }
900
+ });
901
+ const details = await this.detailsRepository.getList({
902
+ data: { assetId, user },
903
+ sorting: [{ column: "access_level", direction: "DESC" }]
904
+ });
905
+ if (details.items.length === 0)
906
+ return { level: 0 /* NONE */, levelAsText: "NONE" };
907
+ const priority = {
908
+ ["USER" /* USER */]: 3,
909
+ ["ROLE" /* ROLE */]: 2,
910
+ ["ORGANIZATION" /* ORGANIZATION */]: 1
911
+ };
912
+ const priorityDetail = details.items.reduce((best, current) => {
913
+ const bestPriority = priority[best.shared_type];
914
+ const currentPriority = priority[current.shared_type];
915
+ const isBetter = currentPriority > bestPriority || currentPriority === bestPriority && current.access_level > best.access_level;
916
+ if (isBetter) return current;
917
+ return best;
918
+ });
919
+ const currentDetail = SharingDetail.parse(priorityDetail);
920
+ return {
921
+ level: currentDetail.getAccessLevel,
922
+ levelAsText: currentDetail.getAccessLevelAsText
923
+ };
924
+ }
925
+ };
926
+ CheckUserAccessLevel = __decorateClass([
927
+ injectable(),
928
+ __decorateParam(0, inject("SharingDetailsRepository"))
929
+ ], CheckUserAccessLevel);
930
+ var DeleteSharing = class {
931
+ constructor(sharingRepository) {
932
+ this.sharingRepository = sharingRepository;
933
+ }
934
+ async execute({ body }) {
935
+ const { assetId } = body;
936
+ const sharing = await this.sharingRepository.getOne(assetId);
937
+ if (!sharing) return null;
938
+ const detailSharing = await this.sharingRepository.delete(assetId);
939
+ return detailSharing;
940
+ }
941
+ };
942
+ DeleteSharing = __decorateClass([
943
+ injectable(),
944
+ __decorateParam(0, inject("SharingRepository"))
945
+ ], DeleteSharing);
946
+ var GetOrgTree = class {
947
+ constructor(organizationRepository) {
948
+ this.organizationRepository = organizationRepository;
949
+ }
950
+ async execute(orgId, contentSharingStatus) {
951
+ return this.organizationRepository.getOrgTree(
952
+ orgId,
953
+ contentSharingStatus
954
+ );
955
+ }
956
+ };
957
+ GetOrgTree = __decorateClass([
958
+ injectable(),
959
+ __decorateParam(0, inject("OrganizationRepository"))
960
+ ], GetOrgTree);
961
+ var ListSharing = class {
962
+ constructor(sharingRepository, detailsRepository) {
963
+ this.sharingRepository = sharingRepository;
964
+ this.detailsRepository = detailsRepository;
965
+ }
966
+ async execute({
967
+ body
968
+ }) {
969
+ var _a;
970
+ const {
971
+ data: { assetId },
972
+ pagination
973
+ } = body;
974
+ const sharing = await this.sharingRepository.getOne(assetId);
975
+ if (!sharing) return null;
976
+ const sorting = body.sorting && ((_a = body.sorting) == null ? void 0 : _a.length) > 0 ? body.sorting : [{ column: "created_at", direction: "ASC" }];
977
+ const detailSharing = await this.detailsRepository.getList({
978
+ data: { assetId },
979
+ pagination,
980
+ sorting
981
+ });
982
+ const items = detailSharing.items.map(
983
+ (item) => SharingDetail.parse(item).toJSON()
984
+ );
985
+ return __spreadProps(__spreadValues({}, detailSharing), {
986
+ items
987
+ });
988
+ }
989
+ };
990
+ ListSharing = __decorateClass([
991
+ injectable(),
992
+ __decorateParam(0, inject("SharingRepository")),
993
+ __decorateParam(1, inject("SharingDetailsRepository"))
994
+ ], ListSharing);
995
+
996
+ // src/sharing/entities/sharing.ts
997
+ var Sharing = class _Sharing {
998
+ constructor(params) {
999
+ var _a, _b;
1000
+ this.assetId = params.assetId;
1001
+ this.assetType = params.assetType;
1002
+ this.createdAt = params.createdAt;
1003
+ this.updatedAt = params.updatedAt;
1004
+ this.deletedAt = params.deletedAt;
1005
+ this.defaultContentSharingEnabled = (_a = params.defaultContentSharingEnabled) != null ? _a : true;
1006
+ this.defaultAccessLevel = (_b = params.defaultAccessLevel) != null ? _b : null;
1007
+ }
1008
+ static create(params) {
1009
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1010
+ const obj = __spreadProps(__spreadValues({}, params), {
1011
+ createdAt: now,
1012
+ updatedAt: now
1013
+ });
1014
+ return new _Sharing(obj);
1015
+ }
1016
+ static parse(params) {
1017
+ return new _Sharing({
1018
+ assetId: params.asset_id,
1019
+ assetType: params.asset_type,
1020
+ createdAt: params.created_at,
1021
+ updatedAt: params.updated_at,
1022
+ deletedAt: params.deleted_at
1023
+ });
1024
+ }
1025
+ update() {
1026
+ this.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
1027
+ }
1028
+ toTable() {
1029
+ return {
1030
+ asset_id: this.assetId,
1031
+ asset_type: this.assetType,
1032
+ created_at: this.createdAt,
1033
+ updated_at: this.updatedAt,
1034
+ deleted_at: this.deletedAt
1035
+ };
1036
+ }
1037
+ toPlain() {
1038
+ return {
1039
+ assetId: this.assetId,
1040
+ assetType: this.assetType,
1041
+ createdAt: this.createdAt,
1042
+ updatedAt: this.updatedAt,
1043
+ deletedAt: this.deletedAt
1044
+ };
1045
+ }
1046
+ toJSON() {
1047
+ return {
1048
+ assetId: this.assetId,
1049
+ assetType: this.assetType,
1050
+ createdAt: this.createdAt,
1051
+ updatedAt: this.updatedAt
1052
+ };
1053
+ }
1054
+ set setDefaultContentSharingEnabled(defaultContentSharingEnabled) {
1055
+ this.defaultContentSharingEnabled = defaultContentSharingEnabled;
1056
+ }
1057
+ set setDefaultAccessLevel(defaultAccessLevel) {
1058
+ this.defaultAccessLevel = defaultAccessLevel;
1059
+ }
1060
+ get getAssetId() {
1061
+ return this.assetId;
1062
+ }
1063
+ get getAssetType() {
1064
+ return this.assetType;
1065
+ }
1066
+ };
1067
+
1068
+ // src/sharing/services/upsert.ts
1069
+ var UpsertSharing = class {
1070
+ constructor(sharingRepository, detailsRepository, dxRepository, dmRepository, userRepository, roleRepository) {
1071
+ this.sharingRepository = sharingRepository;
1072
+ this.detailsRepository = detailsRepository;
1073
+ this.dxRepository = dxRepository;
1074
+ this.dmRepository = dmRepository;
1075
+ this.userRepository = userRepository;
1076
+ this.roleRepository = roleRepository;
1077
+ }
1078
+ async execute({
1079
+ body
1080
+ }) {
1081
+ const assetId = body.assetId;
1082
+ const isValidAsset = await this.validateIfAssetExists(
1083
+ assetId,
1084
+ body.assetType
1085
+ );
1086
+ if (!isValidAsset)
1087
+ throw new Error(`Asset with ID ${assetId} not found`);
1088
+ const sharing = await this.sharingRepository.getOne(assetId);
1089
+ let currentSharing;
1090
+ if (sharing) {
1091
+ currentSharing = Sharing.parse(sharing);
1092
+ } else {
1093
+ const createSharing = {
1094
+ assetId,
1095
+ assetType: body.assetType
1096
+ };
1097
+ currentSharing = Sharing.create(createSharing);
1098
+ await this.sharingRepository.create(currentSharing.toTable());
1099
+ }
1100
+ let updatedList = [];
1101
+ if (body.add) updatedList = await this.addDetails(assetId, body.add);
1102
+ if (body.remove) await this.removeDetails(body.remove);
1103
+ currentSharing.update();
1104
+ await this.sharingRepository.patch(
1105
+ currentSharing.getAssetId,
1106
+ currentSharing.toTable()
1107
+ );
1108
+ return updatedList.map((detail) => detail.toJSON());
1109
+ }
1110
+ async validateIfAssetExists(assetId, assetType) {
1111
+ switch (assetType) {
1112
+ case "DX" /* DX */: {
1113
+ const result = await this.dxRepository.get(assetId);
1114
+ if (result) return true;
1115
+ return false;
1116
+ }
1117
+ case "DM" /* DM */: {
1118
+ return true;
1119
+ }
1120
+ default:
1121
+ return false;
1122
+ }
1123
+ }
1124
+ async addDetails(assetId, list) {
1125
+ const sharingList = [];
1126
+ for (const detail of list) {
1127
+ const currentDetail = await this.addSpecificDetail(assetId, detail);
1128
+ if (currentDetail) sharingList.push(currentDetail);
1129
+ }
1130
+ return sharingList;
1131
+ }
1132
+ async addSpecificDetail(assetId, detail) {
1133
+ var _a;
1134
+ let currentDetail;
1135
+ const isValid = await this.validateIfSharedWithExist(detail);
1136
+ if (!isValid) return null;
1137
+ let sharingDetail = await this.detailsRepository.getBySharedWith(
1138
+ assetId,
1139
+ detail.sharedWith
1140
+ );
1141
+ if (!sharingDetail && detail.sharedId) {
1142
+ sharingDetail = await this.detailsRepository.getOne(
1143
+ detail.sharedId
1144
+ );
1145
+ }
1146
+ if (sharingDetail) {
1147
+ currentDetail = SharingDetail.parse(sharingDetail);
1148
+ currentDetail.update(detail);
1149
+ await this.detailsRepository.patch(
1150
+ currentDetail.getSharingDetailsId,
1151
+ currentDetail.toTable()
1152
+ );
1153
+ } else {
1154
+ const createDetails = __spreadValues({
1155
+ assetId,
1156
+ orgId: (_a = detail.orgId) != null ? _a : ORGANIZATION_QRVEY
1157
+ }, detail);
1158
+ currentDetail = SharingDetail.create(createDetails);
1159
+ await this.detailsRepository.create(currentDetail.toTable());
1160
+ }
1161
+ return currentDetail;
1162
+ }
1163
+ async validateIfSharedWithExist(detail) {
1164
+ if (detail.sharedOrigin === "EXTERNAL" /* EXTERNAL */ || detail.sharedOrigin === "UNKNOWN" /* UNKNOWN */)
1165
+ return true;
1166
+ const isQrveyOrg = detail.sharedType === "ORGANIZATION" /* ORGANIZATION */ && detail.sharedWith === ORGANIZATION_QRVEY;
1167
+ if (isQrveyOrg) return true;
1168
+ if (detail.sharedType === "USER" /* USER */) {
1169
+ const user = await this.userRepository.getOne(detail.sharedWith);
1170
+ if (user) return true;
1171
+ }
1172
+ if (detail.sharedType === "ROLE" /* ROLE */) {
1173
+ const role = await this.roleRepository.getOne(detail.sharedWith);
1174
+ if (role) return true;
1175
+ }
1176
+ return false;
1177
+ }
1178
+ async removeDetails(list) {
1179
+ for (const detail of list) {
1180
+ await this.detailsRepository.delete(detail.sharedId);
1181
+ }
1182
+ }
1183
+ };
1184
+ UpsertSharing = __decorateClass([
1185
+ injectable(),
1186
+ __decorateParam(0, inject("SharingRepository")),
1187
+ __decorateParam(1, inject("SharingDetailsRepository")),
1188
+ __decorateParam(2, inject("dxRepository")),
1189
+ __decorateParam(3, inject("dmRepository")),
1190
+ __decorateParam(4, inject("UserRepository")),
1191
+ __decorateParam(5, inject("RoleRepository"))
1192
+ ], UpsertSharing);
1193
+ var UpsertDefaultSharing = class {
1194
+ constructor(sharingRepository) {
1195
+ this.sharingRepository = sharingRepository;
1196
+ }
1197
+ async execute({
1198
+ body
1199
+ }) {
1200
+ const {
1201
+ assetId,
1202
+ assetType,
1203
+ contentSharingEnabled = true,
1204
+ defaultAccessLevel
1205
+ } = body;
1206
+ const sharingRecord = await this.sharingRepository.getOne(assetId);
1207
+ let currentSharing;
1208
+ if (sharingRecord) {
1209
+ currentSharing = Sharing.parse(sharingRecord);
1210
+ } else {
1211
+ const createSharing = {
1212
+ assetId,
1213
+ assetType,
1214
+ defaultContentSharingEnabled: contentSharingEnabled,
1215
+ defaultAccessLevel: defaultAccessLevel != null ? defaultAccessLevel : null
1216
+ };
1217
+ currentSharing = Sharing.create(createSharing);
1218
+ await this.sharingRepository.create(currentSharing.toTable());
1219
+ }
1220
+ currentSharing.setDefaultAccessLevel = defaultAccessLevel != null ? defaultAccessLevel : null;
1221
+ currentSharing.setDefaultContentSharingEnabled = contentSharingEnabled;
1222
+ currentSharing.update();
1223
+ await this.sharingRepository.patch(
1224
+ currentSharing.getAssetId,
1225
+ currentSharing.toTable()
1226
+ );
1227
+ return currentSharing;
1228
+ }
1229
+ };
1230
+ UpsertDefaultSharing = __decorateClass([
1231
+ injectable(),
1232
+ __decorateParam(0, inject("SharingRepository"))
1233
+ ], UpsertDefaultSharing);
1234
+
1235
+ // src/index.ts
1236
+ var api = {
1237
+ upsert: (...args) => Context.resolve(UpsertSharing).execute(...args),
1238
+ list: (...args) => Context.resolve(ListSharing).execute(...args),
1239
+ delete: (...args) => Context.resolve(DeleteSharing).execute(...args),
1240
+ upsertDefaultSharing: (...args) => Context.resolve(UpsertDefaultSharing).execute(...args)
1241
+ };
1242
+ var checkUserAccessLevel = (...args) => Context.resolve(CheckUserAccessLevel).execute(...args);
1243
+ var checkAppPermissions = (...args) => Context.resolve(CheckAppPermissions).execute(...args);
1244
+ var checkOrgPermissions = (...args) => Context.resolve(CheckOrgPermissions).execute(...args);
1245
+ var getOrgTree = (...args) => Context.resolve(GetOrgTree).execute(...args);
1246
+ var fromTokenToUser = (...args) => Context.resolve(FromTokenToUser).execute(...args);
1247
+ var hasAdminRole = (...args) => Context.resolve(HasAdminRole).execute(...args);
1248
+
1249
+ export { ACCESS_LEVEL, APP_SHARING_STATUS, ASSET_TYPE, ORGANIZATION_ANONYMOUS, ORGANIZATION_QRVEY, SHARED_ORIGIN, SHARE_TYPE, api, checkAppPermissions, checkOrgPermissions, checkUserAccessLevel, fromTokenToUser, getOrgTree, hasAdminRole };
1250
+ //# sourceMappingURL=index.mjs.map
1251
+ //# sourceMappingURL=index.mjs.map