bkper-js 1.35.6 → 1.37.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.
package/lib/index.d.ts CHANGED
@@ -194,6 +194,7 @@ export declare class Account {
194
194
  * Perform delete account.
195
195
  */
196
196
  remove(): Promise<Account>;
197
+
197
198
  }
198
199
 
199
200
  /**
@@ -723,7 +724,6 @@ export declare class Book {
723
724
 
724
725
 
725
726
 
726
-
727
727
  constructor(payload?: bkper.Book);
728
728
  /**
729
729
  * @returns An immutable copy of the json payload
@@ -1025,10 +1025,6 @@ export declare class Book {
1025
1025
 
1026
1026
 
1027
1027
 
1028
-
1029
-
1030
-
1031
-
1032
1028
  /**
1033
1029
  * Gets a [[Group]] object
1034
1030
  *
@@ -1043,14 +1039,19 @@ export declare class Book {
1043
1039
  * @returns The retrieved Group objects
1044
1040
  */
1045
1041
  getGroups(): Promise<Group[]>;
1046
- private mapGroups;
1042
+
1047
1043
  /**
1048
1044
  * Gets all [[Accounts]] of this Book
1049
1045
  *
1050
1046
  * @returns The retrieved Account objects
1051
1047
  */
1052
1048
  getAccounts(): Promise<Account[]>;
1053
- private mapAccounts;
1049
+
1050
+
1051
+
1052
+
1053
+
1054
+
1054
1055
 
1055
1056
  /**
1056
1057
  * Lists transactions in the Book based on the provided query, limit, and cursor, for pagination.
@@ -1763,6 +1764,20 @@ export declare class Group {
1763
1764
  * @returns This Group, for chainning.
1764
1765
  */
1765
1766
  setName(name: string): Group;
1767
+ /**
1768
+ * Tells if the Group is locked by the Book owner.
1769
+ *
1770
+ * @returns True if the Group is locked.
1771
+ */
1772
+ isLocked(): boolean;
1773
+ /**
1774
+ * Sets the locked state of the Group.
1775
+ *
1776
+ * @param locked - The locked state of the Group.
1777
+ *
1778
+ * @returns This Group, for chainning.
1779
+ */
1780
+ setLocked(locked: boolean): Group;
1766
1781
  /**
1767
1782
  * @returns The name of this group without spaces and special characters
1768
1783
  */
@@ -1855,8 +1870,6 @@ export declare class Group {
1855
1870
  */
1856
1871
  getChildren(): Group[];
1857
1872
 
1858
-
1859
-
1860
1873
  /**
1861
1874
  * Retrieves all descendant Groups of the current Group.
1862
1875
  *
@@ -1908,8 +1921,6 @@ export declare class Group {
1908
1921
 
1909
1922
 
1910
1923
 
1911
-
1912
-
1913
1924
  /**
1914
1925
  * @returns True if this group has any account in it
1915
1926
  */
@@ -1926,6 +1937,7 @@ export declare class Group {
1926
1937
  * Perform delete group.
1927
1938
  */
1928
1939
  remove(): Promise<Group>;
1940
+
1929
1941
  }
1930
1942
 
1931
1943
  /**
@@ -341,7 +341,7 @@ export class Account {
341
341
  create() {
342
342
  return __awaiter(this, void 0, void 0, function* () {
343
343
  this.payload = yield AccountService.createAccount(this.book.getId(), this.payload);
344
- this.book.updateAccountCache(this);
344
+ this.updateAccountCache();
345
345
  return this;
346
346
  });
347
347
  }
@@ -350,9 +350,8 @@ 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 || "");
354
353
  this.payload = yield AccountService.updateAccount(this.book.getId(), this.payload);
355
- this.book.updateAccountCache(this, previousGroupIds);
354
+ this.updateAccountCache();
356
355
  return this;
357
356
  });
358
357
  }
@@ -362,9 +361,14 @@ export class Account {
362
361
  remove() {
363
362
  return __awaiter(this, void 0, void 0, function* () {
364
363
  this.payload = yield AccountService.deleteAccount(this.book.getId(), this.payload);
365
- this.book.removeAccountCache(this);
364
+ this.updateAccountCache(true);
366
365
  return this;
367
366
  });
368
367
  }
368
+ /** @internal */
369
+ updateAccountCache(remove) {
370
+ this.book.setAccount(this.payload, remove);
371
+ this.book.clearCache();
372
+ }
369
373
  }
