@openlup/core 0.0.0 → 0.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.
Files changed (192) hide show
  1. package/CHANGELOG.md +225 -0
  2. package/CODE_OF_CONDUCT.md +39 -0
  3. package/CONTRIBUTING.md +34 -0
  4. package/LICENSE +204 -0
  5. package/MAINTAINERS.md +33 -0
  6. package/README.md +85 -3
  7. package/SECURITY.md +29 -0
  8. package/dist/bundle/contracts.d.ts +9 -0
  9. package/dist/bundle/contracts.js +1 -0
  10. package/dist/bundle/ports.d.ts +41 -0
  11. package/dist/bundle/ports.js +1 -0
  12. package/dist/catalog/identifiers.d.ts +31 -0
  13. package/dist/catalog/identifiers.js +46 -0
  14. package/dist/catalog/index.d.ts +2 -0
  15. package/dist/catalog/index.js +2 -0
  16. package/dist/checkout/idempotency.d.ts +29 -0
  17. package/dist/checkout/idempotency.js +37 -0
  18. package/dist/checkout/index.d.ts +2 -0
  19. package/dist/checkout/index.js +2 -0
  20. package/dist/company-identity/contracts.d.ts +220 -0
  21. package/dist/company-identity/contracts.js +167 -0
  22. package/dist/company-identity/index.d.ts +6 -0
  23. package/dist/company-identity/index.js +2 -0
  24. package/dist/company-identity/ports.d.ts +13 -0
  25. package/dist/company-identity/ports.js +1 -0
  26. package/dist/fulfillment/facts.d.ts +103 -0
  27. package/dist/fulfillment/facts.js +195 -0
  28. package/dist/fulfillment/index.d.ts +4 -0
  29. package/dist/fulfillment/index.js +2 -0
  30. package/dist/inventory/atp.d.ts +172 -0
  31. package/dist/inventory/atp.js +211 -0
  32. package/dist/inventory/index.d.ts +2 -0
  33. package/dist/inventory/index.js +2 -0
  34. package/dist/marketing/research/contracts.d.ts +75 -0
  35. package/dist/marketing/research/contracts.js +60 -0
  36. package/dist/marketing/research/index.d.ts +4 -0
  37. package/dist/marketing/research/index.js +4 -0
  38. package/dist/marketing/research/ports.d.ts +15 -0
  39. package/dist/marketing/research/ports.js +1 -0
  40. package/dist/partners/index.d.ts +4 -0
  41. package/dist/partners/index.js +4 -0
  42. package/dist/partners/ports.d.ts +12 -0
  43. package/dist/partners/ports.js +1 -0
  44. package/dist/partners/status.d.ts +48 -0
  45. package/dist/partners/status.js +35 -0
  46. package/dist/payment/executionBaseContracts.d.ts +81 -0
  47. package/dist/payment/executionBaseContracts.js +8 -0
  48. package/dist/payment/index.d.ts +36 -0
  49. package/dist/payment/index.js +18 -0
  50. package/dist/payment/methodLifecycle.d.ts +91 -0
  51. package/dist/payment/methodLifecycle.js +76 -0
  52. package/dist/payment/paymentControlTypes.d.ts +135 -0
  53. package/dist/payment/paymentControlTypes.js +110 -0
  54. package/dist/payment/paymentFailureTaxonomy.d.ts +80 -0
  55. package/dist/payment/paymentFailureTaxonomy.js +155 -0
  56. package/dist/payment/paymentFailureTaxonomyContracts.d.ts +106 -0
  57. package/dist/payment/paymentFailureTaxonomyContracts.js +177 -0
  58. package/dist/payment/paymentRecovery.d.ts +26 -0
  59. package/dist/payment/paymentRecovery.js +73 -0
  60. package/dist/payment/paymentRecoveryContracts.d.ts +53 -0
  61. package/dist/payment/paymentRecoveryContracts.js +10 -0
  62. package/dist/payment/paymentStateMachine.d.ts +28 -0
  63. package/dist/payment/paymentStateMachine.js +184 -0
  64. package/dist/payment/providerAttemptIdempotency.d.ts +23 -0
  65. package/dist/payment/providerAttemptIdempotency.js +31 -0
  66. package/dist/payment/providerCapability.d.ts +148 -0
  67. package/dist/payment/providerCapability.js +13 -0
  68. package/dist/platform-runtime/contracts.d.ts +9 -0
  69. package/dist/platform-runtime/contracts.js +13 -0
  70. package/dist/platform-runtime/index.d.ts +10 -0
  71. package/dist/platform-runtime/index.js +4 -0
  72. package/dist/platform-runtime/platformKernel.d.ts +44 -0
  73. package/dist/platform-runtime/platformKernel.js +39 -0
  74. package/dist/platform-runtime/ports.d.ts +100 -0
  75. package/dist/platform-runtime/ports.js +1 -0
  76. package/dist/pricing/index.d.ts +8 -0
  77. package/dist/pricing/index.js +8 -0
  78. package/dist/pricing/ports.d.ts +22 -0
  79. package/dist/pricing/ports.js +7 -0
  80. package/dist/pricing/pricingBreakdown.d.ts +86 -0
  81. package/dist/pricing/pricingBreakdown.js +155 -0
  82. package/dist/pricing/targetPriceAllocation.d.ts +97 -0
  83. package/dist/pricing/targetPriceAllocation.js +202 -0
  84. package/dist/pricing/types.d.ts +32 -0
  85. package/dist/pricing/types.js +4 -0
  86. package/dist/promo/adjustmentEngine.d.ts +4 -0
  87. package/dist/promo/adjustmentEngine.js +194 -0
  88. package/dist/promo/adjustmentTypes.d.ts +103 -0
  89. package/dist/promo/adjustmentTypes.js +13 -0
  90. package/dist/promo/index.d.ts +8 -0
  91. package/dist/promo/index.js +8 -0
  92. package/dist/promo/promoEvaluator.d.ts +25 -0
  93. package/dist/promo/promoEvaluator.js +135 -0
  94. package/dist/promo/types.d.ts +86 -0
  95. package/dist/promo/types.js +12 -0
  96. package/dist/risk/core/evaluator.d.ts +3 -0
  97. package/dist/risk/core/evaluator.js +32 -0
  98. package/dist/risk/core/rules.d.ts +3 -0
  99. package/dist/risk/core/rules.js +118 -0
  100. package/dist/risk/core/types.d.ts +65 -0
  101. package/dist/risk/core/types.js +1 -0
  102. package/dist/risk/index.d.ts +8 -0
  103. package/dist/risk/index.js +8 -0
  104. package/dist/risk/types.d.ts +32 -0
  105. package/dist/risk/types.js +43 -0
  106. package/dist/shipping/index.d.ts +4 -0
  107. package/dist/shipping/index.js +4 -0
  108. package/dist/shipping/shippingResolver.d.ts +26 -0
  109. package/dist/shipping/shippingResolver.js +88 -0
  110. package/dist/shipping/types.d.ts +45 -0
  111. package/dist/shipping/types.js +4 -0
  112. package/dist/subscription/cycleHardening.d.ts +95 -0
  113. package/dist/subscription/cycleHardening.js +118 -0
  114. package/dist/subscription/index.d.ts +14 -0
  115. package/dist/subscription/index.js +14 -0
  116. package/dist/subscription/subscriptionEngineCore.d.ts +13 -0
  117. package/dist/subscription/subscriptionEngineCore.js +145 -0
  118. package/dist/subscription/subscriptionEngineLifecycle.d.ts +150 -0
  119. package/dist/subscription/subscriptionEngineLifecycle.js +433 -0
  120. package/dist/subscription/subscriptionEnginePayment.d.ts +25 -0
  121. package/dist/subscription/subscriptionEnginePayment.js +127 -0
  122. package/dist/subscription/subscriptionEngineTypes.d.ts +94 -0
  123. package/dist/subscription/subscriptionEngineTypes.js +4 -0
  124. package/dist/subscription/subscriptionEngineUtils.d.ts +32 -0
  125. package/dist/subscription/subscriptionEngineUtils.js +74 -0
  126. package/dist/subscription/types.d.ts +58 -0
  127. package/dist/subscription/types.js +47 -0
  128. package/dist/testing.d.ts +48 -0
  129. package/dist/testing.js +105 -0
  130. package/package.json +215 -4
  131. package/release-gates.json +232 -0
  132. package/src/bundle/contracts.ts +15 -0
  133. package/src/bundle/ports.ts +32 -0
  134. package/src/catalog/identifiers.ts +50 -0
  135. package/src/catalog/index.ts +2 -0
  136. package/src/checkout/idempotency.ts +49 -0
  137. package/src/checkout/index.ts +2 -0
  138. package/src/company-identity/contracts.ts +208 -0
  139. package/src/company-identity/index.ts +28 -0
  140. package/src/company-identity/ports.ts +19 -0
  141. package/src/fulfillment/facts.ts +300 -0
  142. package/src/fulfillment/index.ts +34 -0
  143. package/src/inventory/atp.ts +249 -0
  144. package/src/inventory/index.ts +2 -0
  145. package/src/marketing/research/contracts.ts +91 -0
  146. package/src/marketing/research/index.ts +4 -0
  147. package/src/marketing/research/ports.ts +26 -0
  148. package/src/partners/index.ts +4 -0
  149. package/src/partners/ports.ts +18 -0
  150. package/src/partners/status.ts +49 -0
  151. package/src/payment/executionBaseContracts.ts +93 -0
  152. package/src/payment/index.ts +128 -0
  153. package/src/payment/methodLifecycle.ts +130 -0
  154. package/src/payment/paymentControlTypes.ts +197 -0
  155. package/src/payment/paymentFailureTaxonomy.ts +201 -0
  156. package/src/payment/paymentFailureTaxonomyContracts.ts +228 -0
  157. package/src/payment/paymentRecovery.ts +95 -0
  158. package/src/payment/paymentRecoveryContracts.ts +61 -0
  159. package/src/payment/paymentStateMachine.ts +263 -0
  160. package/src/payment/providerAttemptIdempotency.ts +57 -0
  161. package/src/payment/providerCapability.ts +172 -0
  162. package/src/platform-runtime/contracts.ts +33 -0
  163. package/src/platform-runtime/index.ts +36 -0
  164. package/src/platform-runtime/platformKernel.ts +86 -0
  165. package/src/platform-runtime/ports.ts +88 -0
  166. package/src/pricing/index.ts +8 -0
  167. package/src/pricing/ports.ts +27 -0
  168. package/src/pricing/pricingBreakdown.ts +243 -0
  169. package/src/pricing/targetPriceAllocation.ts +293 -0
  170. package/src/pricing/types.ts +36 -0
  171. package/src/promo/adjustmentEngine.ts +253 -0
  172. package/src/promo/adjustmentTypes.ts +94 -0
  173. package/src/promo/index.ts +8 -0
  174. package/src/promo/promoEvaluator.ts +137 -0
  175. package/src/promo/types.ts +97 -0
  176. package/src/risk/core/evaluator.ts +41 -0
  177. package/src/risk/core/rules.ts +128 -0
  178. package/src/risk/core/types.ts +73 -0
  179. package/src/risk/index.ts +8 -0
  180. package/src/risk/types.ts +60 -0
  181. package/src/shipping/index.ts +4 -0
  182. package/src/shipping/shippingResolver.ts +101 -0
  183. package/src/shipping/types.ts +49 -0
  184. package/src/subscription/cycleHardening.ts +174 -0
  185. package/src/subscription/index.ts +14 -0
  186. package/src/subscription/subscriptionEngineCore.ts +181 -0
  187. package/src/subscription/subscriptionEngineLifecycle.ts +574 -0
  188. package/src/subscription/subscriptionEnginePayment.ts +179 -0
  189. package/src/subscription/subscriptionEngineTypes.ts +114 -0
  190. package/src/subscription/subscriptionEngineUtils.ts +110 -0
  191. package/src/subscription/types.ts +104 -0
  192. package/src/testing.ts +169 -0
