@nymphjs/tilmeld 1.0.0-beta.0 → 1.0.0-beta.2

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.
package/dist/Group.js CHANGED
@@ -1,13 +1,4 @@
1
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
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -17,30 +8,23 @@ const lodash_1 = require("lodash");
17
8
  const AbleObject_1 = __importDefault(require("./AbleObject"));
18
9
  const errors_1 = require("./errors");
19
10
  class Group extends AbleObject_1.default {
20
- static factory(guid) {
21
- const _super = Object.create(null, {
22
- factory: { get: () => super.factory }
23
- });
24
- return __awaiter(this, void 0, void 0, function* () {
25
- return (yield _super.factory.call(this, guid));
26
- });
11
+ static async factory(guid) {
12
+ return (await super.factory(guid));
27
13
  }
28
- static factoryGroupname(groupname) {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- const entity = new this();
31
- if (groupname != null) {
32
- const entity = yield this.nymph.getEntity({
33
- class: this,
34
- }, {
35
- type: '&',
36
- ilike: ['groupname', groupname.replace(/([\\%_])/g, (s) => `\\${s}`)],
37
- });
38
- if (entity != null) {
39
- return entity;
40
- }
14
+ static async factoryGroupname(groupname) {
15
+ const entity = new this();
16
+ if (groupname != null) {
17
+ const entity = await this.nymph.getEntity({
18
+ class: this,
19
+ }, {
20
+ type: '&',
21
+ ilike: ['groupname', groupname.replace(/([\\%_])/g, (s) => `\\${s}`)],
22
+ });
23
+ if (entity != null) {
24
+ return entity;
41
25
  }
42
- return entity;
43
- });
26
+ }
27
+ return entity;
44
28
  }
45
29
  static factorySync(guid) {
46
30
  return super.factorySync(guid);
@@ -67,43 +51,37 @@ class Group extends AbleObject_1.default {
67
51
  this.$updateDataProtection();
68
52
  }
69
53
  }
70
- static getPrimaryGroups(options, selectors) {
71
- return __awaiter(this, void 0, void 0, function* () {
72
- if (!this.tilmeld.gatekeeper('tilmeld/admin')) {
73
- throw new Error("You don't have permission to do that.");
74
- }
75
- return yield this.getAssignableGroups(this.tilmeld.config.highestPrimary, Object.assign(Object.assign({}, options), { class: this.tilmeld.Group, return: 'entity' }), [...(selectors !== null && selectors !== void 0 ? selectors : [])]);
76
- });
54
+ static async getPrimaryGroups(options, selectors) {
55
+ if (!this.tilmeld.gatekeeper('tilmeld/admin')) {
56
+ throw new Error("You don't have permission to do that.");
57
+ }
58
+ return await this.getAssignableGroups(this.tilmeld.config.highestPrimary, { ...options, class: this.tilmeld.Group, return: 'entity' }, [...(selectors ?? [])]);
77
59
  }
78
- static getSecondaryGroups(options, selectors) {
79
- return __awaiter(this, void 0, void 0, function* () {
80
- if (!this.tilmeld.gatekeeper('tilmeld/admin')) {
81
- throw new Error("You don't have permission to do that.");
82
- }
83
- return yield this.getAssignableGroups(this.tilmeld.config.highestSecondary, Object.assign(Object.assign({}, options), { class: this.tilmeld.Group, return: 'entity' }), [...(selectors !== null && selectors !== void 0 ? selectors : [])]);
84
- });
60
+ static async getSecondaryGroups(options, selectors) {
61
+ if (!this.tilmeld.gatekeeper('tilmeld/admin')) {
62
+ throw new Error("You don't have permission to do that.");
63
+ }
64
+ return await this.getAssignableGroups(this.tilmeld.config.highestSecondary, { ...options, class: this.tilmeld.Group, return: 'entity' }, [...(selectors ?? [])]);
85
65
  }
86
- static getAssignableGroups(highestParent, options, selectors) {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- let assignableGroups = [];
89
- if (highestParent === false) {
90
- return assignableGroups;
91
- }
92
- assignableGroups = yield this.nymph.getEntities(Object.assign(Object.assign({}, options), { class: this.tilmeld.Group }), ...selectors);
93
- if (highestParent !== true) {
94
- assignableGroups = assignableGroups.filter((group) => {
95
- let curGroup = group;
96
- while (curGroup.parent != null && curGroup.parent.cdate != null) {
97
- if (curGroup.parent.guid === highestParent) {
98
- return true;
99
- }
100
- curGroup = curGroup.parent;
101
- }
102
- return false;
103
- });
104
- }
66
+ static async getAssignableGroups(highestParent, options, selectors) {
67
+ let assignableGroups = [];
68
+ if (highestParent === false) {
105
69
  return assignableGroups;
106
- });
70
+ }
71
+ assignableGroups = await this.nymph.getEntities({ ...options, class: this.tilmeld.Group }, ...selectors);
72
+ if (highestParent !== true) {
73
+ assignableGroups = assignableGroups.filter((group) => {
74
+ let curGroup = group;
75
+ while (curGroup.parent != null && curGroup.parent.cdate != null) {
76
+ if (curGroup.parent.guid === highestParent) {
77
+ return true;
78
+ }
79
+ curGroup = curGroup.parent;
80
+ }
81
+ return false;
82
+ });
83
+ }
84
+ return assignableGroups;
107
85
  }
108
86
  $getAvatar() {
109
87
  if (this.$data.avatar != null && this.$data.avatar !== '') {
@@ -117,11 +95,10 @@ class Group extends AbleObject_1.default {
117
95
  '?d=identicon&s=40');
118
96
  }
119
97
  $jsonAcceptData(input, allowConflict = false) {
120
- var _a, _b;
121
98
  const tilmeld = this.$nymph.tilmeld;
122
99
  this.$referenceWake();
123
- if (((_a = input.data.abilities) === null || _a === void 0 ? void 0 : _a.indexOf('system/admin')) !== -1 &&
124
- ((_b = this.$data.abilities) === null || _b === void 0 ? void 0 : _b.indexOf('system/admin')) === -1 &&
100
+ if (input.data.abilities?.indexOf('system/admin') !== -1 &&
101
+ this.$data.abilities?.indexOf('system/admin') === -1 &&
125
102
  tilmeld.gatekeeper('tilmeld/admin') &&
126
103
  !tilmeld.gatekeeper('system/admin')) {
127
104
  throw new errors_1.BadDataError("You don't have the authority to make this group a system admin.");
@@ -129,11 +106,10 @@ class Group extends AbleObject_1.default {
129
106
  super.$jsonAcceptData(input, allowConflict);
130
107
  }
131
108
  $jsonAcceptPatch(patch, allowConflict = false) {
132
- var _a, _b;
133
109
  const tilmeld = this.$nymph.tilmeld;
134
110
  this.$referenceWake();
135
- if (((_a = patch.set.abilities) === null || _a === void 0 ? void 0 : _a.indexOf('system/admin')) !== -1 &&
136
- ((_b = this.$data.abilities) === null || _b === void 0 ? void 0 : _b.indexOf('system/admin')) === -1 &&
111
+ if (patch.set.abilities?.indexOf('system/admin') !== -1 &&
112
+ this.$data.abilities?.indexOf('system/admin') === -1 &&
137
113
  tilmeld.gatekeeper('tilmeld/admin') &&
138
114
  !tilmeld.gatekeeper('system/admin')) {
139
115
  throw new errors_1.BadDataError("You don't have the authority to make this group a system admin.");
@@ -145,17 +121,16 @@ class Group extends AbleObject_1.default {
145
121
  this.$updateDataProtection();
146
122
  }
147
123
  $updateDataProtection(givenUser) {
148
- var _a, _b, _c, _d;
149
124
  const tilmeld = this.$nymph.tilmeld;
150
- let user = givenUser !== null && givenUser !== void 0 ? givenUser : tilmeld.User.current();
125
+ let user = givenUser ?? tilmeld.User.current();
151
126
  this.$privateData = [...Group.DEFAULT_PRIVATE_DATA];
152
127
  this.$allowlistData = [...Group.DEFAULT_ALLOWLIST_DATA];
153
128
  if (tilmeld.config.emailUsernames) {
154
129
  this.$privateData.push('groupname');
155
130
  }
156
131
  if (user != null &&
157
- (((_b = (_a = user.abilities) === null || _a === void 0 ? void 0 : _a.indexOf('tilmeld/admin')) !== null && _b !== void 0 ? _b : -1) !== -1 ||
158
- ((_d = (_c = user.abilities) === null || _c === void 0 ? void 0 : _c.indexOf('system/admin')) !== null && _d !== void 0 ? _d : -1) !== -1)) {
132
+ ((user.abilities?.indexOf('tilmeld/admin') ?? -1) !== -1 ||
133
+ (user.abilities?.indexOf('system/admin') ?? -1) !== -1)) {
159
134
  this.$privateData = [];
160
135
  this.$allowlistData = undefined;
161
136
  }
@@ -188,33 +163,29 @@ class Group extends AbleObject_1.default {
188
163
  }
189
164
  return false;
190
165
  }
191
- $getChildren() {
192
- return __awaiter(this, void 0, void 0, function* () {
193
- const tilmeld = this.$nymph.tilmeld;
194
- return yield this.$nymph.getEntities({ class: tilmeld.Group }, {
195
- type: '&',
196
- equal: ['enabled', true],
197
- ref: ['parent', this],
198
- });
166
+ async $getChildren() {
167
+ const tilmeld = this.$nymph.tilmeld;
168
+ return await this.$nymph.getEntities({ class: tilmeld.Group }, {
169
+ type: '&',
170
+ equal: ['enabled', true],
171
+ ref: ['parent', this],
199
172
  });
200
173
  }
201
- $getDescendants(andSelf = false) {
202
- return __awaiter(this, void 0, void 0, function* () {
203
- const tilmeld = this.$nymph.tilmeld;
204
- let groups = [];
205
- const entities = yield this.$nymph.getEntities({ class: tilmeld.Group }, {
206
- type: '&',
207
- equal: ['enabled', true],
208
- ref: ['parent', this],
209
- });
210
- for (let entity of entities) {
211
- groups = (yield entity.$getDescendants(true)).concat(groups);
212
- }
213
- if (andSelf) {
214
- groups.push(this);
215
- }
216
- return groups;
174
+ async $getDescendants(andSelf = false) {
175
+ const tilmeld = this.$nymph.tilmeld;
176
+ let groups = [];
177
+ const entities = await this.$nymph.getEntities({ class: tilmeld.Group }, {
178
+ type: '&',
179
+ equal: ['enabled', true],
180
+ ref: ['parent', this],
217
181
  });
182
+ for (let entity of entities) {
183
+ groups = (await entity.$getDescendants(true)).concat(groups);
184
+ }
185
+ if (andSelf) {
186
+ groups.push(this);
187
+ }
188
+ return groups;
218
189
  }
219
190
  $getDescendantsSync(andSelf = false) {
220
191
  const tilmeld = this.$nymph.tilmeld;
@@ -238,9 +209,8 @@ class Group extends AbleObject_1.default {
238
209
  return groups;
239
210
  }
240
211
  $getLevel() {
241
- var _a;
242
212
  const tilmeld = this.$nymph.tilmeld;
243
- let group = tilmeld.Group.factorySync((_a = this.guid) !== null && _a !== void 0 ? _a : undefined);
213
+ let group = tilmeld.Group.factorySync(this.guid ?? undefined);
244
214
  let level = 0;
245
215
  while (group.parent != null && group.parent.cdate != null && level < 1024) {
246
216
  level++;
@@ -248,228 +218,214 @@ class Group extends AbleObject_1.default {
248
218
  }
249
219
  return level;
250
220
  }
251
- $getUsers(descendants = false, limit, offset) {
252
- return __awaiter(this, void 0, void 0, function* () {
253
- const tilmeld = this.$nymph.tilmeld;
254
- let groups = [];
255
- if (descendants) {
256
- groups = yield this.$getDescendants();
257
- }
258
- return yield this.$nymph.getEntities({
259
- class: tilmeld.User,
260
- limit,
261
- offset,
262
- }, {
263
- type: '&',
264
- equal: ['enabled', true],
265
- }, {
266
- type: '|',
267
- ref: [
268
- ['group', this],
269
- ['groups', this],
270
- ...groups.map((group) => [
271
- 'group',
272
- group,
273
- ]),
274
- ...groups.map((group) => [
275
- 'groups',
276
- group,
277
- ]),
278
- ],
279
- });
221
+ async $getUsers(descendants = false, limit, offset) {
222
+ const tilmeld = this.$nymph.tilmeld;
223
+ let groups = [];
224
+ if (descendants) {
225
+ groups = await this.$getDescendants();
226
+ }
227
+ return await this.$nymph.getEntities({
228
+ class: tilmeld.User,
229
+ limit,
230
+ offset,
231
+ }, {
232
+ type: '&',
233
+ equal: ['enabled', true],
234
+ }, {
235
+ type: '|',
236
+ ref: [
237
+ ['group', this],
238
+ ['groups', this],
239
+ ...groups.map((group) => [
240
+ 'group',
241
+ group,
242
+ ]),
243
+ ...groups.map((group) => [
244
+ 'groups',
245
+ group,
246
+ ]),
247
+ ],
280
248
  });
281
249
  }
282
- $checkGroupname() {
283
- return __awaiter(this, void 0, void 0, function* () {
284
- const tilmeld = this.$nymph.tilmeld;
285
- if (!tilmeld.config.emailUsernames) {
286
- if (this.$data.groupname == null || !this.$data.groupname.length) {
287
- return { result: false, message: 'Please specify a groupname.' };
288
- }
289
- if (tilmeld.config.maxUsernameLength > 0 &&
290
- this.$data.groupname.length > tilmeld.config.maxUsernameLength) {
291
- return {
292
- result: false,
293
- message: 'Groupnames must not exceed ' +
294
- tilmeld.config.maxUsernameLength +
295
- ' characters.',
296
- };
297
- }
298
- if ((0, lodash_1.difference)(this.$data.groupname.split(''), tilmeld.config.validChars.split('')).length) {
299
- return {
300
- result: false,
301
- message: tilmeld.config.validCharsNotice,
302
- };
303
- }
304
- if (!tilmeld.config.validRegex.test(this.$data.groupname)) {
305
- return {
306
- result: false,
307
- message: tilmeld.config.validRegexNotice,
308
- };
309
- }
310
- const selector = {
311
- type: '&',
312
- ilike: [
313
- 'groupname',
314
- this.$data.groupname.replace(/([\\%_])/g, (s) => `\\${s}`),
315
- ],
316
- };
317
- if (this.guid != null) {
318
- selector['!guid'] = this.guid;
319
- }
320
- const test = yield this.$nymph.getEntity({ class: tilmeld.Group, skipAc: true }, selector);
321
- if (test != null) {
322
- return { result: false, message: 'That groupname is taken.' };
323
- }
250
+ async $checkGroupname() {
251
+ const tilmeld = this.$nymph.tilmeld;
252
+ if (!tilmeld.config.emailUsernames) {
253
+ if (this.$data.groupname == null || !this.$data.groupname.length) {
254
+ return { result: false, message: 'Please specify a groupname.' };
255
+ }
256
+ if (tilmeld.config.maxUsernameLength > 0 &&
257
+ this.$data.groupname.length > tilmeld.config.maxUsernameLength) {
324
258
  return {
325
- result: true,
326
- message: this.guid != null ? 'Groupname is valid.' : 'Groupname is available!',
259
+ result: false,
260
+ message: 'Groupnames must not exceed ' +
261
+ tilmeld.config.maxUsernameLength +
262
+ ' characters.',
327
263
  };
328
264
  }
329
- else {
330
- this.$data.email = this.$data.groupname;
331
- if (this.$data.groupname == null || !this.$data.groupname.length) {
332
- return { result: false, message: 'Please specify an email.' };
333
- }
334
- if (tilmeld.config.maxUsernameLength > 0 &&
335
- this.$data.groupname.length > tilmeld.config.maxUsernameLength) {
336
- return {
337
- result: false,
338
- message: 'Emails must not exceed ' +
339
- tilmeld.config.maxUsernameLength +
340
- ' characters.',
341
- };
342
- }
343
- return yield this.$checkEmail();
344
- }
345
- });
346
- }
347
- $checkEmail() {
348
- return __awaiter(this, void 0, void 0, function* () {
349
- const tilmeld = this.$nymph.tilmeld;
350
- if (this.$data.email === '') {
351
- return { result: true, message: '' };
352
- }
353
- if (this.$data.email == null) {
354
- return { result: false, message: 'Please specify a valid email.' };
265
+ if ((0, lodash_1.difference)(this.$data.groupname.split(''), tilmeld.config.validChars.split('')).length) {
266
+ return {
267
+ result: false,
268
+ message: tilmeld.config.validCharsNotice,
269
+ };
355
270
  }
356
- if (!tilmeld.config.validEmailRegex.test(this.$data.email)) {
271
+ if (!tilmeld.config.validRegex.test(this.$data.groupname)) {
357
272
  return {
358
273
  result: false,
359
- message: tilmeld.config.validEmailRegexNotice,
274
+ message: tilmeld.config.validRegexNotice,
360
275
  };
361
276
  }
362
277
  const selector = {
363
278
  type: '&',
364
- ilike: ['email', this.$data.email.replace(/([\\%_])/g, (s) => `\\${s}`)],
279
+ ilike: [
280
+ 'groupname',
281
+ this.$data.groupname.replace(/([\\%_])/g, (s) => `\\${s}`),
282
+ ],
365
283
  };
366
284
  if (this.guid != null) {
367
285
  selector['!guid'] = this.guid;
368
286
  }
369
- const test = yield this.$nymph.getEntity({ class: tilmeld.Group, skipAc: true }, selector);
287
+ const test = await this.$nymph.getEntity({ class: tilmeld.Group, skipAc: true }, selector);
370
288
  if (test != null) {
289
+ return { result: false, message: 'That groupname is taken.' };
290
+ }
291
+ return {
292
+ result: true,
293
+ message: this.guid != null ? 'Groupname is valid.' : 'Groupname is available!',
294
+ };
295
+ }
296
+ else {
297
+ this.$data.email = this.$data.groupname;
298
+ if (this.$data.groupname == null || !this.$data.groupname.length) {
299
+ return { result: false, message: 'Please specify an email.' };
300
+ }
301
+ if (tilmeld.config.maxUsernameLength > 0 &&
302
+ this.$data.groupname.length > tilmeld.config.maxUsernameLength) {
371
303
  return {
372
304
  result: false,
373
- message: 'That email address is already registered.',
305
+ message: 'Emails must not exceed ' +
306
+ tilmeld.config.maxUsernameLength +
307
+ ' characters.',
374
308
  };
375
309
  }
310
+ return await this.$checkEmail();
311
+ }
312
+ }
313
+ async $checkEmail() {
314
+ const tilmeld = this.$nymph.tilmeld;
315
+ if (this.$data.email === '') {
316
+ return { result: true, message: '' };
317
+ }
318
+ if (this.$data.email == null) {
319
+ return { result: false, message: 'Please specify a valid email.' };
320
+ }
321
+ if (!tilmeld.config.validEmailRegex.test(this.$data.email)) {
376
322
  return {
377
- result: true,
378
- message: this.guid != null ? 'Email is valid.' : 'Email address is valid!',
323
+ result: false,
324
+ message: tilmeld.config.validEmailRegexNotice,
379
325
  };
380
- });
326
+ }
327
+ const selector = {
328
+ type: '&',
329
+ ilike: ['email', this.$data.email.replace(/([\\%_])/g, (s) => `\\${s}`)],
330
+ };
331
+ if (this.guid != null) {
332
+ selector['!guid'] = this.guid;
333
+ }
334
+ const test = await this.$nymph.getEntity({ class: tilmeld.Group, skipAc: true }, selector);
335
+ if (test != null) {
336
+ return {
337
+ result: false,
338
+ message: 'That email address is already registered.',
339
+ };
340
+ }
341
+ return {
342
+ result: true,
343
+ message: this.guid != null ? 'Email is valid.' : 'Email address is valid!',
344
+ };
381
345
  }
382
- $save() {
383
- const _super = Object.create(null, {
384
- $save: { get: () => super.$save }
385
- });
386
- var _a, _b, _c, _d;
387
- return __awaiter(this, void 0, void 0, function* () {
388
- const tilmeld = this.$nymph.tilmeld;
389
- if (this.$data.groupname == null || !this.$data.groupname.trim().length) {
390
- return false;
391
- }
392
- this.$data.groupname = this.$data.groupname.trim();
393
- if (tilmeld.config.emailUsernames) {
394
- this.$data.email = this.$data.groupname;
395
- }
396
- this.$data.email = ((_a = this.$data.email) !== null && _a !== void 0 ? _a : '').trim();
397
- this.$data.name = ((_b = this.$data.name) !== null && _b !== void 0 ? _b : '').trim();
398
- this.$data.phone = ((_c = this.$data.phone) !== null && _c !== void 0 ? _c : '').trim();
399
- this.$data.enabled = !!this.$data.enabled;
400
- if (this.$data.name === '') {
401
- delete this.$data.name;
402
- }
403
- if (this.$data.email === '') {
404
- delete this.$data.email;
405
- }
406
- if (this.$data.avatar === '') {
407
- delete this.$data.avatar;
408
- }
409
- if (this.$data.phone === '') {
410
- delete this.$data.phone;
411
- }
412
- if (this.$data.parent == null) {
413
- delete this.$data.parent;
414
- }
415
- if (this.$data.user == null) {
416
- delete this.$data.user;
417
- }
418
- if (!this.$data.defaultPrimary) {
419
- delete this.$data.defaultPrimary;
420
- }
421
- if (!this.$data.defaultSecondary) {
422
- delete this.$data.defaultSecondary;
423
- }
424
- if (!this.$data.unverifiedSecondary) {
425
- delete this.$data.unverifiedSecondary;
426
- }
427
- this.$data.abilities = (0, lodash_1.difference)((_d = this.$data.abilities) !== null && _d !== void 0 ? _d : [], [
428
- 'system/admin',
429
- 'tilmeld/admin',
430
- null,
431
- undefined,
432
- ]);
433
- const unCheck = yield this.$checkGroupname();
434
- if (!unCheck.result) {
435
- throw new errors_1.BadUsernameError(unCheck.message);
436
- }
437
- if (!tilmeld.config.emailUsernames) {
438
- const emCheck = yield this.$checkEmail();
439
- if (!emCheck.result) {
440
- throw new errors_1.BadEmailError(emCheck.message);
441
- }
442
- }
443
- if (this.$data.parent &&
444
- (this.$data.parent.cdate == null ||
445
- this.$is(this.$data.parent) ||
446
- this.$data.parent.$isDescendant(this))) {
447
- throw new errors_1.BadDataError("Group parent can't be itself or descendant of itself.");
448
- }
449
- try {
450
- tilmeld.config.validatorGroup(this);
451
- }
452
- catch (e) {
453
- throw new errors_1.BadDataError(e === null || e === void 0 ? void 0 : e.message);
346
+ async $save() {
347
+ const tilmeld = this.$nymph.tilmeld;
348
+ if (this.$data.groupname == null || !this.$data.groupname.trim().length) {
349
+ return false;
350
+ }
351
+ this.$data.groupname = this.$data.groupname.trim();
352
+ if (tilmeld.config.emailUsernames) {
353
+ this.$data.email = this.$data.groupname;
354
+ }
355
+ this.$data.email = (this.$data.email ?? '').trim();
356
+ this.$data.name = (this.$data.name ?? '').trim();
357
+ this.$data.phone = (this.$data.phone ?? '').trim();
358
+ this.$data.enabled = !!this.$data.enabled;
359
+ if (this.$data.name === '') {
360
+ delete this.$data.name;
361
+ }
362
+ if (this.$data.email === '') {
363
+ delete this.$data.email;
364
+ }
365
+ if (this.$data.avatar === '') {
366
+ delete this.$data.avatar;
367
+ }
368
+ if (this.$data.phone === '') {
369
+ delete this.$data.phone;
370
+ }
371
+ if (this.$data.parent == null) {
372
+ delete this.$data.parent;
373
+ }
374
+ if (this.$data.user == null) {
375
+ delete this.$data.user;
376
+ }
377
+ if (!this.$data.defaultPrimary) {
378
+ delete this.$data.defaultPrimary;
379
+ }
380
+ if (!this.$data.defaultSecondary) {
381
+ delete this.$data.defaultSecondary;
382
+ }
383
+ if (!this.$data.unverifiedSecondary) {
384
+ delete this.$data.unverifiedSecondary;
385
+ }
386
+ this.$data.abilities = (0, lodash_1.difference)(this.$data.abilities ?? [], [
387
+ 'system/admin',
388
+ 'tilmeld/admin',
389
+ null,
390
+ undefined,
391
+ ]);
392
+ const unCheck = await this.$checkGroupname();
393
+ if (!unCheck.result) {
394
+ throw new errors_1.BadUsernameError(unCheck.message);
395
+ }
396
+ if (!tilmeld.config.emailUsernames) {
397
+ const emCheck = await this.$checkEmail();
398
+ if (!emCheck.result) {
399
+ throw new errors_1.BadEmailError(emCheck.message);
454
400
  }
455
- if (this.$data.defaultPrimary) {
456
- const currentPrimary = yield this.$nymph.getEntity({ class: tilmeld.Group }, { type: '&', truthy: 'defaultPrimary' });
457
- if (currentPrimary != null && !this.$is(currentPrimary)) {
458
- currentPrimary.defaultPrimary = false;
459
- if (!(yield currentPrimary.$save())) {
460
- throw new errors_1.CouldNotChangeDefaultPrimaryGroupError('Could not change new user primary group from ' +
461
- `${currentPrimary.groupname}.`);
462
- }
401
+ }
402
+ if (this.$data.parent &&
403
+ (this.$data.parent.cdate == null ||
404
+ this.$is(this.$data.parent) ||
405
+ this.$data.parent.$isDescendant(this))) {
406
+ throw new errors_1.BadDataError("Group parent can't be itself or descendant of itself.");
407
+ }
408
+ try {
409
+ tilmeld.config.validatorGroup(this);
410
+ }
411
+ catch (e) {
412
+ throw new errors_1.BadDataError(e?.message);
413
+ }
414
+ if (this.$data.defaultPrimary) {
415
+ const currentPrimary = await this.$nymph.getEntity({ class: tilmeld.Group }, { type: '&', truthy: 'defaultPrimary' });
416
+ if (currentPrimary != null && !this.$is(currentPrimary)) {
417
+ currentPrimary.defaultPrimary = false;
418
+ if (!(await currentPrimary.$save())) {
419
+ throw new errors_1.CouldNotChangeDefaultPrimaryGroupError('Could not change new user primary group from ' +
420
+ `${currentPrimary.groupname}.`);
463
421
  }
464
422
  }
465
- return yield _super.$save.call(this);
466
- });
423
+ }
424
+ return await super.$save();
467
425
  }
468
- $saveSkipAC() {
469
- return __awaiter(this, void 0, void 0, function* () {
470
- this.$skipAcWhenSaving = true;
471
- return yield this.$save();
472
- });
426
+ async $saveSkipAC() {
427
+ this.$skipAcWhenSaving = true;
428
+ return await this.$save();
473
429
  }
474
430
  $tilmeldSaveSkipAC() {
475
431
  if (this.$skipAcWhenSaving) {
@@ -478,69 +434,64 @@ class Group extends AbleObject_1.default {
478
434
  }
479
435
  return false;
480
436
  }
481
- $delete() {
482
- const _super = Object.create(null, {
483
- $delete: { get: () => super.$delete }
484
- });
485
- return __awaiter(this, void 0, void 0, function* () {
486
- let tilmeld = this.$nymph.tilmeld;
487
- if (!tilmeld.gatekeeper('tilmeld/admin')) {
488
- throw new errors_1.BadDataError("You don't have the authority to delete groups.");
489
- }
490
- const transaction = 'tilmeld-delete-' + this.guid;
491
- const nymph = this.$nymph;
492
- const tnymph = yield nymph.startTransaction(transaction);
493
- this.$nymph = tnymph;
494
- tilmeld = this.$nymph.tilmeld;
495
- const descendants = yield this.$getDescendants();
496
- if (descendants.length) {
497
- for (let curGroup of descendants) {
498
- if (!(yield curGroup.$delete())) {
499
- yield tnymph.rollback(transaction);
500
- this.$nymph = nymph;
501
- return false;
502
- }
503
- }
504
- }
505
- const primaryUsers = yield tnymph.getEntities({
506
- class: tilmeld.User,
507
- skipAc: true,
508
- }, {
509
- type: '&',
510
- ref: ['group', this],
511
- });
512
- for (let user of primaryUsers) {
513
- delete user.group;
514
- if (!(yield user.$save())) {
515
- yield tnymph.rollback(transaction);
437
+ async $delete() {
438
+ let tilmeld = this.$nymph.tilmeld;
439
+ if (!tilmeld.gatekeeper('tilmeld/admin')) {
440
+ throw new errors_1.BadDataError("You don't have the authority to delete groups.");
441
+ }
442
+ const transaction = 'tilmeld-delete-' + this.guid;
443
+ const nymph = this.$nymph;
444
+ const tnymph = await nymph.startTransaction(transaction);
445
+ this.$nymph = tnymph;
446
+ tilmeld = this.$nymph.tilmeld;
447
+ const descendants = await this.$getDescendants();
448
+ if (descendants.length) {
449
+ for (let curGroup of descendants) {
450
+ if (!(await curGroup.$delete())) {
451
+ await tnymph.rollback(transaction);
516
452
  this.$nymph = nymph;
517
453
  return false;
518
454
  }
519
455
  }
520
- const secondaryUsers = yield tnymph.getEntities({
521
- class: tilmeld.User,
522
- skipAc: true,
523
- }, {
524
- type: '&',
525
- ref: ['groups', this],
526
- });
527
- for (let user of secondaryUsers) {
528
- user.$delGroup(this);
529
- if (!(yield user.$save())) {
530
- yield tnymph.rollback(transaction);
531
- return false;
532
- }
533
- }
534
- const success = yield _super.$delete.call(this);
535
- if (success) {
536
- yield tnymph.commit(transaction);
537
- }
538
- else {
539
- yield tnymph.rollback(transaction);
456
+ }
457
+ const primaryUsers = await tnymph.getEntities({
458
+ class: tilmeld.User,
459
+ skipAc: true,
460
+ }, {
461
+ type: '&',
462
+ ref: ['group', this],
463
+ });
464
+ for (let user of primaryUsers) {
465
+ delete user.group;
466
+ if (!(await user.$save())) {
467
+ await tnymph.rollback(transaction);
468
+ this.$nymph = nymph;
469
+ return false;
540
470
  }
541
- this.$nymph = nymph;
542
- return success;
471
+ }
472
+ const secondaryUsers = await tnymph.getEntities({
473
+ class: tilmeld.User,
474
+ skipAc: true,
475
+ }, {
476
+ type: '&',
477
+ ref: ['groups', this],
543
478
  });
479
+ for (let user of secondaryUsers) {
480
+ user.$delGroup(this);
481
+ if (!(await user.$save())) {
482
+ await tnymph.rollback(transaction);
483
+ return false;
484
+ }
485
+ }
486
+ const success = await super.$delete();
487
+ if (success) {
488
+ await tnymph.commit(transaction);
489
+ }
490
+ else {
491
+ await tnymph.rollback(transaction);
492
+ }
493
+ this.$nymph = nymph;
494
+ return success;
544
495
  }
545
496
  }
546
497
  exports.default = Group;