bkper-js 1.35.5 → 1.36.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.
@@ -1855,8 +1856,6 @@ export declare class Group {
1855
1856
  */
1856
1857
  getChildren(): Group[];
1857
1858
 
1858
-
1859
-
1860
1859
  /**
1861
1860
  * Retrieves all descendant Groups of the current Group.
1862
1861
  *
@@ -1908,8 +1907,6 @@ export declare class Group {
1908
1907
 
1909
1908
 
1910
1909
 
1911
-
1912
-
1913
1910
  /**
1914
1911
  * @returns True if this group has any account in it
1915
1912
  */
@@ -1926,6 +1923,7 @@ export declare class Group {
1926
1923
  * Perform delete group.
1927
1924
  */
1928
1925
  remove(): Promise<Group>;
1926
+
1929
1927
  }
1930
1928
 
1931
1929
  /**
@@ -291,7 +291,7 @@ export class Account {
291
291
  }
292
292
  if (groupObject) {
293
293
  for (let i = 0; i < this.payload.groups.length; i++) {
294
- const groupId = this.payload.groups[i];
294
+ const groupId = this.payload.groups[i].id;
295
295
  if (groupId == groupObject.getId()) {
296
296
  this.payload.groups.splice(i, 1);
297
297
  }
@@ -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);
@@ -222,23 +222,6 @@ export class Group {
222
222
  const id = child.getId();
223
223
  if (id) {
224
224
  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
225
  }
243
226
  }
244
227
  /**
@@ -351,19 +334,6 @@ export class Group {
351
334
  }
352
335
  }
353
336
  /** @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
337
  addAccount(account) {
368
338
  const id = account === null || account === void 0 ? void 0 : account.getId();
369
339
  if (id) {
@@ -373,14 +343,6 @@ export class Group {
373
343
  this.accounts.set(id, account);
374
344
  }
375
345
  }
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
346
  /**
385
347
  * @returns True if this group has any account in it
386
348
  */
@@ -393,7 +355,7 @@ export class Group {
393
355
  create() {
394
356
  return __awaiter(this, void 0, void 0, function* () {
395
357
  this.payload = yield GroupService.createGroup(this.book.getId(), this.payload);
396
- this.book.updateGroupCache(this);
358
+ this.updateGroupCache();
397
359
  return this;
398
360
  });
399
361
  }
@@ -402,10 +364,8 @@ export class Group {
402
364
  */
403
365
  update() {
404
366
  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
367
  this.payload = yield GroupService.updateGroup(this.book.getId(), this.payload);
408
- this.book.updateGroupCache(this, previousParentId);
368
+ this.updateGroupCache();
409
369
  return this;
410
370
  });
411
371
  }
@@ -415,9 +375,14 @@ export class Group {
415
375
  remove() {
416
376
  return __awaiter(this, void 0, void 0, function* () {
417
377
  this.payload = yield GroupService.deleteGroup(this.book.getId(), this.payload);
418
- this.book.removeGroupCache(this);
378
+ this.updateGroupCache(true);
419
379
  return this;
420
380
  });
421
381
  }
382
+ /** @internal */
383
+ updateGroupCache(remove) {
384
+ this.book.setGroup(this.payload, remove);
385
+ this.book.clearCache();
386
+ }
422
387
  }
423
388
  //# sourceMappingURL=Group.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bkper-js",
3
- "version": "1.35.5",
3
+ "version": "1.36.0",
4
4
  "description": "Javascript client for Bkper REST API",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",