@fantastic.dev/better-stripe 0.91.0 → 0.93.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/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalAttach/internal/configure/actor.d.ts +6 -0
- package/dist/client/billing/renewalAttach/internal/configure/actor.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/configure/actor.js +46 -56
- package/dist/client/billing/renewalAttach/internal/configure/actor.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.d.ts +3 -0
- package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.js +61 -0
- package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/actor.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/actor.js +25 -4
- package/dist/client/billing/renewalAttach/internal/observation/actor.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/capture.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/capture.js +3 -2
- package/dist/client/billing/renewalAttach/internal/observation/capture.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.d.ts +5 -0
- package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.js +11 -0
- package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/types.d.ts +5 -0
- package/dist/client/billing/renewalAttach/internal/observation/types.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/types.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts +30 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js +113 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js.map +1 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +562 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +281 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +562 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +281 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/index.d.ts +3 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +16 -3
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/api.d.ts +6 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +406 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
- package/dist/component/earnings/triggers.d.ts +294 -0
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- 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/renewalOperations/helpers.d.ts +236 -0
- package/dist/component/renewalOperations/helpers.d.ts.map +1 -0
- package/dist/component/renewalOperations/helpers.js +457 -0
- package/dist/component/renewalOperations/helpers.js.map +1 -0
- package/dist/component/renewalOperations/mutations.d.ts +252 -0
- package/dist/component/renewalOperations/mutations.d.ts.map +1 -0
- package/dist/component/renewalOperations/mutations.js +340 -0
- package/dist/component/renewalOperations/mutations.js.map +1 -0
- package/dist/component/renewalOperations/queries.d.ts +163 -0
- package/dist/component/renewalOperations/queries.d.ts.map +1 -0
- package/dist/component/renewalOperations/queries.js +167 -0
- package/dist/component/renewalOperations/queries.js.map +1 -0
- package/dist/component/renewalOperations/schema.d.ts +1988 -0
- package/dist/component/renewalOperations/schema.d.ts.map +1 -0
- package/dist/component/renewalOperations/schema.js +227 -0
- package/dist/component/renewalOperations/schema.js.map +1 -0
- package/dist/component/schema.d.ts +281 -0
- package/dist/component/schema.js +5 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/usage/allowance.fixtures.d.ts +281 -0
- package/dist/component/usage/allowance.fixtures.d.ts.map +1 -1
- package/dist/component/usageCredits/accounting.fixtures.d.ts +281 -0
- package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -1
- package/dist/shared/renewalOperationJournal.d.ts +43 -0
- package/dist/shared/renewalOperationJournal.d.ts.map +1 -0
- package/dist/shared/renewalOperationJournal.js +294 -0
- package/dist/shared/renewalOperationJournal.js.map +1 -0
- package/docs/renewal-context-reads.md +152 -0
- package/docs/renewal-material-observation.md +32 -11
- package/package.json +1 -1
- package/src/client/billing/renewalAttach/internal/configure/actor.ts +63 -52
- package/src/client/billing/renewalAttach/internal/configure/ownedContinuation.ts +56 -0
- package/src/client/billing/renewalAttach/internal/observation/actor.ts +46 -3
- package/src/client/billing/renewalAttach/internal/observation/capture.ts +3 -2
- package/src/client/billing/renewalAttach/internal/observation/configureAcknowledgement.ts +20 -0
- package/src/client/billing/renewalAttach/internal/observation/observationConfigureACK.integration.test.ts +36 -0
- package/src/client/billing/renewalAttach/internal/observation/observationConfigureACKControls.integration.test.ts +48 -0
- package/src/client/billing/renewalAttach/internal/observation/observationConfigureACKLifecycle.integration.test.ts +44 -0
- package/src/client/billing/renewalAttach/internal/observation/observationConfiguredHistory.integration.test.ts +34 -0
- package/src/client/billing/renewalAttach/internal/observation/types.ts +5 -0
- package/src/client/billing/renewalAttach/internal/operationJournal.integration.test.ts +380 -0
- package/src/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.ts +155 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteAssertions.ts +5 -1
- package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteDeclarations.ts +12 -1
- package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteDelegate.ts +38 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteHistory.ts +8 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteOperation.ts +10 -23
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKAssertions.ts +115 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKDeclarations.ts +141 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleAssertions.ts +74 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleDeclarations.ts +79 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleInstrumentation.ts +207 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleOperation.ts +494 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKOperation.ts +452 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryAssertions.ts +88 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryDeclarations.ts +170 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryNative.ts +99 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryOperation.ts +411 -0
- package/src/client/index.ts +24 -3
- package/src/component/_generated/api.ts +6 -0
- package/src/component/_generated/component.ts +450 -0
- package/src/component/renewalOperations/acknowledgement.fixtures.ts +552 -0
- package/src/component/renewalOperations/acknowledgement.test.ts +573 -0
- package/src/component/renewalOperations/acknowledgementReads.test.ts +442 -0
- package/src/component/renewalOperations/acknowledgementV2.test.ts +493 -0
- package/src/component/renewalOperations/acknowledgementV2Publication.test.ts +373 -0
- package/src/component/renewalOperations/basisBinding.test.ts +567 -0
- package/src/component/renewalOperations/basisLifecycle.test.ts +569 -0
- package/src/component/renewalOperations/boundHistory.test.ts +474 -0
- package/src/component/renewalOperations/helpers.ts +592 -0
- package/src/component/renewalOperations/historyReads.test.ts +595 -0
- package/src/component/renewalOperations/initialBasis.test.ts +578 -0
- package/src/component/renewalOperations/journal.test.ts +567 -0
- package/src/component/renewalOperations/mutations.ts +456 -0
- package/src/component/renewalOperations/payload.test.ts +589 -0
- package/src/component/renewalOperations/payloadEncoding.test.ts +592 -0
- package/src/component/renewalOperations/queries.ts +204 -0
- package/src/component/renewalOperations/schema.ts +268 -0
- package/src/component/renewalOperations/timingPolicy.fixtures.ts +193 -0
- package/src/component/renewalOperations/timingPolicy.test.ts +523 -0
- package/src/component/schema.ts +11 -0
- package/src/shared/renewalOperationJournal.ts +333 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { query } from "../_generated/server.js";
|
|
3
|
+
import {
|
|
4
|
+
originalRecord,
|
|
5
|
+
currentReservation,
|
|
6
|
+
eventChain,
|
|
7
|
+
validateAttachAcknowledgement,
|
|
8
|
+
validateKnownAcknowledgement,
|
|
9
|
+
validateV2KnownAcknowledgement,
|
|
10
|
+
} from "./helpers.js";
|
|
11
|
+
import {
|
|
12
|
+
loadedResult,
|
|
13
|
+
attemptResult,
|
|
14
|
+
providerAcknowledgementReadResult,
|
|
15
|
+
} from "./schema.js";
|
|
16
|
+
|
|
17
|
+
/** Full immutable DATA, not a dispatch or Owner capability. */
|
|
18
|
+
export const loadAuthorizedOperation = query({
|
|
19
|
+
args: { operationId: v.id("renewalOperations") },
|
|
20
|
+
returns: loadedResult,
|
|
21
|
+
handler: async (ctx, args) => {
|
|
22
|
+
const row = await ctx.db.get("renewalOperations", args.operationId);
|
|
23
|
+
if (!row) return null;
|
|
24
|
+
const full = await originalRecord(ctx, row);
|
|
25
|
+
const reservation = await currentReservation(ctx, row, full);
|
|
26
|
+
if (reservation && "recordVersion" in reservation) {
|
|
27
|
+
const events = await eventChain(ctx, reservation);
|
|
28
|
+
await validateV2KnownAcknowledgement(ctx, row, full, reservation, events);
|
|
29
|
+
}
|
|
30
|
+
if ("recordVersion" in full) {
|
|
31
|
+
if (!reservation)
|
|
32
|
+
return {
|
|
33
|
+
recordVersion: 2 as const,
|
|
34
|
+
operationId: row._id,
|
|
35
|
+
recordJSON: full.json,
|
|
36
|
+
revision: 0 as const,
|
|
37
|
+
activeAttempt: null,
|
|
38
|
+
initialBasis: null,
|
|
39
|
+
};
|
|
40
|
+
if (!("recordVersion" in reservation))
|
|
41
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
42
|
+
const { recordJSON: _recordJSON, ...summary } = reservation;
|
|
43
|
+
void _recordJSON;
|
|
44
|
+
return {
|
|
45
|
+
recordVersion: 2 as const,
|
|
46
|
+
operationId: row._id,
|
|
47
|
+
recordJSON: full.json,
|
|
48
|
+
revision: 1 as const,
|
|
49
|
+
activeAttempt: summary,
|
|
50
|
+
initialBasis: row.initialBasis ?? null,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (reservation && "recordVersion" in reservation)
|
|
54
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
55
|
+
let activeAttempt: Omit<
|
|
56
|
+
NonNullable<typeof reservation>,
|
|
57
|
+
"recordJSON"
|
|
58
|
+
> | null = null;
|
|
59
|
+
if (reservation) {
|
|
60
|
+
const { recordJSON: _recordJSON, ...summary } = reservation;
|
|
61
|
+
void _recordJSON;
|
|
62
|
+
activeAttempt = summary;
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
operationId: row._id,
|
|
66
|
+
recordJSON: full.json,
|
|
67
|
+
revision: row.revision,
|
|
68
|
+
activeAttempt,
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
/** Checked bounded manifest; complete event JSON is retained separately. */
|
|
74
|
+
export const readAttempt = query({
|
|
75
|
+
args: {
|
|
76
|
+
operationId: v.id("renewalOperations"),
|
|
77
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
78
|
+
},
|
|
79
|
+
returns: attemptResult,
|
|
80
|
+
handler: async (ctx, args) => {
|
|
81
|
+
const row = await ctx.db.get("renewalOperations", args.operationId);
|
|
82
|
+
if (!row) return null;
|
|
83
|
+
const full = await originalRecord(ctx, row);
|
|
84
|
+
const reservation = await currentReservation(ctx, row, full);
|
|
85
|
+
if (!reservation || reservation.attemptId !== args.attemptId)
|
|
86
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
87
|
+
const checkedEvents = await eventChain(ctx, reservation);
|
|
88
|
+
if ("recordVersion" in reservation)
|
|
89
|
+
await validateV2KnownAcknowledgement(
|
|
90
|
+
ctx,
|
|
91
|
+
row,
|
|
92
|
+
full,
|
|
93
|
+
reservation,
|
|
94
|
+
checkedEvents,
|
|
95
|
+
);
|
|
96
|
+
const events = checkedEvents.map((event) => {
|
|
97
|
+
const { dataJSON: _dataJSON, ...descriptor } = event;
|
|
98
|
+
void _dataJSON;
|
|
99
|
+
return descriptor;
|
|
100
|
+
});
|
|
101
|
+
return { reservation, events };
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
/** One complete event, scoped to the checked operation and active attempt. */
|
|
106
|
+
export const readStageEvent = query({
|
|
107
|
+
args: {
|
|
108
|
+
operationId: v.id("renewalOperations"),
|
|
109
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
110
|
+
sequence: v.union(v.literal(1), v.literal(2), v.literal(3)),
|
|
111
|
+
},
|
|
112
|
+
returns: v.union(
|
|
113
|
+
v.null(),
|
|
114
|
+
v.object({
|
|
115
|
+
operationId: v.id("renewalOperations"),
|
|
116
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
117
|
+
stage: v.union(v.literal("attach"), v.literal("configure")),
|
|
118
|
+
sourcePayloadId: v.id("renewalOperationPayloads"),
|
|
119
|
+
sequence: v.union(v.literal(1), v.literal(2), v.literal(3)),
|
|
120
|
+
binding: v.string(),
|
|
121
|
+
payloadId: v.id("renewalOperationPayloads"),
|
|
122
|
+
eventId: v.id("renewalOperationEvents"),
|
|
123
|
+
dataJSON: v.string(),
|
|
124
|
+
}),
|
|
125
|
+
),
|
|
126
|
+
handler: async (ctx, args) => {
|
|
127
|
+
const row = await ctx.db.get("renewalOperations", args.operationId);
|
|
128
|
+
if (!row) return null;
|
|
129
|
+
const full = await originalRecord(ctx, row);
|
|
130
|
+
const reservation = await currentReservation(ctx, row, full);
|
|
131
|
+
if (!reservation || reservation.attemptId !== args.attemptId)
|
|
132
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
133
|
+
const events = await eventChain(ctx, reservation);
|
|
134
|
+
if ("recordVersion" in reservation)
|
|
135
|
+
await validateV2KnownAcknowledgement(ctx, row, full, reservation, events);
|
|
136
|
+
return events.find((event) => event.sequence === args.sequence) ?? null;
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
/** Full retained acknowledgement DATA; never runtime ownership or replay authority. */
|
|
141
|
+
export const readProviderAcknowledgement = query({
|
|
142
|
+
args: { operationId: v.id("renewalOperations") },
|
|
143
|
+
returns: providerAcknowledgementReadResult,
|
|
144
|
+
handler: async (ctx, { operationId }) => {
|
|
145
|
+
try {
|
|
146
|
+
const row = await ctx.db.get("renewalOperations", operationId);
|
|
147
|
+
if (!row) return null;
|
|
148
|
+
const full = await originalRecord(ctx, row);
|
|
149
|
+
const reservation = await currentReservation(ctx, row, full);
|
|
150
|
+
if (!reservation) {
|
|
151
|
+
if (row.knownAcknowledgement !== undefined)
|
|
152
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
const events = await eventChain(ctx, reservation);
|
|
156
|
+
if ("recordVersion" in reservation) {
|
|
157
|
+
const known = await validateV2KnownAcknowledgement(
|
|
158
|
+
ctx,
|
|
159
|
+
row,
|
|
160
|
+
full,
|
|
161
|
+
reservation,
|
|
162
|
+
events,
|
|
163
|
+
);
|
|
164
|
+
if (known === null) return null;
|
|
165
|
+
return {
|
|
166
|
+
retention: {
|
|
167
|
+
status: "retained" as const,
|
|
168
|
+
operationId,
|
|
169
|
+
...known.retained,
|
|
170
|
+
},
|
|
171
|
+
receiptJSON: known.admitted.receiptJSON,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if (row.knownAcknowledgement === undefined) return null;
|
|
175
|
+
if (reservation.stage !== "attach")
|
|
176
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
177
|
+
const payload = await ctx.db.get(
|
|
178
|
+
"renewalOperationPayloads",
|
|
179
|
+
row.knownAcknowledgement.receiptPayloadId,
|
|
180
|
+
);
|
|
181
|
+
if (!payload) throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
182
|
+
const admitted = await validateAttachAcknowledgement(
|
|
183
|
+
full,
|
|
184
|
+
reservation,
|
|
185
|
+
events,
|
|
186
|
+
payload.json,
|
|
187
|
+
);
|
|
188
|
+
const retained = await validateKnownAcknowledgement(
|
|
189
|
+
ctx,
|
|
190
|
+
row,
|
|
191
|
+
reservation,
|
|
192
|
+
events,
|
|
193
|
+
admitted,
|
|
194
|
+
);
|
|
195
|
+
if (retained === null) throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
196
|
+
return {
|
|
197
|
+
retention: { status: "retained" as const, operationId, ...retained },
|
|
198
|
+
receiptJSON: admitted.receiptJSON,
|
|
199
|
+
};
|
|
200
|
+
} catch {
|
|
201
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
});
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
export const stage = v.union(v.literal("attach"), v.literal("configure"));
|
|
5
|
+
const sequence = v.union(v.literal(1), v.literal(2), v.literal(3));
|
|
6
|
+
const payloadOwner = v.union(
|
|
7
|
+
v.object({
|
|
8
|
+
kind: v.literal("operation"),
|
|
9
|
+
operationId: v.id("renewalOperations"),
|
|
10
|
+
}),
|
|
11
|
+
v.object({
|
|
12
|
+
kind: v.literal("event"),
|
|
13
|
+
operationId: v.id("renewalOperations"),
|
|
14
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
15
|
+
stage,
|
|
16
|
+
sequence,
|
|
17
|
+
}),
|
|
18
|
+
v.object({
|
|
19
|
+
kind: v.literal("receipt"),
|
|
20
|
+
operationId: v.id("renewalOperations"),
|
|
21
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
22
|
+
stage,
|
|
23
|
+
}),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/** Immutable creation, first reservation and bounded append-only stage DATA. */
|
|
27
|
+
export const payloadsTable = defineTable({
|
|
28
|
+
codecVersion: v.literal(1),
|
|
29
|
+
json: v.string(),
|
|
30
|
+
// The operation link is filled before the creation transaction commits.
|
|
31
|
+
owner: v.optional(payloadOwner),
|
|
32
|
+
});
|
|
33
|
+
const knownAcknowledgementFields = {
|
|
34
|
+
schemaVersion: v.literal(1),
|
|
35
|
+
stage: v.literal("attach"),
|
|
36
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
37
|
+
sourcePayloadId: v.id("renewalOperationPayloads"),
|
|
38
|
+
dispatchEventId: v.id("renewalOperationEvents"),
|
|
39
|
+
dispatchPayloadId: v.id("renewalOperationPayloads"),
|
|
40
|
+
responseEventId: v.id("renewalOperationEvents"),
|
|
41
|
+
responsePayloadId: v.id("renewalOperationPayloads"),
|
|
42
|
+
receiptPayloadId: v.id("renewalOperationPayloads"),
|
|
43
|
+
receiptBinding: v.string(),
|
|
44
|
+
};
|
|
45
|
+
export const knownAcknowledgement = v.object(knownAcknowledgementFields);
|
|
46
|
+
export const retainAcknowledgementArgs = {
|
|
47
|
+
operationId: v.id("renewalOperations"),
|
|
48
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
49
|
+
expectedSequence: v.union(v.literal(2), v.literal(3)),
|
|
50
|
+
receiptJSON: v.string(),
|
|
51
|
+
};
|
|
52
|
+
export const retentionReceipt = v.object({
|
|
53
|
+
status: v.literal("retained"),
|
|
54
|
+
operationId: v.id("renewalOperations"),
|
|
55
|
+
...knownAcknowledgementFields,
|
|
56
|
+
});
|
|
57
|
+
/** One complete retained receipt, without duplicating source or history DATA. */
|
|
58
|
+
export const providerAcknowledgementReadResult = v.union(
|
|
59
|
+
v.null(),
|
|
60
|
+
v.object({ retention: retentionReceipt, receiptJSON: v.string() }),
|
|
61
|
+
);
|
|
62
|
+
/** Closed unsigned basis DATA; not a clock, Origin or Owner capability. */
|
|
63
|
+
export const initialBasisDescriptor = v.object({
|
|
64
|
+
version: v.literal(1),
|
|
65
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
66
|
+
sourcePayloadId: v.id("renewalOperationPayloads"),
|
|
67
|
+
stage: v.literal("attach"),
|
|
68
|
+
policyJSON: v.string(),
|
|
69
|
+
basisJSON: v.string(),
|
|
70
|
+
});
|
|
71
|
+
export const operationsTable = defineTable({
|
|
72
|
+
recordVersion: v.optional(v.literal(2)),
|
|
73
|
+
initialBasis: v.optional(initialBasisDescriptor),
|
|
74
|
+
authorizationScope: v.string(),
|
|
75
|
+
authorizationId: v.string(),
|
|
76
|
+
authorizationRevision: v.number(),
|
|
77
|
+
logicalOperationId: v.string(),
|
|
78
|
+
requestBinding: v.string(),
|
|
79
|
+
payloadId: v.id("renewalOperationPayloads"),
|
|
80
|
+
revision: v.union(v.literal(0), v.literal(1)),
|
|
81
|
+
state: v.union(v.literal("PREPARED"), v.literal("RESERVED")),
|
|
82
|
+
activeAttemptId: v.optional(v.id("renewalOperationAttempts")),
|
|
83
|
+
knownAcknowledgement: v.optional(knownAcknowledgement),
|
|
84
|
+
}).index("by_authorizationScope_and_logicalOperationId", [
|
|
85
|
+
"authorizationScope",
|
|
86
|
+
"logicalOperationId",
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
export const creationArgs = {
|
|
90
|
+
authorizationScope: v.string(),
|
|
91
|
+
logicalOperationId: v.string(),
|
|
92
|
+
authorizationRevision: v.number(),
|
|
93
|
+
recordJSON: v.string(),
|
|
94
|
+
};
|
|
95
|
+
export const creationResult = v.union(
|
|
96
|
+
v.object({
|
|
97
|
+
status: v.literal("created"),
|
|
98
|
+
operationId: v.id("renewalOperations"),
|
|
99
|
+
}),
|
|
100
|
+
v.object({
|
|
101
|
+
status: v.literal("existing"),
|
|
102
|
+
operationId: v.id("renewalOperations"),
|
|
103
|
+
}),
|
|
104
|
+
v.object({
|
|
105
|
+
status: v.literal("conflict"),
|
|
106
|
+
reason: v.literal("identity_changed"),
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
109
|
+
const attemptFields = {
|
|
110
|
+
operationId: v.id("renewalOperations"),
|
|
111
|
+
payloadId: v.id("renewalOperationPayloads"),
|
|
112
|
+
authorizationScope: v.string(),
|
|
113
|
+
authorizationId: v.string(),
|
|
114
|
+
authorizationRevision: v.number(),
|
|
115
|
+
logicalOperationId: v.string(),
|
|
116
|
+
requestBinding: v.string(),
|
|
117
|
+
key: v.string(),
|
|
118
|
+
apiVersion: v.string(),
|
|
119
|
+
accountJSON: v.string(),
|
|
120
|
+
firstDispatchedAt: v.number(),
|
|
121
|
+
ordinal: v.literal(1),
|
|
122
|
+
preparedRevision: v.literal(0),
|
|
123
|
+
committedRevision: v.literal(1),
|
|
124
|
+
};
|
|
125
|
+
const legacyAttemptFields = {
|
|
126
|
+
...attemptFields,
|
|
127
|
+
stage,
|
|
128
|
+
basisJSON: v.string(),
|
|
129
|
+
};
|
|
130
|
+
const operationalAttemptFields = {
|
|
131
|
+
...attemptFields,
|
|
132
|
+
recordVersion: v.literal(2),
|
|
133
|
+
stage: v.literal("attach"),
|
|
134
|
+
timingPolicyJSON: v.string(),
|
|
135
|
+
};
|
|
136
|
+
export const attemptsTable = defineTable(
|
|
137
|
+
v.union(
|
|
138
|
+
v.object({
|
|
139
|
+
...legacyAttemptFields,
|
|
140
|
+
lastSequence: v.union(v.literal(0), sequence),
|
|
141
|
+
}),
|
|
142
|
+
v.object({
|
|
143
|
+
...operationalAttemptFields,
|
|
144
|
+
lastSequence: v.union(v.literal(0), sequence),
|
|
145
|
+
}),
|
|
146
|
+
),
|
|
147
|
+
).index("by_operationId", ["operationId"]);
|
|
148
|
+
export const reservationData = v.union(
|
|
149
|
+
v.object({
|
|
150
|
+
...legacyAttemptFields,
|
|
151
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
152
|
+
recordJSON: v.string(),
|
|
153
|
+
}),
|
|
154
|
+
v.object({
|
|
155
|
+
...operationalAttemptFields,
|
|
156
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
157
|
+
recordJSON: v.string(),
|
|
158
|
+
}),
|
|
159
|
+
);
|
|
160
|
+
// Loads return the complete record once, alongside checked attempt DATA.
|
|
161
|
+
const legacyReservationSummary = v.object({
|
|
162
|
+
...legacyAttemptFields,
|
|
163
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
164
|
+
});
|
|
165
|
+
const operationalReservationSummary = v.object({
|
|
166
|
+
...operationalAttemptFields,
|
|
167
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
168
|
+
});
|
|
169
|
+
export const reservationSummary = v.union(
|
|
170
|
+
legacyReservationSummary,
|
|
171
|
+
operationalReservationSummary,
|
|
172
|
+
);
|
|
173
|
+
export const reserveArgs = {
|
|
174
|
+
operationId: v.id("renewalOperations"),
|
|
175
|
+
expectedRevision: v.number(),
|
|
176
|
+
stage,
|
|
177
|
+
expectedRequestBinding: v.string(),
|
|
178
|
+
recordJSON: v.string(),
|
|
179
|
+
};
|
|
180
|
+
export const reserveResult = v.union(
|
|
181
|
+
v.object({
|
|
182
|
+
disposition: v.literal("acquired"),
|
|
183
|
+
previousRevision: v.literal(0),
|
|
184
|
+
committedRevision: v.literal(1),
|
|
185
|
+
reservation: reservationData,
|
|
186
|
+
}),
|
|
187
|
+
v.object({
|
|
188
|
+
disposition: v.literal("existing"),
|
|
189
|
+
committedRevision: v.literal(1),
|
|
190
|
+
reservation: reservationData,
|
|
191
|
+
}),
|
|
192
|
+
v.object({
|
|
193
|
+
disposition: v.literal("conflict"),
|
|
194
|
+
reason: v.literal("identity_changed"),
|
|
195
|
+
}),
|
|
196
|
+
);
|
|
197
|
+
export const loadedResult = v.union(
|
|
198
|
+
v.null(),
|
|
199
|
+
v.object({
|
|
200
|
+
recordVersion: v.literal(2),
|
|
201
|
+
operationId: v.id("renewalOperations"),
|
|
202
|
+
recordJSON: v.string(),
|
|
203
|
+
revision: v.literal(0),
|
|
204
|
+
activeAttempt: v.null(),
|
|
205
|
+
initialBasis: v.null(),
|
|
206
|
+
}),
|
|
207
|
+
v.object({
|
|
208
|
+
recordVersion: v.literal(2),
|
|
209
|
+
operationId: v.id("renewalOperations"),
|
|
210
|
+
recordJSON: v.string(),
|
|
211
|
+
revision: v.literal(1),
|
|
212
|
+
activeAttempt: operationalReservationSummary,
|
|
213
|
+
initialBasis: v.union(v.null(), initialBasisDescriptor),
|
|
214
|
+
}),
|
|
215
|
+
v.object({
|
|
216
|
+
operationId: v.id("renewalOperations"),
|
|
217
|
+
recordJSON: v.string(),
|
|
218
|
+
revision: v.union(v.literal(0), v.literal(1)),
|
|
219
|
+
activeAttempt: v.union(v.null(), legacyReservationSummary),
|
|
220
|
+
}),
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
const eventFields = {
|
|
224
|
+
operationId: v.id("renewalOperations"),
|
|
225
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
226
|
+
stage,
|
|
227
|
+
sourcePayloadId: v.id("renewalOperationPayloads"),
|
|
228
|
+
sequence,
|
|
229
|
+
binding: v.string(),
|
|
230
|
+
payloadId: v.id("renewalOperationPayloads"),
|
|
231
|
+
};
|
|
232
|
+
export const eventsTable = defineTable(eventFields).index(
|
|
233
|
+
"by_attemptId_and_sequence",
|
|
234
|
+
["attemptId", "sequence"],
|
|
235
|
+
);
|
|
236
|
+
export const appendArgs = {
|
|
237
|
+
reservation: reservationData,
|
|
238
|
+
expectedSequence: v.number(),
|
|
239
|
+
event: v.object({ sequence, binding: v.string(), dataJSON: v.string() }),
|
|
240
|
+
};
|
|
241
|
+
export const eventReceipt = v.object({
|
|
242
|
+
...eventFields,
|
|
243
|
+
eventId: v.id("renewalOperationEvents"),
|
|
244
|
+
reservation: reservationData,
|
|
245
|
+
});
|
|
246
|
+
export const eventDescriptor = v.object({
|
|
247
|
+
...eventFields,
|
|
248
|
+
eventId: v.id("renewalOperationEvents"),
|
|
249
|
+
});
|
|
250
|
+
export const attemptResult = v.union(
|
|
251
|
+
v.null(),
|
|
252
|
+
v.object({
|
|
253
|
+
reservation: reservationData,
|
|
254
|
+
events: v.array(eventDescriptor),
|
|
255
|
+
}),
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
export const bindInitialBasisArgs = {
|
|
259
|
+
operationId: v.id("renewalOperations"),
|
|
260
|
+
attemptId: v.id("renewalOperationAttempts"),
|
|
261
|
+
policyJSON: v.string(),
|
|
262
|
+
basisJSON: v.string(),
|
|
263
|
+
};
|
|
264
|
+
export const basisBindingReceipt = v.object({
|
|
265
|
+
status: v.literal("bound"),
|
|
266
|
+
operationId: v.id("renewalOperations"),
|
|
267
|
+
initialBasis: initialBasisDescriptor,
|
|
268
|
+
});
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import { makeFunctionReference } from "convex/server";
|
|
3
|
+
import { getConvexSize } from "convex/values";
|
|
4
|
+
import { expect, vi } from "vitest";
|
|
5
|
+
import { modules, schema } from "../../test.js";
|
|
6
|
+
import { canonicalJournalRecord } from "../../shared/renewalOperationJournal.js";
|
|
7
|
+
import { renewalSnapshot } from "../../client/billing/renewalReplacementSnapshot.js";
|
|
8
|
+
import {
|
|
9
|
+
prepareRenewalPriceReplacementV2,
|
|
10
|
+
prepareRenewalScheduleAttachmentV2,
|
|
11
|
+
} from "../../client/billing/renewalV2/index.js";
|
|
12
|
+
import { receiptInput } from "../../client/billing/renewalAttach/internal/acquisition/test-fixtures/fixture.mjs";
|
|
13
|
+
|
|
14
|
+
export const canonical = (value: unknown) =>
|
|
15
|
+
JSON.stringify(renewalSnapshot(value));
|
|
16
|
+
|
|
17
|
+
export async function codecFixture() {
|
|
18
|
+
let fetchCalls = 0;
|
|
19
|
+
vi.stubGlobal("fetch", () => {
|
|
20
|
+
fetchCalls++;
|
|
21
|
+
throw Error("UNEXPECTED_EXTERNAL_FETCH");
|
|
22
|
+
});
|
|
23
|
+
// Complete synthetic raw recipe, never a provider/profile/Owner qualification.
|
|
24
|
+
// All source and policy choices precede public constructor calls.
|
|
25
|
+
const raw = receiptInput();
|
|
26
|
+
const rawJSON = canonical(raw.attachRequest);
|
|
27
|
+
const timingPolicy = {
|
|
28
|
+
version: 1,
|
|
29
|
+
firstDispatchedAt: 1700000001,
|
|
30
|
+
periodStart: 1700000000,
|
|
31
|
+
periodEnd: 1702592000,
|
|
32
|
+
workEndWallMs: 1700000005000,
|
|
33
|
+
endWallMs: 1700000006000,
|
|
34
|
+
};
|
|
35
|
+
const originalBundle = {
|
|
36
|
+
original: raw.attachRequest.preparation,
|
|
37
|
+
acquisition: { couponRead: null, futureRaw: ["retained", null, 1.25] },
|
|
38
|
+
};
|
|
39
|
+
try {
|
|
40
|
+
const prepared = await prepareRenewalPriceReplacementV2(
|
|
41
|
+
raw.attachRequest.preparation,
|
|
42
|
+
);
|
|
43
|
+
const request = await prepareRenewalScheduleAttachmentV2({
|
|
44
|
+
prepared,
|
|
45
|
+
apiVersion: raw.attachRequest.apiVersion,
|
|
46
|
+
attachKey: raw.attachRequest.attachKey,
|
|
47
|
+
firstDispatchedAt: raw.attachRequest.firstDispatchedAt,
|
|
48
|
+
});
|
|
49
|
+
expect(raw.actualDispatch).toEqual({
|
|
50
|
+
scope: request.scope,
|
|
51
|
+
apiVersion: request.apiVersion,
|
|
52
|
+
method: request.method,
|
|
53
|
+
path: request.path,
|
|
54
|
+
payload: request.payload,
|
|
55
|
+
key: request.key,
|
|
56
|
+
firstDispatchedAt: request.firstDispatchedAt,
|
|
57
|
+
});
|
|
58
|
+
expect(prepared.expected.periodStart).toBe(timingPolicy.periodStart);
|
|
59
|
+
expect(prepared.expected.periodEnd).toBe(timingPolicy.periodEnd);
|
|
60
|
+
expect(request.firstDispatchedAt).toBe(timingPolicy.firstDispatchedAt);
|
|
61
|
+
expect(canonical(raw.attachRequest)).toBe(rawJSON);
|
|
62
|
+
const record = {
|
|
63
|
+
schemaVersion: 2,
|
|
64
|
+
requestBinding: request.requestIdentity.binding,
|
|
65
|
+
authorization: {
|
|
66
|
+
scope: "codec01-host",
|
|
67
|
+
id: "codec01-consent",
|
|
68
|
+
revision: 1,
|
|
69
|
+
},
|
|
70
|
+
logicalOperationId: "codec01-original-operation",
|
|
71
|
+
stage: "attach",
|
|
72
|
+
account: { mode: "platform", stripeAccount: null, livemode: false },
|
|
73
|
+
apiVersion: raw.attachRequest.apiVersion,
|
|
74
|
+
profile: "unqualified-offline-codec-DATA",
|
|
75
|
+
attachRequest: raw.attachRequest,
|
|
76
|
+
dispatch: raw.actualDispatch,
|
|
77
|
+
originalBundle,
|
|
78
|
+
timingPolicy,
|
|
79
|
+
};
|
|
80
|
+
const recordJSON = canonical(record);
|
|
81
|
+
expect(recordJSON.length).toBeLessThanOrEqual(500000);
|
|
82
|
+
expect(new TextEncoder().encode(recordJSON).length).toBeLessThanOrEqual(
|
|
83
|
+
500000,
|
|
84
|
+
);
|
|
85
|
+
expect(Object.prototype.hasOwnProperty.call(record, "basis")).toBe(false);
|
|
86
|
+
expect(originalBundle).not.toEqual(raw.attachRequest.preparation);
|
|
87
|
+
// Existing actual synchronous export is the target; no absent API/type seam.
|
|
88
|
+
const normalized = canonicalJournalRecord(recordJSON);
|
|
89
|
+
expect(normalized).toEqual({
|
|
90
|
+
json: recordJSON,
|
|
91
|
+
authorizationScope: "codec01-host",
|
|
92
|
+
authorizationId: "codec01-consent",
|
|
93
|
+
authorizationRevision: 1,
|
|
94
|
+
logicalOperationId: "codec01-original-operation",
|
|
95
|
+
requestBinding: request.requestIdentity.binding,
|
|
96
|
+
recordVersion: 2,
|
|
97
|
+
});
|
|
98
|
+
const retained = JSON.parse(normalized.json);
|
|
99
|
+
expect(retained).toEqual(JSON.parse(recordJSON));
|
|
100
|
+
expect(Object.prototype.hasOwnProperty.call(retained, "basis")).toBe(false);
|
|
101
|
+
expect(retained.originalBundle.acquisition).toEqual(
|
|
102
|
+
originalBundle.acquisition,
|
|
103
|
+
);
|
|
104
|
+
expect(
|
|
105
|
+
Object.prototype.hasOwnProperty.call(
|
|
106
|
+
retained.originalBundle.acquisition,
|
|
107
|
+
"couponRead",
|
|
108
|
+
),
|
|
109
|
+
).toBe(true);
|
|
110
|
+
expect(canonical(raw.attachRequest)).toBe(rawJSON);
|
|
111
|
+
return {
|
|
112
|
+
record,
|
|
113
|
+
normalized,
|
|
114
|
+
recordJSON,
|
|
115
|
+
request,
|
|
116
|
+
fetchCalls: () => fetchCalls,
|
|
117
|
+
};
|
|
118
|
+
} finally {
|
|
119
|
+
expect(fetchCalls).toBe(0);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export const create = makeFunctionReference<"mutation">(
|
|
124
|
+
"renewalOperations/mutations:createAuthorizedOperation",
|
|
125
|
+
);
|
|
126
|
+
export const load = makeFunctionReference<"query">(
|
|
127
|
+
"renewalOperations/queries:loadAuthorizedOperation",
|
|
128
|
+
);
|
|
129
|
+
export const VALUE_LIMIT = 1048576;
|
|
130
|
+
|
|
131
|
+
export function componentFixture() {
|
|
132
|
+
const t = convexTest(schema, modules);
|
|
133
|
+
async function physical() {
|
|
134
|
+
const [operations, attempts, events, payloads] = await Promise.all([
|
|
135
|
+
t.run((ctx) => ctx.db.query("renewalOperations").take(3)),
|
|
136
|
+
t.run((ctx) => ctx.db.query("renewalOperationAttempts").take(3)),
|
|
137
|
+
t.run((ctx) => ctx.db.query("renewalOperationEvents").take(4)),
|
|
138
|
+
t.run((ctx) => ctx.db.query("renewalOperationPayloads").take(5)),
|
|
139
|
+
]);
|
|
140
|
+
return { operations, attempts, events, payloads };
|
|
141
|
+
}
|
|
142
|
+
return { t, physical };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export async function expectInvalidCreate(
|
|
146
|
+
h: Awaited<ReturnType<typeof codecFixture>>,
|
|
147
|
+
recordJSON: string,
|
|
148
|
+
label: string,
|
|
149
|
+
) {
|
|
150
|
+
const { t, physical } = componentFixture();
|
|
151
|
+
const args = {
|
|
152
|
+
authorizationScope: h.record.authorization.scope,
|
|
153
|
+
logicalOperationId: h.record.logicalOperationId,
|
|
154
|
+
authorizationRevision: h.record.authorization.revision,
|
|
155
|
+
recordJSON,
|
|
156
|
+
};
|
|
157
|
+
expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
|
|
158
|
+
const before = await physical();
|
|
159
|
+
expect(before).toEqual({
|
|
160
|
+
operations: [],
|
|
161
|
+
attempts: [],
|
|
162
|
+
events: [],
|
|
163
|
+
payloads: [],
|
|
164
|
+
});
|
|
165
|
+
let targetFailed = false;
|
|
166
|
+
try {
|
|
167
|
+
await expect(t.mutation(create, args)).rejects.toThrow(
|
|
168
|
+
/^RENEWAL_JOURNAL_CONFLICT$/,
|
|
169
|
+
);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
targetFailed = true;
|
|
172
|
+
throw error;
|
|
173
|
+
} finally {
|
|
174
|
+
try {
|
|
175
|
+
const after = await physical();
|
|
176
|
+
console.info(
|
|
177
|
+
`${label}_SETTLEMENT`,
|
|
178
|
+
JSON.stringify({
|
|
179
|
+
physicalEqual: JSON.stringify(after) === JSON.stringify(before),
|
|
180
|
+
rows: Object.fromEntries(
|
|
181
|
+
Object.entries(after).map(([key, rows]) => [key, rows.length]),
|
|
182
|
+
),
|
|
183
|
+
fetchCalls: h.fetchCalls(),
|
|
184
|
+
}),
|
|
185
|
+
);
|
|
186
|
+
expect(after).toEqual(before);
|
|
187
|
+
expect(h.fetchCalls()).toBe(0);
|
|
188
|
+
} catch (inspectionError) {
|
|
189
|
+
if (!targetFailed) throw inspectionError;
|
|
190
|
+
console.info(`${label}_READONLY_INSPECTION_FAILED`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|