@nehorai/credits 2.2.0 → 3.0.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.
Files changed (41) hide show
  1. package/dist/adapters/index.cjs +1 -0
  2. package/dist/adapters/index.cjs.map +1 -1
  3. package/dist/adapters/index.d.cts +2 -2
  4. package/dist/adapters/index.d.ts +2 -2
  5. package/dist/adapters/index.js +1 -0
  6. package/dist/adapters/index.js.map +1 -1
  7. package/dist/core/index.cjs +139 -23
  8. package/dist/core/index.cjs.map +1 -1
  9. package/dist/core/index.d.cts +113 -22
  10. package/dist/core/index.d.ts +113 -22
  11. package/dist/core/index.js +128 -21
  12. package/dist/core/index.js.map +1 -1
  13. package/dist/index.cjs +246 -61
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.cts +4 -4
  16. package/dist/index.d.ts +4 -4
  17. package/dist/index.js +235 -59
  18. package/dist/index.js.map +1 -1
  19. package/dist/repository/index.cjs +183 -41
  20. package/dist/repository/index.cjs.map +1 -1
  21. package/dist/repository/index.d.cts +6 -5
  22. package/dist/repository/index.d.ts +6 -5
  23. package/dist/repository/index.js +183 -41
  24. package/dist/repository/index.js.map +1 -1
  25. package/dist/sdk/index.cjs +1 -0
  26. package/dist/sdk/index.cjs.map +1 -1
  27. package/dist/sdk/index.d.cts +2 -2
  28. package/dist/sdk/index.d.ts +2 -2
  29. package/dist/sdk/index.js +1 -0
  30. package/dist/sdk/index.js.map +1 -1
  31. package/dist/service/index.cjs +25 -16
  32. package/dist/service/index.cjs.map +1 -1
  33. package/dist/service/index.d.cts +2 -2
  34. package/dist/service/index.d.ts +2 -2
  35. package/dist/service/index.js +25 -16
  36. package/dist/service/index.js.map +1 -1
  37. package/dist/{types-MaViK5tD.d.cts → types-C3sxDvNb.d.cts} +29 -6
  38. package/dist/{types-MaViK5tD.d.ts → types-C3sxDvNb.d.ts} +29 -6
  39. package/dist/{types-3c-IAwZI.d.ts → types-CY-oHIMA.d.ts} +19 -3
  40. package/dist/{types-XSjNXO5l.d.cts → types-DOjguhEX.d.cts} +19 -3
  41. package/package.json +1 -1
@@ -1,5 +1,5 @@
1
- import { c as PortableUserCredits, g as CreditCheckResult, C as CreditOperationType, a as PortableReservation, d as PortableUsageLog, D as DeductCreditsResult, S as SubscriptionTier, j as UsageHistoryResponse, f as PortableJournalEntry } from '../types-MaViK5tD.cjs';
2
- import { I as ICreditRepository, o as ReserveOutcome, m as ReservationTransitionOptions, C as CommitOutcome, R as ReleaseOutcome, A as AddCreditsAtomicOptions, a as AddCreditsOutcome, J as JournalEntryQuery } from '../types-XSjNXO5l.cjs';
1
+ import { c as PortableUserCredits, g as CreditCheckResult, C as CreditOperationType, a as PortableReservation, d as PortableUsageLog, D as DeductCreditsResult, S as SubscriptionTier, j as UsageHistoryResponse, f as PortableJournalEntry } from '../types-C3sxDvNb.cjs';
2
+ import { I as ICreditRepository, o as ReserveOutcome, m as ReservationTransitionOptions, C as CommitOutcome, R as ReleaseOutcome, A as AddCreditsAtomicOptions, a as AddCreditsOutcome, J as JournalEntryQuery } from '../types-DOjguhEX.cjs';
3
3
 
