bkper-js 1.34.7 → 1.35.1

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
@@ -968,6 +968,18 @@ export declare class Book {
968
968
  * Replay [[Events]] on the Book, in batch.
969
969
  */
970
970
  batchReplayEvents(events: Event[], errorOnly?: boolean): Promise<void>;
971
+ /**
972
+ * Create [[Accounts]] on the Book, in batch.
973
+ *
974
+ * @return The created Accounts
975
+ */
976
+ batchCreateAccounts(accounts: Account[]): Promise<Account[]>;
977
+ /**
978
+ * Create [[Groups]] on the Book, in batch.
979
+ *
980
+ * @return The created Groups
981
+ */
982
+ batchCreateGroups(groups: Group[]): Promise<Group[]>;
971
983
  /**
972
984
  * Trigger [Balances Audit](https://help.bkper.com/en/articles/4412038-balances-audit) async process.
973
985
  */
@@ -1014,6 +1026,8 @@ export declare class Book {
1014
1026
 
1015
1027
 
1016
1028
 
1029
+
1030
+
1017
1031
  /**
1018
1032
  * Gets a [[Group]] object
1019
1033
  *
@@ -1839,6 +1853,8 @@ export declare class Group {
1839
1853
  * @returns An array of child Groups.
1840
1854
  */
1841
1855
  getChildren(): Group[];
1856
+
1857
+
1842
1858
  /**
1843
1859
  * Retrieves all descendant Groups of the current Group.
1844
1860
  *
@@ -1890,6 +1906,8 @@ export declare class Group {
1890
1906
 
1891
1907
 
1892
1908
 
1909
+
1910
+
1893
1911
  /**
1894
1912
  * @returns True if this group has any account in it
1895
1913
  */
package/lib/model/Book.js CHANGED
@@ -436,6 +436,51 @@ export class Book {
436
436
  yield EventService.replayEventsBatch(this, eventList, errorOnly);
437
437
  });
438
438
  }
439
+ /**
440
+ * Create [[Accounts]] on the Book, in batch.
441
+ *
442
+ * @return The created Accounts
443
+ */
444
+ batchCreateAccounts(accounts) {
445
+ return __awaiter(this, void 0, void 0, function* () {
446
+ if (accounts.length > 0) {
447
+ const accountList = { items: accounts.map(a => a.json()) };
448
+ const payloads = yield AccountService.createAccounts(this.getId(), accountList);
449
+ const createdAccounts = [];
450
+ for (const payload of payloads) {
451
+ const account = new Account(this, payload);
452
+ createdAccounts.push(account);
453
+ this.updateAccountCache(account);
454
+ }
455
+ return createdAccounts;
456
+ }
457
+ return [];
458
+ });
459
+ }
460
+ /**
461
+ * Create [[Groups]] on the Book, in batch.
462
+ *
463
+ * @return The created Groups
464
+ */
465
+ batchCreateGroups(groups) {
466
+ return __awaiter(this, void 0, void 0, function* () {
467
+ if (groups.length > 0) {
468
+ const groupList = { items: groups.map(g => g.json()) };
469
+ const payloads = yield GroupService.createGroups(this.getId(), groupList);
470
+ const createdGroups = [];
471
+ for (const payload of payloads) {
472
+ const group = new Group(this, payload);
473
+ createdGroups.push(group);
474
+ this.updateGroupCache(group);
475
+ if (this.idGroupMap) {
476
+ group.buildGroupTree(this.idGroupMap);
477
+ }
478
+ }
479
+ return createdGroups;
480
+ }
481
+ return [];
482
+ });
483
+ }
439
484
  /**
440
485
  * Trigger [Balances Audit](https://help.bkper.com/en/articles/4412038-balances-audit) async process.
441
486
  */
@@ -517,34 +562,37 @@ export class Book {
517
562
  if (!idOrName || idOrName.trim() == '') {
518
563
  return undefined;
519
564
  }
520
- let account;
521
565
  if (this.idAccountMap) {
522
- account = this.idAccountMap.get(idOrName);
523
- if (account) {
524
- return account;
525
- }
566
+ return this.idAccountMap.get(idOrName);
526
567
  }
527
- const accountPlain = yield AccountService.getAccount(this.getId(), idOrName);
528
- if (accountPlain) {
529
- account = new Account(this, accountPlain);
530
- return account;
568
+ else {
569
+ const accountPayload = yield AccountService.getAccount(this.getId(), idOrName);
570
+ if (accountPayload) {
571
+ return new Account(this, accountPayload);
572
+ }
531
573
  }
532
574
  return undefined;
533
575
  });
534
576
  }
535
577
  /** @internal */
578
+ getGroupsMap() {
579
+ return this.idGroupMap;
580
+ }
581
+ /** @internal */
536
582
  updateGroupCache(group) {
537
583
  if (this.idGroupMap) {
538
584
  const id = group.getId();
539
585
  if (id) {
540
586
  this.idGroupMap.set(id, group);
541
587
  }
588
+ group.buildGroupTree(this.idGroupMap);
542
589
  }
543
590
  }
544
591
  /** @internal */
545
592
  removeGroupCache(group) {
546
593
  if (this.idGroupMap) {
547
594
  this.idGroupMap.delete(group.getId() || '');
595
+ group.destroyGroupTree(this.idGroupMap);
548
596
  }
549
597
  }
