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,678 @@
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
+
8
+ // src/currency.ts
9
+ function toMinorUnit(amount) {
10
+ return Math.round(amount * 100);
11
+ }
12
+ __name(toMinorUnit, "toMinorUnit");
13
+ function fromMinorUnit(amount) {
14
+ return amount / 100;
15
+ }
16
+ __name(fromMinorUnit, "fromMinorUnit");
17
+
18
+ // src/types.ts
19
+ var WalletAccountType = /* @__PURE__ */ (function(WalletAccountType2) {
20
+ WalletAccountType2["CUSTOMER"] = "customer";
21
+ WalletAccountType2["VENDOR"] = "vendor";
22
+ WalletAccountType2["PLATFORM"] = "platform";
23
+ return WalletAccountType2;
24
+ })({});
25
+
26
+ // src/hooks/order.ts
27
+ function isWalletOnlyPayment(paymentMethod) {
28
+ return String(paymentMethod ?? "").trim().toLowerCase() === "wallet";
29
+ }
30
+ __name(isWalletOnlyPayment, "isWalletOnlyPayment");
31
+ function createOrderHooks(runtime) {
32
+ const { client, platformOwnerId, flags } = runtime;
33
+ return {
34
+ async onOrderPaid(ctx) {
35
+ if (!flags.enabled || !flags.ledgerWrites) {
36
+ return;
37
+ }
38
+ const txCtx = {
39
+ nativeTx: ctx.nativeTx
40
+ };
41
+ const platform = await client.getOrCreateAccount(WalletAccountType.PLATFORM, platformOwnerId, ctx.currency, txCtx);
42
+ const orderTotalPaise = toMinorUnit(ctx.orderTotalRupees);
43
+ const shouldDebitCustomer = isWalletOnlyPayment(ctx.paymentMethod) && ctx.customerId && !ctx.skipCustomerDebit;
44
+ if (shouldDebitCustomer) {
45
+ const debitRupees = ctx.walletAmountRupees ?? ctx.orderTotalRupees;
46
+ const customer = await client.getOrCreateAccount(WalletAccountType.CUSTOMER, ctx.customerId, ctx.currency, txCtx);
47
+ await client.debit({
48
+ accountId: customer.id,
49
+ amount: toMinorUnit(debitRupees),
50
+ idempotencyKey: `wallet:payment:${ctx.paymentNumber}`,
51
+ reference: {
52
+ referenceType: "order",
53
+ referenceId: String(ctx.orderId)
54
+ }
55
+ }, txCtx);
56
+ }
57
+ await client.credit({
58
+ accountId: platform.id,
59
+ amount: orderTotalPaise,
60
+ idempotencyKey: `payment:${ctx.paymentNumber}`,
61
+ reference: {
62
+ referenceType: "order",
63
+ referenceId: String(ctx.orderId)
64
+ }
65
+ }, txCtx);
66
+ for (const line of ctx.lines) {
67
+ const vendorNetRupees = line.commissionBaseRupees - line.commissionRupees;
68
+ if (vendorNetRupees <= 0) {
69
+ continue;
70
+ }
71
+ const vendor = await client.getOrCreateAccount(WalletAccountType.VENDOR, line.vendorId, ctx.currency, txCtx);
72
+ await client.hold({
73
+ sourceAccountId: platform.id,
74
+ targetAccountId: vendor.id,
75
+ amount: toMinorUnit(vendorNetRupees),
76
+ idempotencyKey: `order:${ctx.orderId}:hold:vendorOrder:${line.vendorOrderId}`,
77
+ reference: {
78
+ referenceType: "vendor_order",
79
+ referenceId: String(line.vendorOrderId)
80
+ }
81
+ }, txCtx);
82
+ }
83
+ },
84
+ async onLineDelivered(ctx) {
85
+ if (!flags.enabled || !flags.ledgerWrites) {
86
+ return;
87
+ }
88
+ await client.release({
89
+ reference: {
90
+ referenceType: "vendor_order",
91
+ referenceId: String(ctx.vendorOrderId)
92
+ },
93
+ idempotencyKey: `vendorOrder:${ctx.vendorOrderId}:release`
94
+ }, ctx.nativeTx != null ? {
95
+ nativeTx: ctx.nativeTx
96
+ } : void 0);
97
+ }
98
+ };
99
+ }
100
+ __name(createOrderHooks, "createOrderHooks");
101
+
102
+ // src/hooks/refund.ts
103
+ function isWalletPayment(paymentMethod) {
104
+ return String(paymentMethod ?? "").trim().toLowerCase() === "wallet";
105
+ }
106
+ __name(isWalletPayment, "isWalletPayment");
107
+ function walletCreditRupees(ctx) {
108
+ if (ctx.walletRefundRupees != null && ctx.walletRefundRupees > 0) {
109
+ return ctx.walletRefundRupees;
110
+ }
111
+ return isWalletPayment(ctx.paymentMethod) ? ctx.lineTotalRupees : 0;
112
+ }
113
+ __name(walletCreditRupees, "walletCreditRupees");
114
+ function idempotencyPrefix(ctx) {
115
+ const scope = ctx.idempotencyScope ?? String(ctx.orderProductId);
116
+ return ctx.refundReason === "return" ? `return:${scope}` : `refund:${ctx.orderProductId}`;
117
+ }
118
+ __name(idempotencyPrefix, "idempotencyPrefix");
119
+ function createRefundHooks(runtime) {
120
+ const { client, platformOwnerId, flags } = runtime;
121
+ return {
122
+ async onRefund(ctx) {
123
+ if (!flags.enabled || !flags.ledgerWrites) {
124
+ await ctx.processExternalRefund();
125
+ return;
126
+ }
127
+ const keyPrefix = idempotencyPrefix(ctx);
128
+ if (!ctx.skipEscrow) {
129
+ await client.cancelHold({
130
+ reference: {
131
+ referenceType: "vendor_order",
132
+ referenceId: String(ctx.vendorOrderId)
133
+ },
134
+ idempotencyKey: `${keyPrefix}:cancel_hold`
135
+ });
136
+ }
137
+ const refundPaise = toMinorUnit(ctx.lineTotalRupees);
138
+ if (refundPaise > 0) {
139
+ const platform = await client.getOrCreateAccount(WalletAccountType.PLATFORM, platformOwnerId, ctx.currency);
140
+ const balance = await client.getBalance(platform.id);
141
+ if (balance.available >= refundPaise) {
142
+ await client.debit({
143
+ accountId: platform.id,
144
+ amount: refundPaise,
145
+ idempotencyKey: `${keyPrefix}:platform_debit`,
146
+ reference: {
147
+ referenceType: "order_product",
148
+ referenceId: String(ctx.orderProductId)
149
+ }
150
+ });
151
+ }
152
+ const creditRupees = walletCreditRupees(ctx);
153
+ if (creditRupees > 0 && ctx.customerId) {
154
+ const customer = await client.getOrCreateAccount(WalletAccountType.CUSTOMER, ctx.customerId, ctx.currency);
155
+ await client.credit({
156
+ accountId: customer.id,
157
+ amount: toMinorUnit(creditRupees),
158
+ idempotencyKey: `${keyPrefix}:customer_credit`,
159
+ reference: {
160
+ referenceType: "order",
161
+ referenceId: String(ctx.orderId ?? ctx.orderProductId)
162
+ }
163
+ });
164
+ }
165
+ }
166
+ await ctx.processExternalRefund();
167
+ }
168
+ };
169
+ }
170
+ __name(createRefundHooks, "createRefundHooks");
171
+
172
+ // src/hooks/settlement.ts
173
+ function createSettlementHooks(runtime) {
174
+ const { client, flags } = runtime;
175
+ return {
176
+ async onSettlementDebit(ctx) {
177
+ if (!flags.enabled || !flags.settlementDebit) {
178
+ return;
179
+ }
180
+ const txCtx = {
181
+ nativeTx: ctx.nativeTx
182
+ };
183
+ const amountPaise = toMinorUnit(ctx.amountRupees);
184
+ const vendorAccount = await client.getOrCreateAccount(WalletAccountType.VENDOR, ctx.vendorId, ctx.currency, txCtx);
185
+ const balance = await client.getBalance(vendorAccount.id, txCtx);
186
+ if (balance.available < amountPaise) {
187
+ throw new Error(`Insufficient vendor wallet balance for settlement (vendor ${ctx.vendorId})`);
188
+ }
189
+ await client.debit({
190
+ accountId: vendorAccount.id,
191
+ amount: amountPaise,
192
+ idempotencyKey: `settlement:${ctx.settlementId}:debit:${ctx.vendorId}`,
193
+ reference: {
194
+ referenceType: "settlement",
195
+ referenceId: String(ctx.settlementId)
196
+ }
197
+ }, txCtx);
198
+ }
199
+ };
200
+ }
201
+ __name(createSettlementHooks, "createSettlementHooks");
202
+ function resolveWalletConnection() {
203
+ {
204
+ throw new Error("Wallet database connection is not registered. Pass host TypeORM Connection as dataSource.");
205
+ }
206
+ }
207
+ __name(resolveWalletConnection, "resolveWalletConnection");
208
+ function _ts_decorate(decorators, target, key, desc) {
209
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
210
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
211
+ 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;
212
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
213
+ }
214
+ __name(_ts_decorate, "_ts_decorate");
215
+ function _ts_metadata(k, v) {
216
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
217
+ }
218
+ __name(_ts_metadata, "_ts_metadata");
219
+ function nowTimestamp() {
220
+ return (/* @__PURE__ */ new Date()).toISOString().slice(0, 19).replace("T", " ");
221
+ }
222
+ __name(nowTimestamp, "nowTimestamp");
223
+ var _WalletAccountEntity = class _WalletAccountEntity {
224
+ constructor() {
225
+ __publicField(this, "id");
226
+ __publicField(this, "ownerType");
227
+ __publicField(this, "ownerId");
228
+ __publicField(this, "currency");
229
+ __publicField(this, "availableBalance");
230
+ __publicField(this, "heldBalance");
231
+ __publicField(this, "createdBy");
232
+ __publicField(this, "createdDate");
233
+ __publicField(this, "modifiedBy");
234
+ __publicField(this, "modifiedDate");
235
+ }
236
+ setCreated() {
237
+ this.createdDate = nowTimestamp();
238
+ this.modifiedDate = nowTimestamp();
239
+ }
240
+ setModified() {
241
+ this.modifiedDate = nowTimestamp();
242
+ }
243
+ };
244
+ __name(_WalletAccountEntity, "WalletAccountEntity");
245
+ var WalletAccountEntity = _WalletAccountEntity;
246
+ _ts_decorate([
247
+ PrimaryGeneratedColumn({
248
+ name: "id"
249
+ }),
250
+ _ts_metadata("design:type", Number)
251
+ ], WalletAccountEntity.prototype, "id", void 0);
252
+ _ts_decorate([
253
+ Column({
254
+ name: "owner_type",
255
+ length: 32
256
+ }),
257
+ _ts_metadata("design:type", String)
258
+ ], WalletAccountEntity.prototype, "ownerType", void 0);
259
+ _ts_decorate([
260
+ Column({
261
+ name: "owner_id"
262
+ }),
263
+ _ts_metadata("design:type", Number)
264
+ ], WalletAccountEntity.prototype, "ownerId", void 0);
265
+ _ts_decorate([
266
+ Column({
267
+ name: "currency",
268
+ length: 8,
269
+ default: "INR"
270
+ }),
271
+ _ts_metadata("design:type", String)
272
+ ], WalletAccountEntity.prototype, "currency", void 0);
273
+ _ts_decorate([
274
+ Column({
275
+ name: "available_balance",
276
+ type: "bigint",
277
+ default: 0
278
+ }),
279
+ _ts_metadata("design:type", String)
280
+ ], WalletAccountEntity.prototype, "availableBalance", void 0);
281
+ _ts_decorate([
282
+ Column({
283
+ name: "held_balance",
284
+ type: "bigint",
285
+ default: 0
286
+ }),
287
+ _ts_metadata("design:type", String)
288
+ ], WalletAccountEntity.prototype, "heldBalance", void 0);
289
+ _ts_decorate([
290
+ Column({
291
+ name: "created_by",
292
+ nullable: true
293
+ }),
294
+ _ts_metadata("design:type", Number)
295
+ ], WalletAccountEntity.prototype, "createdBy", void 0);
296
+ _ts_decorate([
297
+ Column({
298
+ name: "created_date",
299
+ nullable: true
300
+ }),
301
+ _ts_metadata("design:type", String)
302
+ ], WalletAccountEntity.prototype, "createdDate", void 0);
303
+ _ts_decorate([
304
+ Column({
305
+ name: "modified_by",
306
+ nullable: true
307
+ }),
308
+ _ts_metadata("design:type", Number)
309
+ ], WalletAccountEntity.prototype, "modifiedBy", void 0);
310
+ _ts_decorate([
311
+ Column({
312
+ name: "modified_date",
313
+ nullable: true
314
+ }),
315
+ _ts_metadata("design:type", String)
316
+ ], WalletAccountEntity.prototype, "modifiedDate", void 0);
317
+ _ts_decorate([
318
+ BeforeInsert(),
319
+ _ts_metadata("design:type", Function),
320
+ _ts_metadata("design:paramtypes", []),
321
+ _ts_metadata("design:returntype", void 0)
322
+ ], WalletAccountEntity.prototype, "setCreated", null);
323
+ _ts_decorate([
324
+ BeforeUpdate(),
325
+ _ts_metadata("design:type", Function),
326
+ _ts_metadata("design:paramtypes", []),
327
+ _ts_metadata("design:returntype", void 0)
328
+ ], WalletAccountEntity.prototype, "setModified", null);
329
+ WalletAccountEntity = _ts_decorate([
330
+ Entity("wallet_account")
331
+ ], WalletAccountEntity);
332
+ var _WalletLedgerEntryEntity = class _WalletLedgerEntryEntity {
333
+ constructor() {
334
+ __publicField(this, "id");
335
+ __publicField(this, "accountId");
336
+ __publicField(this, "entryType");
337
+ __publicField(this, "amount");
338
+ __publicField(this, "balanceAfterAvailable");
339
+ __publicField(this, "balanceAfterHeld");
340
+ __publicField(this, "idempotencyKey");
341
+ __publicField(this, "referenceType");
342
+ __publicField(this, "referenceId");
343
+ __publicField(this, "metadata");
344
+ __publicField(this, "createdBy");
345
+ __publicField(this, "createdDate");
346
+ __publicField(this, "modifiedBy");
347
+ __publicField(this, "modifiedDate");
348
+ }
349
+ setCreated() {
350
+ this.createdDate = nowTimestamp();
351
+ this.modifiedDate = nowTimestamp();
352
+ }
353
+ };
354
+ __name(_WalletLedgerEntryEntity, "WalletLedgerEntryEntity");
355
+ var WalletLedgerEntryEntity = _WalletLedgerEntryEntity;
356
+ _ts_decorate([
357
+ PrimaryGeneratedColumn({
358
+ name: "id"
359
+ }),
360
+ _ts_metadata("design:type", Number)
361
+ ], WalletLedgerEntryEntity.prototype, "id", void 0);
362
+ _ts_decorate([
363
+ Column({
364
+ name: "account_id"
365
+ }),
366
+ _ts_metadata("design:type", Number)
367
+ ], WalletLedgerEntryEntity.prototype, "accountId", void 0);
368
+ _ts_decorate([
369
+ Column({
370
+ name: "entry_type",
371
+ length: 32
372
+ }),
373
+ _ts_metadata("design:type", String)
374
+ ], WalletLedgerEntryEntity.prototype, "entryType", void 0);
375
+ _ts_decorate([
376
+ Column({
377
+ name: "amount",
378
+ type: "bigint"
379
+ }),
380
+ _ts_metadata("design:type", String)
381
+ ], WalletLedgerEntryEntity.prototype, "amount", void 0);
382
+ _ts_decorate([
383
+ Column({
384
+ name: "balance_after_available",
385
+ type: "bigint"
386
+ }),
387
+ _ts_metadata("design:type", String)
388
+ ], WalletLedgerEntryEntity.prototype, "balanceAfterAvailable", void 0);
389
+ _ts_decorate([
390
+ Column({
391
+ name: "balance_after_held",
392
+ type: "bigint"
393
+ }),
394
+ _ts_metadata("design:type", String)
395
+ ], WalletLedgerEntryEntity.prototype, "balanceAfterHeld", void 0);
396
+ _ts_decorate([
397
+ Column({
398
+ name: "idempotency_key",
399
+ length: 255,
400
+ unique: true
401
+ }),
402
+ _ts_metadata("design:type", String)
403
+ ], WalletLedgerEntryEntity.prototype, "idempotencyKey", void 0);
404
+ _ts_decorate([
405
+ Column({
406
+ name: "reference_type",
407
+ length: 64,
408
+ nullable: true
409
+ }),
410
+ _ts_metadata("design:type", String)
411
+ ], WalletLedgerEntryEntity.prototype, "referenceType", void 0);
412
+ _ts_decorate([
413
+ Column({
414
+ name: "reference_id",
415
+ length: 128,
416
+ nullable: true
417
+ }),
418
+ _ts_metadata("design:type", String)
419
+ ], WalletLedgerEntryEntity.prototype, "referenceId", void 0);
420
+ _ts_decorate([
421
+ Column({
422
+ name: "metadata",
423
+ type: "json",
424
+ nullable: true
425
+ }),
426
+ _ts_metadata("design:type", typeof Record === "undefined" ? Object : Record)
427
+ ], WalletLedgerEntryEntity.prototype, "metadata", void 0);
428
+ _ts_decorate([
429
+ Column({
430
+ name: "created_by",
431
+ nullable: true
432
+ }),
433
+ _ts_metadata("design:type", Number)
434
+ ], WalletLedgerEntryEntity.prototype, "createdBy", void 0);
435
+ _ts_decorate([
436
+ Column({
437
+ name: "created_date",
438
+ nullable: true
439
+ }),
440
+ _ts_metadata("design:type", String)
441
+ ], WalletLedgerEntryEntity.prototype, "createdDate", void 0);
442
+ _ts_decorate([
443
+ Column({
444
+ name: "modified_by",
445
+ nullable: true
446
+ }),
447
+ _ts_metadata("design:type", Number)
448
+ ], WalletLedgerEntryEntity.prototype, "modifiedBy", void 0);
449
+ _ts_decorate([
450
+ Column({
451
+ name: "modified_date",
452
+ nullable: true
453
+ }),
454
+ _ts_metadata("design:type", String)
455
+ ], WalletLedgerEntryEntity.prototype, "modifiedDate", void 0);
456
+ _ts_decorate([
457
+ BeforeInsert(),
458
+ _ts_metadata("design:type", Function),
459
+ _ts_metadata("design:paramtypes", []),
460
+ _ts_metadata("design:returntype", void 0)
461
+ ], WalletLedgerEntryEntity.prototype, "setCreated", null);
462
+ WalletLedgerEntryEntity = _ts_decorate([
463
+ Entity("wallet_ledger_entry")
464
+ ], WalletLedgerEntryEntity);
465
+ var _WalletHoldEntity = class _WalletHoldEntity {
466
+ constructor() {
467
+ __publicField(this, "id");
468
+ __publicField(this, "sourceAccountId");
469
+ __publicField(this, "targetAccountId");
470
+ __publicField(this, "amount");
471
+ __publicField(this, "status");
472
+ __publicField(this, "referenceType");
473
+ __publicField(this, "referenceId");
474
+ __publicField(this, "idempotencyKey");
475
+ __publicField(this, "createdBy");
476
+ __publicField(this, "createdDate");
477
+ __publicField(this, "modifiedBy");
478
+ __publicField(this, "modifiedDate");
479
+ }
480
+ setCreated() {
481
+ this.createdDate = nowTimestamp();
482
+ this.modifiedDate = nowTimestamp();
483
+ }
484
+ setModified() {
485
+ this.modifiedDate = nowTimestamp();
486
+ }
487
+ };
488
+ __name(_WalletHoldEntity, "WalletHoldEntity");
489
+ var WalletHoldEntity = _WalletHoldEntity;
490
+ _ts_decorate([
491
+ PrimaryGeneratedColumn({
492
+ name: "id"
493
+ }),
494
+ _ts_metadata("design:type", Number)
495
+ ], WalletHoldEntity.prototype, "id", void 0);
496
+ _ts_decorate([
497
+ Column({
498
+ name: "source_account_id"
499
+ }),
500
+ _ts_metadata("design:type", Number)
501
+ ], WalletHoldEntity.prototype, "sourceAccountId", void 0);
502
+ _ts_decorate([
503
+ Column({
504
+ name: "target_account_id"
505
+ }),
506
+ _ts_metadata("design:type", Number)
507
+ ], WalletHoldEntity.prototype, "targetAccountId", void 0);
508
+ _ts_decorate([
509
+ Column({
510
+ name: "amount",
511
+ type: "bigint"
512
+ }),
513
+ _ts_metadata("design:type", String)
514
+ ], WalletHoldEntity.prototype, "amount", void 0);
515
+ _ts_decorate([
516
+ Column({
517
+ name: "status",
518
+ length: 16,
519
+ default: "active"
520
+ }),
521
+ _ts_metadata("design:type", String)
522
+ ], WalletHoldEntity.prototype, "status", void 0);
523
+ _ts_decorate([
524
+ Column({
525
+ name: "reference_type",
526
+ length: 64
527
+ }),
528
+ _ts_metadata("design:type", String)
529
+ ], WalletHoldEntity.prototype, "referenceType", void 0);
530
+ _ts_decorate([
531
+ Column({
532
+ name: "reference_id",
533
+ length: 128
534
+ }),
535
+ _ts_metadata("design:type", String)
536
+ ], WalletHoldEntity.prototype, "referenceId", void 0);
537
+ _ts_decorate([
538
+ Column({
539
+ name: "idempotency_key",
540
+ length: 255
541
+ }),
542
+ _ts_metadata("design:type", String)
543
+ ], WalletHoldEntity.prototype, "idempotencyKey", void 0);
544
+ _ts_decorate([
545
+ Column({
546
+ name: "created_by",
547
+ nullable: true
548
+ }),
549
+ _ts_metadata("design:type", Number)
550
+ ], WalletHoldEntity.prototype, "createdBy", void 0);
551
+ _ts_decorate([
552
+ Column({
553
+ name: "created_date",
554
+ nullable: true
555
+ }),
556
+ _ts_metadata("design:type", String)
557
+ ], WalletHoldEntity.prototype, "createdDate", void 0);
558
+ _ts_decorate([
559
+ Column({
560
+ name: "modified_by",
561
+ nullable: true
562
+ }),
563
+ _ts_metadata("design:type", Number)
564
+ ], WalletHoldEntity.prototype, "modifiedBy", void 0);
565
+ _ts_decorate([
566
+ Column({
567
+ name: "modified_date",
568
+ nullable: true
569
+ }),
570
+ _ts_metadata("design:type", String)
571
+ ], WalletHoldEntity.prototype, "modifiedDate", void 0);
572
+ _ts_decorate([
573
+ BeforeInsert(),
574
+ _ts_metadata("design:type", Function),
575
+ _ts_metadata("design:paramtypes", []),
576
+ _ts_metadata("design:returntype", void 0)
577
+ ], WalletHoldEntity.prototype, "setCreated", null);
578
+ _ts_decorate([
579
+ BeforeUpdate(),
580
+ _ts_metadata("design:type", Function),
581
+ _ts_metadata("design:paramtypes", []),
582
+ _ts_metadata("design:returntype", void 0)
583
+ ], WalletHoldEntity.prototype, "setModified", null);
584
+ WalletHoldEntity = _ts_decorate([
585
+ Entity("wallet_hold")
586
+ ], WalletHoldEntity);
587
+
588
+ // src/hooks/reconciliation.ts
589
+ async function runWalletReconciliation() {
590
+ const parityDeltas = await findVendorPaymentHoldParityDeltas();
591
+ const cacheMismatches = await findCacheLedgerMismatches();
592
+ return {
593
+ parityDeltas,
594
+ cacheMismatches
595
+ };
596
+ }
597
+ __name(runWalletReconciliation, "runWalletReconciliation");
598
+ async function findVendorPaymentHoldParityDeltas() {
599
+ const rows = await resolveWalletConnection().query(`
600
+ SELECT
601
+ vo.order_id AS orderId,
602
+ vp.vendor_order_id AS vendorOrderId,
603
+ vp.amount AS vendorPaymentAmount,
604
+ wh.amount AS holdAmount,
605
+ wh.status AS holdStatus
606
+ FROM vendor_payment vp
607
+ INNER JOIN vendor_orders vo ON vo.vendor_order_id = vp.vendor_order_id
608
+ LEFT JOIN wallet_hold wh
609
+ ON wh.reference_type = 'vendor_order'
610
+ AND wh.reference_id = CAST(vp.vendor_order_id AS CHAR)
611
+ WHERE vp.vendor_order_id IS NOT NULL
612
+ `);
613
+ const deltas = [];
614
+ for (const row of rows) {
615
+ const vendorPaymentRupees = Number(row.vendorPaymentAmount) || 0;
616
+ const holdPaise = row.holdAmount != null ? Number(row.holdAmount) : 0;
617
+ const holdRupees = fromMinorUnit(holdPaise);
618
+ const delta = Math.abs(vendorPaymentRupees - holdRupees);
619
+ if (delta > 0.01 && row.holdStatus === "active") {
620
+ deltas.push({
621
+ orderId: Number(row.orderId),
622
+ vendorOrderId: Number(row.vendorOrderId),
623
+ vendorPaymentRupees,
624
+ holdPaise,
625
+ deltaRupees: delta
626
+ });
627
+ }
628
+ }
629
+ return deltas;
630
+ }
631
+ __name(findVendorPaymentHoldParityDeltas, "findVendorPaymentHoldParityDeltas");
632
+ async function findCacheLedgerMismatches() {
633
+ const connection = resolveWalletConnection();
634
+ const accounts = await connection.getRepository(WalletAccountEntity).find();
635
+ const mismatches = [];
636
+ for (const account of accounts) {
637
+ const lastEntry = await connection.getRepository(WalletLedgerEntryEntity).findOne({
638
+ where: {
639
+ accountId: account.id
640
+ },
641
+ order: {
642
+ id: "DESC"
643
+ }
644
+ });
645
+ if (!lastEntry) {
646
+ continue;
647
+ }
648
+ const cachedAvailable = Number(account.availableBalance);
649
+ const cachedHeld = Number(account.heldBalance);
650
+ const ledgerAvailable = Number(lastEntry.balanceAfterAvailable);
651
+ const ledgerHeld = Number(lastEntry.balanceAfterHeld);
652
+ if (cachedAvailable !== ledgerAvailable || cachedHeld !== ledgerHeld) {
653
+ mismatches.push({
654
+ accountId: account.id,
655
+ cachedAvailable,
656
+ cachedHeld,
657
+ ledgerAvailable,
658
+ ledgerHeld
659
+ });
660
+ }
661
+ }
662
+ return mismatches;
663
+ }
664
+ __name(findCacheLedgerMismatches, "findCacheLedgerMismatches");
665
+
666
+ // src/hooks/index.ts
667
+ function createLifecycleHooks(runtime) {
668
+ return {
669
+ ...createOrderHooks(runtime),
670
+ ...createSettlementHooks(runtime),
671
+ ...createRefundHooks(runtime)
672
+ };
673
+ }
674
+ __name(createLifecycleHooks, "createLifecycleHooks");
675
+
676
+ export { createLifecycleHooks, runWalletReconciliation };
677
+ //# sourceMappingURL=index.js.map
678
+ //# sourceMappingURL=index.js.map