bruce-models 4.1.5 → 4.1.7

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.
@@ -30,17 +30,18 @@ var AccountFeatures;
30
30
  EFeature["EntityLexicalSearch"] = "Feature_Entity_Lexical_Search";
31
31
  })(EFeature = AccountFeatures.EFeature || (AccountFeatures.EFeature = {}));
32
32
  /**
33
- * Gets account features corresponding to an account ID.
33
+ * Gets account features corresponding to the API instance's account.
34
34
  * @param params
35
35
  * @returns
36
36
  */
37
37
  function Get(params) {
38
38
  return __awaiter(this, void 0, void 0, function* () {
39
- let { api, accountId: id, req: reqParams } = params;
39
+ let { api, req: reqParams } = params;
40
40
  if (!api) {
41
41
  api = environment_1.ENVIRONMENT.Api().GetBruceApi();
42
42
  }
43
- const cache = yield api.GetCacheItem(GetCacheKey(id), reqParams);
43
+ const accountId = api.AccountId;
44
+ const cache = yield api.GetCacheItem(GetCacheKey(accountId), reqParams);
44
45
  if (cache === null || cache === void 0 ? void 0 : cache.found) {
45
46
  return cache.data;
46
47
  }
@@ -57,7 +58,7 @@ var AccountFeatures;
57
58
  }
58
59
  }));
59
60
  api.SetCacheItem({
60
- key: GetCacheKey(id),
61
+ key: GetCacheKey(accountId),
61
62
  value: prom,
62
63
  req: reqParams
63
64
  });
@@ -67,10 +68,11 @@ var AccountFeatures;
67
68
  AccountFeatures.Get = Get;
68
69
  function Update(params) {
69
70
  return __awaiter(this, void 0, void 0, function* () {
70
- let { api, accountId: id, features, req: reqParams } = params;
71
+ let { api, features, req: reqParams } = params;
71
72
  if (!api) {
72
73
  api = environment_1.ENVIRONMENT.Api().GetBruceApi();
73
74
  }
75
+ const accountId = api.AccountId;
74
76
  const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
75
77
  try {
76
78
  const data = yield api.POST("features", {
@@ -85,7 +87,7 @@ var AccountFeatures;
85
87
  rej(e);
86
88
  }
87
89
  }));
88
- api.Cache.Remove(GetCacheKey(id));
90
+ api.Cache.Remove(GetCacheKey(accountId));
89
91
  return prom;
90
92
  });
91
93
  }
