@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.
- package/dist/adapters/index.cjs +1 -0
- package/dist/adapters/index.cjs.map +1 -1
- package/dist/adapters/index.d.cts +2 -2
- package/dist/adapters/index.d.ts +2 -2
- package/dist/adapters/index.js +1 -0
- package/dist/adapters/index.js.map +1 -1
- package/dist/core/index.cjs +139 -23
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +113 -22
- package/dist/core/index.d.ts +113 -22
- package/dist/core/index.js +128 -21
- package/dist/core/index.js.map +1 -1
- package/dist/index.cjs +246 -61
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +235 -59
- package/dist/index.js.map +1 -1
- package/dist/repository/index.cjs +183 -41
- package/dist/repository/index.cjs.map +1 -1
- package/dist/repository/index.d.cts +6 -5
- package/dist/repository/index.d.ts +6 -5
- package/dist/repository/index.js +183 -41
- package/dist/repository/index.js.map +1 -1
- package/dist/sdk/index.cjs +1 -0
- package/dist/sdk/index.cjs.map +1 -1
- package/dist/sdk/index.d.cts +2 -2
- package/dist/sdk/index.d.ts +2 -2
- package/dist/sdk/index.js +1 -0
- package/dist/sdk/index.js.map +1 -1
- package/dist/service/index.cjs +25 -16
- package/dist/service/index.cjs.map +1 -1
- package/dist/service/index.d.cts +2 -2
- package/dist/service/index.d.ts +2 -2
- package/dist/service/index.js +25 -16
- package/dist/service/index.js.map +1 -1
- package/dist/{types-MaViK5tD.d.cts → types-C3sxDvNb.d.cts} +29 -6
- package/dist/{types-MaViK5tD.d.ts → types-C3sxDvNb.d.ts} +29 -6
- package/dist/{types-3c-IAwZI.d.ts → types-CY-oHIMA.d.ts} +19 -3
- package/dist/{types-XSjNXO5l.d.cts → types-DOjguhEX.d.cts} +19 -3
- package/package.json +1 -1
package/dist/service/index.d.cts
CHANGED
|
@@ -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-
|
|
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-
|
|
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
|
package/dist/service/index.d.ts
CHANGED
|
@@ -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-
|
|
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-
|
|
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
|
package/dist/service/index.js
CHANGED
|
@@ -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
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
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,
|