multivendor-wallet 0.0.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.
@@ -0,0 +1,764 @@
1
+ import { PrimaryGeneratedColumn, Column, BeforeInsert, BeforeUpdate, Entity } from 'typeorm';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ function _ts_decorate(decorators, target, key, desc) {
8
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
9
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
10
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
11
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
12
+ }
13
+ __name(_ts_decorate, "_ts_decorate");
14
+ function _ts_metadata(k, v) {
15
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
16
+ }
17
+ __name(_ts_metadata, "_ts_metadata");
18
+ function nowTimestamp() {
19
+ return (/* @__PURE__ */ new Date()).toISOString().slice(0, 19).replace("T", " ");
20
+ }
21
+ __name(nowTimestamp, "nowTimestamp");
22
+ var _WalletAccountEntity = class _WalletAccountEntity {
23
+ constructor() {
24
+ __publicField(this, "id");
25
+ __publicField(this, "ownerType");
26
+ __publicField(this, "ownerId");
27
+ __publicField(this, "currency");
28
+ __publicField(this, "availableBalance");
29
+ __publicField(this, "heldBalance");
30
+ __publicField(this, "createdBy");
31
+ __publicField(this, "createdDate");
32
+ __publicField(this, "modifiedBy");
33
+ __publicField(this, "modifiedDate");
34
+ }
35
+ setCreated() {
36
+ this.createdDate = nowTimestamp();
37
+ this.modifiedDate = nowTimestamp();
38
+ }
39
+ setModified() {
40
+ this.modifiedDate = nowTimestamp();
41
+ }
42
+ };
43
+ __name(_WalletAccountEntity, "WalletAccountEntity");
44
+ var WalletAccountEntity = _WalletAccountEntity;
45
+ _ts_decorate([
46
+ PrimaryGeneratedColumn({
47
+ name: "id"
48
+ }),
49
+ _ts_metadata("design:type", Number)
50
+ ], WalletAccountEntity.prototype, "id", void 0);
51
+ _ts_decorate([
52
+ Column({
53
+ name: "owner_type",
54
+ length: 32
55
+ }),
56
+ _ts_metadata("design:type", String)
57
+ ], WalletAccountEntity.prototype, "ownerType", void 0);
58
+ _ts_decorate([
59
+ Column({
60
+ name: "owner_id"
61
+ }),
62
+ _ts_metadata("design:type", Number)
63
+ ], WalletAccountEntity.prototype, "ownerId", void 0);
64
+ _ts_decorate([
65
+ Column({
66
+ name: "currency",
67
+ length: 8,
68
+ default: "INR"
69
+ }),
70
+ _ts_metadata("design:type", String)
71
+ ], WalletAccountEntity.prototype, "currency", void 0);
72
+ _ts_decorate([
73
+ Column({
74
+ name: "available_balance",
75
+ type: "bigint",
76
+ default: 0
77
+ }),
78
+ _ts_metadata("design:type", String)
79
+ ], WalletAccountEntity.prototype, "availableBalance", void 0);
80
+ _ts_decorate([
81
+ Column({
82
+ name: "held_balance",
83
+ type: "bigint",
84
+ default: 0
85
+ }),
86
+ _ts_metadata("design:type", String)
87
+ ], WalletAccountEntity.prototype, "heldBalance", void 0);
88
+ _ts_decorate([
89
+ Column({
90
+ name: "created_by",
91
+ nullable: true
92
+ }),
93
+ _ts_metadata("design:type", Number)
94
+ ], WalletAccountEntity.prototype, "createdBy", void 0);
95
+ _ts_decorate([
96
+ Column({
97
+ name: "created_date",
98
+ nullable: true
99
+ }),
100
+ _ts_metadata("design:type", String)
101
+ ], WalletAccountEntity.prototype, "createdDate", void 0);
102
+ _ts_decorate([
103
+ Column({
104
+ name: "modified_by",
105
+ nullable: true
106
+ }),
107
+ _ts_metadata("design:type", Number)
108
+ ], WalletAccountEntity.prototype, "modifiedBy", void 0);
109
+ _ts_decorate([
110
+ Column({
111
+ name: "modified_date",
112
+ nullable: true
113
+ }),
114
+ _ts_metadata("design:type", String)
115
+ ], WalletAccountEntity.prototype, "modifiedDate", void 0);
116
+ _ts_decorate([
117
+ BeforeInsert(),
118
+ _ts_metadata("design:type", Function),
119
+ _ts_metadata("design:paramtypes", []),
120
+ _ts_metadata("design:returntype", void 0)
121
+ ], WalletAccountEntity.prototype, "setCreated", null);
122
+ _ts_decorate([
123
+ BeforeUpdate(),
124
+ _ts_metadata("design:type", Function),
125
+ _ts_metadata("design:paramtypes", []),
126
+ _ts_metadata("design:returntype", void 0)
127
+ ], WalletAccountEntity.prototype, "setModified", null);
128
+ WalletAccountEntity = _ts_decorate([
129
+ Entity("wallet_account")
130
+ ], WalletAccountEntity);
131
+ var _WalletLedgerEntryEntity = class _WalletLedgerEntryEntity {
132
+ constructor() {
133
+ __publicField(this, "id");
134
+ __publicField(this, "accountId");
135
+ __publicField(this, "entryType");
136
+ __publicField(this, "amount");
137
+ __publicField(this, "balanceAfterAvailable");
138
+ __publicField(this, "balanceAfterHeld");
139
+ __publicField(this, "idempotencyKey");
140
+ __publicField(this, "referenceType");
141
+ __publicField(this, "referenceId");
142
+ __publicField(this, "metadata");
143
+ __publicField(this, "createdBy");
144
+ __publicField(this, "createdDate");
145
+ __publicField(this, "modifiedBy");
146
+ __publicField(this, "modifiedDate");
147
+ }
148
+ setCreated() {
149
+ this.createdDate = nowTimestamp();
150
+ this.modifiedDate = nowTimestamp();
151
+ }
152
+ };
153
+ __name(_WalletLedgerEntryEntity, "WalletLedgerEntryEntity");
154
+ var WalletLedgerEntryEntity = _WalletLedgerEntryEntity;
155
+ _ts_decorate([
156
+ PrimaryGeneratedColumn({
157
+ name: "id"
158
+ }),
159
+ _ts_metadata("design:type", Number)
160
+ ], WalletLedgerEntryEntity.prototype, "id", void 0);
161
+ _ts_decorate([
162
+ Column({
163
+ name: "account_id"
164
+ }),
165
+ _ts_metadata("design:type", Number)
166
+ ], WalletLedgerEntryEntity.prototype, "accountId", void 0);
167
+ _ts_decorate([
168
+ Column({
169
+ name: "entry_type",
170
+ length: 32
171
+ }),
172
+ _ts_metadata("design:type", String)
173
+ ], WalletLedgerEntryEntity.prototype, "entryType", void 0);
174
+ _ts_decorate([
175
+ Column({
176
+ name: "amount",
177
+ type: "bigint"
178
+ }),
179
+ _ts_metadata("design:type", String)
180
+ ], WalletLedgerEntryEntity.prototype, "amount", void 0);
181
+ _ts_decorate([
182
+ Column({
183
+ name: "balance_after_available",
184
+ type: "bigint"
185
+ }),
186
+ _ts_metadata("design:type", String)
187
+ ], WalletLedgerEntryEntity.prototype, "balanceAfterAvailable", void 0);
188
+ _ts_decorate([
189
+ Column({
190
+ name: "balance_after_held",
191
+ type: "bigint"
192
+ }),
193
+ _ts_metadata("design:type", String)
194
+ ], WalletLedgerEntryEntity.prototype, "balanceAfterHeld", void 0);
195
+ _ts_decorate([
196
+ Column({
197
+ name: "idempotency_key",
198
+ length: 255,
199
+ unique: true
200
+ }),
201
+ _ts_metadata("design:type", String)
202
+ ], WalletLedgerEntryEntity.prototype, "idempotencyKey", void 0);
203
+ _ts_decorate([
204
+ Column({
205
+ name: "reference_type",
206
+ length: 64,
207
+ nullable: true
208
+ }),
209
+ _ts_metadata("design:type", String)
210
+ ], WalletLedgerEntryEntity.prototype, "referenceType", void 0);
211
+ _ts_decorate([
212
+ Column({
213
+ name: "reference_id",
214
+ length: 128,
215
+ nullable: true
216
+ }),
217
+ _ts_metadata("design:type", String)
218
+ ], WalletLedgerEntryEntity.prototype, "referenceId", void 0);
219
+ _ts_decorate([
220
+ Column({
221
+ name: "metadata",
222
+ type: "json",
223
+ nullable: true
224
+ }),
225
+ _ts_metadata("design:type", typeof Record === "undefined" ? Object : Record)
226
+ ], WalletLedgerEntryEntity.prototype, "metadata", void 0);
227
+ _ts_decorate([
228
+ Column({
229
+ name: "created_by",
230
+ nullable: true
231
+ }),
232
+ _ts_metadata("design:type", Number)
233
+ ], WalletLedgerEntryEntity.prototype, "createdBy", void 0);
234
+ _ts_decorate([
235
+ Column({
236
+ name: "created_date",
237
+ nullable: true
238
+ }),
239
+ _ts_metadata("design:type", String)
240
+ ], WalletLedgerEntryEntity.prototype, "createdDate", void 0);
241
+ _ts_decorate([
242
+ Column({
243
+ name: "modified_by",
244
+ nullable: true
245
+ }),
246
+ _ts_metadata("design:type", Number)
247
+ ], WalletLedgerEntryEntity.prototype, "modifiedBy", void 0);
248
+ _ts_decorate([
249
+ Column({
250
+ name: "modified_date",
251
+ nullable: true
252
+ }),
253
+ _ts_metadata("design:type", String)
254
+ ], WalletLedgerEntryEntity.prototype, "modifiedDate", void 0);
255
+ _ts_decorate([
256
+ BeforeInsert(),
257
+ _ts_metadata("design:type", Function),
258
+ _ts_metadata("design:paramtypes", []),
259
+ _ts_metadata("design:returntype", void 0)
260
+ ], WalletLedgerEntryEntity.prototype, "setCreated", null);
261
+ WalletLedgerEntryEntity = _ts_decorate([
262
+ Entity("wallet_ledger_entry")
263
+ ], WalletLedgerEntryEntity);
264
+ var _WalletHoldEntity = class _WalletHoldEntity {
265
+ constructor() {
266
+ __publicField(this, "id");
267
+ __publicField(this, "sourceAccountId");
268
+ __publicField(this, "targetAccountId");
269
+ __publicField(this, "amount");
270
+ __publicField(this, "status");
271
+ __publicField(this, "referenceType");
272
+ __publicField(this, "referenceId");
273
+ __publicField(this, "idempotencyKey");
274
+ __publicField(this, "createdBy");
275
+ __publicField(this, "createdDate");
276
+ __publicField(this, "modifiedBy");
277
+ __publicField(this, "modifiedDate");
278
+ }
279
+ setCreated() {
280
+ this.createdDate = nowTimestamp();
281
+ this.modifiedDate = nowTimestamp();
282
+ }
283
+ setModified() {
284
+ this.modifiedDate = nowTimestamp();
285
+ }
286
+ };
287
+ __name(_WalletHoldEntity, "WalletHoldEntity");
288
+ var WalletHoldEntity = _WalletHoldEntity;
289
+ _ts_decorate([
290
+ PrimaryGeneratedColumn({
291
+ name: "id"
292
+ }),
293
+ _ts_metadata("design:type", Number)
294
+ ], WalletHoldEntity.prototype, "id", void 0);
295
+ _ts_decorate([
296
+ Column({
297
+ name: "source_account_id"
298
+ }),
299
+ _ts_metadata("design:type", Number)
300
+ ], WalletHoldEntity.prototype, "sourceAccountId", void 0);
301
+ _ts_decorate([
302
+ Column({
303
+ name: "target_account_id"
304
+ }),
305
+ _ts_metadata("design:type", Number)
306
+ ], WalletHoldEntity.prototype, "targetAccountId", void 0);
307
+ _ts_decorate([
308
+ Column({
309
+ name: "amount",
310
+ type: "bigint"
311
+ }),
312
+ _ts_metadata("design:type", String)
313
+ ], WalletHoldEntity.prototype, "amount", void 0);
314
+ _ts_decorate([
315
+ Column({
316
+ name: "status",
317
+ length: 16,
318
+ default: "active"
319
+ }),
320
+ _ts_metadata("design:type", String)
321
+ ], WalletHoldEntity.prototype, "status", void 0);
322
+ _ts_decorate([
323
+ Column({
324
+ name: "reference_type",
325
+ length: 64
326
+ }),
327
+ _ts_metadata("design:type", String)
328
+ ], WalletHoldEntity.prototype, "referenceType", void 0);
329
+ _ts_decorate([
330
+ Column({
331
+ name: "reference_id",
332
+ length: 128
333
+ }),
334
+ _ts_metadata("design:type", String)
335
+ ], WalletHoldEntity.prototype, "referenceId", void 0);
336
+ _ts_decorate([
337
+ Column({
338
+ name: "idempotency_key",
339
+ length: 255
340
+ }),
341
+ _ts_metadata("design:type", String)
342
+ ], WalletHoldEntity.prototype, "idempotencyKey", void 0);
343
+ _ts_decorate([
344
+ Column({
345
+ name: "created_by",
346
+ nullable: true
347
+ }),
348
+ _ts_metadata("design:type", Number)
349
+ ], WalletHoldEntity.prototype, "createdBy", void 0);
350
+ _ts_decorate([
351
+ Column({
352
+ name: "created_date",
353
+ nullable: true
354
+ }),
355
+ _ts_metadata("design:type", String)
356
+ ], WalletHoldEntity.prototype, "createdDate", void 0);
357
+ _ts_decorate([
358
+ Column({
359
+ name: "modified_by",
360
+ nullable: true
361
+ }),
362
+ _ts_metadata("design:type", Number)
363
+ ], WalletHoldEntity.prototype, "modifiedBy", void 0);
364
+ _ts_decorate([
365
+ Column({
366
+ name: "modified_date",
367
+ nullable: true
368
+ }),
369
+ _ts_metadata("design:type", String)
370
+ ], WalletHoldEntity.prototype, "modifiedDate", void 0);
371
+ _ts_decorate([
372
+ BeforeInsert(),
373
+ _ts_metadata("design:type", Function),
374
+ _ts_metadata("design:paramtypes", []),
375
+ _ts_metadata("design:returntype", void 0)
376
+ ], WalletHoldEntity.prototype, "setCreated", null);
377
+ _ts_decorate([
378
+ BeforeUpdate(),
379
+ _ts_metadata("design:type", Function),
380
+ _ts_metadata("design:paramtypes", []),
381
+ _ts_metadata("design:returntype", void 0)
382
+ ], WalletHoldEntity.prototype, "setModified", null);
383
+ WalletHoldEntity = _ts_decorate([
384
+ Entity("wallet_hold")
385
+ ], WalletHoldEntity);
386
+
387
+ // src/errors.ts
388
+ var _WalletError = class _WalletError extends Error {
389
+ constructor(message) {
390
+ super(message);
391
+ this.name = "WalletError";
392
+ }
393
+ };
394
+ __name(_WalletError, "WalletError");
395
+ var WalletError = _WalletError;
396
+ var _DuplicateOperationError = class _DuplicateOperationError extends WalletError {
397
+ constructor(message, existingEntry) {
398
+ super(message);
399
+ __publicField(this, "existingEntry");
400
+ this.name = "DuplicateOperationError";
401
+ this.existingEntry = existingEntry;
402
+ }
403
+ };
404
+ __name(_DuplicateOperationError, "DuplicateOperationError");
405
+ var DuplicateOperationError = _DuplicateOperationError;
406
+
407
+ // src/typeorm/store.ts
408
+ function toNumber(value) {
409
+ if (value == null) {
410
+ return 0;
411
+ }
412
+ return Number(value);
413
+ }
414
+ __name(toNumber, "toNumber");
415
+ function toLedgerAccount(row) {
416
+ return {
417
+ id: row.id,
418
+ ownerType: row.ownerType,
419
+ ownerId: row.ownerId,
420
+ currency: row.currency,
421
+ availableBalance: toNumber(row.availableBalance),
422
+ heldBalance: toNumber(row.heldBalance)
423
+ };
424
+ }
425
+ __name(toLedgerAccount, "toLedgerAccount");
426
+ function toLedgerHold(row) {
427
+ return {
428
+ id: row.id,
429
+ sourceAccountId: row.sourceAccountId,
430
+ targetAccountId: row.targetAccountId,
431
+ amount: toNumber(row.amount),
432
+ status: row.status,
433
+ referenceType: row.referenceType,
434
+ referenceId: row.referenceId,
435
+ idempotencyKey: row.idempotencyKey,
436
+ createdAt: new Date(row.createdDate)
437
+ };
438
+ }
439
+ __name(toLedgerHold, "toLedgerHold");
440
+ function toLedgerEntry(row) {
441
+ return {
442
+ id: row.id,
443
+ accountId: row.accountId,
444
+ entryType: row.entryType,
445
+ amount: toNumber(row.amount),
446
+ balanceAfterAvailable: toNumber(row.balanceAfterAvailable),
447
+ balanceAfterHeld: toNumber(row.balanceAfterHeld),
448
+ idempotencyKey: row.idempotencyKey,
449
+ referenceType: row.referenceType,
450
+ referenceId: row.referenceId,
451
+ metadata: row.metadata,
452
+ createdAt: new Date(row.createdDate)
453
+ };
454
+ }
455
+ __name(toLedgerEntry, "toLedgerEntry");
456
+ function isEntityManager(value) {
457
+ return value != null && typeof value === "object" && typeof value.getRepository === "function";
458
+ }
459
+ __name(isEntityManager, "isEntityManager");
460
+ var _a;
461
+ var TypeOrmWalletStoreTx = (_a = class {
462
+ constructor(em) {
463
+ __publicField(this, "em");
464
+ this.em = em;
465
+ }
466
+ async getOrCreateAccount(type, ownerId, currency) {
467
+ const repo = this.em.getRepository(WalletAccountEntity);
468
+ let row = await repo.findOne({
469
+ where: {
470
+ ownerType: type,
471
+ ownerId,
472
+ currency
473
+ }
474
+ });
475
+ if (!row) {
476
+ row = repo.create({
477
+ ownerType: type,
478
+ ownerId,
479
+ currency,
480
+ availableBalance: "0",
481
+ heldBalance: "0"
482
+ });
483
+ row = await repo.save(row);
484
+ }
485
+ return toLedgerAccount(row);
486
+ }
487
+ async lockAccount(accountId) {
488
+ const row = await this.em.getRepository(WalletAccountEntity).createQueryBuilder("wa").setLock("pessimistic_write").where("wa.id = :id", {
489
+ id: accountId
490
+ }).getOne();
491
+ if (!row) {
492
+ throw new Error(`Wallet account ${accountId} not found`);
493
+ }
494
+ return toLedgerAccount(row);
495
+ }
496
+ async getAccountById(accountId) {
497
+ const row = await this.em.getRepository(WalletAccountEntity).findOne({
498
+ where: {
499
+ id: accountId
500
+ }
501
+ });
502
+ return row ? toLedgerAccount(row) : null;
503
+ }
504
+ async appendLedgerEntry(entry) {
505
+ const existing = await this.findLedgerByIdempotencyKey(entry.idempotencyKey);
506
+ if (existing) {
507
+ return existing;
508
+ }
509
+ const repo = this.em.getRepository(WalletLedgerEntryEntity);
510
+ try {
511
+ const row = await repo.save(repo.create({
512
+ accountId: entry.accountId,
513
+ entryType: entry.entryType,
514
+ amount: String(entry.amount),
515
+ balanceAfterAvailable: String(entry.balanceAfterAvailable),
516
+ balanceAfterHeld: String(entry.balanceAfterHeld),
517
+ idempotencyKey: entry.idempotencyKey,
518
+ referenceType: entry.referenceType,
519
+ referenceId: entry.referenceId,
520
+ metadata: entry.metadata
521
+ }));
522
+ return toLedgerEntry(row);
523
+ } catch (error) {
524
+ const err = error;
525
+ const code = err?.code || err?.errno;
526
+ if (code === "ER_DUP_ENTRY" || code === 1062) {
527
+ const dup = await this.findLedgerByIdempotencyKey(entry.idempotencyKey);
528
+ if (dup) {
529
+ return dup;
530
+ }
531
+ throw new DuplicateOperationError("Duplicate idempotency key", null);
532
+ }
533
+ throw error;
534
+ }
535
+ }
536
+ async updateBalances(accountId, deltas) {
537
+ const row = await this.em.getRepository(WalletAccountEntity).createQueryBuilder("wa").setLock("pessimistic_write").where("wa.id = :id", {
538
+ id: accountId
539
+ }).getOne();
540
+ if (!row) {
541
+ throw new Error(`Wallet account ${accountId} not found`);
542
+ }
543
+ const available = toNumber(row.availableBalance) + (deltas.availableDelta ?? 0);
544
+ const held = toNumber(row.heldBalance) + (deltas.heldDelta ?? 0);
545
+ if (available < 0 || held < 0) {
546
+ throw new Error("Balance cannot be negative");
547
+ }
548
+ row.availableBalance = String(available);
549
+ row.heldBalance = String(held);
550
+ const saved = await this.em.getRepository(WalletAccountEntity).save(row);
551
+ return toLedgerAccount(saved);
552
+ }
553
+ async findHoldByReference(ref) {
554
+ const row = await this.em.getRepository(WalletHoldEntity).findOne({
555
+ where: {
556
+ referenceType: ref.referenceType,
557
+ referenceId: ref.referenceId
558
+ },
559
+ order: {
560
+ id: "DESC"
561
+ }
562
+ });
563
+ return row ? toLedgerHold(row) : null;
564
+ }
565
+ async upsertHold(hold) {
566
+ const repo = this.em.getRepository(WalletHoldEntity);
567
+ let row;
568
+ if (hold.id != null) {
569
+ row = await repo.findOne({
570
+ where: {
571
+ id: hold.id
572
+ }
573
+ });
574
+ }
575
+ if (!row) {
576
+ row = await repo.findOne({
577
+ where: {
578
+ referenceType: hold.referenceType,
579
+ referenceId: hold.referenceId
580
+ }
581
+ });
582
+ }
583
+ if (!row) {
584
+ row = repo.create({
585
+ sourceAccountId: hold.sourceAccountId,
586
+ targetAccountId: hold.targetAccountId,
587
+ amount: String(hold.amount),
588
+ status: hold.status,
589
+ referenceType: hold.referenceType,
590
+ referenceId: hold.referenceId,
591
+ idempotencyKey: hold.idempotencyKey
592
+ });
593
+ } else {
594
+ row.sourceAccountId = hold.sourceAccountId;
595
+ row.targetAccountId = hold.targetAccountId;
596
+ row.amount = String(hold.amount);
597
+ row.status = hold.status;
598
+ row.idempotencyKey = hold.idempotencyKey;
599
+ }
600
+ const saved = await repo.save(row);
601
+ return toLedgerHold(saved);
602
+ }
603
+ async findLedgerByIdempotencyKey(key) {
604
+ const row = await this.em.getRepository(WalletLedgerEntryEntity).findOne({
605
+ where: {
606
+ idempotencyKey: key
607
+ }
608
+ });
609
+ return row ? toLedgerEntry(row) : null;
610
+ }
611
+ async listLedgerEntries(accountId, limit, offset) {
612
+ const rows = await this.em.getRepository(WalletLedgerEntryEntity).find({
613
+ where: {
614
+ accountId
615
+ },
616
+ order: {
617
+ id: "DESC"
618
+ },
619
+ take: limit,
620
+ skip: offset
621
+ });
622
+ return rows.map(toLedgerEntry);
623
+ }
624
+ }, __name(_a, "TypeOrmWalletStoreTx"), _a);
625
+ var _TypeOrmWalletStore = class _TypeOrmWalletStore {
626
+ constructor(connection) {
627
+ __publicField(this, "connection");
628
+ this.connection = connection;
629
+ }
630
+ async runInTransaction(fn, ctx) {
631
+ if (isEntityManager(ctx?.nativeTx)) {
632
+ return fn(new TypeOrmWalletStoreTx(ctx.nativeTx));
633
+ }
634
+ return this.connection.manager.transaction(async (em) => fn(new TypeOrmWalletStoreTx(em)));
635
+ }
636
+ };
637
+ __name(_TypeOrmWalletStore, "TypeOrmWalletStore");
638
+ var TypeOrmWalletStore = _TypeOrmWalletStore;
639
+
640
+ // src/typeorm/migrations/CreateWalletTables.ts
641
+ async function indexExists(queryRunner, tableName, indexName) {
642
+ const rows = await queryRunner.query(`SELECT COUNT(1) AS c
643
+ FROM information_schema.statistics
644
+ WHERE table_schema = DATABASE()
645
+ AND table_name = ?
646
+ AND index_name = ?`, [
647
+ tableName,
648
+ indexName
649
+ ]);
650
+ return Number(rows[0]?.c ?? 0) > 0;
651
+ }
652
+ __name(indexExists, "indexExists");
653
+ var _CreateWalletTables1790000000000 = class _CreateWalletTables1790000000000 {
654
+ async up(queryRunner) {
655
+ if (!await queryRunner.hasTable("wallet_account")) {
656
+ await queryRunner.query(`
657
+ CREATE TABLE \`wallet_account\` (
658
+ \`id\` int NOT NULL AUTO_INCREMENT,
659
+ \`owner_type\` varchar(32) NOT NULL,
660
+ \`owner_id\` int NOT NULL,
661
+ \`currency\` varchar(8) NOT NULL DEFAULT 'INR',
662
+ \`available_balance\` bigint NOT NULL DEFAULT 0,
663
+ \`held_balance\` bigint NOT NULL DEFAULT 0,
664
+ \`created_by\` int NULL,
665
+ \`created_date\` datetime NULL,
666
+ \`modified_by\` int NULL,
667
+ \`modified_date\` datetime NULL,
668
+ PRIMARY KEY (\`id\`)
669
+ ) ENGINE=InnoDB
670
+ `);
671
+ }
672
+ if (!await indexExists(queryRunner, "wallet_account", "IDX_wallet_account_owner_currency")) {
673
+ await queryRunner.query(`
674
+ CREATE UNIQUE INDEX \`IDX_wallet_account_owner_currency\`
675
+ ON \`wallet_account\` (\`owner_type\`, \`owner_id\`, \`currency\`)
676
+ `);
677
+ }
678
+ if (!await queryRunner.hasTable("wallet_ledger_entry")) {
679
+ await queryRunner.query(`
680
+ CREATE TABLE \`wallet_ledger_entry\` (
681
+ \`id\` int NOT NULL AUTO_INCREMENT,
682
+ \`account_id\` int NOT NULL,
683
+ \`entry_type\` varchar(32) NOT NULL,
684
+ \`amount\` bigint NOT NULL,
685
+ \`balance_after_available\` bigint NOT NULL,
686
+ \`balance_after_held\` bigint NOT NULL,
687
+ \`idempotency_key\` varchar(255) NOT NULL,
688
+ \`reference_type\` varchar(64) NULL,
689
+ \`reference_id\` varchar(128) NULL,
690
+ \`metadata\` json NULL,
691
+ \`created_by\` int NULL,
692
+ \`created_date\` datetime NULL,
693
+ \`modified_by\` int NULL,
694
+ \`modified_date\` datetime NULL,
695
+ PRIMARY KEY (\`id\`),
696
+ UNIQUE INDEX \`UQ_wallet_ledger_idempotency_key\` (\`idempotency_key\`)
697
+ ) ENGINE=InnoDB
698
+ `);
699
+ }
700
+ if (!await indexExists(queryRunner, "wallet_ledger_entry", "IDX_wallet_ledger_account_id")) {
701
+ await queryRunner.query(`
702
+ CREATE INDEX \`IDX_wallet_ledger_account_id\`
703
+ ON \`wallet_ledger_entry\` (\`account_id\`)
704
+ `);
705
+ }
706
+ if (!await queryRunner.hasTable("wallet_hold")) {
707
+ await queryRunner.query(`
708
+ CREATE TABLE \`wallet_hold\` (
709
+ \`id\` int NOT NULL AUTO_INCREMENT,
710
+ \`source_account_id\` int NOT NULL,
711
+ \`target_account_id\` int NOT NULL,
712
+ \`amount\` bigint NOT NULL,
713
+ \`status\` varchar(16) NOT NULL DEFAULT 'active',
714
+ \`reference_type\` varchar(64) NOT NULL,
715
+ \`reference_id\` varchar(128) NOT NULL,
716
+ \`idempotency_key\` varchar(255) NOT NULL,
717
+ \`created_by\` int NULL,
718
+ \`created_date\` datetime NULL,
719
+ \`modified_by\` int NULL,
720
+ \`modified_date\` datetime NULL,
721
+ PRIMARY KEY (\`id\`)
722
+ ) ENGINE=InnoDB
723
+ `);
724
+ }
725
+ if (!await indexExists(queryRunner, "wallet_hold", "IDX_wallet_hold_reference")) {
726
+ await queryRunner.query(`
727
+ CREATE INDEX \`IDX_wallet_hold_reference\`
728
+ ON \`wallet_hold\` (\`reference_type\`, \`reference_id\`)
729
+ `);
730
+ }
731
+ }
732
+ async down(queryRunner) {
733
+ if (await queryRunner.hasTable("wallet_hold")) {
734
+ await queryRunner.query("DROP TABLE `wallet_hold`");
735
+ }
736
+ if (await queryRunner.hasTable("wallet_ledger_entry")) {
737
+ await queryRunner.query("DROP TABLE `wallet_ledger_entry`");
738
+ }
739
+ if (await queryRunner.hasTable("wallet_account")) {
740
+ await queryRunner.query("DROP TABLE `wallet_account`");
741
+ }
742
+ }
743
+ };
744
+ __name(_CreateWalletTables1790000000000, "CreateWalletTables1790000000000");
745
+ var CreateWalletTables1790000000000 = _CreateWalletTables1790000000000;
746
+
747
+ // src/typeorm/metadata.ts
748
+ function getWalletOrmMetadata() {
749
+ return {
750
+ entities: [
751
+ WalletAccountEntity,
752
+ WalletLedgerEntryEntity,
753
+ WalletHoldEntity
754
+ ],
755
+ migrations: [
756
+ CreateWalletTables1790000000000
757
+ ]
758
+ };
759
+ }
760
+ __name(getWalletOrmMetadata, "getWalletOrmMetadata");
761
+
762
+ export { CreateWalletTables1790000000000, TypeOrmWalletStore, WalletAccountEntity, WalletHoldEntity, WalletLedgerEntryEntity, getWalletOrmMetadata };
763
+ //# sourceMappingURL=index.js.map
764
+ //# sourceMappingURL=index.js.map