@agentbadge/database 0.6.1 → 0.6.2

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,8 @@
1
+ import type { PrismaDb } from "./event-repository.js";
2
+ import type { ArchiveCursorCommit, ArchiveCursorRecord, ArchiveCursorStore } from "./archive-cursor-store.js";
3
+ export declare class ArchiveCursorRepository implements ArchiveCursorStore {
4
+ private readonly db;
5
+ constructor(db: PrismaDb);
6
+ getCursor(stream: string): Promise<ArchiveCursorRecord | null>;
7
+ commitCursor(stream: string, commit: ArchiveCursorCommit): Promise<void>;
8
+ }
@@ -0,0 +1,38 @@
1
+ export class ArchiveCursorRepository {
2
+ db;
3
+ constructor(db) {
4
+ this.db = db;
5
+ }
6
+ async getCursor(stream) {
7
+ const r = await this.db.orm.public.ArchiveCursor.where({ stream }).first();
8
+ if (!r)
9
+ return null;
10
+ return {
11
+ stream: r.stream,
12
+ throughAt: BigInt(r.throughAt),
13
+ lastMessageId: r.lastMessageId === null || r.lastMessageId === undefined
14
+ ? null
15
+ : BigInt(r.lastMessageId),
16
+ note: r.note ?? null,
17
+ updatedAt: r.updatedAt,
18
+ };
19
+ }
20
+ async commitCursor(stream, commit) {
21
+ const patch = {
22
+ throughAt: commit.throughAt,
23
+ lastMessageId: (commit.messageId ?? null),
24
+ note: (commit.note ?? null),
25
+ };
26
+ const existing = await this.db.orm.public.ArchiveCursor.where({
27
+ stream,
28
+ }).first();
29
+ if (existing) {
30
+ await this.db.orm.public.ArchiveCursor.where({ stream }).update(patch);
31
+ return;
32
+ }
33
+ await this.db.orm.public.ArchiveCursor.create({
34
+ stream,
35
+ ...patch,
36
+ });
37
+ }
38
+ }
@@ -0,0 +1,21 @@
1
+ export interface ArchiveCursorRecord {
2
+ stream: string;
3
+ throughAt: bigint;
4
+ lastMessageId: bigint | null;
5
+ note: string | null;
6
+ updatedAt: string;
7
+ }
8
+ export interface ArchiveCursorCommit {
9
+ throughAt: bigint;
10
+ messageId?: bigint | null;
11
+ note?: string | null;
12
+ }
13
+ export interface ArchiveCursorStore {
14
+ getCursor(stream: string): Promise<ArchiveCursorRecord | null>;
15
+ commitCursor(stream: string, commit: ArchiveCursorCommit): Promise<void>;
16
+ }
17
+ export declare class InMemoryArchiveCursorStore implements ArchiveCursorStore {
18
+ private rows;
19
+ getCursor(stream: string): Promise<ArchiveCursorRecord | null>;
20
+ commitCursor(stream: string, commit: ArchiveCursorCommit): Promise<void>;
21
+ }
@@ -0,0 +1,16 @@
1
+ export class InMemoryArchiveCursorStore {
2
+ rows = new Map();
3
+ async getCursor(stream) {
4
+ const r = this.rows.get(stream);
5
+ return r ? { ...r } : null;
6
+ }
7
+ async commitCursor(stream, commit) {
8
+ this.rows.set(stream, {
9
+ stream,
10
+ throughAt: commit.throughAt,
11
+ lastMessageId: commit.messageId ?? null,
12
+ note: commit.note ?? null,
13
+ updatedAt: new Date().toISOString(),
14
+ });
15
+ }
16
+ }
@@ -10,6 +10,10 @@ export declare class ForensicsEdgeRepository implements ForensicsEdgeStore {
10
10
  fromBlock?: bigint;
11
11
  }): Promise<ForensicsEdgeRow[]>;
12
12
  listByPayer(chain: string, payer: string): Promise<ForensicsEdgeRow[]>;