370
374
  //# sourceMappingURL=Account.js.map
package/lib/model/Book.js CHANGED
@@ -450,8 +450,9 @@ export class Book {
450
450
  for (const payload of payloads) {
451
451
  const account = new Account(this, payload);
452
452
  createdAccounts.push(account);
453
- this.updateAccountCache(account);
453
+ this.setAccount(payload);
454
454
  }
455
+ this.clearCache();
455
456
  return createdAccounts;
456
457
  }
457
458
  return [];
@@ -471,11 +472,9 @@ export class Book {
471
472
  for (const payload of payloads) {
472
473
  const group = new Group(this, payload);
473
474
  createdGroups.push(group);
474
- this.updateGroupCache(group);
475
- if (this.idGroupMap) {
476
- group.buildGroupTree(this.idGroupMap);
477
- }
475
+ this.setGroup(payload);
478
476
  }
477
+ this.clearCache();
479
478
  return createdGroups;
480
479
  }
481
480
  return [];
@@ -575,41 +574,22 @@ export class Book {
575
574
  });
576
575
  }
577
576
  /** @internal */
578
- getGroupsMap() {
579
- return this.idGroupMap;
580
- }
581
- /** @internal */
582
- updateGroupCache(group, previousParentId) {
583
- var _a;
577
+ updateGroupCache(group) {
584
578
  if (this.idGroupMap) {
585
579
  const id = group.getId();
586
580
  if (id) {
587
581
  this.idGroupMap.set(id, group);
588
582
  }
589
- const parentId = (_a = group.payload.parent) === null || _a === void 0 ? void 0 : _a.id;
590
- if ((previousParentId || '') !== (parentId || '')) {
591
- group.destroyGroupTree(this.idGroupMap, previousParentId);
592
- }
593
583
  group.buildGroupTree(this.idGroupMap);
594
584
  }
595
585
  }
596
586
  /** @internal */
597
- removeGroupCache(group) {
598
- if (this.idGroupMap) {
599
- this.idGroupMap.delete(group.getId() || '');
600
- group.destroyGroupTree(this.idGroupMap);
601
- }
602
- }
603
- /** @internal */
604
- updateAccountCache(account, previousGroupIds) {
587
+ updateAccountCache(account) {
605
588
  if (this.idAccountMap) {
606
589
  const id = account.getId();
607
590
  if (id) {
608
591
  this.idAccountMap.set(id, account);
609
592
  }
610
- if (previousGroupIds && previousGroupIds.length > 0) {
611
- this.unlinkAccountsAndGroups(account, previousGroupIds);
612
- }
613
593
  this.linkAccountsAndGroups(account);
614
594
  }
615
595
  }
@@ -621,31 +601,10 @@ export class Book {
621
601
  const group = (_a = this.idGroupMap) === null || _a === void 0 ? void 0 : _a.get(groupPayload.id || "");
622
602
  if (group != null) {
623
603
  group.addAccount(account);
624
- // TODO add known group to account
625
- }
626
- }
627
- }
628
- /** @internal */
629
- unlinkAccountsAndGroups(account, groupIds) {
630
- var _a, _b;
631
- if (!groupIds) {
632
- groupIds = ((_a = account.payload.groups) === null || _a === void 0 ? void 0 : _a.map(g => g.id || "")) || [];
633
- }
634
- for (const groupId of groupIds) {
635
- const group = (_b = this.idGroupMap) === null || _b === void 0 ? void 0 : _b.get(groupId || "");
636
- if (group != null) {
637
- group.removeAccount(account);
638
604
  }
639
605
  }
640
606
  }
641
607
  /** @internal */
642
- removeAccountCache(account) {
643
- if (this.idAccountMap) {
644
- this.idAccountMap.delete(account.getId() || '');
645
- }
646
- this.unlinkAccountsAndGroups(account);
647
- }
648
- /** @internal */
649
608
  getMostRecentLockDate_() {
650
609
  const closingDate = this.getClosingDate();
651
610
  const lockDate = this.getLockDate();
@@ -703,13 +662,13 @@ export class Book {
703
662
  return this.mapGroups(groups);
704
663
  });
705
664
  }