@@ -0,0 +1,181 @@
1
+ import type { SubscriptionCycleStatus } from "./types.js";
2
+ import type {
3
+ CreateInitialSubscriptionCheckoutInput,
4
+ CycleMutationResult,
5
+ EngineSubscription,
6
+ InitialSubscriptionCheckoutModel,
7
+ PlanCycleInput,
8
+ SubscriptionEngineResult,
9
+ } from "./subscriptionEngineTypes.js";
10
+ import { DEFAULT_EDIT_WINDOW_HOURS, DEFAULT_TIMEZONE } from "./subscriptionEngineTypes.js";
11
+ import {
12
+ cloneRecord,
13
+ cloneTemplate,
14
+ failure,
15
+ makeEvent,
16
+ parseIso,
17
+ success,
18
+ validateTemplate,
19
+ } from "./subscriptionEngineUtils.js";
20
+
21
+ /** @beta */
22
+ export function createInitialSubscriptionCheckoutModel(
23
+ input: CreateInitialSubscriptionCheckoutInput,
24
+ ): SubscriptionEngineResult<InitialSubscriptionCheckoutModel> {
25
+ const now = parseIso(input.now);
26
+ const firstCycleAt = parseIso(input.firstCycleAt);
27
+ if (!now || !firstCycleAt) {
28
+ return failure("invalid_timestamp", "now and firstCycleAt must be valid ISO timestamps");
29
+ }
30
+
31
+ const template = cloneTemplate(input.template);
32
+ const templateError = validateTemplate(template);
33
+ if (templateError) return templateError;
34
+
35
+ const subscription: EngineSubscription = {
36
+ id: input.subscriptionId,
37
+ clientRef: input.clientRef,
38
+ status: "active",
39
+ template,
40
+ templateVersion: 1,
41
+ nextCycleAt: firstCycleAt.toISOString(),
42
+ paymentMethodRef: input.paymentMethodRef ?? null,
43
+ paymentMethodKind: input.paymentMethodKind ?? null,
44
+ timezone: input.timezone ?? DEFAULT_TIMEZONE,
45
+ createdAt: now.toISOString(),
46
+ updatedAt: now.toISOString(),
47
+ };
48
+
49
+ const planned = planSubscriptionCycle({
50
+ subscription,
51
+ cycleNumber: 1,
52
+ now: now.toISOString(),
53
+ pricingSnapshot: input.pricingSnapshot,
54
+ idempotencyKey: input.idempotencyKey,
55
+ });
56
+ if (planned.ok === false) {
57
+ return failure(planned.error.code, planned.error.message);
58
+ }
59
+
60
+ return success({
61
+ subscription,
62
+ cycle: planned.value.cycle,
63
+ events: [
64
+ makeEvent({
65
+ eventType: "subscription.created",
66
+ subscription,
67
+ idempotencyKey: `${input.idempotencyKey}:subscription.created`,
68
+ occurredAt: now.toISOString(),
69
+ payload: { clientRef: input.clientRef, templateVersion: 1 },
70
+ }),
71
+ ...planned.value.events,
72
+ ],
73
+ });
74
+ }
75
+
76
+ /** @beta */
77
+ export function planSubscriptionCycle(
78
+ input: PlanCycleInput,
79
+ ): SubscriptionEngineResult<CycleMutationResult> {
80
+ const now = parseIso(input.now);
81
+ const scheduledAt = parseIso(input.subscription.nextCycleAt);
82
+ if (!now || !scheduledAt) {
83
+ return failure("invalid_timestamp", "now and subscription.nextCycleAt must be valid ISO timestamps");
84
+ }
85
+
86
+ if (input.subscription.status !== "active") {
87
+ return failure("inactive_subscription", "only active subscriptions can generate cycles");
88
+ }
89
+
90
+ const templateSnapshot = cloneTemplate(input.subscription.template);
91
+ const hasPaymentMethod = Boolean(input.subscription.paymentMethodRef);
92
+ const status: SubscriptionCycleStatus = hasPaymentMethod ? "payment_pending" : "payment_failed";
93
+ const failureReason = hasPaymentMethod ? null : "missing_payment_method";
94
+
95
+ const cycle = {
96
+ subscriptionId: input.subscription.id,
97
+ cycleNumber: input.cycleNumber,
98
+ status,
99
+ scheduledAt: scheduledAt.toISOString(),
100
+ paidAt: null,
101
+ templateVersion: input.subscription.templateVersion,
102
+ templateSnapshot,
103
+ pricingSnapshot: cloneRecord(input.pricingSnapshot),
104
+ paymentMethodRef: input.subscription.paymentMethodRef,
105
+ retryAttempt: 0,
106
+ nextRetryAt: null,
107
+ failureReason,
108
+ engineIdempotencyKey: input.idempotencyKey,
109
+ };
110
+
111
+ const events = [
112
+ makeEvent({
113
+ eventType: "subscription.cycle_planned",
114
+ subscription: input.subscription,
115
+ cycleNumber: input.cycleNumber,
116
+ idempotencyKey: `${input.idempotencyKey}:cycle_planned`,
117
+ occurredAt: now.toISOString(),
118
+ payload: {
119
+ scheduledAt: cycle.scheduledAt,
120
+ templateVersion: cycle.templateVersion,
121
+ },
122
+ }),
123
+ ];
124
+
125
+ events.push(
126
+ hasPaymentMethod
127
+ ? makeEvent({
128
+ eventType: "subscription.payment_requested",
129
+ subscription: input.subscription,
130
+ cycleNumber: input.cycleNumber,
131
+ idempotencyKey: `${input.idempotencyKey}:payment_requested`,
132
+ occurredAt: now.toISOString(),
133
+ payload: { paymentMethodRef: input.subscription.paymentMethodRef },
134
+ })
135
+ : makeEvent({
136
+ eventType: "subscription.payment_failed",
137
+ subscription: input.subscription,
138
+ cycleNumber: input.cycleNumber,
139
+ idempotencyKey: `${input.idempotencyKey}:missing_payment_method`,
140
+ occurredAt: now.toISOString(),
141
+ payload: { reason: failureReason },
142
+ }),
143
+ );
144
+
145
+ return success({ cycle, events });
146
+ }
147
+
148
+ /** @beta */
149
+ export function findDueSubscriptions(
150
+ subscriptions: EngineSubscription[],
151
+ nowIso: string,
152
+ ): EngineSubscription[] {
153
+ return subscriptions.filter((subscription) => isRenewalDue(subscription, nowIso));
154
+ }
155
+
156
+ /** @beta */
157
+ export function isRenewalDue(subscription: EngineSubscription, nowIso: string): boolean {
158
+ if (subscription.status !== "active") return false;
159
+ const now = parseIso(nowIso);
160
+ const nextCycleAt = parseIso(subscription.nextCycleAt);
161
+ if (!now || !nextCycleAt) return false;
162
+ return nextCycleAt.getTime() <= now.getTime();
163
+ }
164
+
165
+ /** @beta */
166
+ export function editCutoffAt(subscription: EngineSubscription): string | null {
167
+ const nextCycleAt = parseIso(subscription.nextCycleAt);
168
+ if (!nextCycleAt) return null;
169
+ const editWindowHours = subscription.template.edit_window_hours ?? DEFAULT_EDIT_WINDOW_HOURS;
170
+ return new Date(nextCycleAt.getTime() - editWindowHours * 60 * 60 * 1000).toISOString();
171
+ }
172
+
173
+ /** @beta */
174
+ export function isEditWindowOpen(subscription: EngineSubscription, nowIso: string): boolean {
175
+ if (subscription.status !== "active") return false;
176
+ const now = parseIso(nowIso);
177
+ const cutoff = editCutoffAt(subscription);
178
+ const cutoffAt = cutoff ? parseIso(cutoff) : null;
179
+ if (!now || !cutoffAt) return false;
180
+ return now.getTime() < cutoffAt.getTime();
181
+ }