@@ -1 +1 @@
1
- {"version":3,"file":"account-features.js","sourceRoot":"","sources":["../../../src/account/account-features.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oCAAiC;AAEjC,gDAA6C;AAE7C;;;GAGG;AACH,IAAiB,eAAe,CA0G/B;AA1GD,WAAiB,eAAe;IAC5B,IAAY,QASX;IATD,WAAY,QAAQ;QAChB,4DAA4D;QAC5D,8FAA8F;QAC9F,sDAA0C,CAAA;QAC1C,yDAAyD;QACzD,yCAAyC;QACzC,6EAA6E;QAC7E,iIAAiI;QACjI,iEAAqD,CAAA;IACzD,CAAC,EATW,QAAQ,GAAR,wBAAQ,KAAR,wBAAQ,QASnB;IAED;;;;OAIG;IACH,SAAsB,GAAG,CAAC,MAKzB;;YAKG,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,EAAE,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,MAAM,CAAC;YACpD,IAAI,CAAC,GAAG,EAAE;gBACN,GAAG,GAAG,yBAAW,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAC;aACzC;YAED,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,YAAY,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;YACjE,IAAI,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,KAAK,EAAE;gBACd,OAAO,KAAK,CAAC,IAAI,CAAC;aACrB;YACD,MAAM,IAAI,GAAiB,IAAI,OAAO,CAAC,CAAO,GAAG,EAAE,GAAG,EAAE,EAAE;gBACtD,IAAI;oBACA,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,GAAG,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;oBAClD,MAAM,QAAQ,GAAG,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,QAAQ,CAAC;oBAChC,GAAG,CAAC;wBACA,QAAQ,EAAE,QAAQ,C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QAChB,CAAC;KAAA;IAnCqB,sBAAM,SAmC3B,CAAA;IAED;;;;;;;;;OASG;IACH,SAAgB,WAAW,CAAC,SAAiB;QACzC,OAAO,SAAG,CAAC,SAAS,CAAC,eAAe,GAAG,SAAG,CAAC,SAAS,CAAC,EAAE,GAAG,SAAS,CAAC;IACxE,CAAC;IAFe,2BAAW,cAE1B,CAAA;AACL,CAAC,EA1GgB,eAAe,GAAf,uBAAe,KAAf,uBAAe,QA0G/B"}
1
+ {"version":3,"file":"account-features.js","sourceRoot":"","sources":["../../../src/account/account-features.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oCAAiC;AAEjC,gDAA6C;AAE7C;;;GAGG;AACH,IAAiB,eAAe,CAwG/B;AAxGD,WAAiB,eAAe;IAC5B,IAAY,QASX;IATD,WAAY,QAAQ;QAChB,4DAA4D;QAC5D,8FAA8F;QAC9F,sDAA0C,CAAA;QAC1C,yDAAyD;QACzD,yCAAyC;QACzC,6EAA6E;QAC7E,iIAAiI;QACjI,iEAAqD,CAAA;IACzD,CAAC,EATW,QAAQ,GAAR,wBAAQ,KAAR,wBAAQ,QASnB;IAED;;;;OAIG;IACH,SAAsB,GAAG,CAAC,MAGzB;;YAKG,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,MAAM,CAAC;YACrC,IAAI,CAAC,GAAG,EAAE;gBACN,GAAG,GAAG,yBAAW,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAC;aACzC;YAED,MAAM,SAAS,GAAG,GAAG,CAAC,SAAS,CAAC;YAChC,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,YAAY,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC,CAAC;YACxE,IAAI,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,KAAK,EAAE;gBACd,OAAO,KAAK,CAAC,IAAI,CAAC;aACrB;YACD,MAAM,IAAI,GAAiB,IAAI,OAAO,CAAC,CAAO,GAAG,EA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,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;qBACnC,CAAC,CAAC;iBACN;gBACD,OAAO,CAAC,EAAE;oBACN,GAAG,CAAC,CAAC,CAAC,CAAC;iBACV;YACL,CAAC,CAAA,CAAC,CAAC;YACH,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC;YACzC,OAAO,IAAI,CAAC;QAChB,CAAC;KAAA;IAlCqB,sBAAM,SAkC3B,CAAA;IAED;;;;;;;;;OASG;IACH,SAAgB,WAAW,CAAC,SAAiB;QACzC,OAAO,SAAG,CAAC,SAAS,CAAC,eAAe,GAAG,SAAG,CAAC,SAAS,CAAC,EAAE,GAAG,SAAS,CAAC;IACxE,CAAC;IAFe,2BAAW,cAE1B,CAAA;AACL,CAAC,EAxGgB,eAAe,GAAf,uBAAe,KAAf,uBAAe,QAwG/B"}
@@ -0,0 +1,617 @@
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.AccountLimits = void 0;
13
+ const bruce_models_1 = require("../bruce-models");
14
+ var AccountLimits;
15
+ (function (AccountLimits) {
16
+ let ELimit;
17
+ (function (ELimit) {
18
+ ELimit["MaxProjectViewCount"] = "MaxProjectViewCount";
19
+ ELimit["MaxStorageUseGigabytes"] = "MaxStorageUseGigabytes";
20
+ ELimit["MaxEntitiesCount"] = "MaxEntitiesCount";
21
+ ELimit["MaxFileImportSizeGigabytes"] = "MaxFileImportSizeGigabytes";
22
+ ELimit["OperatorAccessAllowed"] = "OperatorAccessAllowed";
23
+ ELimit["DenyPublicAccess"] = "DenyPublicAccess";
24
+ ELimit["EnableSSOLogin"] = "EnableSSOLogin";
25
+ ELimit["EnableSSOLoginAutoRedirect"] = "EnableSSOLoginAutoRedirect";
26
+ })(ELimit = AccountLimits.ELimit || (AccountLimits.ELimit = {}));
27
+ /**
28
+ * Returns a dictionary of limits for the given account.
29
+ * @param params
30
+ * @returns
31
+ */
32
+ function GetLimits(params) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ let { accountId, api, req } = params;
35
+ if (!api) {
36
+ api = bruce_models_1.ENVIRONMENT.Api().GetGuardianApi();
37
+ }
38
+ req = bruce_models_1.Api.PrepReqParams(req);
39
+ const cacheData = api.GetCacheItem(GetLimitsCacheKey(accountId));
40
+ if (cacheData === null || cacheData === void 0 ? void 0 : cacheData.found) {
41
+ return cacheData.data;
42
+ }
43
+ const prom = new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
44
+ try {
45
+ const data = yield api.GET(`account/${accountId}?WithLimits=true`);
46
+ const limits = data.Limits ? data.Limits : {};
47
+ res({
48
+ limits: limits
49
+ });
50
+ }
51
+ catch (e) {
52
+ rej(e);
53
+ }
54
+ }));
55
+ api.SetCacheItem({
56
+ key: GetLimitsCacheKey(accountId),
57
+ value: prom,
58
+ req: req
59
+ });
60
+ return prom;
61
+ });
62
+ }
63
+ AccountLimits.GetLimits = GetLimits;
64
+ /**
65
+ * Returns a numeric limit for the given account.
66
+ * @param params
67
+ */
68
+ function GetNumericLimit(params) {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ const { key, api, req, onErrorValue, onEmptyValue, accountId } = params;
71
+ try {
72
+ const { limits } = yield GetLimits({
73
+ accountId: accountId,
74
+ api: api,
75
+ req: req
76
+ });
77
+ const limit = limits[key];
78
+ if (limit == null || limit.Numeric == null) {
79
+ return onEmptyValue;
80
+ }
81
+ return Number(limit.Numeric);
82
+ }
83
+ catch (e) {
84
+ return onErrorValue;
85
+ }
86
+ });
87
+ }
88
+ AccountLimits.GetNumericLimit = GetNumericLimit;
89
+ /**
90
+ * Returns a boolean limit for the given account.
91
+ * @param params
92
+ */
93
+ function GetBooleanLimit(params) {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ const { key, api, req, onErrorValue, onEmptyValue, accountId } = params;
96
+ try {
97
+ const { limits } = yield GetLimits({
98
+ accountId: accountId,
99
+ api: api,
100
+ req: req
101
+ });
102
+ const limit = limits[key];
103
+ if (limit == null || limit.Boolean == null) {
104
+ return onEmptyValue;
105
+ }
106
+ return Boolean(limit.Boolean);
107
+ }
108
+ catch (e) {
109
+ return onErrorValue;
110
+ }
111
+ });
112
+ }
113
+ AccountLimits.GetBooleanLimit = GetBooleanLimit;
114
+ /**
115
+ * Returns a string limit for the given account.
116
+ * @param params
117
+ */
118
+ function GetStringLimit(params) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ const { key, api, req, onErrorValue, onEmptyValue, accountId } = params;
121
+ try {
122
+ const { limits } = yield GetLimits({
123
+ accountId: accountId,
124
+ api: api,
125
+ req: req
126
+ });
127
+ const limit = limits[key];
128
+ if (limit == null || limit.String == null) {
129
+ return onEmptyValue;
130
+ }
131
+ return limit.String;
132
+ }
133
+ catch (e) {
134
+ return onErrorValue;
135
+ }
136
+ });
137
+ }
138
+ AccountLimits.GetStringLimit = GetStringLimit;
139
+ /**
140
+ * Functions for validating if a user can do things sit here.
141
+ */
142
+ let Assert;
143
+ (function (Assert) {
144
+ /**
145
+ * Returns if project view creation is allowed for the account.
146
+ */
147
+ function GetCanCreateProjectView(params) {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ if (!params.getters) {
150
+ params.getters = bruce_models_1.ENVIRONMENT.Api();
151
+ }
152
+ if (!params.accountId) {
153
+ params.accountId = params.getters.GetAccountId();
154
+ }
155
+ let current = -1;
156
+ let max = yield GetNumericLimit({
157
+ accountId: params.accountId,
158
+ key: ELimit.MaxProjectViewCount,
159
+ onEmptyValue: 0,
160
+ onErrorValue: null,
161
+ api: params.getters.GetGuardianApi()
162
+ });
163
+ if (max != null) {
164
+ // Should use proper statistic API as this will exclude project views the user cannot access.
165
+ try {
166
+ const { views } = yield bruce_models_1.ProjectView.GetList({
167
+ api: params.getters.GetBruceApi({
168
+ accountId: params.accountId
169
+ })
170
+ });
171
+ current = views.length;
172
+ }
173
+ catch (e) {
174
+ console.error(e);
175
+ max = null;
176
+ }
177
+ }
178
+ if (max == null) {
179
+ return {
180
+ allowed: false,
181
+ errorText: "We apologize but Project View creation is currently unavailable."
182
+ };
183
+ }
184
+ else if (max == -1) {
185
+ return {
186
+ allowed: true
187
+ };
188
+ }
189
+ else if (current == -1) {
190
+ return {
191
+ allowed: false,
192
+ errorText: "We apologize but Project View creation is currently unavailable."
193
+ };
194
+ }
195
+ else if (current >= max) {
196
+ return {
197
+ allowed: false,
198
+ errorText: `You have reached the maximum number of Project Views allowed for your account. Your limit is ${max} Project ${max == 1 ? "View" : "Views"} and you have ${current}.`
199
+ };
200
+ }
201
+ return {
202
+ allowed: true
203
+ };
204
+ });
205
+ }
206
+ Assert.GetCanCreateProjectView = GetCanCreateProjectView;
207
+ /**
208
+ * Returns the project view limit for an account.
209
+ * If -1 is returned then it is unlimited.
210
+ * @param params
211
+ * @returns
212
+ */
213
+ function GetProjectViewLimit(params) {
214
+ return __awaiter(this, void 0, void 0, function* () {
215
+ if (!params.api) {
216
+ params.api = bruce_models_1.ENVIRONMENT.Api().GetGuardianApi();
217
+ }
218
+ if (!params.accountId) {
219
+ params.accountId = bruce_models_1.ENVIRONMENT.Api().GetAccountId();
220
+ }
221
+ const max = yield GetNumericLimit({
222
+ accountId: params.accountId,
223
+ key: ELimit.MaxProjectViewCount,
224
+ onEmptyValue: 0,
225
+ onErrorValue: null,
226
+ api: params.api
227
+ });
228
+ // Our error so we'll just allow unlimited for the time being.
229
+ if (max == null) {
230
+ return -1;
231
+ }
232
+ return max;
233
+ });
234
+ }
235
+ Assert.GetProjectViewLimit = GetProjectViewLimit;
236
+ /**
237
+ * Returns if file uploads are allowed for the account.
238
+ */
239
+ function GetCanUploadFile(params) {
240
+ return __awaiter(this, void 0, void 0, function* () {
241
+ if (!params.getters) {
242
+ params.getters = bruce_models_1.ENVIRONMENT.Api();
243
+ }
244
+ if (!params.accountId) {
245
+ params.accountId = params.getters.GetAccountId();
246
+ }
247
+ let current = -1;
248
+ let max = yield GetNumericLimit({
249
+ accountId: params.accountId,
250
+ key: ELimit.MaxStorageUseGigabytes,
251
+ onEmptyValue: 0,
252
+ onErrorValue: null,
253
+ api: params.getters.GetGuardianApi()
254
+ });
255
+ if (max != null && max > 0) {
256
+ try {
257
+ const api = params.getters.GetBruceApi({
258
+ accountId: params.accountId
259
+ });
260
+ const stats = yield api.GET("account/summary-stats");
261
+ if (stats === null || stats === void 0 ? void 0 : stats.ID) {
262
+ const result = stats === null || stats === void 0 ? void 0 : stats.Result;
263
+ try {
264
+ const resJson = JSON.parse(result);
265
+ const fileBytes = resJson.TotalStorageUseBytes;
266
+ const fileGB = fileBytes != null && !isNaN(fileBytes) && fileBytes != -1 ? bruce_models_1.MathUtils.Round(fileBytes / 1024 / 1024 / 1024, 2) : null;
267
+ // Still counting so we'll give unlimited for the time being.
268
+ // This count can take ages so we won't make the user wait.
269
+ if (fileGB == null || isNaN(fileGB)) {
270
+ max = -1;
271
+ }
272
+ else {
273
+ current = fileGB;
274
+ }
275
+ }
276
+ // We won't count this as a fatal error because who knows how stable this is.
277
+ catch (e) {
278
+ console.error(e);
279
+ }
280
+ }
281
+ // Unknown so we'll just give unlimited for the time being.
282
+ else {
283
+ max = -1;
284
+ }
285
+ }
286
+ catch (e) {
287
+ console.error(e);
288
+ max = null;
289
+ }
290
+ }
291
+ // Optional param for a file size to add to the current storage amount.
292
+ // Eg: user is uploading a 3gb file, is that allowed right now?
293
+ if (current != -1 && !isNaN(params.fileSize) && params.fileSize) {
294
+ current += bruce_models_1.MathUtils.Round(params.fileSize / 1024 / 1024 / 1024, 3);
295
+ }
296
+ if (max == null) {
297
+ return {
298
+ allowed: false,
299
+ errorText: "We apologize but file uploads are currently unavailable."
300
+ };
301
+ }
302
+ else if (max == -1) {
303
+ return {
304
+ allowed: true
305
+ };
306
+ }
307
+ else if (current == -1) {
308
+ return {
309
+ allowed: false,
310
+ errorText: "We apologize but file uploads are currently unavailable."
311
+ };
312
+ }
313
+ else if (current >= max) {
314
+ return {
315
+ allowed: false,
316
+ errorText: `You have reached the maximum amount of storage allowed for your account. You are limited to ${max.toLocaleString()} ${max == 1 ? "GB" : "GBs"} of storage.`
317
+ };
318
+ }
319
+ return {
320
+ allowed: true
321
+ };
322
+ });
323
+ }
324
+ Assert.GetCanUploadFile = GetCanUploadFile;
325
+ /**
326
+ * Returns the file limit for an account.
327
+ * If -1 is returned then it is unlimited.
328
+ * @param params
329
+ * @returns
330
+ */
331
+ function GetFileGBLimit(params) {
332
+ return __awaiter(this, void 0, void 0, function* () {
333
+ if (!params.api) {
334
+ params.api = bruce_models_1.ENVIRONMENT.Api().GetGuardianApi();
335
+ }
336
+ if (!params.accountId) {
337
+ params.accountId = bruce_models_1.ENVIRONMENT.Api().GetAccountId();
338
+ }
339
+ const max = yield GetNumericLimit({
340
+ accountId: params.accountId,
341
+ key: ELimit.MaxStorageUseGigabytes,
342
+ onEmptyValue: 0,
343
+ onErrorValue: null,
344
+ api: params.api
345
+ });
346
+ // Our error so we'll just allow unlimited for the time being.
347
+ if (max == null) {
348
+ return -1;
349
+ }
350
+ return max;
351
+ });
352
+ }
353
+ Assert.GetFileGBLimit = GetFileGBLimit;
354
+ /**
355
+ * Returns the maximum file import size for an account.
356
+ * If -1 is returned then it is unlimited.
357
+ * @param params
358
+ * @returns
359
+ */
360
+ function GetMaxImportSizeGB(params) {
361
+ return __awaiter(this, void 0, void 0, function* () {
362
+ if (!params.api) {
363
+ params.api = bruce_models_1.ENVIRONMENT.Api().GetGuardianApi();
364
+ }
365
+ if (!params.accountId) {
366
+ params.accountId = bruce_models_1.ENVIRONMENT.Api().GetAccountId();
367
+ }
368
+ const max = yield GetNumericLimit({
369
+ accountId: params.accountId,
370
+ key: ELimit.MaxFileImportSizeGigabytes,
371
+ onEmptyValue: 0,
372
+ onErrorValue: null,
373
+ api: params.api
374
+ });
375
+ // Our error so we'll just allow unlimited for the time being.
376
+ if (max == null) {
377
+ return -1;
378
+ }
379
+ return max;
380
+ });
381
+ }
382
+ Assert.GetMaxImportSizeGB = GetMaxImportSizeGB;
383
+ /**
384
+ * Returns if entity creation is allowed for the account.
385
+ */
386
+ function GetCanCreateEntity(params) {
387
+ return __awaiter(this, void 0, void 0, function* () {
388
+ if (!params.getters) {
389
+ params.getters = bruce_models_1.ENVIRONMENT.Api();
390
+ }
391
+ if (!params.accountId) {
392
+ params.accountId = params.getters.GetAccountId();
393
+ }
394
+ let current = -1;
395
+ let max = yield GetNumericLimit({
396
+ accountId: params.accountId,
397
+ key: ELimit.MaxEntitiesCount,
398
+ onEmptyValue: 0,
399
+ onErrorValue: null,
400
+ api: params.getters.GetGuardianApi()
401
+ });
402
+ if (max != null && max > 0) {
403
+ try {
404
+ const api = params.getters.GetBruceApi({
405
+ accountId: params.accountId
406
+ });
407
+ const stats = yield api.GET("account/summary-stats");
408
+ if (stats === null || stats === void 0 ? void 0 : stats.ID) {
409
+ const result = stats === null || stats === void 0 ? void 0 : stats.Result;
410
+ try {
411
+ const resJson = JSON.parse(result);
412
+ const eCount = resJson.EntitiesCount;
413
+ // Still counting so we'll give unlimited for the time being.
414
+ // This count can take ages so we won't make the user wait.
415
+ if (eCount == null || isNaN(eCount)) {
416
+ max = -1;
417
+ }
418
+ else {
419
+ current = eCount;
420
+ }
421
+ }
422
+ // We won't count this as a fatal error because who knows how stable this is.
423
+ catch (e) {
424
+ console.error(e);
425
+ }
426
+ }
427
+ // Unknown so we'll just give unlimited for the time being.
428
+ else {
429
+ max = -1;
430
+ }
431
+ }
432
+ catch (e) {
433
+ console.error(e);
434
+ max = null;
435
+ }
436
+ }
437
+ // Optional param for an entity count to add to the current entity amount.
438
+ // Eg: user is creating 3 entities, is that allowed right now?
439
+ if (current != -1 && !isNaN(params.count) && params.count) {
440
+ current += params.count;
441
+ // Remove one since we do an inclusive check later in the code.
442
+ current -= 1;
443
+ }
444
+ if (max == null) {
445
+ return {
446
+ allowed: false,
447
+ errorText: "We apologize but Entity creation is currently unavailable."
448
+ };
449
+ }
450
+ else if (max == -1) {
451
+ return {
452
+ allowed: true
453
+ };
454
+ }
455
+ else if (current == -1) {
456
+ return {
457
+ allowed: false,
458
+ errorText: "We apologize but Entity creation is currently unavailable."
459
+ };
460
+ }
461
+ else if (current >= max) {
462
+ return {
463
+ allowed: false,
464
+ errorText: `You have reached the maximum number of Entities allowed for your account. You are limited to ${max.toLocaleString()} ${max == 1 ? "Entity" : "Entities"}.`
465
+ };
466
+ }
467
+ return {
468
+ allowed: true
469
+ };
470
+ });
471
+ }
472
+ Assert.GetCanCreateEntity = GetCanCreateEntity;
473
+ /**
474
+ * Returns the entity limit for an account.
475
+ * If -1 is returned then it is unlimited.
476
+ */
477
+ function GetEntityLimit(params) {
478
+ return __awaiter(this, void 0, void 0, function* () {
479
+ if (!params.api) {
480
+ params.api = bruce_models_1.ENVIRONMENT.Api().GetGuardianApi();
481
+ }
482
+ if (!params.accountId) {
483
+ params.accountId = bruce_models_1.ENVIRONMENT.Api().GetAccountId();
484
+ }
485
+ const max = yield GetNumericLimit({
486
+ accountId: params.accountId,
487
+ key: ELimit.MaxEntitiesCount,
488
+ onEmptyValue: 0,
489
+ onErrorValue: null,
490
+ api: params.api
491
+ });
492
+ // Our error so we'll just allow unlimited for the time being.
493
+ if (max == null) {
494
+ return -1;
495
+ }
496
+ return max;
497
+ });
498
+ }
499
+ Assert.GetEntityLimit = GetEntityLimit;
500
+ /**
501
+ * Returns if Operator access is allowed for the account.
502
+ * @param params
503
+ * @returns
504
+ */
505
+ function GetCanOpenOperator(params) {
506
+ return __awaiter(this, void 0, void 0, function* () {
507
+ if (!params.api) {
508
+ params.api = bruce_models_1.ENVIRONMENT.Api().GetGuardianApi();
509
+ }
510
+ if (!params.accountId) {
511
+ params.accountId = bruce_models_1.ENVIRONMENT.Api().GetAccountId();
512
+ }
513
+ const allowed = yield GetBooleanLimit({
514
+ accountId: params.accountId,
515
+ key: ELimit.OperatorAccessAllowed,
516
+ onEmptyValue: false,
517
+ onErrorValue: null,
518
+ api: params.api
519
+ });
520
+ if (allowed == null) {
521
+ return {
522
+ allowed: false,
523
+ errorText: "We apologize but Operator access is currently unavailable."
524
+ };
525
+ }
526
+ else if (allowed) {
527
+ return {
528
+ allowed: true
529
+ };
530
+ }
531
+ else {
532
+ return {
533
+ allowed: false,
534
+ errorText: "You do not have access to Operator."
535
+ };
536
+ }
537
+ });
538
+ }
539
+ Assert.GetCanOpenOperator = GetCanOpenOperator;
540
+ /**
541
+ * Returns SSO login settings for the account.
542
+ * @param params
543
+ * @returns
544
+ */
545
+ function GetSSOLoginSettings(params) {
546
+ return __awaiter(this, void 0, void 0, function* () {
547
+ if (!params.api) {
548
+ params.api = bruce_models_1.ENVIRONMENT.Api().GetGuardianApi();
549
+ }
550
+ if (!params.accountId) {
551
+ params.accountId = bruce_models_1.ENVIRONMENT.Api().GetAccountId();
552
+ }
553
+ const enabled = yield GetBooleanLimit({
554
+ accountId: params.accountId,
555
+ key: ELimit.EnableSSOLogin,
556
+ onEmptyValue: false,
557
+ onErrorValue: null,
558
+ api: params.api
559
+ });
560
+ const autoRedirect = yield GetBooleanLimit({
561
+ accountId: params.accountId,
562
+ key: ELimit.EnableSSOLoginAutoRedirect,
563
+ onEmptyValue: false,
564
+ onErrorValue: null,
565
+ api: params.api
566
+ });
567
+ if (enabled == null || autoRedirect == null) {
568
+ return {
569
+ enabled: false,
570
+ autoRedirect: false,
571
+ errorText: "We apologize but SSO login is currently unavailable."
572
+ };
573
+ }
574
+ return {
575
+ enabled: enabled,
576
+ autoRedirect: autoRedirect
577
+ };
578
+ });
579
+ }
580
+ Assert.GetSSOLoginSettings = GetSSOLoginSettings;
581
+ /**
582
+ * Returns if public access is denied to this account.
583
+ * @param params
584
+ * @returns
585
+ */
586
+ function GetDenyPublicAccess(params) {
587
+ return __awaiter(this, void 0, void 0, function* () {
588
+ if (!params.api) {
589
+ params.api = bruce_models_1.ENVIRONMENT.Api().GetGuardianApi();
590
+ }
591
+ if (!params.accountId) {
592
+ params.accountId = bruce_models_1.ENVIRONMENT.Api().GetAccountId();
593
+ }
594
+ const enabled = yield GetBooleanLimit({
595
+ accountId: params.accountId,
596
+ key: ELimit.DenyPublicAccess,
597
+ // If empty then we'll assume we are not denying public access.
598
+ onEmptyValue: false,
599
+ // If we crash we are denying public access.
600
+ onErrorValue: true,
601
+ api: params.api
602
+ });
603
+ return enabled;
604
+ });
605
+ }
606
+ Assert.GetDenyPublicAccess = GetDenyPublicAccess;
607
+ })(Assert = AccountLimits.Assert || (AccountLimits.Assert = {}));
608
+ /**
609
+ * Cache key for requesting account limits for a specific account.
610
+ * @param accountId
611
+ */
612
+ function GetLimitsCacheKey(accountId) {
613
+ return bruce_models_1.Api.ECacheKey.AccountLimits + bruce_models_1.Api.ECacheKey.Id + accountId;
614
+ }
615
+ AccountLimits.GetLimitsCacheKey = GetLimitsCacheKey;
616
+ })(AccountLimits = exports.AccountLimits || (exports.AccountLimits = {}));
617
+ //# sourceMappingURL=account-limits.js.map