@classytic/revenue 1.1.4 → 2.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.
Files changed (82) hide show
  1. package/CHANGELOG.md +90 -0
  2. package/README.md +638 -632
  3. package/dist/audit-B39B0Sdq.mjs +53 -0
  4. package/dist/audit-DZ0eTr9g.d.mts +89 -0
  5. package/dist/bridges/index.d.mts +2 -0
  6. package/dist/bridges/index.mjs +1 -0
  7. package/dist/context-DRqSeTPM.d.mts +35 -0
  8. package/dist/core/state-machines.d.mts +53 -0
  9. package/dist/core/state-machines.mjs +153 -0
  10. package/dist/engine-types-CcjIb4Fy.d.mts +611 -0
  11. package/dist/enums/index.d.mts +3 -157
  12. package/dist/enums/index.mjs +3 -55
  13. package/dist/errors-DHa8JVQ-.mjs +92 -0
  14. package/dist/escrow.schema-BBv9oVEW.mjs +322 -0
  15. package/dist/escrow.schema-D5X32LwX.d.mts +629 -0
  16. package/dist/event-constants-CEMitnIV.mjs +53 -0
  17. package/dist/events/index.d.mts +3 -0
  18. package/dist/events/index.mjs +4 -0
  19. package/dist/index.d.mts +77 -9
  20. package/dist/index.mjs +465 -29
  21. package/dist/monetization.enums-BtiU3t8o.mjs +39 -0
  22. package/dist/monetization.enums-D2xbxXJM.d.mts +34 -0
  23. package/dist/plugins/plugin.interface.d.mts +28 -0
  24. package/dist/plugins/plugin.interface.mjs +26 -0
  25. package/dist/providers/index.d.mts +2 -3
  26. package/dist/providers/index.mjs +2 -2
  27. package/dist/{base-DCoyIUj6.mjs → registry-DhFMsSn5.mjs} +34 -36
  28. package/dist/{base-CsTlVQJe.d.mts → registry-SvIGPAx_.d.mts} +73 -66
  29. package/dist/repositories/create-repositories.d.mts +21 -0
  30. package/dist/repositories/create-repositories.mjs +12 -0
  31. package/dist/revenue-bridges-sdlrR85c.d.mts +145 -0
  32. package/dist/revenue-event-catalog-BX3g7RUi.d.mts +823 -0
  33. package/dist/revenue-event-catalog-LqxPnsU_.mjs +388 -0
  34. package/dist/settlement.repository-DHIPx5S4.mjs +771 -0
  35. package/dist/shared/index.d.mts +2 -0
  36. package/dist/shared/index.mjs +4 -0
  37. package/dist/splits-BAfY-a9P.mjs +123 -0
  38. package/dist/subscription.enums-tfoAgsTv.mjs +172 -0
  39. package/dist/transaction.enums-u4MshXcL.d.mts +154 -0
  40. package/dist/validators/index.d.mts +2 -0
  41. package/dist/validators/index.mjs +3 -0
  42. package/package.json +37 -38
  43. package/dist/application/services/index.d.mts +0 -4
  44. package/dist/application/services/index.mjs +0 -3
  45. package/dist/category-resolver-DV83N8ok.mjs +0 -284
  46. package/dist/commission-split-BzB8cd39.mjs +0 -485
  47. package/dist/core/events.d.mts +0 -294
  48. package/dist/core/events.mjs +0 -100
  49. package/dist/core/index.d.mts +0 -9
  50. package/dist/core/index.mjs +0 -8
  51. package/dist/errors-rRdOqnWx.d.mts +0 -787
  52. package/dist/escrow.enums-CZGrrdg7.mjs +0 -101
  53. package/dist/escrow.enums-DwdLuuve.d.mts +0 -78
  54. package/dist/idempotency-DaYcUGY1.mjs +0 -172
  55. package/dist/index-Dsp7H5Wb.d.mts +0 -471
  56. package/dist/infrastructure/plugins/index.d.mts +0 -239
  57. package/dist/infrastructure/plugins/index.mjs +0 -345
  58. package/dist/money-CvrDOijQ.mjs +0 -271
  59. package/dist/money-DPG8AtJ8.d.mts +0 -112
  60. package/dist/payment.enums-HAuAS9Pp.d.mts +0 -70
  61. package/dist/payment.enums-tEFVa-Xp.mjs +0 -69
  62. package/dist/plugin-BbK0OVHy.d.mts +0 -327
  63. package/dist/plugin-Cd_V04Em.mjs +0 -210
  64. package/dist/reconciliation/index.d.mts +0 -193
  65. package/dist/reconciliation/index.mjs +0 -192
  66. package/dist/retry-HHCOXYdn.d.mts +0 -186
  67. package/dist/revenue-BhdS7nXh.mjs +0 -553
  68. package/dist/schemas/index.d.mts +0 -2665
  69. package/dist/schemas/index.mjs +0 -717
  70. package/dist/schemas/validation.d.mts +0 -375
  71. package/dist/schemas/validation.mjs +0 -325
  72. package/dist/settlement.enums-DFhkqZEY.d.mts +0 -132
  73. package/dist/settlement.schema-DnNSFpGd.d.mts +0 -344
  74. package/dist/settlement.service-DjzAjezU.d.mts +0 -594
  75. package/dist/settlement.service-DmdKv0Zu.mjs +0 -2511
  76. package/dist/split.enums-BrjabxIX.mjs +0 -86
  77. package/dist/split.enums-DmskfLOM.d.mts +0 -43
  78. package/dist/tax-BoCt5cEd.d.mts +0 -61
  79. package/dist/tax-EQ15DO81.mjs +0 -162
  80. package/dist/transaction.enums-pCyMFT4Z.mjs +0 -96
  81. package/dist/utils/index.d.mts +0 -428
  82. package/dist/utils/index.mjs +0 -346
