@nehorai/credits 1.6.0 → 1.7.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/index.d.cts CHANGED
@@ -3,7 +3,7 @@ export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult, a as Cre
3
3
  export { CreditError, CreditErrorCode, CreditErrorCodeType, commitReservationWithJournal, createInsufficientCreditsError, createInvalidOperationTypeError, createReservationAlreadyProcessedError, createReservationExpiredError, createReservationNotFoundError, createUserNotFoundError, isCreditError, isInsufficientCreditsError, releaseReservationWithJournal, reserveCreditsForOperation } from './core/index.cjs';
4
4
  export { D as DeferredExecutor, c as createDeferredExecutor, g as genericDeferred, n as noopDeferred, s as synchronousDeferred } from './deferred-C7rrRfkm.cjs';
5
5
  import { z } from 'zod';
6
- export { C as CreateJournalEntryInput, a as CreateReservationInput, b as CreateTransactionInput, c as CreateUsageLogInput, d as CreditBalanceUpdate, e as CreditRepositoryFactory, I as ICreditRepository, J as JournalEntryQuery, T as TierUpdateInput, U as UsageLogQuery, t as toClientUserCredits } from './types-CZNv1U3e.cjs';
6
+ export { A as AddCreditsAtomicOptions, C as CreateJournalEntryInput, a as CreateReservationInput, b as CreateTransactionInput, c as CreateUsageLogInput, d as CreditBalanceUpdate, e as CreditRepositoryFactory, I as ICreditRepository, J as JournalEntryQuery, T as TierUpdateInput, U as UsageLogQuery, t as toClientUserCredits } from './types-DFe5BoJY.cjs';
7
7
  export { InMemoryCreditRepository, createInMemoryCreditRepository, generateId, getNextMonthlyReset } from './repository/index.cjs';
8
8
  export { C as CreditsAuthProviderFactory, a as CreditsUser, I as ICreditsAuthProvider } from './types-OZU3I5OM.cjs';
9
9
  export { ApiKeyCreditsProvider, createApiKeyProvider, verifyAdminApiKey, verifyBearerToken } from './auth/index.cjs';
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export { A as AIProviderType, B as BuiltinTier, g as CreditCheckResult, a as Cre
3
3
  export { CreditError, CreditErrorCode, CreditErrorCodeType, commitReservationWithJournal, createInsufficientCreditsError, createInvalidOperationTypeError, createReservationAlreadyProcessedError, createReservationExpiredError, createReservationNotFoundError, createUserNotFoundError, isCreditError, isInsufficientCreditsError, releaseReservationWithJournal, reserveCreditsForOperation } from './core/index.js';
4
4
  export { D as DeferredExecutor, c as createDeferredExecutor, g as genericDeferred, n as noopDeferred, s as synchronousDeferred } from './deferred-C7rrRfkm.js';
5
5
  import { z } from 'zod';
6
- export { C as CreateJournalEntryInput, a as CreateReservationInput, b as CreateTransactionInput, c as CreateUsageLogInput, d as CreditBalanceUpdate, e as CreditRepositoryFactory, I as ICreditRepository, J as JournalEntryQuery, T as TierUpdateInput, U as UsageLogQuery, t as toClientUserCredits } from './types-G4WK0rM-.js';
6
+ export { A as AddCreditsAtomicOptions, C as CreateJournalEntryInput, a as CreateReservationInput, b as CreateTransactionInput, c as CreateUsageLogInput, d as CreditBalanceUpdate, e as CreditRepositoryFactory, I as ICreditRepository, J as JournalEntryQuery, T as TierUpdateInput, U as UsageLogQuery, t as toClientUserCredits } from './types-_bSybQnb.js';
7
7
  export { InMemoryCreditRepository, createInMemoryCreditRepository, generateId, getNextMonthlyReset } from './repository/index.js';
8
8
  export { C as CreditsAuthProviderFactory, a as CreditsUser, I as ICreditsAuthProvider } from './types-OZU3I5OM.js';
9
9
  export { ApiKeyCreditsProvider, createApiKeyProvider, verifyAdminApiKey, verifyBearerToken } from './auth/index.js';
package/dist/index.js CHANGED
@@ -798,23 +798,39 @@ var InMemoryCreditRepository = class {
798
798
  this.users.set(userId, credits);
799
799
  await this.updateReservationStatus(userId, reservationId, "released", /* @__PURE__ */ new Date());
800
800
  }
801
- async addCreditsAtomic(userId, amount, description, paymentRef) {
801
+ async addCreditsAtomic(userId, amount, description, paymentRef, options) {
802
802
  const credits = this.users.get(userId);
803
803
  if (!credits) {
804
804
  throw new Error(`User ${userId} not found`);
805
805
  }
806
- const previousBalance = credits.bonusCredits;
806
+ const previousTotal = credits.balance + credits.bonusCredits;
807
807
  credits.bonusCredits += amount;
808
808
  credits.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
809
809
  this.users.set(userId, credits);
810
- await this.createTransaction({
810
+ const newTotal = credits.balance + credits.bonusCredits;
811
+ const transaction = await this.createTransaction({
811
812
  userId,
812
813
  type: "purchase",
813
814
  amount,
814
815
  description,
815
816
  paymentRef,
816
- previousBalance,
817
- newBalance: credits.bonusCredits
817
+ previousBalance: previousTotal,
818
+ newBalance: newTotal
819
+ });
820
+ const journalMetadata = {
821
+ ...paymentRef ? { paymentRef } : {},
822
+ ...options?.metadata ?? {}
823
+ };
824
+ await this.createJournalEntry({
825
+ userId,
826
+ entryType: "credit",
827
+ amount,
828
+ balanceAfter: newTotal,
829
+ source: options?.source ?? "purchase",
830
+ referenceId: transaction.id ?? paymentRef ?? "unknown",
831
+ referenceType: options?.referenceType ?? "transaction",
832
+ description,
833
+ metadata: Object.keys(journalMetadata).length > 0 ? journalMetadata : void 0
818
834
  });
819
835
  }
820
836
  async deductCreditsAtomic(userId, amount) {
@@ -1396,9 +1412,11 @@ var CreditsService = class {
1396
1412
  * @param amount - Credits to add
1397
1413
  * @param description - Transaction description
1398
1414
  * @param paymentRef - Optional payment reference
1415
+ * @param options - Optional journal source / reference type / extra metadata
1416
+ * for the credit journal entry (e.g. revenue-attribution fields).
1399
1417
  */
1400
- async addCredits(userId, amount, description, paymentRef) {
1401
- return this.repository.addCreditsAtomic(userId, amount, description, paymentRef);
1418
+ async addCredits(userId, amount, description, paymentRef, options) {
1419
+ return this.repository.addCreditsAtomic(userId, amount, description, paymentRef, options);
1402
1420
  }
1403
1421
  /**
1404
1422
  * One-shot atomic "deduct-if-sufficient" credit charge.