@qrvey/assets-sharing 0.3.3 → 0.3.4-dev.2089

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