@@ -1,346 +0,0 @@
1
- import { a as reverseCommission, c as retry, i as calculateCommission, n as calculateSplits, o as CircuitBreaker, r as reverseSplits, s as createCircuitBreaker, t as calculateOrganizationPayout } from "../commission-split-BzB8cd39.mjs";
2
- import { n as createIdempotencyManager, t as IdempotencyManager } from "../idempotency-DaYcUGY1.mjs";
3
- import { t as resolveCategory } from "../category-resolver-DV83N8ok.mjs";
4
- import { f as SUBSCRIPTION_STATUS } from "../split.enums-BrjabxIX.mjs";
5
- import { n as fromSmallestUnit, r as toSmallestUnit, t as Money } from "../money-CvrDOijQ.mjs";
6
- import { n as getTaxType, r as reverseTax, t as calculateTax } from "../tax-EQ15DO81.mjs";
7
-
8
- //#region src/utils/transaction-type.ts
9
- /**
10
- * Transaction types with different protection rules
11
- */
12
- const TRANSACTION_MANAGEMENT_TYPE = {
13
- MONETIZATION: "monetization",
14
- MANUAL: "manual"
15
- };
16
- /**
17
- * Default monetization categories
18
- * Users can extend this via config.categoryMappings
19
- */
20
- const DEFAULT_MONETIZATION_CATEGORIES = ["subscription", "purchase"];
21
- /**
22
- * Check if category is monetization-related
23
- * @param category - Transaction category
24
- * @param additionalCategories - Additional categories from user config
25
- */
26
- function isMonetizationCategory(category, additionalCategories = []) {
27
- return [...DEFAULT_MONETIZATION_CATEGORIES, ...additionalCategories].includes(category);
28
- }
29
- /**
30
- * Check if transaction is monetization-managed
31
- *
32
- * Monetization-managed means:
33
- * - Created through subscription/purchase flows via the library
34
- * - Status controlled by payment webhooks/verification
35
- * - Amount/commission calculated by library
36
- * - Protected fields: status, amount, commission, gateway, verifiedAt, verifiedBy
37
- *
38
- * @param transaction - Transaction document or data
39
- * @param options - Options
40
- */
41
- function isMonetizationTransaction(transaction, options = {}) {
42
- const { targetModels = ["Subscription", "Membership"], additionalCategories = [] } = options;
43
- if (transaction.sourceModel && targetModels.includes(transaction.sourceModel)) return true;
44
- if (transaction.category) return isMonetizationCategory(transaction.category, additionalCategories);
45
- return false;
46
- }
47
- /**
48
- * Check if transaction is manual admin transaction
49
- *
50
- * Manual transactions:
51
- * - Created directly by admins for operational expenses/income
52
- * - Can be self-verified by admins
53
- * - More flexible updates allowed
54
- * - No commission/gateway complexity
55
- *
56
- * @param transaction - Transaction document or data
57
- * @param options - Options (same as isMonetizationTransaction)
58
- */
59
- function isManualTransaction(transaction, options = {}) {
60
- return !isMonetizationTransaction(transaction, options);
61
- }
62
- /**
63
- * Get transaction type
64
- *
65
- * @param transaction - Transaction document or data
66
- * @param options - Options (same as isMonetizationTransaction)
67
- */
68
- function getTransactionType(transaction, options = {}) {
69
- return isMonetizationTransaction(transaction, options) ? TRANSACTION_MANAGEMENT_TYPE.MONETIZATION : TRANSACTION_MANAGEMENT_TYPE.MANUAL;
70
- }
71
- /**
72
- * Protected fields for monetization transactions
73
- * These fields cannot be updated directly by admins
74
- */
75
- const PROTECTED_MONETIZATION_FIELDS = [
76
- "status",
77
- "amount",
78
- "platformCommission",
79
- "netAmount",
80
- "verifiedAt",
81
- "verifiedBy",
82
- "gateway",
83
- "webhook",
84
- "metadata.commission",
85
- "metadata.gateway",
86
- "type",
87
- "category",
88
- "sourceModel",
89
- "sourceId"
90
- ];
91
- /**
92
- * Editable fields for monetization transactions (before verification)
93
- * These fields can be updated by frontend/customer before payment is verified
94
- */
95
- const EDITABLE_MONETIZATION_FIELDS_PRE_VERIFICATION = [
96
- "reference",
97
- "paymentDetails",
98
- "notes"
99
- ];
100
- /**
101
- * Allowed fields for manual transaction creation
102
- */
103
- const MANUAL_TRANSACTION_CREATE_FIELDS = [
104
- "organizationId",
105
- "type",
106
- "category",
107
- "amount",
108
- "method",
109
- "reference",
110
- "paymentDetails",
111
- "notes",
112
- "date",
113
- "description"
114
- ];
115
- /**
116
- * Allowed fields for manual transaction updates
117
- */
118
- const MANUAL_TRANSACTION_UPDATE_FIELDS = [
119
- "amount",
120
- "method",
121
- "reference",
122
- "paymentDetails",
123
- "notes",
124
- "date",
125
- "description"
126
- ];
127
- /**
128
- * Get allowed update fields based on transaction type and status
129
- *
130
- * @param transaction - Transaction document
131
- * @param options - Options for transaction type detection
132
- */
133
- function getAllowedUpdateFields(transaction, options = {}) {
134
- if (getTransactionType(transaction, options) === TRANSACTION_MANAGEMENT_TYPE.MONETIZATION) {
135
- if (transaction.status === "pending") return EDITABLE_MONETIZATION_FIELDS_PRE_VERIFICATION;
136
- return [];
137
- }
138
- if (transaction.status === "verified" || transaction.status === "completed") return ["notes"];
139
- return MANUAL_TRANSACTION_UPDATE_FIELDS;
140
- }
141
- /**
142
- * Validate if field update is allowed
143
- *
144
- * @param transaction - Transaction document
145
- * @param fieldName - Field being updated
146
- * @param options - Options for transaction type detection
147
- */
148
- function validateFieldUpdate(transaction, fieldName, options = {}) {
149
- if (getAllowedUpdateFields(transaction, options).includes(fieldName)) return { allowed: true };
150
- if (getTransactionType(transaction, options) === TRANSACTION_MANAGEMENT_TYPE.MONETIZATION) {
151
- if (PROTECTED_MONETIZATION_FIELDS.includes(fieldName)) return {
152
- allowed: false,
153
- reason: `Field "${fieldName}" is protected for monetization transactions. Updates must go through payment flow.`
154
- };
155
- }
156
- return {
157
- allowed: false,
158
- reason: `Field "${fieldName}" cannot be updated for ${transaction.status} transactions.`
159
- };
160
- }
161
- /**
162
- * Check if transaction can be self-verified by admin
163
- *
164
- * @param transaction - Transaction document
165
- * @param options - Options for transaction type detection
166
- */
167
- function canSelfVerify(transaction, options = {}) {
168
- if (getTransactionType(transaction, options) === TRANSACTION_MANAGEMENT_TYPE.MANUAL) return transaction.status === "pending";
169
- return false;
170
- }
171
-
172
- //#endregion
173
- //#region src/utils/logger.ts
174
- let _logger = console;
175
- /**
176
- * Set custom logger implementation
177
- * @param customLogger - Logger instance with info, warn, error, debug methods
178
- */
179
- function setLogger(customLogger) {
180
- _logger = customLogger;
181
- }
182
- /**
183
- * Logger proxy - delegates to current logger implementation
184
- */
185
- const logger = {
186
- info: (...args) => {
187
- (_logger.info ?? _logger.log)?.call(_logger, ...args);
188
- },
189
- warn: (...args) => {
190
- (_logger.warn ?? _logger.log)?.call(_logger, "WARN:", ...args);
191
- },
192
- error: (...args) => {
193
- (_logger.error ?? _logger.log)?.call(_logger, "ERROR:", ...args);
194
- },
195
- debug: (...args) => {
196
- (_logger.debug ?? _logger.log)?.call(_logger, "DEBUG:", ...args);
197
- }
198
- };
199
-
200
- //#endregion
201
- //#region src/utils/subscription/period.ts
202
- /**
203
- * Add duration to date
204
- */
205
- function addDuration(startDate, duration, unit = "days") {
206
- const date = new Date(startDate);
207
- switch (unit) {
208
- case "months":
209
- case "month":
210
- date.setMonth(date.getMonth() + duration);
211
- return date;
212
- case "years":
213
- case "year":
214
- date.setFullYear(date.getFullYear() + duration);
215
- return date;
216
- case "weeks":
217
- case "week":
218
- date.setDate(date.getDate() + duration * 7);
219
- return date;
220
- default:
221
- date.setDate(date.getDate() + duration);
222
- return date;
223
- }
224
- }
225
- /**
226
- * Calculate subscription period start/end dates
227
- */
228
- function calculatePeriodRange(params) {
229
- const { currentEndDate = null, startDate = null, duration, unit = "days", now = /* @__PURE__ */ new Date() } = params;
230
- let periodStart;
231
- if (startDate) periodStart = new Date(startDate);
232
- else if (currentEndDate) {
233
- const end = new Date(currentEndDate);
234
- periodStart = end > now ? end : now;
235
- } else periodStart = now;
236
- const periodEnd = addDuration(periodStart, duration, unit);
237
- return {
238
- startDate: periodStart,
239
- endDate: periodEnd
240
- };
241
- }
242
- /**
243
- * Calculate prorated refund amount for unused period
244
- */
245
- function calculateProratedAmount(params) {
246
- const { amountPaid, startDate, endDate, asOfDate = /* @__PURE__ */ new Date(), precision = 2 } = params;
247
- if (!amountPaid || amountPaid <= 0) return 0;
248
- const start = new Date(startDate);
249
- const end = new Date(endDate);
250
- const asOf = new Date(asOfDate);
251
- const totalMs = end.getTime() - start.getTime();
252
- if (totalMs <= 0) return 0;
253
- const remainingMs = Math.max(0, end.getTime() - asOf.getTime());
254
- if (remainingMs <= 0) return 0;
255
- const amount = amountPaid * (remainingMs / totalMs);
256
- const factor = 10 ** precision;
257
- return Math.round(amount * factor) / factor;
258
- }
259
- /**
260
- * Convert interval + count to duration/unit
261
- */
262
- function resolveIntervalToDuration(interval = "month", intervalCount = 1) {
263
- const normalized = (interval || "month").toLowerCase();
264
- const count = Number(intervalCount) > 0 ? Number(intervalCount) : 1;
265
- switch (normalized) {
266
- case "year":
267
- case "years": return {
268
- duration: count,
269
- unit: "years"
270
- };
271
- case "week":
272
- case "weeks": return {
273
- duration: count,
274
- unit: "weeks"
275
- };
276
- case "quarter":
277
- case "quarters": return {
278
- duration: count * 3,
279
- unit: "months"
280
- };
281
- case "day":
282
- case "days": return {
283
- duration: count,
284
- unit: "days"
285
- };
286
- default: return {
287
- duration: count,
288
- unit: "months"
289
- };
290
- }
291
- }
292
-
293
- //#endregion
294
- //#region src/utils/subscription/actions.ts
295
- /**
296
- * Subscription Action Utilities
297
- * @classytic/revenue/utils/subscription
298
- *
299
- * Eligibility checks for subscription actions
300
- */
301
- /**
302
- * Check if subscription is active
303
- */
304
- function isSubscriptionActive(subscription) {
305
- if (!subscription) return false;
306
- if (!subscription.isActive) return false;
307
- if (subscription.endDate) {
308
- const now = /* @__PURE__ */ new Date();
309
- if (new Date(subscription.endDate) < now) return false;
310
- }
311
- return true;
312
- }
313
- /**
314
- * Check if can renew
315
- */
316
- function canRenewSubscription(entity) {
317
- if (!entity?.subscription) return false;
318
- return isSubscriptionActive(entity.subscription);
319
- }
320
- /**
321
- * Check if can cancel
322
- */
323
- function canCancelSubscription(entity) {
324
- if (!entity?.subscription) return false;
325
- if (!isSubscriptionActive(entity.subscription)) return false;
326
- return !entity.subscription.canceledAt;
327
- }
328
- /**
329
- * Check if can pause
330
- */
331
- function canPauseSubscription(entity) {
332
- if (!entity?.subscription) return false;
333
- if (entity.status === SUBSCRIPTION_STATUS.PAUSED) return false;
334
- if (entity.status === SUBSCRIPTION_STATUS.CANCELLED) return false;
335
- return isSubscriptionActive(entity.subscription);
336
- }
337
- /**
338
- * Check if can resume
339
- */
340
- function canResumeSubscription(entity) {
341
- if (!entity?.subscription) return false;
342
- return entity.status === SUBSCRIPTION_STATUS.PAUSED;
343
- }
344
-
345
- //#endregion
346
- export { CircuitBreaker, EDITABLE_MONETIZATION_FIELDS_PRE_VERIFICATION, IdempotencyManager, MANUAL_TRANSACTION_CREATE_FIELDS, MANUAL_TRANSACTION_UPDATE_FIELDS, Money, PROTECTED_MONETIZATION_FIELDS, TRANSACTION_MANAGEMENT_TYPE, addDuration, calculateCommission, calculateOrganizationPayout, calculatePeriodRange, calculateProratedAmount, calculateSplits, calculateTax, canCancelSubscription, canPauseSubscription, canRenewSubscription, canResumeSubscription, canSelfVerify, createCircuitBreaker, createIdempotencyManager, fromSmallestUnit, getAllowedUpdateFields, getTaxType, getTransactionType, isManualTransaction, isMonetizationTransaction, isSubscriptionActive, logger, logger as loggerDefault, resolveCategory, resolveIntervalToDuration, retry, reverseCommission, reverseSplits, reverseTax, setLogger, toSmallestUnit, validateFieldUpdate };