bruce-models 1.9.8 → 2.0.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.
Files changed (112) hide show
  1. package/dist/bruce-models.es5.js +317 -74
  2. package/dist/bruce-models.es5.js.map +1 -1
  3. package/dist/bruce-models.umd.js +317 -74
  4. package/dist/bruce-models.umd.js.map +1 -1
  5. package/dist/lib/account/account-settings.js +2 -2
  6. package/dist/lib/account/account.js +201 -185
  7. package/dist/lib/account/account.js.map +1 -1
  8. package/dist/lib/ann-document/ann-document.js +94 -86
  9. package/dist/lib/ann-document/ann-document.js.map +1 -1
  10. package/dist/lib/api/abstract-api.js +260 -246
  11. package/dist/lib/api/abstract-api.js.map +1 -1
  12. package/dist/lib/api/api-getters.js +134 -134
  13. package/dist/lib/api/api.js +93 -93
  14. package/dist/lib/api/bruce-api.js +216 -216
  15. package/dist/lib/api/cam-api.js +85 -82
  16. package/dist/lib/api/cam-api.js.map +1 -1
  17. package/dist/lib/api/global-api.js +81 -81
  18. package/dist/lib/api/idm-api.js +85 -82
  19. package/dist/lib/api/idm-api.js.map +1 -1
  20. package/dist/lib/bruce-models.js +86 -86
  21. package/dist/lib/calculator/calculator.js +341 -341
  22. package/dist/lib/client-file/client-file.js +249 -245
  23. package/dist/lib/client-file/client-file.js.map +1 -1
  24. package/dist/lib/common/bounds.js +61 -61
  25. package/dist/lib/common/bruce-event.js +46 -46
  26. package/dist/lib/common/bruce-variable.js +60 -60
  27. package/dist/lib/common/cache.js +165 -165
  28. package/dist/lib/common/camera.js +11 -11
  29. package/dist/lib/common/cartes.js +123 -123
  30. package/dist/lib/common/carto.js +60 -60
  31. package/dist/lib/common/color.js +86 -86
  32. package/dist/lib/common/delay-queue.js +56 -56
  33. package/dist/lib/common/dictionary.js +2 -2
  34. package/dist/lib/common/geometry.js +120 -120
  35. package/dist/lib/common/transform.js +2 -2
  36. package/dist/lib/common/utc.js +39 -39
  37. package/dist/lib/custom-form/custom-form-content.js +26 -26
  38. package/dist/lib/custom-form/custom-form.js +114 -106
  39. package/dist/lib/custom-form/custom-form.js.map +1 -1
  40. package/dist/lib/data-lab/data-lab.js +49 -49
  41. package/dist/lib/entity/entity-attachment-type.js +87 -83
  42. package/dist/lib/entity/entity-attachment-type.js.map +1 -1
  43. package/dist/lib/entity/entity-attachment.js +126 -122
  44. package/dist/lib/entity/entity-attachment.js.map +1 -1
  45. package/dist/lib/entity/entity-attribute.js +29 -29
  46. package/dist/lib/entity/entity-comment.js +86 -82
  47. package/dist/lib/entity/entity-comment.js.map +1 -1
  48. package/dist/lib/entity/entity-coords.js +153 -149
  49. package/dist/lib/entity/entity-coords.js.map +1 -1
  50. package/dist/lib/entity/entity-link.js +86 -82
  51. package/dist/lib/entity/entity-link.js.map +1 -1
  52. package/dist/lib/entity/entity-lod-category.js +108 -100
  53. package/dist/lib/entity/entity-lod-category.js.map +1 -1
  54. package/dist/lib/entity/entity-lod.js +197 -185
  55. package/dist/lib/entity/entity-lod.js.map +1 -1
  56. package/dist/lib/entity/entity-relation-type.js +117 -109
  57. package/dist/lib/entity/entity-relation-type.js.map +1 -1
  58. package/dist/lib/entity/entity-relation.js +140 -132
  59. package/dist/lib/entity/entity-relation.js.map +1 -1
  60. package/dist/lib/entity/entity-source.js +136 -136
  61. package/dist/lib/entity/entity-tag.js +157 -149
  62. package/dist/lib/entity/entity-tag.js.map +1 -1
  63. package/dist/lib/entity/entity-type-visual-settings.js +13 -13
  64. package/dist/lib/entity/entity-type.js +121 -113
  65. package/dist/lib/entity/entity-type.js.map +1 -1
  66. package/dist/lib/entity/entity.js +331 -319
  67. package/dist/lib/entity/entity.js.map +1 -1
  68. package/dist/lib/entity/getters/batched-data-getter.js +84 -84
  69. package/dist/lib/entity/getters/entity-filter-getter.js +306 -303
  70. package/dist/lib/entity/getters/entity-filter-getter.js.map +1 -1
  71. package/dist/lib/entity/getters/entity-globe.js +211 -211
  72. package/dist/lib/entity/getters/view-monitor.js +2 -2
  73. package/dist/lib/import/import-cad.js +53 -53
  74. package/dist/lib/import/import-csv.js +23 -23
  75. package/dist/lib/import/import-json.js +23 -23
  76. package/dist/lib/import/import-kml.js +23 -23
  77. package/dist/lib/import/imported-file.js +99 -91
  78. package/dist/lib/import/imported-file.js.map +1 -1
  79. package/dist/lib/internal/uploader.js +76 -76
  80. package/dist/lib/markup/markup.js +185 -185
  81. package/dist/lib/plugin/plugin.js +91 -83
  82. package/dist/lib/plugin/plugin.js.map +1 -1
  83. package/dist/lib/program-key/program-key.js +119 -111
  84. package/dist/lib/program-key/program-key.js.map +1 -1
  85. package/dist/lib/project/menu-item.js +121 -121
  86. package/dist/lib/project/project-view-bookmark.js +159 -147
  87. package/dist/lib/project/project-view-bookmark.js.map +1 -1
  88. package/dist/lib/project/project-view-legacy-tile.js +77 -77
  89. package/dist/lib/project/project-view-tile.js +125 -125
  90. package/dist/lib/project/project-view.js +141 -133
  91. package/dist/lib/project/project-view.js.map +1 -1
  92. package/dist/lib/project/zoom-control.js +17 -17
  93. package/dist/lib/server/message-broker.js +142 -142
  94. package/dist/lib/server/pending-action.js +71 -71
  95. package/dist/lib/server/task.js +63 -63
  96. package/dist/lib/style/style.js +125 -117
  97. package/dist/lib/style/style.js.map +1 -1
  98. package/dist/lib/tileset/tileset.js +431 -415
  99. package/dist/lib/tileset/tileset.js.map +1 -1
  100. package/dist/lib/user/permission.js +20 -20
  101. package/dist/lib/user/session.js +141 -137
  102. package/dist/lib/user/session.js.map +1 -1
  103. package/dist/lib/user/user-group.js +94 -94
  104. package/dist/lib/user/user.js +474 -446
  105. package/dist/lib/user/user.js.map +1 -1
  106. package/dist/lib/util/encrypt-utils.js +19 -19
  107. package/dist/lib/util/math-utils.js +40 -40
  108. package/dist/lib/util/object-utils.js +17 -17
  109. package/dist/lib/util/path-utils.js +61 -61
  110. package/dist/lib/util/url-utils.js +94 -94
  111. package/dist/types/api/abstract-api.d.ts +6 -0
  112. package/package.json +3 -2
