bkper-js 2.12.4 → 2.13.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/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@ See what's new and what has changed in bkper-js
4
4
 
5
5
  ## 2025
6
6
 
7
+ **October 2025**
8
+
9
+ - Files attached to transactions are now created internally when transaction is persisted
10
+ - Added `Transaction.removeFile`
11
+
7
12
  **September 2025**
8
13
 
9
14
  - **v2.8.0 - INTERNAL REFACTOR:**
@@ -42,7 +47,6 @@ See what's new and what has changed in bkper-js
42
47
 
43
48
  **August 2025**
44
49
 
45
- - Added `Transaction.setFiles`
46
50
  - Added `File.getProperties`
47
51
  - Added `File.setProperties`
48
52
  - Added `File.getProperty`
package/lib/index.d.ts CHANGED
@@ -3297,6 +3297,7 @@ export declare class Template extends Resource<bkper.Template> {
3297
3297
  */
3298
3298
  export declare class Transaction extends Resource<bkper.Transaction> {
3299
3299
 
3300
+
3300
3301
  constructor(book: Book, payload?: bkper.Transaction);
3301
3302
 
3302
3303
  /**
@@ -3416,23 +3417,24 @@ export declare class Transaction extends Resource<bkper.Transaction> {
3416
3417
  */
3417
3418
  getFiles(): File[];
3418
3419
  /**
3419
- * Sets the files attached to the Transaction.
3420
+ * Removes a file attachment from the Transaction.
3420
3421
  *
3421
- * @param files - The files to set
3422
+ * @param file - The File to remove from this Transaction
3422
3423
  *
3423
3424
  * @returns This Transaction, for chaining
3424
3425
  */
3425
- setFiles(files: File[]): Transaction;
3426
+ removeFile(file: File): Transaction;
3426
3427
  /**
3427
3428
  * Adds a file attachment to the Transaction.
3428
3429
  *
3429
- * Files MUST be previously created in the Book.
3430
+ * Files not previously created in the Book will be automatically created when the transaction is persisted.
3430
3431
  *
3431
- * @param file - The file to add
3432
+ * @param file - The File to add to this Transaction
3432
3433
  *
3433
3434
  * @returns This Transaction, for chaining
3434
3435
  */
3435
3436
  addFile(file: File): Transaction;
3437
+
3436
3438
  /**
3437
3439
  * Check if the transaction has the specified tag.
3438
3440
  *
@@ -13,6 +13,7 @@ import { Resource } from "./Resource.js";
13
13
  import * as TransactionService from "../service/transaction-service.js";
14
14
  import * as Utils from "../utils.js";
15
15
  import { Amount } from "./Amount.js";
16
+ import { v4 as uuidv4 } from "uuid";
16
17
  /**
17
18
  *
18
19
  * This class defines a Transaction between [credit and debit](http://en.wikipedia.org/wiki/Debits_and_credits) [[Accounts]].
@@ -24,6 +25,8 @@ import { Amount } from "./Amount.js";
24
25
  export class Transaction extends Resource {
25
26
  constructor(book, payload) {
26
27
  super(payload || { createdAt: `${Date.now()}` });
28
+ /** @internal */
29
+ this.pendingFiles = new Map();
27
30
  this.book = book;
28
31
  }
29
32
  /** @internal */
@@ -219,38 +222,80 @@ export class Transaction extends Resource {
219
222
  }
220
223
  }
221
224
  /**
222
- * Sets the files attached to the Transaction.
225
+ * Removes a file attachment from the Transaction.
223
226
  *
224
- * @param files - The files to set
227
+ * @param file - The File to remove from this Transaction
225
228
  *
226
229
  * @returns This Transaction, for chaining
227
230
  */
