@agentbadge/database 0.6.1 → 0.6.3

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.
@@ -0,0 +1,291 @@
1
+ import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
2
+ export interface PaymentInsert {
3
+ id: string;
4
+ reference: string;
5
+ idempotencyKey?: string | null;
6
+ customerId?: string | null;
7
+ agentId?: string | null;
8
+ productId?: string | null;
9
+ orderId?: string | null;
10
+ amount: string;
11
+ currency?: string;
12
+ status: string;
13
+ purpose: string;
14
+ expiresAtMs?: number | null;
15
+ createdAtMs: number;
16
+ updatedAtMs: number;
17
+ payload: JsonValue;
18
+ }
19
+ export interface PaymentRow extends PaymentInsert {
20
+ createdAt: string;
21
+ updatedAt: string;
22
+ }
23
+ export interface PaymentStore {
24
+ create(p: PaymentInsert): Promise<PaymentRow | null>;
25
+ getById(id: string): Promise<PaymentRow | null>;
26
+ getByReference(reference: string): Promise<PaymentRow | null>;
27
+ getByIdempotencyKey(key: string): Promise<PaymentRow | null>;
28
+ listByCustomer(customerId: string): Promise<PaymentRow[]>;
29
+ applyStatus(id: string, expectedFrom: string, to: string, updatedAtMs: number): Promise<PaymentRow | null>;
30
+ listExpired(nowMs: number): Promise<PaymentRow[]>;
31
+ }
32
+ export interface PaymentAttemptInsert {
33
+ id: string;
34
+ paymentId: string;
35
+ provider: string;
36
+ network?: string | null;
37
+ token?: string | null;
38
+ amount: string;
39
+ currency: string;
40
+ status: string;
41
+ providerReference?: string | null;
42
+ transactionHash?: string | null;
43
+ expiresAtMs?: number | null;
44
+ confirmedAtMs?: number | null;
45
+ payload: JsonValue;
46
+ }
47
+ export interface PaymentAttemptRow extends PaymentAttemptInsert {
48
+ createdAt: string;
49
+ updatedAt: string;
50
+ }
51
+ export declare const TERMINAL_ATTEMPT_STATUSES: readonly string[];
52
+ export interface AttemptStatusPatch {
53
+ confirmedAtMs?: number;
54
+ transactionHash?: string | null;
55
+ providerReference?: string | null;
56
+ }
57
+ export interface PaymentAttemptStore {
58
+ create(a: PaymentAttemptInsert): Promise<PaymentAttemptRow | null>;
59
+ getById(id: string): Promise<PaymentAttemptRow | null>;
60
+ listByPayment(paymentId: string): Promise<PaymentAttemptRow[]>;
61
+ getByProviderRef(provider: string, providerReference: string): Promise<PaymentAttemptRow | null>;
62
+ getByTxHash(transactionHash: string): Promise<PaymentAttemptRow | null>;
63
+ applyStatus(id: string, expectedFrom: string, to: string, updatedAtMs: number, patch?: AttemptStatusPatch): Promise<PaymentAttemptRow | null>;
64
+ listExpired(nowMs: number): Promise<PaymentAttemptRow[]>;
65
+ listPayable(nowMs: number): Promise<PaymentAttemptRow[]>;
66
+ listRecentTerminal(sinceMs: number): Promise<PaymentAttemptRow[]>;
67
+ }
68
+ export interface PaymentReceiptInsert {
69
+ id: string;
70
+ paymentId: string;
71
+ attemptId: string;
72
+ provider: string;
73
+ externalReference: string;
74
+ network?: string | null;
75
+ transactionHash?: string | null;
76
+ payer?: string | null;
77
+ recipient?: string | null;
78
+ amount: string;
79
+ asset: string;
80
+ verifiedAtMs: number;
81
+ payload: JsonValue;
82
+ }
83
+ export interface PaymentReceiptRow extends PaymentReceiptInsert {
84
+ createdAt: string;
85
+ }
86
+ export interface PaymentReceiptStore {
87
+ append(r: PaymentReceiptInsert): Promise<PaymentReceiptRow | null>;
88
+ getById(id: string): Promise<PaymentReceiptRow | null>;
89
+ listByPayment(paymentId: string): Promise<PaymentReceiptRow[]>;
90
+ getByExternalRef(provider: string, externalReference: string): Promise<PaymentReceiptRow | null>;
91
+ }
92
+ export interface PaymentEventInsert {
93
+ id: string;
94
+ entity: string;
95
+ paymentId: string;
96
+ attemptId?: string | null;
97
+ from: string;
98
+ to: string;
99
+ actor: string;
100
+ reason?: string | null;
101
+ atMs: number;
102
+ payload: JsonValue;
103
+ }
104
+ export interface PaymentEventRow extends PaymentEventInsert {
105
+ createdAt: string;
106
+ }
107
+ export interface PaymentEventStore {
108
+ append(e: PaymentEventInsert): Promise<PaymentEventRow | null>;
109
+ listByPayment(paymentId: string): Promise<PaymentEventRow[]>;
110
+ listByAttempt(attemptId: string): Promise<PaymentEventRow[]>;
111
+ }
112
+ export interface PaymentProductInsert {
113
+ id: string;
114
+ code: string;
115
+ name: string;
116
+ description?: string | null;
117
+ category: string;
118
+ active?: boolean;
119
+ payload: JsonValue;
120
+ }
121
+ export interface PaymentProductRow extends PaymentProductInsert {
122
+ createdAt: string;
123
+ updatedAt: string;
124
+ }
125
+ export interface PaymentProductStore {
126
+ upsert(p: PaymentProductInsert): Promise<PaymentProductRow>;
127
+ getById(id: string): Promise<PaymentProductRow | null>;
128
+ getByCode(code: string): Promise<PaymentProductRow | null>;
129
+ list(opts?: {
130
+ activeOnly?: boolean;
131
+ }): Promise<PaymentProductRow[]>;
132
+ }
133
+ export interface PaymentPriceInsert {
134
+ id: string;
135
+ productId: string;
136
+ amount: string;
137
+ currency?: string;
138
+ billingType: string;
139
+ interval?: string | null;
140
+ active?: boolean;
141
+ payload: JsonValue;
142
+ }
143
+ export interface PaymentPriceRow extends PaymentPriceInsert {
144
+ createdAt: string;
145
+ updatedAt: string;
146
+ }
147
+ export interface PaymentPriceStore {
148
+ create(p: PaymentPriceInsert): Promise<PaymentPriceRow | null>;
149
+ getById(id: string): Promise<PaymentPriceRow | null>;
150
+ listByProduct(productId: string, opts?: {
151
+ activeOnly?: boolean;
152
+ }): Promise<PaymentPriceRow[]>;
153
+ }
154
+ export interface PaymentExceptionInsert {
155
+ id: string;
156
+ paymentId?: string;
157
+ attemptId?: string;
158
+ kind: string;
159
+ severity: string;
160
+ details?: Record<string, unknown>;
161
+ status: string;
162
+ createdAtMs: number;
163
+ }
164
+ export interface PaymentExceptionRow extends PaymentExceptionInsert {
165
+ resolvedAtMs?: number | null;
166
+ resolutionNote?: string | null;
167
+ createdAt?: string;
168
+ }
169
+ export interface PaymentExceptionStore {
170
+ append(e: PaymentExceptionInsert): Promise<PaymentExceptionRow | null>;
171
+ getById(id: string): Promise<PaymentExceptionRow | null>;
172
+ listByPayment(paymentId: string): Promise<PaymentExceptionRow[]>;
173
+ list(opts?: {
174
+ status?: string;
175
+ kind?: string;
176
+ sinceMs?: number;
177
+ limit?: number;
178
+ }): Promise<PaymentExceptionRow[]>;
179
+ resolve(id: string, status: "RESOLVED" | "IGNORED", note?: string): Promise<PaymentExceptionRow | null>;
180
+ }
181
+ export interface UsageRecordInsert {
182
+ id: string;
183
+ paymentId: string;
184
+ agentId?: string | null;
185
+ resource: string;
186
+ requestId: string;
187
+ amount: string;
188
+ currency?: string;
189
+ network: string;
190
+ payerAddress: string;
191
+ txHash?: string | null;
192
+ createdAtMs: number;
193
+ payload: JsonValue;
194
+ }
195
+ export interface UsageRecordRow extends UsageRecordInsert {
196
+ createdAt?: string;
197
+ }
198
+ export interface UsageRecordStore {
199
+ append(r: UsageRecordInsert): Promise<UsageRecordRow | null>;
200
+ getById(id: string): Promise<UsageRecordRow | null>;
201
+ getByRequestId(requestId: string): Promise<UsageRecordRow | null>;
202
+ listByPayment(paymentId: string): Promise<UsageRecordRow[]>;
203
+ listByAgent(agentId: string, limit?: number): Promise<UsageRecordRow[]>;
204
+ listByResource(resource: string, limit?: number): Promise<UsageRecordRow[]>;
205
+ }
206
+ export interface PaymentStores {
207
+ payments: PaymentStore;
208
+ attempts: PaymentAttemptStore;
209
+ receipts: PaymentReceiptStore;
210
+ events: PaymentEventStore;
211
+ exceptions: PaymentExceptionStore;
212
+ products: PaymentProductStore;
213
+ prices: PaymentPriceStore;
214
+ usageRecords: UsageRecordStore;
215
+ }
216
+ export declare class InMemoryPaymentStore implements PaymentStore {
217
+ private readonly rows;
218
+ create(p: PaymentInsert): Promise<PaymentRow | null>;
219
+ getById(id: string): Promise<PaymentRow | null>;
220
+ getByReference(reference: string): Promise<PaymentRow | null>;
221
+ getByIdempotencyKey(key: string): Promise<PaymentRow | null>;
222
+ listByCustomer(customerId: string): Promise<PaymentRow[]>;
223
+ applyStatus(id: string, expectedFrom: string, to: string, updatedAtMs: number): Promise<PaymentRow | null>;
224
+ listExpired(nowMs: number): Promise<PaymentRow[]>;
225
+ }
226
+ export declare class InMemoryPaymentAttemptStore implements PaymentAttemptStore {
227
+ private readonly rows;
228
+ create(a: PaymentAttemptInsert): Promise<PaymentAttemptRow | null>;
229
+ getById(id: string): Promise<PaymentAttemptRow | null>;
230
+ listByPayment(paymentId: string): Promise<PaymentAttemptRow[]>;
231
+ getByProviderRef(provider: string, providerReference: string): Promise<PaymentAttemptRow | null>;
232
+ getByTxHash(transactionHash: string): Promise<PaymentAttemptRow | null>;
233
+ applyStatus(id: string, expectedFrom: string, to: string, updatedAtMs: number, patch?: AttemptStatusPatch): Promise<PaymentAttemptRow | null>;
234
+ listExpired(nowMs: number): Promise<PaymentAttemptRow[]>;
235
+ listPayable(nowMs: number): Promise<PaymentAttemptRow[]>;
236
+ listRecentTerminal(sinceMs: number): Promise<PaymentAttemptRow[]>;
237
+ private readonly terminalStampMs;
238
+ }
239
+ export declare class InMemoryPaymentReceiptStore implements PaymentReceiptStore {
240
+ private readonly rows;
241
+ append(r: PaymentReceiptInsert): Promise<PaymentReceiptRow | null>;
242
+ getById(id: string): Promise<PaymentReceiptRow | null>;
243
+ listByPayment(paymentId: string): Promise<PaymentReceiptRow[]>;
244
+ getByExternalRef(provider: string, externalReference: string): Promise<PaymentReceiptRow | null>;
245
+ }
246
+ export declare class InMemoryPaymentEventStore implements PaymentEventStore {
247
+ private readonly rows;
248
+ append(e: PaymentEventInsert): Promise<PaymentEventRow | null>;
249
+ listByPayment(paymentId: string): Promise<PaymentEventRow[]>;
250
+ listByAttempt(attemptId: string): Promise<PaymentEventRow[]>;
251
+ }
252
+ export declare class InMemoryPaymentProductStore implements PaymentProductStore {
253
+ private readonly rows;
254
+ upsert(p: PaymentProductInsert): Promise<PaymentProductRow>;
255
+ getById(id: string): Promise<PaymentProductRow | null>;
256
+ getByCode(code: string): Promise<PaymentProductRow | null>;
257
+ list(opts?: {
258
+ activeOnly?: boolean;
259
+ }): Promise<PaymentProductRow[]>;
260
+ }
261
+ export declare class InMemoryPaymentPriceStore implements PaymentPriceStore {
262
+ private readonly rows;
263
+ create(p: PaymentPriceInsert): Promise<PaymentPriceRow | null>;
264
+ getById(id: string): Promise<PaymentPriceRow | null>;
265
+ listByProduct(productId: string, opts?: {
266
+ activeOnly?: boolean;
267
+ }): Promise<PaymentPriceRow[]>;
268
+ }
269
+ export declare class InMemoryPaymentExceptionStore implements PaymentExceptionStore {
270
+ private readonly rows;
271
+ append(e: PaymentExceptionInsert): Promise<PaymentExceptionRow | null>;
272
+ getById(id: string): Promise<PaymentExceptionRow | null>;
273
+ listByPayment(paymentId: string): Promise<PaymentExceptionRow[]>;
274
+ list(opts?: {
275
+ status?: string;
276
+ kind?: string;
277
+ sinceMs?: number;
278
+ limit?: number;
279
+ }): Promise<PaymentExceptionRow[]>;
280
+ resolve(id: string, status: "RESOLVED" | "IGNORED", note?: string): Promise<PaymentExceptionRow | null>;
281
+ }
282
+ export declare class InMemoryUsageRecordStore implements UsageRecordStore {
283
+ private readonly rows;
284
+ append(r: UsageRecordInsert): Promise<UsageRecordRow | null>;
285
+ getById(id: string): Promise<UsageRecordRow | null>;
286
+ getByRequestId(requestId: string): Promise<UsageRecordRow | null>;
287
+ listByPayment(paymentId: string): Promise<UsageRecordRow[]>;
288
+ listByAgent(agentId: string, limit?: number): Promise<UsageRecordRow[]>;
289
+ listByResource(resource: string, limit?: number): Promise<UsageRecordRow[]>;
290
+ }
291
+ export declare function createInMemoryPaymentStores(): PaymentStores;
@@ -0,0 +1,374 @@
1
+ export const TERMINAL_ATTEMPT_STATUSES = [
2
+ "CONFIRMED",
3
+ "FAILED",
4
+ "EXPIRED",
5
+ ];
6
+ const nowIso = () => new Date().toISOString();
7
+ export class InMemoryPaymentStore {
8
+ rows = new Map();
9
+ async create(p) {
10
+ if (this.rows.has(p.id))
11
+ return null;
12
+ if (await this.getByReference(p.reference))
13
+ return null;
14
+ if (p.idempotencyKey != null &&
15
+ (await this.getByIdempotencyKey(p.idempotencyKey))) {
16
+ return null;
17
+ }
18
+ const ts = nowIso();
19
+ const row = {
20
+ ...p,
21
+ idempotencyKey: p.idempotencyKey ?? null,
22
+ customerId: p.customerId ?? null,
23
+ agentId: p.agentId ?? null,
24
+ productId: p.productId ?? null,
25
+ orderId: p.orderId ?? null,
26
+ currency: p.currency ?? "USD",
27
+ expiresAtMs: p.expiresAtMs ?? null,
28
+ createdAt: ts,
29
+ updatedAt: ts,
30
+ };
31
+ this.rows.set(row.id, row);
32
+ return row;
33
+ }
34
+ async getById(id) {
35
+ return this.rows.get(id) ?? null;
36
+ }
37
+ async getByReference(reference) {
38
+ return ([...this.rows.values()].find((r) => r.reference === reference) ?? null);
39
+ }
40
+ async getByIdempotencyKey(key) {
41
+ return ([...this.rows.values()].find((r) => r.idempotencyKey === key) ?? null);
42
+ }
43
+ async listByCustomer(customerId) {
44
+ return [...this.rows.values()]
45
+ .filter((r) => r.customerId === customerId)
46
+ .sort((a, b) => b.createdAtMs - a.createdAtMs);
47
+ }
48
+ async applyStatus(id, expectedFrom, to, updatedAtMs) {
49
+ const row = this.rows.get(id);
50
+ if (!row || row.status !== expectedFrom)
51
+ return null;
52
+ const updated = {
53
+ ...row,
54
+ status: to,
55
+ updatedAtMs,
56
+ updatedAt: nowIso(),
57
+ };
58
+ this.rows.set(id, updated);
59
+ return updated;
60
+ }
61
+ async listExpired(nowMs) {
62
+ return [...this.rows.values()].filter((r) => r.expiresAtMs != null &&
63
+ r.expiresAtMs < nowMs &&
64
+ (r.status === "PENDING" || r.status === "PROCESSING"));
65
+ }
66
+ }
67
+ export class InMemoryPaymentAttemptStore {
68
+ rows = new Map();
69
+ async create(a) {
70
+ if (this.rows.has(a.id))
71
+ return null;
72
+ if (a.providerReference != null &&
73
+ (await this.getByProviderRef(a.provider, a.providerReference))) {
74
+ return null;
75
+ }
76
+ if (a.transactionHash != null) {
77
+ const dup = [...this.rows.values()].find((r) => r.provider === a.provider &&
78
+ r.transactionHash === a.transactionHash);
79
+ if (dup)
80
+ return null;
81
+ }
82
+ const ts = nowIso();
83
+ const row = {
84
+ ...a,
85
+ network: a.network ?? null,
86
+ token: a.token ?? null,
87
+ providerReference: a.providerReference ?? null,
88
+ transactionHash: a.transactionHash ?? null,
89
+ expiresAtMs: a.expiresAtMs ?? null,
90
+ confirmedAtMs: a.confirmedAtMs ?? null,
91
+ createdAt: ts,
92
+ updatedAt: ts,
93
+ };
94
+ this.rows.set(row.id, row);
95
+ return row;
96
+ }
97
+ async getById(id) {
98
+ return this.rows.get(id) ?? null;
99
+ }
100
+ async listByPayment(paymentId) {
101
+ return [...this.rows.values()].filter((r) => r.paymentId === paymentId);
102
+ }
103
+ async getByProviderRef(provider, providerReference) {
104
+ return ([...this.rows.values()].find((r) => r.provider === provider &&
105
+ r.providerReference === providerReference) ?? null);
106
+ }
107
+ async getByTxHash(transactionHash) {
108
+ return ([...this.rows.values()].find((r) => r.transactionHash === transactionHash) ?? null);
109
+ }
110
+ async applyStatus(id, expectedFrom, to, updatedAtMs, patch) {
111
+ const row = this.rows.get(id);
112
+ if (!row || row.status !== expectedFrom)
113
+ return null;
114
+ const updated = {
115
+ ...row,
116
+ status: to,
117
+ ...(patch?.confirmedAtMs !== undefined
118
+ ? { confirmedAtMs: patch.confirmedAtMs }
119
+ : {}),
120
+ ...(patch?.transactionHash !== undefined
121
+ ? { transactionHash: patch.transactionHash }
122
+ : {}),
123
+ ...(patch?.providerReference !== undefined
124
+ ? { providerReference: patch.providerReference }
125
+ : {}),
126
+ updatedAt: nowIso(),
127
+ };
128
+ this.rows.set(id, updated);
129
+ if (TERMINAL_ATTEMPT_STATUSES.includes(to)) {
130
+ this.terminalStampMs.set(id, updatedAtMs);
131
+ }
132
+ return updated;
133
+ }
134
+ async listExpired(nowMs) {
135
+ return [...this.rows.values()].filter((r) => r.expiresAtMs != null &&
136
+ r.expiresAtMs < nowMs &&
137
+ !TERMINAL_ATTEMPT_STATUSES.includes(r.status));
138
+ }
139
+ async listPayable(nowMs) {
140
+ return [...this.rows.values()].filter((r) => !TERMINAL_ATTEMPT_STATUSES.includes(r.status) &&
141
+ (r.expiresAtMs == null || r.expiresAtMs > nowMs));
142
+ }
143
+ async listRecentTerminal(sinceMs) {
144
+ return [...this.rows.values()].filter((r) => {
145
+ if (!TERMINAL_ATTEMPT_STATUSES.includes(r.status))
146
+ return false;
147
+ const stamp = this.terminalStampMs.get(r.id) ?? r.confirmedAtMs;
148
+ return stamp == null || stamp >= sinceMs;
149
+ });
150
+ }
151
+ terminalStampMs = new Map();
152
+ }
153
+ export class InMemoryPaymentReceiptStore {
154
+ rows = new Map();
155
+ async append(r) {
156
+ if (this.rows.has(r.id))
157
+ return null;
158
+ if (await this.getByExternalRef(r.provider, r.externalReference)) {
159
+ return null;
160
+ }
161
+ const row = {
162
+ ...r,
163
+ network: r.network ?? null,
164
+ transactionHash: r.transactionHash ?? null,
165
+ payer: r.payer ?? null,
166
+ recipient: r.recipient ?? null,
167
+ createdAt: nowIso(),
168
+ };
169
+ this.rows.set(row.id, row);
170
+ return row;
171
+ }
172
+ async getById(id) {
173
+ return this.rows.get(id) ?? null;
174
+ }
175
+ async listByPayment(paymentId) {
176
+ return [...this.rows.values()].filter((r) => r.paymentId === paymentId);
177
+ }
178
+ async getByExternalRef(provider, externalReference) {
179
+ return ([...this.rows.values()].find((r) => r.provider === provider &&
180
+ r.externalReference === externalReference) ?? null);
181
+ }
182
+ }
183
+ export class InMemoryPaymentEventStore {
184
+ rows = new Map();
185
+ async append(e) {
186
+ if (this.rows.has(e.id))
187
+ return null;
188
+ const row = {
189
+ ...e,
190
+ attemptId: e.attemptId ?? null,
191
+ reason: e.reason ?? null,
192
+ createdAt: nowIso(),
193
+ };
194
+ this.rows.set(row.id, row);
195
+ return row;
196
+ }
197
+ async listByPayment(paymentId) {
198
+ return [...this.rows.values()]
199
+ .filter((r) => r.paymentId === paymentId)
200
+ .sort((a, b) => a.atMs - b.atMs);
201
+ }
202
+ async listByAttempt(attemptId) {
203
+ return [...this.rows.values()]
204
+ .filter((r) => r.attemptId === attemptId)
205
+ .sort((a, b) => a.atMs - b.atMs);
206
+ }
207
+ }
208
+ export class InMemoryPaymentProductStore {
209
+ rows = new Map();
210
+ async upsert(p) {
211
+ const existing = [...this.rows.values()].find((r) => r.code === p.code);
212
+ if (existing) {
213
+ const updated = {
214
+ ...existing,
215
+ name: p.name,
216
+ description: p.description ?? null,
217
+ category: p.category,
218
+ active: p.active ?? true,
219
+ payload: p.payload,
220
+ updatedAt: nowIso(),
221
+ };
222
+ this.rows.set(existing.id, updated);
223
+ return updated;
224
+ }
225
+ const ts = nowIso();
226
+ const row = {
227
+ ...p,
228
+ description: p.description ?? null,
229
+ active: p.active ?? true,
230
+ createdAt: ts,
231
+ updatedAt: ts,
232
+ };
233
+ this.rows.set(row.id, row);
234
+ return row;
235
+ }
236
+ async getById(id) {
237
+ return this.rows.get(id) ?? null;
238
+ }
239
+ async getByCode(code) {
240
+ return [...this.rows.values()].find((r) => r.code === code) ?? null;
241
+ }
242
+ async list(opts) {
243
+ let all = [...this.rows.values()];
244
+ if (opts?.activeOnly)
245
+ all = all.filter((r) => r.active);
246
+ return all;
247
+ }
248
+ }
249
+ export class InMemoryPaymentPriceStore {
250
+ rows = new Map();
251
+ async create(p) {
252
+ if (this.rows.has(p.id))
253
+ return null;
254
+ const ts = nowIso();
255
+ const row = {
256
+ ...p,
257
+ currency: p.currency ?? "USD",
258
+ interval: p.interval ?? null,
259
+ active: p.active ?? true,
260
+ createdAt: ts,
261
+ updatedAt: ts,
262
+ };
263
+ this.rows.set(row.id, row);
264
+ return row;
265
+ }
266
+ async getById(id) {
267
+ return this.rows.get(id) ?? null;
268
+ }
269
+ async listByProduct(productId, opts) {
270
+ let all = [...this.rows.values()].filter((r) => r.productId === productId);
271
+ if (opts?.activeOnly)
272
+ all = all.filter((r) => r.active);
273
+ return all;
274
+ }
275
+ }
276
+ export class InMemoryPaymentExceptionStore {
277
+ rows = new Map();
278
+ async append(e) {
279
+ if (this.rows.has(e.id))
280
+ return null;
281
+ const row = {
282
+ ...e,
283
+ resolvedAtMs: null,
284
+ resolutionNote: null,
285
+ createdAt: nowIso(),
286
+ };
287
+ this.rows.set(row.id, row);
288
+ return row;
289
+ }
290
+ async getById(id) {
291
+ return this.rows.get(id) ?? null;
292
+ }
293
+ async listByPayment(paymentId) {
294
+ return [...this.rows.values()].filter((r) => r.paymentId === paymentId);
295
+ }
296
+ async list(opts = {}) {
297
+ let out = [...this.rows.values()].filter((r) => (opts.status === undefined || r.status === opts.status) &&
298
+ (opts.kind === undefined || r.kind === opts.kind) &&
299
+ (opts.sinceMs === undefined || r.createdAtMs >= opts.sinceMs));
300
+ out.sort((a, b) => b.createdAtMs - a.createdAtMs);
301
+ if (opts.limit !== undefined)
302
+ out = out.slice(0, opts.limit);
303
+ return out;
304
+ }
305
+ async resolve(id, status, note) {
306
+ const row = this.rows.get(id);
307
+ if (!row || row.status !== "OPEN")
308
+ return null;
309
+ const next = {
310
+ ...row,
311
+ status,
312
+ resolvedAtMs: Date.now(),
313
+ resolutionNote: note ?? null,
314
+ };
315
+ this.rows.set(id, next);
316
+ return next;
317
+ }
318
+ }
319
+ export class InMemoryUsageRecordStore {
320
+ rows = new Map();
321
+ async append(r) {
322
+ if (this.rows.has(r.id))
323
+ return null;
324
+ for (const x of this.rows.values()) {
325
+ if (x.requestId === r.requestId)
326
+ return null;
327
+ }
328
+ const row = {
329
+ ...r,
330
+ agentId: r.agentId ?? null,
331
+ currency: r.currency ?? "USDC",
332
+ txHash: r.txHash ?? null,
333
+ createdAt: nowIso(),
334
+ };
335
+ this.rows.set(row.id, row);
336
+ return row;
337
+ }
338
+ async getById(id) {
339
+ return this.rows.get(id) ?? null;
340
+ }
341
+ async getByRequestId(requestId) {
342
+ return ([...this.rows.values()].find((r) => r.requestId === requestId) ??
343
+ null);
344
+ }
345
+ async listByPayment(paymentId) {
346
+ return [...this.rows.values()]
347
+ .filter((r) => r.paymentId === paymentId)
348
+ .sort((a, b) => b.createdAtMs - a.createdAtMs);
349
+ }
350
+ async listByAgent(agentId, limit) {
351
+ const out = [...this.rows.values()]
352
+ .filter((r) => r.agentId === agentId)
353
+ .sort((a, b) => b.createdAtMs - a.createdAtMs);
354
+ return limit === undefined ? out : out.slice(0, limit);
355
+ }
356
+ async listByResource(resource, limit) {
357
+ const out = [...this.rows.values()]
358
+ .filter((r) => r.resource === resource)
359
+ .sort((a, b) => b.createdAtMs - a.createdAtMs);
360
+ return limit === undefined ? out : out.slice(0, limit);
361
+ }
362
+ }
363
+ export function createInMemoryPaymentStores() {
364
+ return {
365
+ payments: new InMemoryPaymentStore(),
366
+ attempts: new InMemoryPaymentAttemptStore(),
367
+ receipts: new InMemoryPaymentReceiptStore(),
368
+ events: new InMemoryPaymentEventStore(),
369
+ exceptions: new InMemoryPaymentExceptionStore(),
370
+ products: new InMemoryPaymentProductStore(),
371
+ prices: new InMemoryPaymentPriceStore(),
372
+ usageRecords: new InMemoryUsageRecordStore(),
373
+ };
374
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/database",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "description": "Shared database layer for AgentBadge — Prisma ORM 8 client, repositories, store abstraction",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -50,7 +50,7 @@
50
50
  "vitest": "^2.0.0"
51
51
  },
52
52
  "dependencies": {
53
- "@agentbadge/econ-events": "^0.1.1",
53
+ "@agentbadge/econ-events": "^0.1.3",
54
54
  "@prisma/orm-postgres": "8.0.0-rc.11",
55
55
  "dotenv": "^18.0.3"
56
56
  }