13
+ listWindow(fromIso: string, toIso: string, opts?: {
14
+ chain?: string;
15
+ limit?: number;
16
+ }): Promise<ForensicsEdgeRow[]>;
13
17
  countByChain(chain: string): Promise<number>;
14
18
  private toRow;
15
19
  }
@@ -47,6 +47,14 @@ export class ForensicsEdgeRepository {
47
47
  }).all();
48
48
  return rows.map((r) => this.toRow(r));
49
49
  }
50
+ async listWindow(fromIso, toIso, opts) {
51
+ const rows = await this.db.orm.public.ForensicsEdge.where(opts?.chain !== undefined ? { chain: opts.chain } : {}).all();
52
+ return rows
53
+ .map((r) => this.toRow(r))
54
+ .filter((r) => r.createdAt >= fromIso && r.createdAt < toIso)
55
+ .sort((a, b) => a.createdAt.localeCompare(b.createdAt))
56
+ .slice(0, opts?.limit ?? Number.MAX_SAFE_INTEGER);
57
+ }
50
58
  async countByChain(chain) {
51
59
  const rows = await this.db.orm.public.ForensicsEdge.where({ chain }).all();
52
60
  return rows.length;
@@ -20,6 +20,10 @@ export interface ForensicsEdgeStore {
20
20
  fromBlock?: bigint;
21
21
  }): Promise<ForensicsEdgeRow[]>;
22
22
  listByPayer(chain: string, payer: string): Promise<ForensicsEdgeRow[]>;
23
+ listWindow(fromIso: string, toIso: string, opts?: {
24
+ chain?: string;
25
+ limit?: number;
26
+ }): Promise<ForensicsEdgeRow[]>;
23
27
  countByChain(chain: string): Promise<number>;
24
28
  }
25
29
  export interface ForensicsFundingInsert {
@@ -55,6 +59,10 @@ export declare class InMemoryForensicsEdgeStore implements ForensicsEdgeStore {
55
59
  fromBlock?: bigint;
56
60
  }): Promise<ForensicsEdgeRow[]>;
57
61
  listByPayer(chain: string, payer: string): Promise<ForensicsEdgeRow[]>;
62
+ listWindow(fromIso: string, toIso: string, opts?: {
63
+ chain?: string;
64
+ limit?: number;
65
+ }): Promise<ForensicsEdgeRow[]>;
58
66
  countByChain(chain: string): Promise<number>;
59
67
  }
