bkper-js 1.35.0 → 1.35.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/lib/index.d.ts CHANGED
@@ -1853,6 +1853,8 @@ export declare class Group {
1853
1853
  * @returns An array of child Groups.
1854
1854
  */
1855
1855
  getChildren(): Group[];
1856
+
1857
+
1856
1858
  /**
1857
1859
  * Retrieves all descendant Groups of the current Group.
1858
1860
  *
@@ -1905,6 +1907,7 @@ export declare class Group {
1905
1907
 
1906
1908
 
1907
1909
 
1910
+
1908
1911
  /**
1909
1912
  * @returns True if this group has any account in it
1910
1913
  */
@@ -350,8 +350,9 @@ export class Account {
350
350
  */
351
351
  update() {
352
352
  return __awaiter(this, void 0, void 0, function* () {
353
+ const previousGroupIds = [...(this.payload.groups || [])].map(g => g.id || "");
353
354
  this.payload = yield AccountService.updateAccount(this.book.getId(), this.payload);
354
- this.book.updateAccountCache(this);
355
+ this.book.updateAccountCache(this, previousGroupIds);
355
356
  return this;
356
357
  });
357
358
  }
package/lib/model/Book.js CHANGED
@@ -585,21 +585,26 @@ export class Book {
585
585
  if (id) {
586
586
  this.idGroupMap.set(id, group);
587
587
  }
588
+ group.buildGroupTree(this.idGroupMap);
588
589
  }
589
590
  }
590
591
  /** @internal */
591
592
  removeGroupCache(group) {
592
593
  if (this.idGroupMap) {
593
594
  this.idGroupMap.delete(group.getId() || '');
595
+ group.destroyGroupTree(this.idGroupMap);
594
596
  }
595
597
  }
596
598
  /** @internal */
597
- updateAccountCache(account) {
599
+ updateAccountCache(account, previousGroupIds) {
598
600
  if (this.idAccountMap) {
599
601
  const id = account.getId();
600
602
  if (id) {
601
603
  this.idAccountMap.set(id, account);
602
604
  }
605
+ if (previousGroupIds && previousGroupIds.length > 0) {
606
+ this.unlinkAccountsAndGroups(account, previousGroupIds);
607
+ }
603
608
  this.linkAccountsAndGroups(account);
604
609
  }
605
610
  }
@@ -616,11 +621,13 @@ export class Book {
616
621
  }
617
622
  }
618
623
  /** @internal */
619
- unlinkAccountsAndGroups(account) {
620
- var _a;
621
- const groupPayloads = account.payload.groups || [];
622
- for (const groupPayload of groupPayloads) {
623
- const group = (_a = this.idGroupMap) === null || _a === void 0 ? void 0 : _a.get(groupPayload.id || "");
624
+ unlinkAccountsAndGroups(account, groupIds) {
625
+ var _a, _b;
626
+ if (!groupIds) {
627
+ groupIds = ((_a = account.payload.groups) === null || _a === void 0 ? void 0 : _a.map(g => g.id || "")) || [];
628
+ }
629
+ for (const groupId of groupIds) {
630
+ const group = (_b = this.idGroupMap) === null || _b === void 0 ? void 0 : _b.get(groupId || "");
624
631
  if (group != null) {
625
632
  group.removeAccount(account);
626
633
  }
@@ -21,7 +21,7 @@ import { Account } from './Account.js';
21
21
  */
22
22
  export class Group {
23
23
  constructor(book, payload) {
24
- this.children = [];
24
+ this.children = new Map();
25
25
  this.book = book;
26
26
  this.payload = payload || {
27
27
  createdAt: `${Date.now()}`
@@ -214,7 +214,21 @@ export class Group {
214
214
  * @returns An array of child Groups.
215
215
  */
216
216
  getChildren() {
217
- return this.children || [];
217
+ return Array.from(this.children.values()) || [];
218
+ }
219
+ /** @internal */
220
+ addChild(child) {
221
+ const id = child.getId();
222
+ if (id) {
223
+ this.children.set(id, child);
224
+ }
225
+ }
226
+ /** @internal */
227
+ removeChild(child) {
228
+ const id = child.getId();
229
+ if (id) {
230
+ this.children.delete(id);
231
+ }
218
232
  }
219
233
  /**
220
234
  * Retrieves all descendant Groups of the current Group.
@@ -321,7 +335,16 @@ export class Group {
321
335
  const parentGroup = groupsMap.get(this.payload.parent.id || "");
322
336
  if (parentGroup) {
323
337
  this.parent = parentGroup;
324
- parentGroup.getChildren().push(this);
338
+ parentGroup.addChild(this);
339
+ }
340
+ }
341
+ }
342
+ /** @internal */
343
+ destroyGroupTree(groupsMap) {
344
+ if (this.payload.parent) {
345
+ const parentGroup = groupsMap.get(this.payload.parent.id || "");
346
+ if (parentGroup) {
347
+ parentGroup.removeChild(this);
325
348
  }
326
349
  }
327
350
  }
@@ -356,10 +379,6 @@ export class Group {
356
379
  return __awaiter(this, void 0, void 0, function* () {
357
380
  this.payload = yield GroupService.createGroup(this.book.getId(), this.payload);
358
381
  this.book.updateGroupCache(this);
359
- const bookGroupsMap = this.book.getGroupsMap();
360
- if (bookGroupsMap) {
361
- this.buildGroupTree(bookGroupsMap);
362
- }
363
382
  return this;
364
383
  });
365
384
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bkper-js",
3
- "version": "1.35.0",
3
+ "version": "1.35.2",
4
4
  "description": "Javascript client for Bkper REST API",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",