228
- setFiles(files) {
229
- const filePayloads = files.map((file) => {
230
- return {
231
- id: file.getId(),
232
- name: file.getName()
233
- };
234
- });
235
- this.payload.files = filePayloads;
231
+ removeFile(file) {
232
+ const fileId = file.getId();
233
+ if (fileId) {
234
+ if (this.payload.files != null) {
235
+ this.payload.files = this.payload.files.filter(f => f.id !== fileId);
236
+ }
237
+ this.pendingFiles.delete(fileId);
238
+ }
236
239
  return this;
237
240
  }
238
241
  /**
239
242
  * Adds a file attachment to the Transaction.
240
243
  *
241
- * Files MUST be previously created in the Book.
244
+ * Files not previously created in the Book will be automatically created when the transaction is persisted.
242
245
  *
243
- * @param file - The file to add
246
+ * @param file - The File to add to this Transaction
244
247
  *
245
248
  * @returns This Transaction, for chaining
246
249
  */
247
250
  addFile(file) {
251
+ var _a;
248
252
  if (this.payload.files == null) {
249
253
  this.payload.files = [];
250
254
  }
255
+ // Store file reference for later creation if needed
256
+ const fileId = file.getId();
257
+ const fileBookId = (_a = file.getBook()) === null || _a === void 0 ? void 0 : _a.getId();
258
+ if (fileId == null || fileBookId != this.book.getId()) {
259
+ // Generate temporary ID if file doesn't have one
260
+ if (fileId == null) {
261
+ file.payload.id = `temporary_${uuidv4()}`;
262
+ }
263
+ this.pendingFiles.set(file.getId(), file);
264
+ }
251
265
  this.payload.files.push(file.json());
252
266
  return this;
253
267
  }
268
+ /** @internal */
269
+ createPendingFiles() {
270
+ return __awaiter(this, void 0, void 0, function* () {
271
+ if (this.pendingFiles.size === 0) {
272
+ return;
273
+ }
274
+ if (this.payload.files == null) {
275
+ this.payload.files = [];
276
+ }
277
+ // Create all pending files in parallel
278
+ const promises = Array.from(this.pendingFiles.entries()).map((_a) => __awaiter(this, [_a], void 0, function* ([fileId, file]) {
279
+ file.book = this.book;
280
+ file.setProperty('upload_method_', 'attachment');
281
+ const createdFile = yield file.create();
282
+ return { fileId, createdFile };
283
+ }));
284
+ const results = yield Promise.all(promises);
285
+ // Update payload with all created files
286
+ for (const { fileId, createdFile } of results) {
287
+ const fileIndex = this.payload.files.findIndex(f => f.id === fileId);
288
+ if (fileIndex >= 0) {
289
+ this.payload.files[fileIndex] = createdFile.json();
290
+ }
291
+ else {
292
+ this.payload.files.push(createdFile.json());
293
+ }
294
+ }
295
+ // Clear pending files after creation
296
+ this.pendingFiles.clear();
297
+ });
298
+ }
254
299
  /**
255
300
  * Check if the transaction has the specified tag.
256
301
  *
@@ -795,6 +840,7 @@ export class Transaction extends Resource {
795
840
  */
796
841
  create() {
797
842
  return __awaiter(this, void 0, void 0, function* () {
843
+ yield this.createPendingFiles();
798
844
  let operation = yield TransactionService.createTransaction(this.book.getId(), this.payload, this.getConfig());
799
845
  this.payload = operation.transaction || {};
800
846
  return this;
@@ -807,6 +853,7 @@ export class Transaction extends Resource {
807
853
  */
808
854
  update() {
809
855
  return __awaiter(this, void 0, void 0, function* () {
856
+ yield this.createPendingFiles();
810
857
  let operation = yield TransactionService.updateTransaction(this.book.getId(), this.payload, this.getConfig());
811
858
  this.payload = operation.transaction || {};
812
859
  return this;
@@ -845,6 +892,7 @@ export class Transaction extends Resource {
845
892
  */
846
893
  post() {
847
894
  return __awaiter(this, void 0, void 0, function* () {
895
+ yield this.createPendingFiles();
848
896
  let operation = yield TransactionService.postTransaction(this.book.getId(), this.payload, this.getConfig());
849
897
  this.payload = operation.transaction || {};
850
898
  return this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bkper-js",
3
- "version": "2.12.4",
3
+ "version": "2.13.0",
4
4
  "description": "Javascript client for Bkper REST API",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",
@@ -40,7 +40,8 @@
40
40
  "dependencies": {
41
41
  "big.js": "^6.0.3",
42
42
  "dayjs": "^1.10.3",
43
- "luxon": "^1.25.0"
43
+ "luxon": "^1.25.0",
44
+ "uuid": "^11.0.3"
44
45
  },
45
46
  "devDependencies": {
46
47
  "@microsoft/api-extractor": "^7.52.12",
@@ -50,6 +51,7 @@
50
51
  "@types/mocha": "^8.2.0",
51
52
  "@types/node": "^14.14.20",
52
53
  "@types/node-fetch": "^2.5.8",
54
+ "@types/uuid": "^10.0.0",
53
55
  "chai": "^5.1.1",
54
56
  "gts": "^3.0.3",
55
57
  "mocha": "^10.7.3",