60
68
  export declare class InMemoryForensicsFundingStore implements ForensicsFundingStore {
@@ -36,6 +36,13 @@ export class InMemoryForensicsEdgeStore {
36
36
  const p = payer.toLowerCase();
37
37
  return [...this.rows.values()].filter((r) => r.chain === chain && r.payer === p);
38
38
  }
39
+ async listWindow(fromIso, toIso, opts) {
40
+ let out = [...this.rows.values()].filter((r) => r.createdAt >= fromIso &&
41
+ r.createdAt < toIso &&
42
+ (opts?.chain === undefined || r.chain === opts.chain));
43
+ out.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
44
+ return opts?.limit !== undefined ? out.slice(0, opts.limit) : out;
45
+ }
39
46
  async countByChain(chain) {
40
47
  return [...this.rows.values()].filter((r) => r.chain === chain).length;
41
48
  }
@@ -0,0 +1,66 @@
1
+ import type { PrismaDb } from "./event-repository.js";
2
+ import type { AttemptStatusPatch, PaymentAttemptInsert, PaymentAttemptRow, PaymentAttemptStore, PaymentEventInsert, PaymentEventRow, PaymentEventStore, PaymentInsert, PaymentPriceInsert, PaymentPriceRow, PaymentPriceStore, PaymentProductInsert, PaymentProductRow, PaymentProductStore, PaymentReceiptInsert, PaymentReceiptRow, PaymentReceiptStore, PaymentRow, PaymentStore, PaymentStores } from "./payment-store.js";
3
+ export declare class PaymentRepository implements PaymentStore {
4
+ private readonly db;
5
+ constructor(db: PrismaDb);
6
+ private get orm();
7
+ create(p: PaymentInsert): Promise<PaymentRow | null>;
8
+ getById(id: string): Promise<PaymentRow | null>;
9
+ getByReference(reference: string): Promise<PaymentRow | null>;
10
+ getByIdempotencyKey(key: string): Promise<PaymentRow | null>;
11
+ listByCustomer(customerId: string): Promise<PaymentRow[]>;
12
+ applyStatus(id: string, expectedFrom: string, to: string, updatedAtMs: number): Promise<PaymentRow | null>;
13
+ listExpired(nowMs: number): Promise<PaymentRow[]>;
14
+ }
15
+ export declare class PaymentAttemptRepository implements PaymentAttemptStore {
16
+ private readonly db;
17
+ constructor(db: PrismaDb);
18
+ private get orm();
19
+ create(a: PaymentAttemptInsert): Promise<PaymentAttemptRow | null>;
20
+ getById(id: string): Promise<PaymentAttemptRow | null>;
21
+ listByPayment(paymentId: string): Promise<PaymentAttemptRow[]>;
22
+ getByProviderRef(provider: string, providerReference: string): Promise<PaymentAttemptRow | null>;
23
+ private getByProviderTx;
24
+ getByTxHash(transactionHash: string): Promise<PaymentAttemptRow | null>;
25
+ applyStatus(id: string, expectedFrom: string, to: string, _updatedAtMs: number, patch?: AttemptStatusPatch): Promise<PaymentAttemptRow | null>;
26
+ listExpired(nowMs: number): Promise<PaymentAttemptRow[]>;
27
+ }
28
+ export declare class PaymentReceiptRepository implements PaymentReceiptStore {
29
+ private readonly db;
30
+ constructor(db: PrismaDb);
31
+ private get orm();
32
+ append(r: PaymentReceiptInsert): Promise<PaymentReceiptRow | null>;
33
+ getById(id: string): Promise<PaymentReceiptRow | null>;
34
+ listByPayment(paymentId: string): Promise<PaymentReceiptRow[]>;
35
+ getByExternalRef(provider: string, externalReference: string): Promise<PaymentReceiptRow | null>;
36
+ }
37
+ export declare class PaymentEventRepository implements PaymentEventStore {
38
+ private readonly db;
39
+ constructor(db: PrismaDb);
40
+ private get orm();
41
+ append(e: PaymentEventInsert): Promise<PaymentEventRow | null>;
42
+ listByPayment(paymentId: string): Promise<PaymentEventRow[]>;
43
+ listByAttempt(attemptId: string): Promise<PaymentEventRow[]>;
44
+ }
45
+ export declare class PaymentProductRepository implements PaymentProductStore {
46
+ private readonly db;
47
+ constructor(db: PrismaDb);
48
+ private get orm();
49
+ upsert(p: PaymentProductInsert): Promise<PaymentProductRow>;
50
+ getById(id: string): Promise<PaymentProductRow | null>;
51
+ getByCode(code: string): Promise<PaymentProductRow | null>;
52
+ list(opts?: {
53
+ activeOnly?: boolean;
54
+ }): Promise<PaymentProductRow[]>;
55
+ }
56
+ export declare class PaymentPriceRepository implements PaymentPriceStore {
57
+ private readonly db;
58
+ constructor(db: PrismaDb);
59
+ private get orm();
60
+ create(p: PaymentPriceInsert): Promise<PaymentPriceRow | null>;
61
+ getById(id: string): Promise<PaymentPriceRow | null>;
62
+ listByProduct(productId: string, opts?: {
63
+ activeOnly?: boolean;
64
+ }): Promise<PaymentPriceRow[]>;
65
+ }
66
+ export declare function createPrismaPaymentStores(db: PrismaDb): PaymentStores;
@@ -0,0 +1,445 @@
1
+ import { TERMINAL_ATTEMPT_STATUSES } from "./payment-store.js";
2
+ const num = (v) => Number(v);
3
+ const numOrNull = (v) => v === null || v === undefined ? null : Number(v);
4
+ const dec = (v) => String(v);
5
+ const toPaymentRow = (r) => ({
6
+ id: r.id,
7
+ reference: r.reference,
8
+ idempotencyKey: r.idempotencyKey,
9
+ customerId: r.customerId,
10
+ agentId: r.agentId,
11
+ productId: r.productId,
12
+ orderId: r.orderId,
13
+ amount: dec(r.amount),
14
+ currency: r.currency,
15
+ status: r.status,
16
+ purpose: r.purpose,
17
+ expiresAtMs: numOrNull(r.expiresAtMs),
18
+ createdAtMs: num(r.createdAtMs),
19
+ updatedAtMs: num(r.updatedAtMs),
20
+ payload: r.payload,
21
+ createdAt: r.createdAt,
22
+ updatedAt: r.updatedAt,
23
+ });
24
+ const toAttemptRow = (r) => ({
25
+ id: r.id,
26
+ paymentId: r.paymentId,
27
+ provider: r.provider,
28
+ network: r.network,
29
+ token: r.token,
30
+ amount: dec(r.amount),
31
+ currency: r.currency,
32
+ status: r.status,
33
+ providerReference: r.providerReference,
34
+ transactionHash: r.transactionHash,
35
+ expiresAtMs: numOrNull(r.expiresAtMs),
36
+ confirmedAtMs: numOrNull(r.confirmedAtMs),
37
+ payload: r.payload,
38
+ createdAt: r.createdAt,
39
+ updatedAt: r.updatedAt,
40
+ });
41
+ const toReceiptRow = (r) => ({
42
+ id: r.id,
43
+ paymentId: r.paymentId,
44
+ attemptId: r.attemptId,
45
+ provider: r.provider,
46
+ externalReference: r.externalReference,
47
+ network: r.network,
48
+ transactionHash: r.transactionHash,
49
+ payer: r.payer,
50
+ recipient: r.recipient,
51
+ amount: dec(r.amount),
52
+ asset: r.asset,
53
+ verifiedAtMs: num(r.verifiedAtMs),
54
+ payload: r.payload,
55
+ createdAt: r.createdAt,
56
+ });
57
+ const toEventRow = (r) => ({
58
+ id: r.id,
59
+ entity: r.entity,
60
+ paymentId: r.paymentId,
61
+ attemptId: r.attemptId,
62
+ from: r.from,
63
+ to: r.to,
64
+ actor: r.actor,
65
+ reason: r.reason,
66
+ atMs: num(r.atMs),
67
+ payload: r.payload,
68
+ createdAt: r.createdAt,
69
+ });
70
+ const toProductRow = (r) => ({
71
+ id: r.id,
72
+ code: r.code,
73
+ name: r.name,
74
+ description: r.description,
75
+ category: r.category,
76
+ active: r.active,
77
+ payload: r.payload,
78
+ createdAt: r.createdAt,
79
+ updatedAt: r.updatedAt,
80
+ });
81
+ const toPriceRow = (r) => ({
82
+ id: r.id,
83
+ productId: r.productId,
84
+ amount: dec(r.amount),
85
+ currency: r.currency,
86
+ billingType: r.billingType,
87
+ interval: r.interval,
88
+ active: r.active,
89
+ payload: r.payload,
90
+ createdAt: r.createdAt,
91
+ updatedAt: r.updatedAt,
92
+ });
93
+ export class PaymentRepository {
94
+ db;
95
+ constructor(db) {
96
+ this.db = db;
97
+ }
98
+ get orm() {
99
+ return this.db.orm.public.Payment;
100
+ }
101
+ async create(p) {
102
+ if (await this.getById(p.id))
103
+ return null;
104
+ if (await this.getByReference(p.reference))
105
+ return null;
106
+ if (p.idempotencyKey != null &&
107
+ (await this.getByIdempotencyKey(p.idempotencyKey))) {
108
+ return null;
109
+ }
110
+ const row = await this.orm.create({
111
+ id: p.id,
112
+ reference: p.reference,
113
+ idempotencyKey: p.idempotencyKey ?? null,
114
+ customerId: p.customerId ?? null,
115
+ agentId: p.agentId ?? null,
116
+ productId: p.productId ?? null,
117
+ orderId: p.orderId ?? null,
118
+ amount: p.amount,
119
+ currency: p.currency ?? "USD",
120
+ status: p.status,
121
+ purpose: p.purpose,
122
+ expiresAtMs: p.expiresAtMs != null
123
+ ? BigInt(p.expiresAtMs)
124
+ : null,
125
+ createdAtMs: BigInt(p.createdAtMs),
126
+ updatedAtMs: BigInt(p.updatedAtMs),
127
+ payload: p.payload,
128
+ });
129
+ return toPaymentRow(row);
130
+ }
131
+ async getById(id) {
132
+ const r = await this.orm.where({ id }).first();
133
+ return r ? toPaymentRow(r) : null;
134
+ }
135
+ async getByReference(reference) {
136
+ const r = await this.orm.where({ reference }).first();
137
+ return r ? toPaymentRow(r) : null;
138
+ }
139
+ async getByIdempotencyKey(key) {
140
+ const r = await this.orm.where({ idempotencyKey: key }).first();
141
+ return r ? toPaymentRow(r) : null;
142
+ }
143
+ async listByCustomer(customerId) {
144
+ const rows = await this.orm
145
+ .orderBy((e) => e.createdAtMs.desc())
146
+ .where({ customerId })
147
+ .all();
148
+ return rows.map(toPaymentRow);
149
+ }
150
+ async applyStatus(id, expectedFrom, to, updatedAtMs) {
151
+ const row = await this.orm.where({ id, status: expectedFrom }).update({
152
+ status: to,
153
+ updatedAtMs: BigInt(updatedAtMs),
154
+ });
155
+ return row ? toPaymentRow(row) : null;
156
+ }
157
+ async listExpired(nowMs) {
158
+ const out = [];
159
+ for (const status of ["PENDING", "PROCESSING"]) {
160
+ const rows = await this.orm.where({ status }).all();
161
+ for (const r of rows) {
162
+ const exp = numOrNull(r.expiresAtMs);
163
+ if (exp !== null && exp < nowMs)
164
+ out.push(toPaymentRow(r));
165
+ }
166
+ }
167
+ return out;
168
+ }
169
+ }
170
+ export class PaymentAttemptRepository {
171
+ db;
172
+ constructor(db) {
173
+ this.db = db;
174
+ }
175
+ get orm() {
176
+ return this.db.orm.public.PaymentAttempt;
177
+ }
178
+ async create(a) {
179
+ if (await this.getById(a.id))
180
+ return null;
181
+ if (a.providerReference != null &&
182
+ (await this.getByProviderRef(a.provider, a.providerReference))) {
183
+ return null;
184
+ }
185
+ if (a.transactionHash != null &&
186
+ (await this.getByProviderTx(a.provider, a.transactionHash))) {
187
+ return null;
188
+ }
189
+ const row = await this.orm.create({
190
+ id: a.id,
191
+ paymentId: a.paymentId,
192
+ provider: a.provider,
193
+ network: a.network ?? null,
194
+ token: a.token ?? null,
195
+ amount: a.amount,
196
+ currency: a.currency,
197
+ status: a.status,
198
+ providerReference: a.providerReference ?? null,
199
+ transactionHash: a.transactionHash ?? null,
200
+ expiresAtMs: a.expiresAtMs != null
201
+ ? BigInt(a.expiresAtMs)
202
+ : null,
203
+ confirmedAtMs: a.confirmedAtMs != null
204
+ ? BigInt(a.confirmedAtMs)
205
+ : null,
206
+ payload: a.payload,
207
+ });
208
+ return toAttemptRow(row);
209
+ }
210
+ async getById(id) {
211
+ const r = await this.orm.where({ id }).first();
212
+ return r ? toAttemptRow(r) : null;
213
+ }
214
+ async listByPayment(paymentId) {
215
+ const rows = await this.orm.where({ paymentId }).all();
216
+ return rows.map(toAttemptRow);
217
+ }
218
+ async getByProviderRef(provider, providerReference) {
219
+ const r = await this.orm
220
+ .where({ provider, providerReference })
221
+ .first();
222
+ return r ? toAttemptRow(r) : null;
223
+ }
224
+ async getByProviderTx(provider, transactionHash) {
225
+ const r = await this.orm
226
+ .where({ provider, transactionHash })
227
+ .first();
228
+ return r ? toAttemptRow(r) : null;
229
+ }
230
+ async getByTxHash(transactionHash) {
231
+ const r = await this.orm.where({ transactionHash }).first();
232
+ return r ? toAttemptRow(r) : null;
233
+ }
234
+ async applyStatus(id, expectedFrom, to, _updatedAtMs, patch) {
235
+ const row = await this.orm.where({ id, status: expectedFrom }).update({
236
+ status: to,
237
+ ...(patch?.confirmedAtMs !== undefined
238
+ ? {
239
+ confirmedAtMs: BigInt(patch.confirmedAtMs),
240
+ }
241
+ : {}),
242
+ ...(patch?.transactionHash !== undefined
243
+ ? { transactionHash: patch.transactionHash }
244
+ : {}),
245
+ ...(patch?.providerReference !== undefined
246
+ ? { providerReference: patch.providerReference }
247
+ : {}),
248
+ });
249
+ return row ? toAttemptRow(row) : null;
250
+ }
251
+ async listExpired(nowMs) {
252
+ const out = [];
253
+ const openStatuses = [
254
+ "CREATED",
255
+ "PENDING",
256
+ "PROCESSING",
257
+ ].filter((s) => !TERMINAL_ATTEMPT_STATUSES.includes(s));
258
+ for (const status of openStatuses) {
259
+ const rows = await this.orm.where({ status }).all();
260
+ for (const r of rows) {
261
+ const exp = numOrNull(r.expiresAtMs);
262
+ if (exp !== null && exp < nowMs)
263
+ out.push(toAttemptRow(r));
264
+ }
265
+ }
266
+ return out;
267
+ }
268
+ }
269
+ export class PaymentReceiptRepository {
270
+ db;
271
+ constructor(db) {
272
+ this.db = db;
273
+ }
274
+ get orm() {
275
+ return this.db.orm.public.PaymentReceipt;
276
+ }
277
+ async append(r) {
278
+ if (await this.getById(r.id))
279
+ return null;
280
+ if (await this.getByExternalRef(r.provider, r.externalReference)) {
281
+ return null;
282
+ }
283
+ const row = await this.orm.create({
284
+ id: r.id,
285
+ paymentId: r.paymentId,
286
+ attemptId: r.attemptId,
287
+ provider: r.provider,
288
+ externalReference: r.externalReference,
289
+ network: r.network ?? null,
290
+ transactionHash: r.transactionHash ?? null,
291
+ payer: r.payer ?? null,
292
+ recipient: r.recipient ?? null,
293
+ amount: r.amount,
294
+ asset: r.asset,
295
+ verifiedAtMs: BigInt(r.verifiedAtMs),
296
+ payload: r.payload,
297
+ });
298
+ return toReceiptRow(row);
299
+ }
300
+ async getById(id) {
301
+ const r = await this.orm.where({ id }).first();
302
+ return r ? toReceiptRow(r) : null;
303
+ }
304
+ async listByPayment(paymentId) {
305
+ const rows = await this.orm.where({ paymentId }).all();
306
+ return rows.map(toReceiptRow);
307
+ }
308
+ async getByExternalRef(provider, externalReference) {
309
+ const r = await this.orm
310
+ .where({ provider, externalReference })
311
+ .first();
312
+ return r ? toReceiptRow(r) : null;
313
+ }
314
+ }
315
+ export class PaymentEventRepository {
316
+ db;
317
+ constructor(db) {
318
+ this.db = db;
319
+ }
320
+ get orm() {
321
+ return this.db.orm.public.PaymentEvent;
322
+ }
323
+ async append(e) {
324
+ const existing = await this.orm.where({ id: e.id }).first();
325
+ if (existing)
326
+ return null;
327
+ const row = await this.orm.create({
328
+ id: e.id,
329
+ entity: e.entity,
330
+ paymentId: e.paymentId,
331
+ attemptId: e.attemptId ?? null,
332
+ from: e.from,
333
+ to: e.to,
334
+ actor: e.actor,
335
+ reason: e.reason ?? null,
336
+ atMs: BigInt(e.atMs),
337
+ payload: e.payload,
338
+ });
339
+ return toEventRow(row);
340
+ }
341
+ async listByPayment(paymentId) {
342
+ const rows = await this.orm
343
+ .orderBy((e) => e.atMs.asc())
344
+ .where({ paymentId })
345
+ .all();
346
+ return rows.map(toEventRow);
347
+ }
348
+ async listByAttempt(attemptId) {
349
+ const rows = await this.orm
350
+ .orderBy((e) => e.atMs.asc())
351
+ .where({ attemptId })
352
+ .all();
353
+ return rows.map(toEventRow);
354
+ }
355
+ }
356
+ export class PaymentProductRepository {
357
+ db;
358
+ constructor(db) {
359
+ this.db = db;
360
+ }
361
+ get orm() {
362
+ return this.db.orm.public.PaymentProduct;
363
+ }
364
+ async upsert(p) {
365
+ const existing = await this.orm.where({ code: p.code }).first();
366
+ if (existing) {
367
+ const updated = await this.orm.where({ id: existing.id }).update({
368
+ name: p.name,
369
+ description: p.description ?? null,
370
+ category: p.category,
371
+ active: p.active ?? true,
372
+ payload: p.payload,
373
+ });
374
+ return toProductRow(updated);
375
+ }
376
+ const row = await this.orm.create({
377
+ id: p.id,
378
+ code: p.code,
379
+ name: p.name,
380
+ description: p.description ?? null,
381
+ category: p.category,
382
+ active: p.active ?? true,
383
+ payload: p.payload,
384
+ });
385
+ return toProductRow(row);
386
+ }
387
+ async getById(id) {
388
+ const r = await this.orm.where({ id }).first();
389
+ return r ? toProductRow(r) : null;
390
+ }
391
+ async getByCode(code) {
392
+ const r = await this.orm.where({ code }).first();
393
+ return r ? toProductRow(r) : null;
394
+ }
395
+ async list(opts) {
396
+ const rows = opts?.activeOnly
397
+ ? await this.orm.where({ active: true }).all()
398
+ : await this.orm.all();
399
+ return rows.map(toProductRow);
400
+ }
401
+ }
402
+ export class PaymentPriceRepository {
403
+ db;
404
+ constructor(db) {
405
+ this.db = db;
406
+ }
407
+ get orm() {
408
+ return this.db.orm.public.PaymentPrice;
409
+ }
410
+ async create(p) {
411
+ if (await this.getById(p.id))
412
+ return null;
413
+ const row = await this.orm.create({
414
+ id: p.id,
415
+ productId: p.productId,
416
+ amount: p.amount,
417
+ currency: p.currency ?? "USD",
418
+ billingType: p.billingType,
419
+ interval: p.interval ?? null,
420
+ active: p.active ?? true,
421
+ payload: p.payload,
422
+ });
423
+ return toPriceRow(row);
424
+ }
425
+ async getById(id) {
426
+ const r = await this.orm.where({ id }).first();
427
+ return r ? toPriceRow(r) : null;
428
+ }
429
+ async listByProduct(productId, opts) {
430
+ const rows = opts?.activeOnly
431
+ ? await this.orm.where({ productId, active: true }).all()
432
+ : await this.orm.where({ productId }).all();
433
+ return rows.map(toPriceRow);
434
+ }
435
+ }
436
+ export function createPrismaPaymentStores(db) {
437
+ return {
438
+ payments: new PaymentRepository(db),
439
+ attempts: new PaymentAttemptRepository(db),
440
+ receipts: new PaymentReceiptRepository(db),
441
+ events: new PaymentEventRepository(db),
442
+ products: new PaymentProductRepository(db),
443
+ prices: new PaymentPriceRepository(db),
444
+ };
445
+ }