665
+ /** @internal */
706
666
  mapGroups(groups) {
707
667
  if (!groups) {
708
668
  return [];
709
669
  }
710
670
  let groupsObj = groups.map(group => new Group(this, group));
711
671
  this.idGroupMap = new Map();
712
- this.parentIdGroupsMap = new Map();
713
672
  for (const group of groupsObj) {
714
673
  this.updateGroupCache(group);
715
674
  }
@@ -732,6 +691,7 @@ export class Book {
732
691
  return this.mapAccounts(accounts);
733
692
  });
734
693
  }
694
+ /** @internal */
735
695
  mapAccounts(accounts) {
736
696
  if (!accounts) {
737
697
  return [];
@@ -754,6 +714,51 @@ export class Book {
754
714
  }
755
715
  }
756
716
  }
717
+ /** @internal */
718
+ clearCache() {
719
+ this.clearGroupCache();
720
+ this.clearAccountCache();
721
+ }
722
+ /** @internal */
723
+ clearGroupCache() {
724
+ this.idGroupMap = undefined;
725
+ }
726
+ /** @internal */
727
+ clearAccountCache() {
728
+ this.idAccountMap = undefined;
729
+ }
730
+ /** @internal */
731
+ setAccount(account, remove) {
732
+ const accountPayloads = this.payload.accounts || [];
733
+ if (remove) {
734
+ this.payload.accounts = accountPayloads.filter(a => a.id !== account.id);
735
+ }
736
+ else {
737
+ const existingAccount = accountPayloads.find(a => a.id === account.id);
738
+ if (existingAccount) {
739
+ this.payload.accounts = accountPayloads.map(a => a.id === account.id ? account : a);
740
+ }
741
+ else {
742
+ this.payload.accounts = [...accountPayloads, account];
743
+ }
744
+ }
745
+ }
746
+ /** @internal */
747
+ setGroup(group, remove) {
748
+ const groupPayloads = this.payload.groups || [];
749
+ if (remove) {
750
+ this.payload.groups = groupPayloads.filter(g => g.id !== group.id);
751
+ }
752
+ else {
753
+ const existingGroup = groupPayloads.find(g => g.id === group.id);
754
+ if (existingGroup) {
755
+ this.payload.groups = groupPayloads.map(g => g.id === group.id ? group : g);
756
+ }
757
+ else {
758
+ this.payload.groups = [...groupPayloads, group];
759
+ }
760
+ }
761
+ }
757
762
  /**
758
763
  * Lists transactions in the Book based on the provided query, limit, and cursor, for pagination.
759
764
  *
@@ -831,23 +836,23 @@ export class Book {
831
836
  });
832
837
  }
833
838
  /**
834
- *
835
- * Create a [[BalancesReport]] based on query
836
- *
837
- * @param query The balances report query
838
- *
839
- * @return The balances report
840
- *
841
- * Example:
842
- *
843
- * ```js
844
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgPXjx7oKDA");
845
- *
846
- * var balancesReport = book.getBalancesReport("group:'Equity' after:7/2018 before:8/2018");
847
- *
848
- * var accountBalance = balancesReport.getBalancesContainer("Bank Account").getCumulativeBalance();
849
- * ```
850
- */
839
+ *
840
+ * Create a [[BalancesReport]] based on query
841
+ *
842
+ * @param query The balances report query
843
+ *
844
+ * @return The balances report
845
+ *
846
+ * Example:
847
+ *
848
+ * ```js
849
+ * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgPXjx7oKDA");
850
+ *
851
+ * var balancesReport = book.getBalancesReport("group:'Equity' after:7/2018 before:8/2018");
852
+ *
853
+ * var accountBalance = balancesReport.getBalancesContainer("Bank Account").getCumulativeBalance();
854
+ * ```
855
+ */
851
856
  getBalancesReport(query) {
852
857
  return __awaiter(this, void 0, void 0, function* () {
853
858
  const balances = yield BalancesService.getBalances(this.getId(), query);
@@ -55,6 +55,28 @@ export class Group {
55
55
  this.payload.name = name;
56
56
  return this;
57
57
  }
58
+ /**
59
+ * Tells if the Group is locked by the Book owner.
60
+ *
61
+ * @returns True if the Group is locked.
62
+ */
63
+ isLocked() {
64
+ if (this.payload.locked == null) {
65
+ return false;
66
+ }
67
+ return this.payload.locked;
68
+ }
69
+ /**
70
+ * Sets the locked state of the Group.
71
+ *
72
+ * @param locked - The locked state of the Group.
73
+ *
74
+ * @returns This Group, for chainning.
75
+ */
76
+ setLocked(locked) {
77
+ this.payload.locked = locked;
78
+ return this;
79
+ }
58
80
  /**
59
81
  * @returns The name of this group without spaces and special characters
60
82
  */
@@ -222,23 +244,6 @@ export class Group {
222
244
  const id = child.getId();
223
245
  if (id) {
224
246
  this.children.set(id, child);
225
- this.updateBookParentIdGroupsMap();
226
- }
227
- }
228
- /** @internal */
229
- removeChild(child) {
230
- const id = child.getId();
231
- if (id) {
232
- this.children.delete(id);
233
- this.updateBookParentIdGroupsMap();
234
- }
235
- }
236
- /** @internal */
237
- updateBookParentIdGroupsMap() {
238
- var _a;
239
- const id = this.getId();
240
- if (id) {
241
- (_a = this.book.parentIdGroupsMap) === null || _a === void 0 ? void 0 : _a.set(id, this.children);
242
247
  }
243
248
  }
244
249
  /**
@@ -351,19 +356,6 @@ export class Group {
351
356
  }
352
357
  }
353
358
  /** @internal */
354
- destroyGroupTree(groupsMap, parentId) {
355
- var _a;
356
- if (!parentId) {
357
- parentId = (_a = this.payload.parent) === null || _a === void 0 ? void 0 : _a.id;
358
- }
359
- if (parentId) {
360
- const parentGroup = groupsMap.get(parentId);
361
- if (parentGroup) {
362
- parentGroup.removeChild(this);
363
- }
364
- }
365
- }
366
- /** @internal */
367
359
  addAccount(account) {
368
360
  const id = account === null || account === void 0 ? void 0 : account.getId();
369
361
  if (id) {
@@ -373,14 +365,6 @@ export class Group {
373
365
  this.accounts.set(id, account);
374
366
  }
375
367
  }
376
- /** @internal */
377
- removeAccount(account) {
378
- var _a;
379
- const id = account === null || account === void 0 ? void 0 : account.getId();
380
- if (id) {
381
- (_a = this.accounts) === null || _a === void 0 ? void 0 : _a.delete(id);
382
- }
383
- }
384
368
  /**
385
369
  * @returns True if this group has any account in it
386
370
  */
@@ -393,7 +377,7 @@ export class Group {
393
377
  create() {
394
378
  return __awaiter(this, void 0, void 0, function* () {
395
379
  this.payload = yield GroupService.createGroup(this.book.getId(), this.payload);
396
- this.book.updateGroupCache(this);
380
+ this.updateGroupCache();
397
381
  return this;
398
382
  });
399
383
  }
@@ -402,10 +386,8 @@ export class Group {
402
386
  */
403
387
  update() {
404
388
  return __awaiter(this, void 0, void 0, function* () {
405
- var _a;
406
- const previousParentId = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getId();
407
389
  this.payload = yield GroupService.updateGroup(this.book.getId(), this.payload);
408
- this.book.updateGroupCache(this, previousParentId);
390
+ this.updateGroupCache();
409
391
  return this;
410
392
  });
411
393
  }
@@ -415,9 +397,14 @@ export class Group {
415
397
  remove() {
416
398
  return __awaiter(this, void 0, void 0, function* () {
417
399
  this.payload = yield GroupService.deleteGroup(this.book.getId(), this.payload);
418
- this.book.removeGroupCache(this);
400
+ this.updateGroupCache(true);
419
401
  return this;
420
402
  });
421
403
  }
404
+ /** @internal */
405
+ updateGroupCache(remove) {
406
+ this.book.setGroup(this.payload, remove);
407
+ this.book.clearCache();
408
+ }
422
409
  }
423
410
  //# sourceMappingURL=Group.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bkper-js",
3
- "version": "1.35.6",
3
+ "version": "1.37.0",
4
4
  "description": "Javascript client for Bkper REST API",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",