bkper 2.5.8 → 2.6.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/cli.js CHANGED
@@ -17,6 +17,7 @@ const commander_1 = __importDefault(require("commander"));
17
17
  const local_auth_service_1 = require("./auth/local-auth-service");
18
18
  const Bkper_1 = require("./model/Bkper");
19
19
  const utils_1 = require("./utils");
20
+ const App_1 = require("./model/App");
20
21
  var fs = require('fs');
21
22
  require('dotenv').config();
22
23
  process.env.NODE_ENV = utils_1.NODE_ENV_DEV;
@@ -39,8 +40,9 @@ commander_1.default
39
40
  .option('-c, --create', 'Create a new App')
40
41
  .action((options) => __awaiter(void 0, void 0, void 0, function* () {
41
42
  try {
42
- let app = Bkper_1.Bkper.setApiKey(process.env.BKPER_API_KEY)
43
- .setJson(JSON.parse(fs.readFileSync('./bkperapp.json', 'utf8')))
43
+ Bkper_1.Bkper.setConfig({ apiKeyProvider: () => __awaiter(void 0, void 0, void 0, function* () { return process.env.BKPER_API_KEY; }) });
44
+ let app = new App_1.App();
45
+ app.setJson(JSON.parse(fs.readFileSync('./bkperapp.json', 'utf8')))
44
46
  .setReadme(fs.readFileSync('./README.md', 'utf8'))
45
47
  .setClientSecret(process.env.BKPER_CLIENT_SECRET)
46
48
  .setDeveloperEmail(process.env.BKPER_DEVELOPER_EMAIL)
package/lib/index.d.ts CHANGED
@@ -97,15 +97,13 @@ export declare class Account {
97
97
  deleteProperty(key: string): Account;
98
98
  /**
99
99
  * Gets the balance based on credit nature of this Account.
100
- *
101
- * @param raw - True to get the raw balance, no matter the credit nature of this Account.
102
- *
100
+ * @deprecated Use `Book.getBalancesReport` instead.
103
101
  * @returns The balance of this account.
104
102
  */
105
- getBalance(raw?: boolean): Amount;
103
+ getBalance(): Amount;
106
104
  /**
107
105
  * Gets the raw balance, no matter credit nature of this Account.
108
- *
106
+ * @deprecated Use `Book.getBalancesReport` instead.
109
107
  * @returns The balance of this account.
110
108
  */
111
109
  getBalanceRaw(): Amount;
@@ -354,25 +352,21 @@ export declare class Bkper {
354
352
  */
355
353
  static getBook(id: string): Promise<Book>;
356
354
  /**
357
- * Sets the API key to identify the agent.
358
- *
359
- * API keys are intended for agent identification only, not for authentication. [Learn more](https://cloud.google.com/endpoints/docs/frameworks/java/when-why-api-key)
360
- *
361
- * See how to create your api key [here](https://cloud.google.com/docs/authentication/api-keys).
362
- *
363
- * @param key - The key from GCP API & Services Credentials console.
364
- *
355
+ * Gets the current logged [[User]].
356
+ */
357
+ static getUser(): Promise<User>;
358
+ /**
359
+ * Sets the API [[Config]] object.
360
+ */
361
+ static setConfig(config: Config): void;
362
+ /**
363
+ * @deprecated Use `setConfig()` instead
365
364
  */
366
365
  static setApiKey(key: string): App;
367
366
  /**
368
- * Sets the [[OAuthTokenProvider]].
369
- *
370
- * OAuthTokenProvider issue a valid OAuth token upon calling the Bkper Rest API.
371
- *
372
- * @param oauthTokenProvider - The [[OAuthTokenProvider]] implementation.
373
- *
367
+ * @deprecated Use `setConfig()` instead
374
368
  */
375
- static setOAuthTokenProvider(oauthTokenProvider: OAuthTokenProvider): Promise<void>;
369
+ static setOAuthTokenProvider(oauthTokenProvider: () => Promise<string>): Promise<void>;
376
370
  }
377
371
 
378
372
  /**
@@ -388,11 +382,8 @@ export declare class Book {
388
382
 
389
383
 
390
384
 
391
-
392
-
393
385
  constructor(json: bkper.Book);
394
386
  /**
395
- *
396
387
  * @returns The wrapped plain json object
397
388
  */
398
389
  json(): bkper.Book;
@@ -584,7 +575,7 @@ export declare class Book {
584
575
  *
585
576
  * @returns The value formated
586
577
  */
587
- formatValue(value: Amount | number): string;
578
+ formatValue(value: Amount | number | null | undefined): string;
588
579
  /**
589
580
  * Parse a value string according to [[DecimalSeparator]] and fraction digits of the Book.
590
581
  */
@@ -601,10 +592,17 @@ export declare class Book {
601
592
  * Create [[Transactions]] on the Book, in batch.
602
593
  */
603
594
  batchCreateTransactions(transactions: Transaction[]): Promise<Transaction[]>;
595
+ /**
596
+ * Trash [[Transactions]] on the Book, in batch.
597
+ */
598
+ batchTrashTransactions(transactions: Transaction[]): Promise<void>;
604
599
  /**
605
600
  * Trigger [Balances Audit](https://help.bkper.com/en/articles/4412038-balances-audit) async process.
606
601
  */
607
602
  audit(): void;
603
+ getIntegrations(): Promise<Integration[]>;
604
+ createIntegration(integration: bkper.Integration | Integration): Promise<Integration>;
605
+ updateIntegration(integration: bkper.Integration): Promise<Integration>;
608
606
  /**
609
607
  * Resumes a transaction iteration using a continuation token from a previous iterator.
610
608
  *
@@ -763,6 +761,141 @@ export declare class Collection {
763
761
  getBooks(): Book[];
764
762
  }
765
763
 
764
+ declare interface Config {
765
+ /**
766
+ * The API key to identify the agent.
767
+ *
768
+ * API keys are intended for agent identification only, not for authentication. [Learn more](https://cloud.google.com/endpoints/docs/frameworks/java/when-why-api-key)
769
+ *
770
+ * See how to create your api key [here](https://cloud.google.com/docs/authentication/api-keys).
771
+ */
772
+ apiKeyProvider?: () => Promise<string>;
773
+ oauthTokenProvider?: () => Promise<string>;
774
+ /**
775
+ * Provides additional headers to append to the API request
776
+ */
777
+ requestHeadersProvider?: () => Promise<{
778
+ [key: string]: string;
779
+ }>;
780
+ requestErrorHandler?: (error: any) => any;
781
+ /**
782
+ * Sets the base api url. Default to https://app.bkper.com/_ah/api/bkper
783
+ */
784
+ apiBaseUrl?: string;
785
+ }
786
+
787
+ /**
788
+ * This class defines a Connection from an User to an external service.
789
+ *
790
+ * @public
791
+ */
792
+ export declare class Connection {
793
+
794
+ constructor(json?: bkper.Connection);
795
+ /**
796
+ * @returns The wrapped plain json object
797
+ */
798
+ json(): bkper.Connection;
799
+ /**
800
+ * @returns The id of this User
801
+ */
802
+ getId(): string;
803
+ /**
804
+ * @returns The Connection agentId
805
+ */
806
+ getAgentId(): string;
807
+ /**
808
+ * Sets the Connection agentId
809
+ *
810
+ * @returns This Connection, for chainning.
811
+ */
812
+ setAgentId(agentId: string): Connection;
813
+ /**
814
+ * @returns The name of this Connection
815
+ */
816
+ getName(): string;
817
+ /**
818
+ * @returns The email of the owner of this Connection
819
+ */
820
+ getEmail(): string;
821
+ /**
822
+ * Sets the name of the Connection.
823
+ *
824
+ * @returns This Connection, for chainning.
825
+ */
826
+ setName(name: string): Connection;
827
+ /**
828
+ * Sets the universal unique identifier of this connection.
829
+ *
830
+ * @returns This Connection, for chainning.
831
+ */
832
+ setUUID(uuid: string): Connection;
833
+ /**
834
+ * @returns The name of universal unique identifier of the connection
835
+ */
836
+ getUUID(): string;
837
+ /**
838
+ * @returns The connection type
839
+ */
840
+ getType(): "APP" | "BANK";
841
+ /**
842
+ * Sets the connection type.
843
+ *
844
+ * @returns This Connection, for chainning.
845
+ */
846
+ setType(type: "APP" | "BANK"): Connection;
847
+ /**
848
+ * Gets the custom properties stored in this Connection
849
+ */
850
+ getProperties(): {
851
+ [key: string]: string;
852
+ };
853
+ /**
854
+ * Sets the custom properties of the Connection
855
+ *
856
+ * @param properties - Object with key/value pair properties
857
+ *
858
+ * @returns This Connection, for chainning.
859
+ */
860
+ setProperties(properties: {
861
+ [key: string]: string;
862
+ }): Connection;
863
+ /**
864
+ * Gets the property value for given keys. First property found will be retrieved
865
+ *
866
+ * @param keys - The property key
867
+ */
868
+ getProperty(...keys: string[]): string;
869
+ /**
870
+ * Sets a custom property in the Connection.
871
+ *
872
+ * @param key - The property key
873
+ * @param value - The property value
874
+ */
875
+ setProperty(key: string, value: string): Connection;
876
+ /**
877
+ * Delete a custom property
878
+ *
879
+ * @param key - The property key
880
+ *
881
+ * @returns This Connection, for chainning.
882
+ */
883
+ deleteProperty(key: string): Connection;
884
+ /**
885
+ * Clean any token property
886
+ */
887
+ clearTokenProperties(): void;
888
+ /**
889
+ * Gets the custom properties keys stored in the Connection.
890
+ */
891
+ getPropertyKeys(): string[];
892
+ getIntegrations(): Promise<Integration[]>;
893
+ /**
894
+ * Perform create new connection.
895
+ */
896
+ create(): Promise<Connection>;
897
+ }
898
+
766
899
  /**
767
900
  * Decimal separator of numbers on book
768
901
  *
@@ -958,6 +1091,66 @@ export declare class Group {
958
1091
  remove(): Promise<Group>;
959
1092
  }
960
1093
 
1094
+ /**
1095
+ * This class defines a Integration from an User to an external service.
1096
+ *
1097
+ * @public
1098
+ */
1099
+ export declare class Integration {
1100
+
1101
+ constructor(json: bkper.Integration);
1102
+ /**
1103
+ * @returns The wrapped plain json object
1104
+ */
1105
+ json(): bkper.Integration;
1106
+ getBookId(): string;
1107
+ /**
1108
+ * @returns The id of this User
1109
+ */
1110
+ getId(): string;
1111
+ /**
1112
+ * @returns The name of this Integration
1113
+ */
1114
+ getName(): string;
1115
+ /**
1116
+ * Gets the custom properties stored in this Integration
1117
+ */
1118
+ getProperties(): {
1119
+ [key: string]: string;
1120
+ };
1121
+ /**
1122
+ * Sets the custom properties of the Integration
1123
+ *
1124
+ * @param properties - Object with key/value pair properties
1125
+ *
1126
+ * @returns This Integration, for chainning.
1127
+ */
1128
+ setProperties(properties: {
1129
+ [key: string]: string;
1130
+ }): Integration;
1131
+ /**
1132
+ * Gets the property value for given keys. First property found will be retrieved
1133
+ *
1134
+ * @param keys - The property key
1135
+ */
1136
+ getProperty(...keys: string[]): string;
1137
+ /**
1138
+ * Sets a custom property in the Integration.
1139
+ *
1140
+ * @param key - The property key
1141
+ * @param value - The property value
1142
+ */
1143
+ setProperty(key: string, value: string): Integration;
1144
+ /**
1145
+ * Delete a custom property
1146
+ *
1147
+ * @param key - The property key
1148
+ *
1149
+ * @returns This Integration, for chainning.
1150
+ */
1151
+ deleteProperty(key: string): Integration;
1152
+ }
1153
+
961
1154
  /**
962
1155
  * Enum that represents a Month.
963
1156
  *
@@ -978,18 +1171,6 @@ declare enum Month {
978
1171
  DECEMBER = "DECEMBER"
979
1172
  }
980
1173
 
981
- /**
982
- * Interface to provide OAuth2 tokens upon calling the API.
983
- *
984
- * @public
985
- */
986
- export declare interface OAuthTokenProvider {
987
- /**
988
- * A valid OAuth2 access token with **https://www.googleapis.com/auth/userinfo.email** scope authorized.
989
- */
990
- (): Promise<string>;
991
- }
992
-
993
1174
  /**
994
1175
  * Enum that represents a period slice.
995
1176
  *
@@ -1237,7 +1418,7 @@ export declare class Transaction {
1237
1418
  getDebitAccountName(): Promise<string>;
1238
1419
  /**
1239
1420
  *
1240
- * Sets the debit/origin Account of the Transaction. Same as to().
1421
+ * Sets the debit/destination Account of the Transaction. Same as to().
1241
1422
  *
1242
1423
  * @param account - Account id, name or object.
1243
1424
  *
@@ -1246,7 +1427,7 @@ export declare class Transaction {
1246
1427
  setDebitAccount(account: Account | bkper.Account): Transaction;
1247
1428
  /**
1248
1429
  *
1249
- * Sets the debit/origin Account of the Transaction. Same as setDebitAccount().
1430
+ * Sets the debit/destination Account of the Transaction. Same as setDebitAccount().
1250
1431
  *
1251
1432
  * @param account - Account id, name or object.
1252
1433
  *
@@ -1444,4 +1625,31 @@ export declare class TransactionIterator {
1444
1625
  getAccount(): Promise<Account>;
1445
1626
  }
1446
1627
 
1628
+ /**
1629
+ * This class defines a User.
1630
+ *
1631
+ * @public
1632
+ */
1633
+ export declare class User {
1634
+
1635
+ constructor(wrapped: bkper.User);
1636
+ /**
1637
+ * @returns The id of the User
1638
+ */
1639
+ getId(): string;
1640
+ /**
1641
+ * @returns The name of the User
1642
+ */
1643
+ getName(): string;
1644
+ /**
1645
+ * @returns The full name of the User
1646
+ */
1647
+ getFullName(): string;
1648
+ /**
1649
+ * @returns The User connections
1650
+ */
1651
+ getConnections(): Promise<Connection[]>;
1652
+ getConnection(id: string): Promise<Connection>;
1653
+ }
1654
+
1447
1655
  export { }
package/lib/index.js CHANGED
@@ -7,7 +7,7 @@
7
7
  * @packageDocumentation
8
8
  */
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.Permission = exports.DecimalSeparator = exports.AccountType = exports.Periodicity = exports.TransactionIterator = exports.Transaction = exports.Group = exports.File = exports.Collection = exports.Book = exports.Account = exports.Bkper = exports.Amount = exports.App = void 0;
10
+ exports.Permission = exports.DecimalSeparator = exports.AccountType = exports.Periodicity = exports.Connection = exports.Integration = exports.User = exports.TransactionIterator = exports.Transaction = exports.Group = exports.File = exports.Collection = exports.Book = exports.Account = exports.Bkper = exports.Amount = exports.App = void 0;
11
11
  var App_1 = require("./model/App");
12
12
  Object.defineProperty(exports, "App", { enumerable: true, get: function () { return App_1.App; } });
13
13
  var Amount_1 = require("./model/Amount");
@@ -28,6 +28,12 @@ var Transaction_1 = require("./model/Transaction");
28
28
  Object.defineProperty(exports, "Transaction", { enumerable: true, get: function () { return Transaction_1.Transaction; } });
29
29
  var TransactionIterator_1 = require("./model/TransactionIterator");
30
30
  Object.defineProperty(exports, "TransactionIterator", { enumerable: true, get: function () { return TransactionIterator_1.TransactionIterator; } });
31
+ var User_1 = require("./model/User");
32
+ Object.defineProperty(exports, "User", { enumerable: true, get: function () { return User_1.User; } });
33
+ var Integration_1 = require("./model/Integration");
34
+ Object.defineProperty(exports, "Integration", { enumerable: true, get: function () { return Integration_1.Integration; } });
35
+ var Connection_1 = require("./model/Connection");
36
+ Object.defineProperty(exports, "Connection", { enumerable: true, get: function () { return Connection_1.Connection; } });
31
37
  var Enums_1 = require("./model/Enums");
32
38
  Object.defineProperty(exports, "Periodicity", { enumerable: true, get: function () { return Enums_1.Periodicity; } });
33
39
  Object.defineProperty(exports, "AccountType", { enumerable: true, get: function () { return Enums_1.AccountType; } });
@@ -165,26 +165,19 @@ class Account {
165
165
  }
166
166
  /**
167
167
  * Gets the balance based on credit nature of this Account.
168
- *
169
- * @param raw - True to get the raw balance, no matter the credit nature of this Account.
170
- *
168
+ * @deprecated Use `Book.getBalancesReport` instead.
171
169
  * @returns The balance of this account.
172
170
  */
173
- getBalance(raw) {
171
+ getBalance() {
174
172
  var balance = new Amount_1.Amount('0');
175
173
  if (this.wrapped.balance != null) {
176
174
  balance = utils_1.round(this.wrapped.balance, this.book.getFractionDigits());
177
175
  }
178
- if (raw) {
179
- return balance;
180
- }
181
- else {
182
- return utils_1.getRepresentativeValue(balance, this.isCredit());
183
- }
176
+ return balance;
184
177
  }
185
178
  /**
186
179
  * Gets the raw balance, no matter credit nature of this Account.
187
- *
180
+ * @deprecated Use `Book.getBalancesReport` instead.
188
181
  * @returns The balance of this account.
189
182
  */
190
183
  getBalanceRaw() {
@@ -290,7 +283,7 @@ class Account {
290
283
  this.wrapped.groups = [];
291
284
  }
292
285
  if (group instanceof Group_1.Group) {
293
- this.wrapped.groups.push(group.wrapped);
286
+ this.wrapped.groups.push(group.json());
294
287
  }
295
288
  else {
296
289
  this.wrapped.groups.push(group);
@@ -32,7 +32,9 @@ exports.Bkper = void 0;
32
32
  const Book_1 = require("./Book");
33
33
  const App_1 = require("./App");
34
34
  const BookService = __importStar(require("../service/book-service"));
35
+ const UserService = __importStar(require("../service/user-service"));
35
36
  const HttpApiRequest_1 = require("../service/HttpApiRequest");
37
+ const User_1 = require("./User");
36
38
  /**
37
39
  * This is the main Entry Point of the [bkper-node](https://www.npmjs.com/package/bkper) library.
38
40
  *
@@ -52,30 +54,33 @@ class Bkper {
52
54
  });
53
55
  }
54
56
  /**
55
- * Sets the API key to identify the agent.
56
- *
57
- * API keys are intended for agent identification only, not for authentication. [Learn more](https://cloud.google.com/endpoints/docs/frameworks/java/when-why-api-key)
58
- *
59
- * See how to create your api key [here](https://cloud.google.com/docs/authentication/api-keys).
60
- *
61
- * @param key - The key from GCP API & Services Credentials console.
62
- *
57
+ * Gets the current logged [[User]].
58
+ */
59
+ static getUser() {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ let user = yield UserService.getUser();
62
+ return new User_1.User(user);
63
+ });
64
+ }
65
+ /**
66
+ * Sets the API [[Config]] object.
67
+ */
68
+ static setConfig(config) {
69
+ HttpApiRequest_1.HttpApiRequest.config = config;
70
+ }
71
+ /**
72
+ * @deprecated Use `setConfig()` instead
63
73
  */
64
74
  static setApiKey(key) {
65
- HttpApiRequest_1.HttpApiRequest.API_KEY = key;
75
+ HttpApiRequest_1.HttpApiRequest.config.apiKeyProvider = () => __awaiter(this, void 0, void 0, function* () { return key; });
66
76
  return new App_1.App();
67
77
  }
68
78
  /**
69
- * Sets the [[OAuthTokenProvider]].
70
- *
71
- * OAuthTokenProvider issue a valid OAuth token upon calling the Bkper Rest API.
72
- *
73
- * @param oauthTokenProvider - The [[OAuthTokenProvider]] implementation.
74
- *
79
+ * @deprecated Use `setConfig()` instead
75
80
  */
76
81
  static setOAuthTokenProvider(oauthTokenProvider) {
77
82
  return __awaiter(this, void 0, void 0, function* () {
78
- HttpApiRequest_1.HttpApiRequest.OAUTH_TOKEN_PROVIDER = oauthTokenProvider;
83
+ HttpApiRequest_1.HttpApiRequest.config.oauthTokenProvider = oauthTokenProvider;
79
84
  });
80
85
  }
81
86
  }
package/lib/model/Book.js CHANGED
@@ -34,6 +34,7 @@ const GroupService = __importStar(require("../service/group-service"));
34
34
  const BookService = __importStar(require("../service/book-service"));
35
35
  const FileService = __importStar(require("../service/file-service"));
36
36
  const TransactionService = __importStar(require("../service/transaction-service"));
37
+ const IntegrationService = __importStar(require("../service/integration-service"));
37
38
  const Utils = __importStar(require("../utils"));
38
39
  const utils_1 = require("../utils");
39
40
  const Account_1 = require("./Account");
@@ -42,6 +43,7 @@ const File_1 = require("./File");
42
43
  const Group_1 = require("./Group");
43
44
  const Transaction_1 = require("./Transaction");
44
45
  const TransactionIterator_1 = require("./TransactionIterator");
46
+ const Integration_1 = require("./Integration");
45
47
  /**
46
48
  *
47
49
  * A Book represents [General Ledger](https://en.wikipedia.org/wiki/General_ledger) for a company or business, but can also represent a [Ledger](https://en.wikipedia.org/wiki/Ledger) for a project or department
@@ -55,7 +57,6 @@ class Book {
55
57
  this.wrapped = json;
56
58
  }
57
59
  /**
58
- *
59
60
  * @returns The wrapped plain json object
60
61
  */
61
62
  json() {
@@ -349,6 +350,9 @@ class Book {
349
350
  * @returns The value formated
350
351
  */
351
352
  formatValue(value) {
353
+ if (!value) {
354
+ return '';
355
+ }
352
356
  return Utils.formatValue(value, this.getDecimalSeparator(), this.getFractionDigits());
353
357
  }
354
358
  /**
@@ -380,12 +384,51 @@ class Book {
380
384
  return transactions;
381
385
  });
382
386
  }
387
+ /**
388
+ * Trash [[Transactions]] on the Book, in batch.
389
+ */
390
+ batchTrashTransactions(transactions) {
391
+ return __awaiter(this, void 0, void 0, function* () {
392
+ let transactionPayloads = [];
393
+ transactions.forEach(tx => transactionPayloads.push(tx.wrapped));
394
+ yield TransactionService.trashTransactionsBatch(this.getId(), transactionPayloads);
395
+ });
396
+ }
383
397
  /**
384
398
  * Trigger [Balances Audit](https://help.bkper.com/en/articles/4412038-balances-audit) async process.
385
399
  */
386
400
  audit() {
387
401
  BookService.audit(this.getId());
388
402
  }
403
+ getIntegrations() {
404
+ return __awaiter(this, void 0, void 0, function* () {
405
+ const integrationsPlain = yield IntegrationService.listIntegrations(this.getId());
406
+ const integrations = integrationsPlain.map(i => new Integration_1.Integration(i));
407
+ return integrations;
408
+ });
409
+ }
410
+ createIntegration(integration) {
411
+ return __awaiter(this, void 0, void 0, function* () {
412
+ if (integration instanceof Integration_1.Integration) {
413
+ integration = yield IntegrationService.createIntegration(this.getId(), integration.json());
414
+ }
415
+ else {
416
+ integration = yield IntegrationService.createIntegration(this.getId(), integration);
417
+ }
418
+ return new Integration_1.Integration(integration);
419
+ });
420
+ }
421
+ updateIntegration(integration) {
422
+ return __awaiter(this, void 0, void 0, function* () {
423
+ if (integration instanceof Integration_1.Integration) {
424
+ integration = yield IntegrationService.updateIntegration(this.getId(), integration.json());
425
+ }
426
+ else {
427
+ integration = yield IntegrationService.updateIntegration(this.getId(), integration);
428
+ }
429
+ return new Integration_1.Integration(integration);
430
+ });
431
+ }
389
432
  /**
390
433
  * Resumes a transaction iteration using a continuation token from a previous iterator.
391
434
  *
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=OAuthTokenProvider.js.map
3
+ //# sourceMappingURL=Config.js.map