4
4
  /**
5
5
  * Notification callback type for low balance notifications
@@ -1,5 +1,5 @@
1
- import { c as PortableUserCredits, g as CreditCheckResult, C as CreditOperationType, a as PortableReservation, d as PortableUsageLog, D as DeductCreditsResult, S as SubscriptionTier, j as UsageHistoryResponse, f as PortableJournalEntry } from '../types-MaViK5tD.js';
2
- import { I as ICreditRepository, o as ReserveOutcome, m as ReservationTransitionOptions, C as CommitOutcome, R as ReleaseOutcome, A as AddCreditsAtomicOptions, a as AddCreditsOutcome, J as JournalEntryQuery } from '../types-3c-IAwZI.js';
1
+ import { c as PortableUserCredits, g as CreditCheckResult, C as CreditOperationType, a as PortableReservation, d as PortableUsageLog, D as DeductCreditsResult, S as SubscriptionTier, j as UsageHistoryResponse, f as PortableJournalEntry } from '../types-C3sxDvNb.js';
2
+ import { I as ICreditRepository, o as ReserveOutcome, m as ReservationTransitionOptions, C as CommitOutcome, R as ReleaseOutcome, A as AddCreditsAtomicOptions, a as AddCreditsOutcome, J as JournalEntryQuery } from '../types-CY-oHIMA.js';
3
3
 
4
4
  /**
5
5
  * Notification callback type for low balance notifications
@@ -130,6 +130,7 @@ function floatSum(values) {
130
130
  for (const value of values) total += value;
131
131
  return total;
132
132
  }
133
+ var FLOAT_NOISE_RELATIVE = 64 * Number.EPSILON;
133
134
  function isValidCreditAmount(value) {
134
135
  if (typeof value !== "number") return false;
135
136
  const cents = toCents(value);
@@ -697,6 +698,7 @@ var CreditsService = class {
697
698
  entryType: "debit",
698
699
  amount: 0,
699
700
  // No credits deducted, just tier change
701
+ balanceBefore: sumAmounts(data.balance, data.bonusCredits),
700
702
  // The ledger balance, matching every other journal writer: the V2
701
703
  // transitions and add/deduct all record `balance + bonusCredits`, so
702
704
  // recording `balance` alone made the audit trail disagree with itself
@@ -740,6 +742,7 @@ var CreditsService = class {
740
742
  userId,
741
743
  entryType: balanceChange > 0 ? "credit" : "debit",
742
744
  amount: Math.abs(balanceChange),
745
+ balanceBefore: sumAmounts(data.balance, data.bonusCredits),
743
746
  balanceAfter: sumAmounts(
744
747
  resetResult.credits.balance,
745
748
  resetResult.credits.bonusCredits
@@ -974,9 +977,18 @@ var CreditsService = class {
974
977
  */
975
978
  async deductCredits(userId, amount, options) {
976
979
  assertValidCreditAmount(amount, { userId, operation: "deductCredits" });
980
+ const operationType = options?.operationType;
981
+ const journal = {
982
+ referenceId: options?.requestId ?? `deduct-${Date.now()}`,
983
+ description: operationType ? `Deducted ${amount} credits for ${getOperationLabel(operationType)}` : `Deducted ${amount} credits`,
984
+ metadata: {
985
+ ...options?.metadata,
986
+ ...operationType && { operationType }
987
+ }
988
+ };
977
989
  let deduction;
978
990
  try {
979
- deduction = await this.repository.deductCreditsAtomic(userId, amount);
991
+ deduction = await this.repository.deductCreditsAtomic(userId, amount, journal);
980
992
  } catch (error) {
981
993
  if (isCreditError(error) && error.code === CreditErrorCode.INVALID_AMOUNT) throw error;
982
994
  const credits = await this.repository.getUserCredits(userId);
@@ -992,21 +1004,18 @@ var CreditsService = class {
992
1004
  shortfall: sumAmounts(amount, -available)
993
1005
  };
994
1006
  }
995
- const operationType = options?.operationType;
996
- await this.repository.createJournalEntry({
997
- userId,
998
- entryType: "debit",
999
- amount,
1000
- balanceAfter: deduction.newBalance,
1001
- source: "operation_commit",
1002
- referenceId: options?.requestId ?? `deduct-${Date.now()}`,
1003
- referenceType: "adjustment",
1004
- description: operationType ? `Deducted ${amount} credits for ${getOperationLabel(operationType)}` : `Deducted ${amount} credits`,
1005
- metadata: {
1006
- ...options?.metadata,
1007
- ...operationType && { operationType }
1008
- }
1009
- });
1007
+ if (deduction.journalEntryId === void 0) {
1008
+ await this.repository.createJournalEntry({
1009
+ userId,
1010
+ entryType: "debit",
1011
+ amount,
1012
+ balanceBefore: deduction.previousBalance,
1013
+ balanceAfter: deduction.newBalance,
1014
+ source: "operation_commit",
1015
+ referenceType: "adjustment",
1016
+ ...journal
1017
+ });
1018
+ }
1010
1019
  if (operationType) {
1011
1020
  await this.logUsage({
1012
1021
  userId,