@@ -1,447 +1,475 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.User = void 0;
13
- const api_1 = require("../api/api");
14
- /**
15
- * Describes the "User" concept within Bruce.
16
- * A user is either a login-user or an access-token.
17
- *
18
- * You cannot login using an access-token, but both concepts are-
19
- * treated the same way in terms of permissions and user groups.
20
- */
21
- var User;
22
- (function (User) {
23
- function GetCacheKey(userId, accountId) {
24
- if (!accountId) {
25
- accountId = "";
26
- }
27
- return api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Id + userId + api_1.Api.ECacheKey.Id + accountId;
28
- }
29
- User.GetCacheKey = GetCacheKey;
30
- function GetEmailCacheKey(email, accountId) {
31
- if (!accountId) {
32
- accountId = "";
33
- }
34
- return api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.UserEmail + email + api_1.Api.ECacheKey.Id + accountId;
35
- }
36
- User.GetEmailCacheKey = GetEmailCacheKey;
37
- function GetSettingsCacheKey(userId, appId) {
38
- return api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.UserSettings + userId + api_1.Api.ECacheKey.Id + appId;
39
- }
40
- User.GetSettingsCacheKey = GetSettingsCacheKey;
41
- let EType;
42
- (function (EType) {
43
- EType["User"] = "LOGIN_USER";
44
- EType["AccessToken"] = "ACCESS_TOKEN";
45
- })(EType = User.EType || (User.EType = {}));
46
- function Get(params) {
47
- return __awaiter(this, void 0, void 0, function* () {
48
- const { api, userId: id, accountId, req: reqParams } = params;
49
- if (!id) {
50
- throw ("ID is required.");
51
- }
52
- const cacheData = api.GetCacheItem(GetCacheKey(id, accountId), reqParams);
53
- if (cacheData) {
54
- return cacheData;
55
- }
56
- const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
57
- try {
58
- const data = yield api.GET(`user/${id}${accountId ? `?clientAccountID=${accountId}` : ""}`, reqParams);
59
- res({
60
- user: data
61
- });
62
- }
63
- catch (e) {
64
- rej(e);
65
- }
66
- }));
67
- api.Cache.Set(GetCacheKey(id, accountId), prom, api_1.Api.DEFAULT_CACHE_DURATION);
68
- return prom;
69
- });
70
- }
71
- User.Get = Get;
72
- function Update(params) {
73
- var _a;
74
- return __awaiter(this, void 0, void 0, function* () {
75
- const { api, user: data, req: reqParams } = params;
76
- if (!(data === null || data === void 0 ? void 0 : data.ID) || !(data === null || data === void 0 ? void 0 : data.Type)) {
77
- throw ("ID and Type are required.");
78
- }
79
- // When getting a user it is structured slightly different to when we update it.
80
- // We can make up the difference here.
81
- if ((_a = data.AccessPermissions) === null || _a === void 0 ? void 0 : _a.length) {
82
- for (let i = 0; i < data.AccessPermissions.length; i++) {
83
- const perm = data.AccessPermissions[i];
84
- if (!perm["UserGroup.ID"] && perm.UserGroups) {
85
- perm["UserGroup.ID"] = [];
86
- for (let j = 0; j < perm.UserGroups.length; j++) {
87
- perm["UserGroup.ID"].push(perm.UserGroups[j].ID);
88
- }
89
- }
90
- }
91
- }
92
- const res = yield api.POST(`user/${data.ID}`, data, reqParams);
93
- api.Cache.RemoveByContains(api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Id + data.ID);
94
- if (data.Type == EType.AccessToken) {
95
- api.Cache.RemoveByStartsWith(api_1.Api.ECacheKey.AccessToken + api_1.Api.ECacheKey.Account);
96
- }
97
- else if (data.Type == EType.User) {
98
- api.Cache.RemoveByStartsWith(api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Account);
99
- }
100
- return {
101
- user: res
102
- };
103
- });
104
- }
105
- User.Update = Update;
106
- /**
107
- * Loads settings for a given user + app.
108
- * The session's account id is used to separate settings between accounts.
109
- * @param params
110
- */
111
- function GetSettings(params) {
112
- return __awaiter(this, void 0, void 0, function* () {
113
- const { api, userId, appId, req } = params;
114
- if (!userId || !appId) {
115
- throw ("UserId and appId are required.");
116
- }
117
- const cacheData = api.GetCacheItem(GetSettingsCacheKey(userId, appId), req);
118
- if (cacheData) {
119
- return cacheData;
120
- }
121
- const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
122
- try {
123
- const data = yield api.GET(`user/${userId}/application/${appId}/settings`);
124
- const settings = (data === null || data === void 0 ? void 0 : data.Settings) ? data.Settings : {};
125
- res({
126
- settings: settings
127
- });
128
- }
129
- catch (e) {
130
- // It will complain if an app id hasn't been used yet.
131
- // We need to suppress that error.
132
- if (e && typeof e == "object") {
133
- if (e.ERROR && typeof e.ERROR == "object" && e.ERROR.Type == "NotFound") {
134
- res({
135
- settings: {}
136
- });
137
- return;
138
- }
139
- }
140
- rej(e);
141
- }
142
- }));
143
- api.Cache.Set(GetSettingsCacheKey(userId, appId), prom, api_1.Api.DEFAULT_CACHE_DURATION);
144
- return prom;
145
- });
146
- }
147
- User.GetSettings = GetSettings;
148
- /**
149
- * Updates settings for a given accId + user + app.
150
- * The settings update will replace the existing record, please ensure to merge with existing settings.
151
- * @param params
152
- */
153
- function UpdateSettings(params) {
154
- return __awaiter(this, void 0, void 0, function* () {
155
- const { api, accId, userId, appId, settings, req } = params;
156
- if (!accId || !userId || !appId) {
157
- throw ("accId, userId and appId are required.");
158
- }
159
- const postBody = {
160
- Settings: settings == null ? {} : settings,
161
- Application: appId,
162
- ClientAccountID: accId,
163
- UserID: userId
164
- };
165
- const prom = api.POST(`user/${userId}/application/${appId}/settings`, postBody, req);
166
- api.Cache.Remove(GetSettingsCacheKey(userId, appId));
167
- return prom;
168
- });
169
- }
170
- User.UpdateSettings = UpdateSettings;
171
- function GetUsernameAvailable(params) {
172
- return __awaiter(this, void 0, void 0, function* () {
173
- const { api, username, excludeUserId, req: reqParams } = params;
174
- if (!username) {
175
- throw ("Username is required.");
176
- }
177
- const req = api.GET(`user/loginavailable/${username}${excludeUserId ? "?excludeUserId=" + excludeUserId : ""}`, reqParams);
178
- const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
179
- try {
180
- const data = yield req;
181
- res({
182
- isAvailable: data.IsAvailable
183
- });
184
- }
185
- catch (e) {
186
- rej(e);
187
- }
188
- }));
189
- return prom;
190
- });
191
- }
192
- User.GetUsernameAvailable = GetUsernameAvailable;
193
- let LoginUser;
194
- (function (LoginUser) {
195
- function GetListCacheKey(accountId) {
196
- return api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Account + api_1.Api.ECacheKey.Id + accountId;
197
- }
198
- LoginUser.GetListCacheKey = GetListCacheKey;
199
- function GetByEmail(params) {
200
- return __awaiter(this, void 0, void 0, function* () {
201
- const { api, email, accountId, req: reqParams } = params;
202
- if (!email) {
203
- throw ("Email is required.");
204
- }
205
- const cacheData = api.GetCacheItem(GetEmailCacheKey(email, accountId), reqParams);
206
- if (cacheData) {
207
- return cacheData;
208
- }
209
- const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
210
- try {
211
- const data = yield api.GET(`userByEmail/${email}${accountId ? `?clientAccountID=${accountId}` : ""}`, reqParams);
212
- res({
213
- user: data
214
- });
215
- }
216
- catch (e) {
217
- rej(e);
218
- }
219
- }));
220
- api.Cache.Set(GetEmailCacheKey(email, accountId), prom, api_1.Api.DEFAULT_CACHE_DURATION);
221
- return prom;
222
- });
223
- }
224
- LoginUser.GetByEmail = GetByEmail;
225
- function GetList(params) {
226
- return __awaiter(this, void 0, void 0, function* () {
227
- let { api, accountId, exclusive, req: reqParams } = params;
228
- if (!accountId) {
229
- throw ("Client account ID is required.");
230
- }
231
- if (!exclusive) {
232
- exclusive = false;
233
- }
234
- const cacheData = exclusive ? null : api.GetCacheItem(GetListCacheKey(accountId), reqParams);
235
- if (cacheData) {
236
- return cacheData;
237
- }
238
- const req = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
239
- try {
240
- const data = yield api.GET(`users?clientAccountID=${accountId}${exclusive ? "&allExcludingClientAccount=true" : ""}`, reqParams);
241
- res({
242
- users: data.Items
243
- });
244
- }
245
- catch (e) {
246
- rej(e);
247
- }
248
- }));
249
- req.then((data) => {
250
- const users = data.users;
251
- for (let i = 0; i < users.length; i++) {
252
- api.Cache.Set(GetCacheKey(users[i].ID, accountId), {
253
- user: users[i]
254
- });
255
- }
256
- });
257
- if (!exclusive) {
258
- api.Cache.Set(GetListCacheKey(accountId), req, api_1.Api.DEFAULT_CACHE_DURATION);
259
- }
260
- return req;
261
- });
262
- }
263
- LoginUser.GetList = GetList;
264
- /**
265
- * Sends a signup email to the specified email address.
266
- * @param params
267
- * @returns
268
- */
269
- function Signup(params) {
270
- return __awaiter(this, void 0, void 0, function* () {
271
- const { api, email, req: reqParams } = params;
272
- if (!email) {
273
- throw ("Email is required.");
274
- }
275
- return api.POST("clientaccount/signup", {
276
- Email: email
277
- }, reqParams);
278
- });
279
- }
280
- LoginUser.Signup = Signup;
281
- /**
282
- * Sends a new signup email using a previous one's expired code.
283
- * @param params
284
- * @returns
285
- */
286
- function SignupResend(params) {
287
- return __awaiter(this, void 0, void 0, function* () {
288
- const { api, expiredCode, req: reqParams } = params;
289
- if (!expiredCode) {
290
- throw ("Expired activation code is required.");
291
- }
292
- return api.POST(`clientaccount/signup/resend?activationKey=${api_1.Api.Encode(expiredCode)}`, null, reqParams);
293
- });
294
- }
295
- LoginUser.SignupResend = SignupResend;
296
- /**
297
- * Completes signup using user data and a signup code.
298
- * @param params
299
- * @returns
300
- */
301
- function SignupComplete(params) {
302
- return __awaiter(this, void 0, void 0, function* () {
303
- let { api, code, user: data, accountId, req: reqParams } = params;
304
- if (!code || !(data === null || data === void 0 ? void 0 : data.Login) || !(data === null || data === void 0 ? void 0 : data.Password)) {
305
- throw ("Login, password, and activation code are required.");
306
- }
307
- data = JSON.parse(JSON.stringify(data));
308
- data.IsActivated = true;
309
- data.IsDisabled = false;
310
- data.key = code;
311
- const res = yield api.POST(`UserActivate/${accountId ? accountId : ""}`, data, reqParams);
312
- api.Cache.RemoveByContains(api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Id + res.ID);
313
- if (data.Email) {
314
- api.Cache.RemoveByContains(api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.UserEmail + data.Email);
315
- }
316
- return {
317
- user: res
318
- };
319
- });
320
- }
321
- LoginUser.SignupComplete = SignupComplete;
322
- /**
323
- * Sends a password reset email to the specified email address.
324
- * @param params
325
- * @returns user id associated with provided email.
326
- */
327
- function ForgotPassword(params) {
328
- return __awaiter(this, void 0, void 0, function* () {
329
- const { api, accountId, email, req: reqParams } = params;
330
- if (!accountId || !email) {
331
- throw ("Account ID and email are required.");
332
- }
333
- const req = api.POST(`UserForgotPassword/${accountId}`, {
334
- Email: email
335
- }, reqParams);
336
- const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
337
- try {
338
- const data = yield req;
339
- res({
340
- userId: data.ID
341
- });
342
- }
343
- catch (e) {
344
- rej(e);
345
- }
346
- }));
347
- return prom;
348
- });
349
- }
350
- LoginUser.ForgotPassword = ForgotPassword;
351
- /**
352
- * Completes password reset using a password reset code and new password.
353
- * @param params
354
- * @returns
355
- */
356
- function ForgotPasswordComplete(params) {
357
- return __awaiter(this, void 0, void 0, function* () {
358
- const { api, code, userId, password, req: reqParams } = params;
359
- const { user } = yield Get({
360
- api: api,
361
- userId: userId,
362
- accountId: "",
363
- req: reqParams
364
- });
365
- const res = yield api.POST("UserSetNameAndPassword", {
366
- ID: userId,
367
- Email: user.Email,
368
- ActivationCode: code,
369
- Password: password,
370
- FullName: user.FullName
371
- }, reqParams);
372
- return {
373
- user: res
374
- };
375
- });
376
- }
377
- LoginUser.ForgotPasswordComplete = ForgotPasswordComplete;
378
- })(LoginUser = User.LoginUser || (User.LoginUser = {}));
379
- let AccessToken;
380
- (function (AccessToken) {
381
- function GetListCacheKey(accountId) {
382
- return api_1.Api.ECacheKey.AccessToken + api_1.Api.ECacheKey.Account + api_1.Api.ECacheKey.Id + accountId;
383
- }
384
- AccessToken.GetListCacheKey = GetListCacheKey;
385
- function GetList(params) {
386
- return __awaiter(this, void 0, void 0, function* () {
387
- const { api, accountId, req: reqParams } = params;
388
- const cacheData = api.GetCacheItem(GetListCacheKey(accountId), reqParams);
389
- if (cacheData) {
390
- return cacheData;
391
- }
392
- const req = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
393
- try {
394
- const data = yield api.GET("usersByClientAccount/accessTokens", reqParams);
395
- res({
396
- users: data.Items
397
- });
398
- }
399
- catch (e) {
400
- rej(e);
401
- }
402
- }));
403
- req.then((data) => {
404
- const users = data.tokens;
405
- for (let i = 0; i < users.length; i++) {
406
- api.Cache.Set(GetCacheKey(users[i].ID, accountId), {
407
- user: users[i]
408
- });
409
- }
410
- });
411
- api.Cache.Set(GetListCacheKey(accountId), req, api_1.Api.DEFAULT_CACHE_DURATION);
412
- return req;
413
- });
414
- }
415
- AccessToken.GetList = GetList;
416
- function Delete(params) {
417
- return __awaiter(this, void 0, void 0, function* () {
418
- const { api, tokenId: id, req: reqParams } = params;
419
- yield api.DELETE(`accessToken/${id}`, reqParams);
420
- api.Cache.RemoveByContains(api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Id + id);
421
- api.Cache.RemoveByStartsWith(api_1.Api.ECacheKey.AccessToken + api_1.Api.ECacheKey.Account);
422
- });
423
- }
424
- AccessToken.Delete = Delete;
425
- function Create(params) {
426
- return __awaiter(this, void 0, void 0, function* () {
427
- let { api, token: data, req: reqParams } = params;
428
- if (!data) {
429
- data = {};
430
- }
431
- if (data.Type == EType.User) {
432
- throw ("User type is not allowed.");
433
- }
434
- if (!data.Type) {
435
- data.Type = EType.AccessToken;
436
- }
437
- data = yield api.POST("accessToken", data, reqParams);
438
- api.Cache.RemoveByStartsWith(api_1.Api.ECacheKey.AccessToken + api_1.Api.ECacheKey.Account);
439
- return {
440
- user: data
441
- };
442
- });
443
- }
444
- AccessToken.Create = Create;
445
- })(AccessToken = User.AccessToken || (User.AccessToken = {}));
446
- })(User = exports.User || (exports.User = {}));
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.User = void 0;
13
+ const api_1 = require("../api/api");
14
+ /**
15
+ * Describes the "User" concept within Bruce.
16
+ * A user is either a login-user or an access-token.
17
+ *
18
+ * You cannot login using an access-token, but both concepts are-
19
+ * treated the same way in terms of permissions and user groups.
20
+ */
21
+ var User;
22
+ (function (User) {
23
+ function GetCacheKey(userId, accountId) {
24
+ if (!accountId) {
25
+ accountId = "";
26
+ }
27
+ return api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Id + userId + api_1.Api.ECacheKey.Id + accountId;
28
+ }
29
+ User.GetCacheKey = GetCacheKey;
30
+ function GetEmailCacheKey(email, accountId) {
31
+ if (!accountId) {
32
+ accountId = "";
33
+ }
34
+ return api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.UserEmail + email + api_1.Api.ECacheKey.Id + accountId;
35
+ }
36
+ User.GetEmailCacheKey = GetEmailCacheKey;
37
+ function GetSettingsCacheKey(userId, appId) {
38
+ return api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.UserSettings + userId + api_1.Api.ECacheKey.Id + appId;
39
+ }
40
+ User.GetSettingsCacheKey = GetSettingsCacheKey;
41
+ let EType;
42
+ (function (EType) {
43
+ EType["User"] = "LOGIN_USER";
44
+ EType["AccessToken"] = "ACCESS_TOKEN";
45
+ })(EType = User.EType || (User.EType = {}));
46
+ function Get(params) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ const { api, userId: id, accountId, req: reqParams } = params;
49
+ if (!id) {
50
+ throw ("ID is required.");
51
+ }
52
+ const cacheData = api.GetCacheItem(GetCacheKey(id, accountId), reqParams);
53
+ if (cacheData) {
54
+ return cacheData;
55
+ }
56
+ const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
57
+ try {
58
+ const data = yield api.GET(`user/${id}${accountId ? `?clientAccountID=${accountId}` : ""}`, reqParams);
59
+ res({
60
+ user: data
61
+ });
62
+ }
63
+ catch (e) {
64
+ rej(e);
65
+ }
66
+ }));
67
+ api.SetCacheItem({
68
+ key: GetCacheKey(id, accountId),
69
+ value: prom,
70
+ req: reqParams
71
+ });
72
+ return prom;
73
+ });
74
+ }
75
+ User.Get = Get;
76
+ function Update(params) {
77
+ var _a;
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ const { api, user: data, req: reqParams } = params;
80
+ if (!(data === null || data === void 0 ? void 0 : data.ID) || !(data === null || data === void 0 ? void 0 : data.Type)) {
81
+ throw ("ID and Type are required.");
82
+ }
83
+ // When getting a user it is structured slightly different to when we update it.
84
+ // We can make up the difference here.
85
+ if ((_a = data.AccessPermissions) === null || _a === void 0 ? void 0 : _a.length) {
86
+ for (let i = 0; i < data.AccessPermissions.length; i++) {
87
+ const perm = data.AccessPermissions[i];
88
+ if (!perm["UserGroup.ID"] && perm.UserGroups) {
89
+ perm["UserGroup.ID"] = [];
90
+ for (let j = 0; j < perm.UserGroups.length; j++) {
91
+ perm["UserGroup.ID"].push(perm.UserGroups[j].ID);
92
+ }
93
+ }
94
+ }
95
+ }
96
+ const res = yield api.POST(`user/${data.ID}`, data, reqParams);
97
+ api.Cache.RemoveByContains(api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Id + data.ID);
98
+ if (data.Type == EType.AccessToken) {
99
+ api.Cache.RemoveByStartsWith(api_1.Api.ECacheKey.AccessToken + api_1.Api.ECacheKey.Account);
100
+ }
101
+ else if (data.Type == EType.User) {
102
+ api.Cache.RemoveByStartsWith(api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Account);
103
+ }
104
+ return {
105
+ user: res
106
+ };
107
+ });
108
+ }
109
+ User.Update = Update;
110
+ /**
111
+ * Loads settings for a given user + app.
112
+ * The session's account id is used to separate settings between accounts.
113
+ * @param params
114
+ */
115
+ function GetSettings(params) {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ const { api, userId, appId, req } = params;
118
+ if (!userId || !appId) {
119
+ throw ("UserId and appId are required.");
120
+ }
121
+ const cacheData = api.GetCacheItem(GetSettingsCacheKey(userId, appId), req);
122
+ if (cacheData) {
123
+ return cacheData;
124
+ }
125
+ const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
126
+ try {
127
+ const data = yield api.GET(`user/${userId}/application/${appId}/settings`);
128
+ const settings = (data === null || data === void 0 ? void 0 : data.Settings) ? data.Settings : {};
129
+ res({
130
+ settings: settings
131
+ });
132
+ }
133
+ catch (e) {
134
+ // It will complain if an app id hasn't been used yet.
135
+ // We need to suppress that error.
136
+ if (e && typeof e == "object") {
137
+ if (e.ERROR && typeof e.ERROR == "object" && e.ERROR.Type == "NotFound") {
138
+ res({
139
+ settings: {}
140
+ });
141
+ return;
142
+ }
143
+ }
144
+ rej(e);
145
+ }
146
+ }));
147
+ api.SetCacheItem({
148
+ key: GetSettingsCacheKey(userId, appId),
149
+ value: prom,
150
+ req: req
151
+ });
152
+ return prom;
153
+ });
154
+ }
155
+ User.GetSettings = GetSettings;
156
+ /**
157
+ * Updates settings for a given accId + user + app.
158
+ * The settings update will replace the existing record, please ensure to merge with existing settings.
159
+ * @param params
160
+ */
161
+ function UpdateSettings(params) {
162
+ return __awaiter(this, void 0, void 0, function* () {
163
+ const { api, accId, userId, appId, settings, req } = params;
164
+ if (!accId || !userId || !appId) {
165
+ throw ("accId, userId and appId are required.");
166
+ }
167
+ const postBody = {
168
+ Settings: settings == null ? {} : settings,
169
+ Application: appId,
170
+ ClientAccountID: accId,
171
+ UserID: userId
172
+ };
173
+ const prom = api.POST(`user/${userId}/application/${appId}/settings`, postBody, req);
174
+ api.Cache.Remove(GetSettingsCacheKey(userId, appId));
175
+ return prom;
176
+ });
177
+ }
178
+ User.UpdateSettings = UpdateSettings;
179
+ function GetUsernameAvailable(params) {
180
+ return __awaiter(this, void 0, void 0, function* () {
181
+ const { api, username, excludeUserId, req: reqParams } = params;
182
+ if (!username) {
183
+ throw ("Username is required.");
184
+ }
185
+ const req = api.GET(`user/loginavailable/${username}${excludeUserId ? "?excludeUserId=" + excludeUserId : ""}`, reqParams);
186
+ const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
187
+ try {
188
+ const data = yield req;
189
+ res({
190
+ isAvailable: data.IsAvailable
191
+ });
192
+ }
193
+ catch (e) {
194
+ rej(e);
195
+ }
196
+ }));
197
+ return prom;
198
+ });
199
+ }
200
+ User.GetUsernameAvailable = GetUsernameAvailable;
201
+ let LoginUser;
202
+ (function (LoginUser) {
203
+ function GetListCacheKey(accountId) {
204
+ return api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Account + api_1.Api.ECacheKey.Id + accountId;
205
+ }
206
+ LoginUser.GetListCacheKey = GetListCacheKey;
207
+ function GetByEmail(params) {
208
+ return __awaiter(this, void 0, void 0, function* () {
209
+ const { api, email, accountId, req: reqParams } = params;
210
+ if (!email) {
211
+ throw ("Email is required.");
212
+ }
213
+ const cacheData = api.GetCacheItem(GetEmailCacheKey(email, accountId), reqParams);
214
+ if (cacheData) {
215
+ return cacheData;
216
+ }
217
+ const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
218
+ try {
219
+ const data = yield api.GET(`userByEmail/${email}${accountId ? `?clientAccountID=${accountId}` : ""}`, reqParams);
220
+ res({
221
+ user: data
222
+ });
223
+ }
224
+ catch (e) {
225
+ rej(e);
226
+ }
227
+ }));
228
+ api.SetCacheItem({
229
+ key: GetEmailCacheKey(email, accountId),
230
+ value: prom,
231
+ req: reqParams
232
+ });
233
+ return prom;
234
+ });
235
+ }
236
+ LoginUser.GetByEmail = GetByEmail;
237
+ function GetList(params) {
238
+ return __awaiter(this, void 0, void 0, function* () {
239
+ let { api, accountId, exclusive, req: reqParams } = params;
240
+ if (!accountId) {
241
+ throw ("Client account ID is required.");
242
+ }
243
+ if (!exclusive) {
244
+ exclusive = false;
245
+ }
246
+ const cacheData = exclusive ? null : api.GetCacheItem(GetListCacheKey(accountId), reqParams);
247
+ if (cacheData) {
248
+ return cacheData;
249
+ }
250
+ const req = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
251
+ try {
252
+ const data = yield api.GET(`users?clientAccountID=${accountId}${exclusive ? "&allExcludingClientAccount=true" : ""}`, reqParams);
253
+ res({
254
+ users: data.Items
255
+ });
256
+ }
257
+ catch (e) {
258
+ rej(e);
259
+ }
260
+ }));
261
+ req.then((data) => {
262
+ const users = data.users;
263
+ for (let i = 0; i < users.length; i++) {
264
+ api.SetCacheItem({
265
+ key: GetCacheKey(users[i].ID, accountId),
266
+ value: {
267
+ user: users[i]
268
+ },
269
+ req: reqParams
270
+ });
271
+ }
272
+ });
273
+ if (!exclusive) {
274
+ api.SetCacheItem({
275
+ key: GetListCacheKey(accountId),
276
+ value: req,
277
+ req: reqParams
278
+ });
279
+ }
280
+ return req;
281
+ });
282
+ }
283
+ LoginUser.GetList = GetList;
284
+ /**
285
+ * Sends a signup email to the specified email address.
286
+ * @param params
287
+ * @returns
288
+ */
289
+ function Signup(params) {
290
+ return __awaiter(this, void 0, void 0, function* () {
291
+ const { api, email, req: reqParams } = params;
292
+ if (!email) {
293
+ throw ("Email is required.");
294
+ }
295
+ return api.POST("clientaccount/signup", {
296
+ Email: email
297
+ }, reqParams);
298
+ });
299
+ }
300
+ LoginUser.Signup = Signup;
301
+ /**
302
+ * Sends a new signup email using a previous one's expired code.
303
+ * @param params
304
+ * @returns
305
+ */
306
+ function SignupResend(params) {
307
+ return __awaiter(this, void 0, void 0, function* () {
308
+ const { api, expiredCode, req: reqParams } = params;
309
+ if (!expiredCode) {
310
+ throw ("Expired activation code is required.");
311
+ }
312
+ return api.POST(`clientaccount/signup/resend?activationKey=${api_1.Api.Encode(expiredCode)}`, null, reqParams);
313
+ });
314
+ }
315
+ LoginUser.SignupResend = SignupResend;
316
+ /**
317
+ * Completes signup using user data and a signup code.
318
+ * @param params
319
+ * @returns
320
+ */
321
+ function SignupComplete(params) {
322
+ return __awaiter(this, void 0, void 0, function* () {
323
+ let { api, code, user: data, accountId, req: reqParams } = params;
324
+ if (!code || !(data === null || data === void 0 ? void 0 : data.Login) || !(data === null || data === void 0 ? void 0 : data.Password)) {
325
+ throw ("Login, password, and activation code are required.");
326
+ }
327
+ data = JSON.parse(JSON.stringify(data));
328
+ data.IsActivated = true;
329
+ data.IsDisabled = false;
330
+ data.key = code;
331
+ const res = yield api.POST(`UserActivate/${accountId ? accountId : ""}`, data, reqParams);
332
+ api.Cache.RemoveByContains(api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Id + res.ID);
333
+ if (data.Email) {
334
+ api.Cache.RemoveByContains(api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.UserEmail + data.Email);
335
+ }
336
+ return {
337
+ user: res
338
+ };
339
+ });
340
+ }
341
+ LoginUser.SignupComplete = SignupComplete;
342
+ /**
343
+ * Sends a password reset email to the specified email address.
344
+ * @param params
345
+ * @returns user id associated with provided email.
346
+ */
347
+ function ForgotPassword(params) {
348
+ return __awaiter(this, void 0, void 0, function* () {
349
+ const { api, accountId, email, req: reqParams } = params;
350
+ if (!accountId || !email) {
351
+ throw ("Account ID and email are required.");
352
+ }
353
+ const req = api.POST(`UserForgotPassword/${accountId}`, {
354
+ Email: email
355
+ }, reqParams);
356
+ const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
357
+ try {
358
+ const data = yield req;
359
+ res({
360
+ userId: data.ID
361
+ });
362
+ }
363
+ catch (e) {
364
+ rej(e);
365
+ }
366
+ }));
367
+ return prom;
368
+ });
369
+ }
370
+ LoginUser.ForgotPassword = ForgotPassword;
371
+ /**
372
+ * Completes password reset using a password reset code and new password.
373
+ * @param params
374
+ * @returns
375
+ */
376
+ function ForgotPasswordComplete(params) {
377
+ return __awaiter(this, void 0, void 0, function* () {
378
+ const { api, code, userId, password, req: reqParams } = params;
379
+ const { user } = yield Get({
380
+ api: api,
381
+ userId: userId,
382
+ accountId: "",
383
+ req: reqParams
384
+ });
385
+ const res = yield api.POST("UserSetNameAndPassword", {
386
+ ID: userId,
387
+ Email: user.Email,
388
+ ActivationCode: code,
389
+ Password: password,
390
+ FullName: user.FullName
391
+ }, reqParams);
392
+ return {
393
+ user: res
394
+ };
395
+ });
396
+ }
397
+ LoginUser.ForgotPasswordComplete = ForgotPasswordComplete;
398
+ })(LoginUser = User.LoginUser || (User.LoginUser = {}));
399
+ let AccessToken;
400
+ (function (AccessToken) {
401
+ function GetListCacheKey(accountId) {
402
+ return api_1.Api.ECacheKey.AccessToken + api_1.Api.ECacheKey.Account + api_1.Api.ECacheKey.Id + accountId;
403
+ }
404
+ AccessToken.GetListCacheKey = GetListCacheKey;
405
+ function GetList(params) {
406
+ return __awaiter(this, void 0, void 0, function* () {
407
+ const { api, accountId, req: reqParams } = params;
408
+ const cacheData = api.GetCacheItem(GetListCacheKey(accountId), reqParams);
409
+ if (cacheData) {
410
+ return cacheData;
411
+ }
412
+ const req = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
413
+ try {
414
+ const data = yield api.GET("usersByClientAccount/accessTokens", reqParams);
415
+ res({
416
+ users: data.Items
417
+ });
418
+ }
419
+ catch (e) {
420
+ rej(e);
421
+ }
422
+ }));
423
+ req.then((data) => {
424
+ const users = data.tokens;
425
+ for (let i = 0; i < users.length; i++) {
426
+ api.SetCacheItem({
427
+ key: GetCacheKey(users[i].ID, accountId),
428
+ value: {
429
+ user: users[i]
430
+ },
431
+ req: reqParams
432
+ });
433
+ }
434
+ });
435
+ api.SetCacheItem({
436
+ key: GetListCacheKey(accountId),
437
+ value: req,
438
+ req: reqParams
439
+ });
440
+ return req;
441
+ });
442
+ }
443
+ AccessToken.GetList = GetList;
444
+ function Delete(params) {
445
+ return __awaiter(this, void 0, void 0, function* () {
446
+ const { api, tokenId: id, req: reqParams } = params;
447
+ yield api.DELETE(`accessToken/${id}`, reqParams);
448
+ api.Cache.RemoveByContains(api_1.Api.ECacheKey.User + api_1.Api.ECacheKey.Id + id);
449
+ api.Cache.RemoveByStartsWith(api_1.Api.ECacheKey.AccessToken + api_1.Api.ECacheKey.Account);
450
+ });
451
+ }
452
+ AccessToken.Delete = Delete;
453
+ function Create(params) {
454
+ return __awaiter(this, void 0, void 0, function* () {
455
+ let { api, token: data, req: reqParams } = params;
456
+ if (!data) {
457
+ data = {};
458
+ }
459
+ if (data.Type == EType.User) {
460
+ throw ("User type is not allowed.");
461
+ }
462
+ if (!data.Type) {
463
+ data.Type = EType.AccessToken;
464
+ }
465
+ data = yield api.POST("accessToken", data, reqParams);
466
+ api.Cache.RemoveByStartsWith(api_1.Api.ECacheKey.AccessToken + api_1.Api.ECacheKey.Account);
467
+ return {
468
+ user: data
469
+ };
470
+ });
471
+ }
472
+ AccessToken.Create = Create;
473
+ })(AccessToken = User.AccessToken || (User.AccessToken = {}));
474
+ })(User = exports.User || (exports.User = {}));
447
475
  //# sourceMappingURL=user.js.map