@fantastic.dev/better-stripe 0.108.0 → 0.109.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.
- package/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/freshActor.d.ts +31 -0
- package/dist/client/billing/renewalAttach/internal/observation/freshActor.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/freshActor.js +112 -0
- package/dist/client/billing/renewalAttach/internal/observation/freshActor.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/freshTransport.d.ts +17 -0
- package/dist/client/billing/renewalAttach/internal/observation/freshTransport.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/freshTransport.js +191 -0
- package/dist/client/billing/renewalAttach/internal/observation/freshTransport.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/invocation.d.ts +29 -0
- package/dist/client/billing/renewalAttach/internal/observation/invocation.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/invocation.js +205 -0
- package/dist/client/billing/renewalAttach/internal/observation/invocation.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/profile.d.ts +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/profile.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/profile.js +5 -1
- package/dist/client/billing/renewalAttach/internal/observation/profile.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/reader.d.ts +4 -58
- package/dist/client/billing/renewalAttach/internal/observation/reader.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/reader.js +12 -152
- package/dist/client/billing/renewalAttach/internal/observation/reader.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.d.ts +7 -0
- package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.js +1 -0
- package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.js.map +1 -1
- package/dist/client/billing/renewalObservation/evidence-invoice.d.ts.map +1 -1
- package/dist/client/billing/renewalObservation/evidence-invoice.js +1 -0
- package/dist/client/billing/renewalObservation/evidence-invoice.js.map +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
- package/dist/component/balanceFunding/mutations.d.ts +27 -27
- package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +16 -16
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/dist/component/usage/allowance.fixtures.d.ts +16 -16
- package/dist/component/usageCredits/accounting.fixtures.d.ts +16 -16
- package/docs/renewal-context-reads.md +55 -0
- package/docs/renewal-material-observation.md +4 -1
- package/package.json +1 -1
- package/src/client/billing/renewalAttach/internal/observation/freshActor.integration.test.ts +360 -0
- package/src/client/billing/renewalAttach/internal/observation/freshActor.ts +175 -0
- package/src/client/billing/renewalAttach/internal/observation/freshActorControls.integration.test.ts +442 -0
- package/src/client/billing/renewalAttach/internal/observation/freshTransport.ts +252 -0
- package/src/client/billing/renewalAttach/internal/observation/invocation.ts +299 -0
- package/src/client/billing/renewalAttach/internal/observation/profile.ts +9 -1
- package/src/client/billing/renewalAttach/internal/observation/reader.ts +15 -221
- package/src/client/billing/renewalAttach/internal/operations/sourceOperations.ts +8 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/freshObservation.ts +94 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/historySnapshot.ts +5 -1
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationReader.ts +2 -1
- package/src/client/billing/renewalAttach/internal/test-fixtures/producerConfigureAdmission.ts +32 -4
- package/src/client/billing/renewalObservation/evidence-invoice-identity.test.ts +48 -0
- package/src/client/billing/renewalObservation/evidence-invoice.ts +1 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { types } from "node:util";
|
|
2
|
+
import {
|
|
3
|
+
getFunctionAddress,
|
|
4
|
+
type GenericActionCtx,
|
|
5
|
+
type GenericDataModel,
|
|
6
|
+
} from "convex/server";
|
|
7
|
+
import { COMPILED_OBSERVATION_HISTORY_ENTRIES } from "../operations/sourceOperations.js";
|
|
8
|
+
import {
|
|
9
|
+
readMaintainedRootComponent,
|
|
10
|
+
captureMaintainedJournalBinding,
|
|
11
|
+
readCheckedJournalActionRPC,
|
|
12
|
+
} from "../producer/classOwnedActionRoot.js";
|
|
13
|
+
import { closed, freeze } from "../c10/ownData.js";
|
|
14
|
+
import {
|
|
15
|
+
object,
|
|
16
|
+
matchConfigureDATA,
|
|
17
|
+
} from "../producer/configureJournalData.js";
|
|
18
|
+
import { captureRetainedObservationHistory } from "./history.js";
|
|
19
|
+
import {
|
|
20
|
+
readerNeed as need,
|
|
21
|
+
readerText as text,
|
|
22
|
+
readerStop,
|
|
23
|
+
captureReaderAuthorization,
|
|
24
|
+
createObservationReaderLifetime,
|
|
25
|
+
captureReaderQueryDATA,
|
|
26
|
+
} from "./readerAuthorization.js";
|
|
27
|
+
|
|
28
|
+
import type { ObservationHistorySourceEntry } from "./readerAuthorization.js";
|
|
29
|
+
type CapturedHistory = Readonly<{
|
|
30
|
+
history: Awaited<ReturnType<typeof captureRetainedObservationHistory>>;
|
|
31
|
+
authorization: ReturnType<typeof captureReaderAuthorization>;
|
|
32
|
+
}>;
|
|
33
|
+
export interface ObservationInvocationState {
|
|
34
|
+
readonly binding: object;
|
|
35
|
+
readonly historyEntry: ObservationHistorySourceEntry;
|
|
36
|
+
guard(): void;
|
|
37
|
+
check(): { wallMs: number; ns: bigint };
|
|
38
|
+
close(): void;
|
|
39
|
+
owned<T>(work: () => T | PromiseLike<T>, extra?: () => void): Promise<T>;
|
|
40
|
+
captureHistory(): Promise<CapturedHistory>;
|
|
41
|
+
authorizeUnchanged(): Promise<ReturnType<typeof captureReaderAuthorization>>;
|
|
42
|
+
readCaptured(): CapturedHistory;
|
|
43
|
+
}
|
|
44
|
+
const invocations = new WeakMap<object, ObservationInvocationState>();
|
|
45
|
+
/** Cleanup does not need a still-valid context and cannot mint or read authority. */
|
|
46
|
+
export function closeObservationInvocation(token: unknown): void {
|
|
47
|
+
if (token !== null && typeof token === "object")
|
|
48
|
+
invocations.get(token)?.close();
|
|
49
|
+
}
|
|
50
|
+
/** Actual private invocation identity only. DATA cannot register a grant. */
|
|
51
|
+
export function readObservationInvocation(
|
|
52
|
+
token: unknown,
|
|
53
|
+
): ObservationInvocationState {
|
|
54
|
+
need(token !== null && typeof token === "object");
|
|
55
|
+
const state = invocations.get(token);
|
|
56
|
+
need(state);
|
|
57
|
+
state.guard();
|
|
58
|
+
return state;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** One fresh lifetime and actual class/Action binding, shared by private wrappers. */
|
|
62
|
+
export async function captureObservationInvocation(
|
|
63
|
+
actualRoot: object,
|
|
64
|
+
raw: unknown,
|
|
65
|
+
) {
|
|
66
|
+
need(arguments.length === 2);
|
|
67
|
+
if (COMPILED_OBSERVATION_HISTORY_ENTRIES.length === 0) readerStop();
|
|
68
|
+
const component = readMaintainedRootComponent(actualRoot);
|
|
69
|
+
const matches = COMPILED_OBSERVATION_HISTORY_ENTRIES.filter(
|
|
70
|
+
(e) => e.component === component,
|
|
71
|
+
);
|
|
72
|
+
need(matches.length === 1);
|
|
73
|
+
const entry = matches[0];
|
|
74
|
+
closed(entry, ["component", "componentScope", "authorizationQuery"]);
|
|
75
|
+
need(Object.isFrozen(entry));
|
|
76
|
+
need(!types.isProxy(raw));
|
|
77
|
+
const input = closed(raw, [
|
|
78
|
+
"actualActionCtx",
|
|
79
|
+
"operationId",
|
|
80
|
+
"parentOperationId",
|
|
81
|
+
]);
|
|
82
|
+
const args = freeze({
|
|
83
|
+
componentScope: text(entry.componentScope),
|
|
84
|
+
operationId: text(input.operationId),
|
|
85
|
+
parentOperationId: text(input.parentOperationId),
|
|
86
|
+
});
|
|
87
|
+
need(args.operationId !== args.parentOperationId);
|
|
88
|
+
need(
|
|
89
|
+
input.actualActionCtx &&
|
|
90
|
+
typeof input.actualActionCtx === "object" &&
|
|
91
|
+
!types.isProxy(input.actualActionCtx),
|
|
92
|
+
);
|
|
93
|
+
const ctx = input.actualActionCtx as GenericActionCtx<GenericDataModel>;
|
|
94
|
+
const auth = ctx.auth,
|
|
95
|
+
getIdentity = auth?.getUserIdentity;
|
|
96
|
+
need(
|
|
97
|
+
auth &&
|
|
98
|
+
!types.isProxy(auth) &&
|
|
99
|
+
typeof getIdentity === "function" &&
|
|
100
|
+
!types.isProxy(getIdentity),
|
|
101
|
+
);
|
|
102
|
+
const captured = {
|
|
103
|
+
query: ctx.runQuery,
|
|
104
|
+
mutation: ctx.runMutation,
|
|
105
|
+
meta: ctx.meta,
|
|
106
|
+
metadata: ctx.meta?.getFunctionMetadata,
|
|
107
|
+
};
|
|
108
|
+
const q = component.renewalOperations.queries;
|
|
109
|
+
const references = () => {
|
|
110
|
+
const current = component.renewalOperations.queries;
|
|
111
|
+
return [
|
|
112
|
+
current.loadAuthorizedOperation,
|
|
113
|
+
current.readAttempt,
|
|
114
|
+
current.readStageEvent,
|
|
115
|
+
current.readProviderAcknowledgement,
|
|
116
|
+
];
|
|
117
|
+
};
|
|
118
|
+
const refs = {
|
|
119
|
+
load: q.loadAuthorizedOperation,
|
|
120
|
+
attempt: q.readAttempt,
|
|
121
|
+
event: q.readStageEvent,
|
|
122
|
+
acknowledgement: q.readProviderAcknowledgement,
|
|
123
|
+
};
|
|
124
|
+
const addresses = references().map((ref) =>
|
|
125
|
+
JSON.stringify(getFunctionAddress(ref)),
|
|
126
|
+
);
|
|
127
|
+
const authAddress = JSON.stringify(
|
|
128
|
+
getFunctionAddress(entry.authorizationQuery),
|
|
129
|
+
);
|
|
130
|
+
const lifetime = createObservationReaderLifetime();
|
|
131
|
+
let binding: object | null = null;
|
|
132
|
+
let authorizedUntil: number | null = null;
|
|
133
|
+
function guard() {
|
|
134
|
+
if (authorizedUntil !== null)
|
|
135
|
+
need(lifetime.check().wallMs < authorizedUntil);
|
|
136
|
+
need(readMaintainedRootComponent(actualRoot) === component);
|
|
137
|
+
need(
|
|
138
|
+
COMPILED_OBSERVATION_HISTORY_ENTRIES.filter(
|
|
139
|
+
(e) => e.component === component,
|
|
140
|
+
).length === 1 && COMPILED_OBSERVATION_HISTORY_ENTRIES.includes(entry),
|
|
141
|
+
);
|
|
142
|
+
need(
|
|
143
|
+
ctx.auth === auth &&
|
|
144
|
+
auth.getUserIdentity === getIdentity &&
|
|
145
|
+
ctx.runQuery === captured.query &&
|
|
146
|
+
ctx.runMutation === captured.mutation &&
|
|
147
|
+
ctx.meta === captured.meta &&
|
|
148
|
+
ctx.meta.getFunctionMetadata === captured.metadata,
|
|
149
|
+
);
|
|
150
|
+
need(
|
|
151
|
+
JSON.stringify(getFunctionAddress(entry.authorizationQuery)) ===
|
|
152
|
+
authAddress,
|
|
153
|
+
);
|
|
154
|
+
references().forEach((ref, index) =>
|
|
155
|
+
need(JSON.stringify(getFunctionAddress(ref)) === addresses[index]),
|
|
156
|
+
);
|
|
157
|
+
if (binding) {
|
|
158
|
+
const rpc = readCheckedJournalActionRPC(binding);
|
|
159
|
+
need(
|
|
160
|
+
rpc.receiver === ctx &&
|
|
161
|
+
rpc.component === component &&
|
|
162
|
+
rpc.operationId === args.operationId &&
|
|
163
|
+
rpc.runQuery === captured.query &&
|
|
164
|
+
rpc.runMutation === captured.mutation,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const owned = <T>(work: () => T | PromiseLike<T>) =>
|
|
169
|
+
lifetime.owned(work, guard);
|
|
170
|
+
try {
|
|
171
|
+
binding = await owned(() =>
|
|
172
|
+
captureMaintainedJournalBinding(actualRoot, {
|
|
173
|
+
actualActionCtx: ctx,
|
|
174
|
+
operationId: args.operationId,
|
|
175
|
+
}),
|
|
176
|
+
);
|
|
177
|
+
guard();
|
|
178
|
+
} catch (error) {
|
|
179
|
+
lifetime.close();
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
async function authorize() {
|
|
183
|
+
const who = await owned(() => getIdentity.call(auth));
|
|
184
|
+
need(who && !types.isProxy(who));
|
|
185
|
+
const token = Object.getOwnPropertyDescriptor(who, "tokenIdentifier");
|
|
186
|
+
need(token && token.enumerable && "value" in token);
|
|
187
|
+
const tokenIdentifier = text(token.value, 2048);
|
|
188
|
+
const reply: unknown = await owned(() =>
|
|
189
|
+
captured.query.call(ctx, entry.authorizationQuery, args),
|
|
190
|
+
);
|
|
191
|
+
return captureReaderAuthorization(
|
|
192
|
+
reply,
|
|
193
|
+
args,
|
|
194
|
+
tokenIdentifier,
|
|
195
|
+
lifetime.check().wallMs,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
async function stage(operationId: string) {
|
|
199
|
+
const operation: unknown = captureReaderQueryDATA(
|
|
200
|
+
await owned(() => captured.query.call(ctx, refs.load, { operationId })),
|
|
201
|
+
);
|
|
202
|
+
const load = object(operation),
|
|
203
|
+
active = object(load.activeAttempt);
|
|
204
|
+
const attemptId = text(active.attemptId);
|
|
205
|
+
const attempt: unknown = captureReaderQueryDATA(
|
|
206
|
+
await owned(() =>
|
|
207
|
+
captured.query.call(ctx, refs.attempt, { operationId, attemptId }),
|
|
208
|
+
),
|
|
209
|
+
);
|
|
210
|
+
const manifest = closed(attempt, ["reservation", "events"]);
|
|
211
|
+
need(Array.isArray(manifest.events) && manifest.events.length <= 3);
|
|
212
|
+
const count = manifest.events.length;
|
|
213
|
+
const events = [];
|
|
214
|
+
for (let index = 0; index < count; index++) {
|
|
215
|
+
const sequence: 1 | 2 | 3 = index === 0 ? 1 : index === 1 ? 2 : 3;
|
|
216
|
+
events.push(
|
|
217
|
+
captureReaderQueryDATA(
|
|
218
|
+
await owned(() =>
|
|
219
|
+
captured.query.call(ctx, refs.event, {
|
|
220
|
+
operationId,
|
|
221
|
+
attemptId,
|
|
222
|
+
sequence,
|
|
223
|
+
}),
|
|
224
|
+
),
|
|
225
|
+
),
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
const acknowledgement: unknown = captureReaderQueryDATA(
|
|
229
|
+
await owned(() =>
|
|
230
|
+
captured.query.call(ctx, refs.acknowledgement, { operationId }),
|
|
231
|
+
),
|
|
232
|
+
);
|
|
233
|
+
return { operation, attempt, events, acknowledgement };
|
|
234
|
+
}
|
|
235
|
+
let capturedHistory: CapturedHistory | null = null;
|
|
236
|
+
let used = false;
|
|
237
|
+
async function captureHistory(): Promise<CapturedHistory> {
|
|
238
|
+
need(!used);
|
|
239
|
+
used = true;
|
|
240
|
+
lifetime.check();
|
|
241
|
+
guard();
|
|
242
|
+
const authorization = await authorize();
|
|
243
|
+
authorizedUntil = authorization.expiresAtMs;
|
|
244
|
+
const parent = await stage(args.parentOperationId),
|
|
245
|
+
configure = await stage(args.operationId);
|
|
246
|
+
const history = await owned(() =>
|
|
247
|
+
captureRetainedObservationHistory({ parent, configure }),
|
|
248
|
+
);
|
|
249
|
+
const receipt = history.attachmentReceipt;
|
|
250
|
+
matchConfigureDATA(authorization.providerScope, {
|
|
251
|
+
mode: "platform",
|
|
252
|
+
stripeAccount: null,
|
|
253
|
+
livemode: receipt.livemode,
|
|
254
|
+
customerAccountId: receipt.customerAccountId,
|
|
255
|
+
subscriptionId: receipt.subscriptionId,
|
|
256
|
+
scheduleId: receipt.scheduleId,
|
|
257
|
+
});
|
|
258
|
+
capturedHistory = Object.freeze({ history, authorization });
|
|
259
|
+
return capturedHistory;
|
|
260
|
+
}
|
|
261
|
+
async function authorizeUnchanged() {
|
|
262
|
+
need(capturedHistory);
|
|
263
|
+
const final = await authorize();
|
|
264
|
+
matchConfigureDATA(final, capturedHistory.authorization);
|
|
265
|
+
need(lifetime.check().wallMs < capturedHistory.authorization.expiresAtMs);
|
|
266
|
+
guard();
|
|
267
|
+
return final;
|
|
268
|
+
}
|
|
269
|
+
need(binding);
|
|
270
|
+
const token = Object.freeze({});
|
|
271
|
+
invocations.set(
|
|
272
|
+
token,
|
|
273
|
+
Object.freeze({
|
|
274
|
+
binding,
|
|
275
|
+
historyEntry: entry,
|
|
276
|
+
guard() {
|
|
277
|
+
lifetime.check();
|
|
278
|
+
guard();
|
|
279
|
+
},
|
|
280
|
+
check: lifetime.check,
|
|
281
|
+
close: lifetime.close,
|
|
282
|
+
owned<T>(work: () => T | PromiseLike<T>, extra?: () => void) {
|
|
283
|
+
return lifetime.owned(work, () => {
|
|
284
|
+
guard();
|
|
285
|
+
extra?.();
|
|
286
|
+
});
|
|
287
|
+
},
|
|
288
|
+
captureHistory,
|
|
289
|
+
authorizeUnchanged,
|
|
290
|
+
readCaptured() {
|
|
291
|
+
lifetime.check();
|
|
292
|
+
guard();
|
|
293
|
+
need(capturedHistory);
|
|
294
|
+
return capturedHistory;
|
|
295
|
+
},
|
|
296
|
+
}),
|
|
297
|
+
);
|
|
298
|
+
return token;
|
|
299
|
+
}
|
|
@@ -123,7 +123,7 @@ function soleItem(subscription: unknown) {
|
|
|
123
123
|
resourceID(own(item, "id"), "si");
|
|
124
124
|
return item;
|
|
125
125
|
}
|
|
126
|
-
export function
|
|
126
|
+
export function validateObservationIdentityRouting(
|
|
127
127
|
record: ObservationRecord,
|
|
128
128
|
reads: ObservationReads,
|
|
129
129
|
) {
|
|
@@ -171,6 +171,14 @@ export function validateBaseRouting(
|
|
|
171
171
|
reference(sub.schedule, "sub_sched", "subscription_schedule", p) ===
|
|
172
172
|
p.schedule,
|
|
173
173
|
);
|
|
174
|
+
}
|
|
175
|
+
export function validateBaseRouting(
|
|
176
|
+
record: ObservationRecord,
|
|
177
|
+
reads: ObservationReads,
|
|
178
|
+
) {
|
|
179
|
+
validateObservationIdentityRouting(record, reads);
|
|
180
|
+
const p = ids(record),
|
|
181
|
+
sub = object(reads.subscription);
|
|
174
182
|
const item = soleItem(sub);
|
|
175
183
|
need(own(item, "subscription") === p.subscription);
|
|
176
184
|
// Do not constrain status, current price, old item ID/period, phases or material
|
|
@@ -1,240 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readerNeed as need } from "./readerAuthorization.js";
|
|
2
|
+
import { freeze } from "../c10/ownData.js";
|
|
2
3
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from "
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
readMaintainedRootComponent,
|
|
10
|
-
captureMaintainedJournalBinding,
|
|
11
|
-
readCheckedJournalActionRPC,
|
|
12
|
-
} from "../producer/classOwnedActionRoot.js";
|
|
13
|
-
import { closed, freeze } from "../c10/ownData.js";
|
|
14
|
-
import {
|
|
15
|
-
object,
|
|
16
|
-
matchConfigureDATA,
|
|
17
|
-
} from "../producer/configureJournalData.js";
|
|
18
|
-
import { captureRetainedObservationHistory } from "./history.js";
|
|
19
|
-
import {
|
|
20
|
-
readerNeed as need,
|
|
21
|
-
readerText as text,
|
|
22
|
-
readerStop,
|
|
23
|
-
captureReaderAuthorization,
|
|
24
|
-
createObservationReaderLifetime,
|
|
25
|
-
captureReaderQueryDATA,
|
|
26
|
-
} from "./readerAuthorization.js";
|
|
27
|
-
|
|
28
|
-
/** Fresh host authorization for four fixed DATA queries. No client or write authority. */
|
|
4
|
+
captureObservationInvocation,
|
|
5
|
+
readObservationInvocation,
|
|
6
|
+
closeObservationInvocation,
|
|
7
|
+
} from "./invocation.js";
|
|
8
|
+
/** Query-only wrapper. Returned DATA does not retain an open invocation. */
|
|
29
9
|
export async function captureAuthorizedObservationHistoryReader(
|
|
30
|
-
|
|
10
|
+
root: object,
|
|
31
11
|
raw: unknown,
|
|
32
12
|
) {
|
|
33
13
|
need(arguments.length === 2);
|
|
34
|
-
|
|
35
|
-
const component = readMaintainedRootComponent(actualRoot);
|
|
36
|
-
const matches = COMPILED_OBSERVATION_HISTORY_ENTRIES.filter(
|
|
37
|
-
(e) => e.component === component,
|
|
38
|
-
);
|
|
39
|
-
need(matches.length === 1);
|
|
40
|
-
const entry = matches[0];
|
|
41
|
-
closed(entry, ["component", "componentScope", "authorizationQuery"]);
|
|
42
|
-
need(Object.isFrozen(entry));
|
|
43
|
-
need(!types.isProxy(raw));
|
|
44
|
-
const input = closed(raw, [
|
|
45
|
-
"actualActionCtx",
|
|
46
|
-
"operationId",
|
|
47
|
-
"parentOperationId",
|
|
48
|
-
]);
|
|
49
|
-
const args = freeze({
|
|
50
|
-
componentScope: text(entry.componentScope),
|
|
51
|
-
operationId: text(input.operationId),
|
|
52
|
-
parentOperationId: text(input.parentOperationId),
|
|
53
|
-
});
|
|
54
|
-
need(args.operationId !== args.parentOperationId);
|
|
55
|
-
need(
|
|
56
|
-
input.actualActionCtx &&
|
|
57
|
-
typeof input.actualActionCtx === "object" &&
|
|
58
|
-
!types.isProxy(input.actualActionCtx),
|
|
59
|
-
);
|
|
60
|
-
const ctx = input.actualActionCtx as GenericActionCtx<GenericDataModel>;
|
|
61
|
-
const auth = ctx.auth,
|
|
62
|
-
getIdentity = auth?.getUserIdentity;
|
|
63
|
-
need(
|
|
64
|
-
auth &&
|
|
65
|
-
!types.isProxy(auth) &&
|
|
66
|
-
typeof getIdentity === "function" &&
|
|
67
|
-
!types.isProxy(getIdentity),
|
|
68
|
-
);
|
|
69
|
-
const captured = {
|
|
70
|
-
query: ctx.runQuery,
|
|
71
|
-
mutation: ctx.runMutation,
|
|
72
|
-
meta: ctx.meta,
|
|
73
|
-
metadata: ctx.meta?.getFunctionMetadata,
|
|
74
|
-
};
|
|
75
|
-
const q = component.renewalOperations.queries;
|
|
76
|
-
const references = () => {
|
|
77
|
-
const current = component.renewalOperations.queries;
|
|
78
|
-
return [
|
|
79
|
-
current.loadAuthorizedOperation,
|
|
80
|
-
current.readAttempt,
|
|
81
|
-
current.readStageEvent,
|
|
82
|
-
current.readProviderAcknowledgement,
|
|
83
|
-
];
|
|
84
|
-
};
|
|
85
|
-
const refs = {
|
|
86
|
-
load: q.loadAuthorizedOperation,
|
|
87
|
-
attempt: q.readAttempt,
|
|
88
|
-
event: q.readStageEvent,
|
|
89
|
-
acknowledgement: q.readProviderAcknowledgement,
|
|
90
|
-
};
|
|
91
|
-
const addresses = references().map((ref) =>
|
|
92
|
-
JSON.stringify(getFunctionAddress(ref)),
|
|
93
|
-
);
|
|
94
|
-
const authAddress = JSON.stringify(
|
|
95
|
-
getFunctionAddress(entry.authorizationQuery),
|
|
96
|
-
);
|
|
97
|
-
const lifetime = createObservationReaderLifetime();
|
|
98
|
-
let binding: object | null = null;
|
|
99
|
-
let authorizedUntil: number | null = null;
|
|
100
|
-
function guard() {
|
|
101
|
-
if (authorizedUntil !== null)
|
|
102
|
-
need(lifetime.check().wallMs < authorizedUntil);
|
|
103
|
-
need(readMaintainedRootComponent(actualRoot) === component);
|
|
104
|
-
need(
|
|
105
|
-
COMPILED_OBSERVATION_HISTORY_ENTRIES.filter(
|
|
106
|
-
(e) => e.component === component,
|
|
107
|
-
).length === 1 && COMPILED_OBSERVATION_HISTORY_ENTRIES.includes(entry),
|
|
108
|
-
);
|
|
109
|
-
need(
|
|
110
|
-
ctx.auth === auth &&
|
|
111
|
-
auth.getUserIdentity === getIdentity &&
|
|
112
|
-
ctx.runQuery === captured.query &&
|
|
113
|
-
ctx.runMutation === captured.mutation &&
|
|
114
|
-
ctx.meta === captured.meta &&
|
|
115
|
-
ctx.meta.getFunctionMetadata === captured.metadata,
|
|
116
|
-
);
|
|
117
|
-
need(
|
|
118
|
-
JSON.stringify(getFunctionAddress(entry.authorizationQuery)) ===
|
|
119
|
-
authAddress,
|
|
120
|
-
);
|
|
121
|
-
references().forEach((ref, index) =>
|
|
122
|
-
need(JSON.stringify(getFunctionAddress(ref)) === addresses[index]),
|
|
123
|
-
);
|
|
124
|
-
if (binding) {
|
|
125
|
-
const rpc = readCheckedJournalActionRPC(binding);
|
|
126
|
-
need(
|
|
127
|
-
rpc.receiver === ctx &&
|
|
128
|
-
rpc.component === component &&
|
|
129
|
-
rpc.operationId === args.operationId &&
|
|
130
|
-
rpc.runQuery === captured.query &&
|
|
131
|
-
rpc.runMutation === captured.mutation,
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
const owned = <T>(work: () => T | PromiseLike<T>) =>
|
|
136
|
-
lifetime.owned(work, guard);
|
|
137
|
-
try {
|
|
138
|
-
binding = await owned(() =>
|
|
139
|
-
captureMaintainedJournalBinding(actualRoot, {
|
|
140
|
-
actualActionCtx: ctx,
|
|
141
|
-
operationId: args.operationId,
|
|
142
|
-
}),
|
|
143
|
-
);
|
|
144
|
-
guard();
|
|
145
|
-
} catch (error) {
|
|
146
|
-
lifetime.close();
|
|
147
|
-
throw error;
|
|
148
|
-
}
|
|
149
|
-
async function authorize() {
|
|
150
|
-
const who = await owned(() => getIdentity.call(auth));
|
|
151
|
-
need(who && !types.isProxy(who));
|
|
152
|
-
const token = Object.getOwnPropertyDescriptor(who, "tokenIdentifier");
|
|
153
|
-
need(token && token.enumerable && "value" in token);
|
|
154
|
-
const tokenIdentifier = text(token.value, 2048);
|
|
155
|
-
const reply: unknown = await owned(() =>
|
|
156
|
-
captured.query.call(ctx, entry.authorizationQuery, args),
|
|
157
|
-
);
|
|
158
|
-
return captureReaderAuthorization(
|
|
159
|
-
reply,
|
|
160
|
-
args,
|
|
161
|
-
tokenIdentifier,
|
|
162
|
-
lifetime.check().wallMs,
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
async function stage(operationId: string) {
|
|
166
|
-
const operation: unknown = captureReaderQueryDATA(
|
|
167
|
-
await owned(() => captured.query.call(ctx, refs.load, { operationId })),
|
|
168
|
-
);
|
|
169
|
-
const load = object(operation),
|
|
170
|
-
active = object(load.activeAttempt);
|
|
171
|
-
const attemptId = text(active.attemptId);
|
|
172
|
-
const attempt: unknown = captureReaderQueryDATA(
|
|
173
|
-
await owned(() =>
|
|
174
|
-
captured.query.call(ctx, refs.attempt, { operationId, attemptId }),
|
|
175
|
-
),
|
|
176
|
-
);
|
|
177
|
-
const manifest = closed(attempt, ["reservation", "events"]);
|
|
178
|
-
need(Array.isArray(manifest.events) && manifest.events.length <= 3);
|
|
179
|
-
const count = manifest.events.length;
|
|
180
|
-
const events = [];
|
|
181
|
-
for (let index = 0; index < count; index++) {
|
|
182
|
-
const sequence: 1 | 2 | 3 = index === 0 ? 1 : index === 1 ? 2 : 3;
|
|
183
|
-
events.push(
|
|
184
|
-
captureReaderQueryDATA(
|
|
185
|
-
await owned(() =>
|
|
186
|
-
captured.query.call(ctx, refs.event, {
|
|
187
|
-
operationId,
|
|
188
|
-
attemptId,
|
|
189
|
-
sequence,
|
|
190
|
-
}),
|
|
191
|
-
),
|
|
192
|
-
),
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
const acknowledgement: unknown = captureReaderQueryDATA(
|
|
196
|
-
await owned(() =>
|
|
197
|
-
captured.query.call(ctx, refs.acknowledgement, { operationId }),
|
|
198
|
-
),
|
|
199
|
-
);
|
|
200
|
-
return { operation, attempt, events, acknowledgement };
|
|
201
|
-
}
|
|
14
|
+
const token = await captureObservationInvocation(root, raw);
|
|
202
15
|
let used = false;
|
|
203
16
|
return Object.freeze({
|
|
204
17
|
async read() {
|
|
205
18
|
need(arguments.length === 0 && !used);
|
|
206
19
|
used = true;
|
|
207
20
|
try {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
const parent = await stage(args.parentOperationId),
|
|
213
|
-
configure = await stage(args.operationId);
|
|
214
|
-
const history = await owned(() =>
|
|
215
|
-
captureRetainedObservationHistory({ parent, configure }),
|
|
216
|
-
);
|
|
217
|
-
const receipt = history.attachmentReceipt;
|
|
218
|
-
matchConfigureDATA(authorization.providerScope, {
|
|
219
|
-
mode: "platform",
|
|
220
|
-
stripeAccount: null,
|
|
221
|
-
livemode: receipt.livemode,
|
|
222
|
-
customerAccountId: receipt.customerAccountId,
|
|
223
|
-
subscriptionId: receipt.subscriptionId,
|
|
224
|
-
scheduleId: receipt.scheduleId,
|
|
225
|
-
});
|
|
226
|
-
const final = await authorize();
|
|
227
|
-
matchConfigureDATA(final, authorization);
|
|
228
|
-
need(lifetime.check().wallMs < authorization.expiresAtMs);
|
|
229
|
-
guard();
|
|
21
|
+
const state = readObservationInvocation(token);
|
|
22
|
+
const captured = await state.captureHistory();
|
|
23
|
+
await state.authorizeUnchanged();
|
|
24
|
+
state.guard();
|
|
230
25
|
return freeze({
|
|
231
26
|
status: "authorized_observation_history_data" as const,
|
|
232
|
-
|
|
233
|
-
authorization,
|
|
27
|
+
...captured,
|
|
234
28
|
operationalAuthority: false as const,
|
|
235
29
|
});
|
|
236
30
|
} finally {
|
|
237
|
-
|
|
31
|
+
closeObservationInvocation(token);
|
|
238
32
|
}
|
|
239
33
|
},
|
|
240
34
|
});
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { ObservationHistorySourceEntry } from "../observation/readerAuthorization.js";
|
|
2
|
+
import type { ObservationRecord } from "../observation/types.js";
|
|
3
|
+
export type FreshObservationProfile = Readonly<{
|
|
4
|
+
historyEntry: ObservationHistorySourceEntry;
|
|
5
|
+
readProfile: "endive-schedule-first-44-v1";
|
|
6
|
+
coversObservation(record: ObservationRecord): boolean;
|
|
7
|
+
}>;
|
|
2
8
|
/** Fixed internal operation associations. No production producer or class is bound. */
|
|
3
9
|
export const COMPILED_INITIAL_ENTRIES: readonly unknown[] = Object.freeze([]);
|
|
4
10
|
export const COMPILED_RECOVERY_ENTRIES: readonly unknown[] = Object.freeze([]);
|
|
@@ -48,6 +54,8 @@ export const COMPILED_OBSERVATION_ENTRIES: readonly unknown[] = Object.freeze(
|
|
|
48
54
|
);
|
|
49
55
|
export const COMPILED_OBSERVATION_HISTORY_ENTRIES: readonly ObservationHistorySourceEntry[] =
|
|
50
56
|
Object.freeze([]);
|
|
57
|
+
export const COMPILED_FRESH_OBSERVATION_ENTRIES: readonly FreshObservationProfile[] =
|
|
58
|
+
Object.freeze([]);
|
|
51
59
|
export function loadFixedObservationOperation(): unknown {
|
|
52
60
|
throw Error("OBSERVATION_SOURCE_UNBOUND");
|
|
53
61
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { vi } from "vitest";
|
|
2
|
+
import { admissionControls } from "./producerConfigureAdmission.js";
|
|
3
|
+
import {
|
|
4
|
+
prepareObservationReader,
|
|
5
|
+
readerControls,
|
|
6
|
+
} from "./observationReader.js";
|
|
7
|
+
// Fixed synthetic native month boundaries. These are test DATA, never host date math.
|
|
8
|
+
function targetSchedule(original: Record<string, unknown>, weekly: boolean) {
|
|
9
|
+
const schedule = structuredClone(original);
|
|
10
|
+
const phases = schedule.phases as Record<string, unknown>[];
|
|
11
|
+
const current = phases[0],
|
|
12
|
+
target = structuredClone(current);
|
|
13
|
+
target.start_date = current.end_date;
|
|
14
|
+
target.end_date = weekly ? 1771718400 : 1773532800;
|
|
15
|
+
target.billing_cycle_anchor = weekly ? "phase_start" : "automatic";
|
|
16
|
+
target.proration_behavior = "none";
|
|
17
|
+
const items = target.items as Record<string, unknown>[];
|
|
18
|
+
items[0].price = "price_target";
|
|
19
|
+
items[0].plan = "price_target";
|
|
20
|
+
schedule.phases = [current, target];
|
|
21
|
+
return schedule;
|
|
22
|
+
}
|
|
23
|
+
export async function prepareFreshObservation(
|
|
24
|
+
h: Parameters<typeof prepareObservationReader>[0],
|
|
25
|
+
options: { active?: boolean; retained?: boolean } = {},
|
|
26
|
+
) {
|
|
27
|
+
const state = await prepareObservationReader(h, {
|
|
28
|
+
write: options.retained === true,
|
|
29
|
+
beforeWrite() {
|
|
30
|
+
const pending = targetSchedule(
|
|
31
|
+
h.baseline.schedule,
|
|
32
|
+
String(
|
|
33
|
+
h.baseline.original.attachRequest.preparation.targetPrice.recurring
|
|
34
|
+
.interval,
|
|
35
|
+
) === "week",
|
|
36
|
+
);
|
|
37
|
+
Object.assign(h.baseline.schedule, pending);
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
const configuration = JSON.parse(state.full.configure.operation.recordJSON)
|
|
41
|
+
.configureRecord.configuration;
|
|
42
|
+
const weekly = configuration.targetPrice.recurring.interval === "week";
|
|
43
|
+
const schedule = targetSchedule(configuration.schedule, weekly);
|
|
44
|
+
const subscription = structuredClone(configuration.freshSubscription);
|
|
45
|
+
const price = structuredClone(configuration.targetPrice);
|
|
46
|
+
const customer = structuredClone(configuration.billingCustomer);
|
|
47
|
+
const target = (schedule.phases as Record<string, unknown>[])[1];
|
|
48
|
+
if (options.active) {
|
|
49
|
+
schedule.current_phase = {
|
|
50
|
+
start_date: target.start_date,
|
|
51
|
+
end_date: target.end_date,
|
|
52
|
+
};
|
|
53
|
+
const item = subscription.items.data[0];
|
|
54
|
+
item.id = "si_successor";
|
|
55
|
+
item.price = structuredClone(price);
|
|
56
|
+
Object.assign(item.plan, {
|
|
57
|
+
id: price.id,
|
|
58
|
+
amount: price.unit_amount,
|
|
59
|
+
amount_decimal: price.unit_amount_decimal,
|
|
60
|
+
product: price.product,
|
|
61
|
+
metadata: price.metadata,
|
|
62
|
+
nickname: price.nickname,
|
|
63
|
+
interval: price.recurring.interval,
|
|
64
|
+
interval_count: price.recurring.interval_count,
|
|
65
|
+
});
|
|
66
|
+
item.current_period_start = target.start_date;
|
|
67
|
+
item.current_period_end = target.end_date;
|
|
68
|
+
for (const discount of item.discounts) discount.subscription_item = item.id;
|
|
69
|
+
if (weekly) subscription.billing_cycle_anchor = target.start_date;
|
|
70
|
+
} else vi.setSystemTime((configuration.firstDispatchedAt + 1000) * 1000);
|
|
71
|
+
readerControls.grant.expiresAtMs = Date.now() + 60000;
|
|
72
|
+
const reads = {
|
|
73
|
+
schedule,
|
|
74
|
+
subscription,
|
|
75
|
+
targetPrice: price,
|
|
76
|
+
billingCustomer: customer,
|
|
77
|
+
coupons: [] as unknown[],
|
|
78
|
+
};
|
|
79
|
+
h.setControlledRead("/v1/subscription_schedules/sub_sched_owned", schedule);
|
|
80
|
+
h.setControlledRead("/v1/subscriptions/sub_original", subscription);
|
|
81
|
+
h.setControlledRead("/v1/prices/price_target", price);
|
|
82
|
+
h.setControlledRead("/v1/customers/cus_buyer", customer);
|
|
83
|
+
const profile = Object.freeze({
|
|
84
|
+
historyEntry: state.entry,
|
|
85
|
+
readProfile: "endive-schedule-first-44-v1",
|
|
86
|
+
coversObservation: () => true,
|
|
87
|
+
});
|
|
88
|
+
admissionControls.observation.splice(
|
|
89
|
+
0,
|
|
90
|
+
admissionControls.observation.length,
|
|
91
|
+
profile,
|
|
92
|
+
);
|
|
93
|
+
return { ...state, reads, profile };
|
|
94
|
+
}
|