550
598
  /** @internal */
@@ -570,10 +618,22 @@ export class Book {
570
618
  }
571
619
  }
572
620
  /** @internal */
621
+ unlinkAccountsAndGroups(account) {
622
+ var _a;
623
+ const groupPayloads = account.payload.groups || [];
624
+ for (const groupPayload of groupPayloads) {
625
+ const group = (_a = this.idGroupMap) === null || _a === void 0 ? void 0 : _a.get(groupPayload.id || "");
626
+ if (group != null) {
627
+ group.removeAccount(account);
628
+ }
629
+ }
630
+ }
631
+ /** @internal */
573
632
  removeAccountCache(account) {
574
633
  if (this.idAccountMap) {
575
634
  this.idAccountMap.delete(account.getId() || '');
576
635
  }
636
+ this.unlinkAccountsAndGroups(account);
577
637
  }
578
638
  /** @internal */
579
639
  getMostRecentLockDate_() {
@@ -604,23 +664,19 @@ export class Book {
604
664
  */
605
665
  getGroup(idOrName) {
606
666
  return __awaiter(this, void 0, void 0, function* () {
607
- if (!idOrName) {
667
+ if (!idOrName || idOrName.trim() == '') {
608
668
  return undefined;
609
669
  }
610
- idOrName = idOrName + '';
611
670
  if (this.idGroupMap) {
612
- let group = this.idGroupMap.get(idOrName);
613
- if (group) {
614
- return group;
615
- }
671
+ return this.idGroupMap.get(idOrName);
616
672
  }
617
- const groupPlain = yield GroupService.getGroup(this.getId(), idOrName);
618
- if (!groupPlain) {
619
- return undefined;
673
+ else {
674
+ const groupPayload = yield GroupService.getGroup(this.getId(), idOrName);
675
+ if (groupPayload) {
676
+ return new Group(this, groupPayload);
677
+ }
620
678
  }
621
- let group = new Group(this, groupPlain);
622
- this.updateGroupCache(group);
623
- return group;
679
+ return undefined;
624
680
  });
625
681
  }
626
682
  /**
@@ -682,7 +738,7 @@ export class Book {
682
738
  if (this.idGroupMap) {
683
739
  for (const group of this.idGroupMap.values()) {
684
740
  if (group.accounts == null) {
685
- group.accounts = new Set();
741
+ group.accounts = new Map();
686
742
  }
687
743
  }
688
744
  }
@@ -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()}`
@@ -71,7 +71,7 @@ export class Group {
71
71
  getAccounts() {
72
72
  return __awaiter(this, void 0, void 0, function* () {
73
73
  if (this.accounts) {
74
- return Array.from(this.accounts);
74
+ return Array.from(this.accounts.values());
75
75
  }
76
76
  let accountsPlain = yield GroupService.getAccounts(this.book.getId(), this.getId());
77
77
  if (!accountsPlain) {
@@ -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,19 +335,36 @@ 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
  }
328
351
  /** @internal */
329
352
  addAccount(account) {
330
- if (account == null) {
331
- return;
353
+ const id = account === null || account === void 0 ? void 0 : account.getId();
354
+ if (id) {
355
+ if (!this.accounts) {
356
+ this.accounts = new Map();
357
+ }
358
+ this.accounts.set(id, account);
332
359
  }
333
- if (!this.accounts) {
334
- this.accounts = new Set();
360
+ }
361
+ /** @internal */
362
+ removeAccount(account) {
363
+ var _a;
364
+ const id = account === null || account === void 0 ? void 0 : account.getId();
365
+ if (id) {
366
+ (_a = this.accounts) === null || _a === void 0 ? void 0 : _a.delete(id);
335
367
  }
336
- this.accounts.add(account);
337
368
  }
338
369
  /**
339
370
  * @returns True if this group has any account in it
@@ -14,6 +14,13 @@ export function createAccount(bookId, account) {
14
14
  return response.data;
15
15
  });
16
16
  }
17
+ export function createAccounts(bookId, payload) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ var _a;
20
+ const response = yield new HttpBooksApiV5Request(`${bookId}/accounts/batch`).setMethod('POST').setPayload(payload).fetch();
21
+ return ((_a = response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
22
+ });
23
+ }
17
24
  export function updateAccount(bookId, account) {
18
25
  return __awaiter(this, void 0, void 0, function* () {
19
26
  var payload = account;
@@ -14,6 +14,13 @@ export function createGroup(bookId, group) {
14
14
  return response.data;
15
15
  });
16
16
  }
17
+ export function createGroups(bookId, payload) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ var _a;
20
+ const response = yield new HttpBooksApiV5Request(`${bookId}/groups/batch`).setMethod('POST').setPayload(payload).fetch();
21
+ return ((_a = response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
22
+ });
23
+ }
17
24
  export function updateGroup(bookId, group) {
18
25
  return __awaiter(this, void 0, void 0, function* () {
19
26
  var response = yield new HttpBooksApiV5Request(`${bookId}/groups`).setMethod('PUT').setPayload(group).fetch();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bkper-js",
3
- "version": "1.34.7",
3
+ "version": "1.35.1",
4
4
  "description": "Javascript client for Bkper REST API",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",