@agentbadge/database 0.6.2 → 0.6.4
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/dist/database.d.ts +6 -0
- package/dist/database.js +10 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.js +6 -2
- package/dist/prisma/contract.json +5454 -2128
- package/dist/repositories/agent-request-repository.d.ts +56 -0
- package/dist/repositories/agent-request-repository.js +145 -0
- package/dist/repositories/agent-survey-repository.d.ts +40 -0
- package/dist/repositories/agent-survey-repository.js +90 -0
- package/dist/repositories/commerce-repository.d.ts +75 -0
- package/dist/repositories/commerce-repository.js +492 -0
- package/dist/repositories/commerce-store.d.ts +249 -0
- package/dist/repositories/commerce-store.js +311 -0
- package/dist/repositories/payment-repository.d.ts +29 -1
- package/dist/repositories/payment-repository.js +179 -0
- package/dist/repositories/payment-store.d.ts +81 -0
- package/dist/repositories/payment-store.js +105 -0
- package/package.json +1 -1
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
|
|
2
|
+
export interface CommerceTaskInsert {
|
|
3
|
+
id: string;
|
|
4
|
+
buyerWallet: string;
|
|
5
|
+
buyerAgentId?: string | null;
|
|
6
|
+
title: string;
|
|
7
|
+
description: string;
|
|
8
|
+
requirements?: JsonValue | null;
|
|
9
|
+
budget?: string | null;
|
|
10
|
+
currency?: string;
|
|
11
|
+
expiresAtMs?: number | null;
|
|
12
|
+
status: string;
|
|
13
|
+
payload: JsonValue;
|
|
14
|
+
}
|
|
15
|
+
export interface CommerceTaskRow extends CommerceTaskInsert {
|
|
16
|
+
createdAt: string;
|
|
17
|
+
updatedAt: string;
|
|
18
|
+
}
|
|
19
|
+
export interface CommerceTaskStore {
|
|
20
|
+
create(t: CommerceTaskInsert): Promise<CommerceTaskRow | null>;
|
|
21
|
+
getById(id: string): Promise<CommerceTaskRow | null>;
|
|
22
|
+
list(opts?: {
|
|
23
|
+
status?: string;
|
|
24
|
+
buyerWallet?: string;
|
|
25
|
+
limit?: number;
|
|
26
|
+
}): Promise<CommerceTaskRow[]>;
|
|
27
|
+
applyStatus(id: string, expectedFrom: string, to: string): Promise<CommerceTaskRow | null>;
|
|
28
|
+
}
|
|
29
|
+
export interface CommerceOfferInsert {
|
|
30
|
+
id: string;
|
|
31
|
+
taskId: string;
|
|
32
|
+
providerWallet: string;
|
|
33
|
+
providerAgentId?: string | null;
|
|
34
|
+
price: string;
|
|
35
|
+
currency?: string;
|
|
36
|
+
eta?: string | null;
|
|
37
|
+
proposal: string;
|
|
38
|
+
status: string;
|
|
39
|
+
payload: JsonValue;
|
|
40
|
+
}
|
|
41
|
+
export interface CommerceOfferRow extends CommerceOfferInsert {
|
|
42
|
+
createdAt: string;
|
|
43
|
+
updatedAt: string;
|
|
44
|
+
}
|
|
45
|
+
export interface CommerceOfferStore {
|
|
46
|
+
create(o: CommerceOfferInsert): Promise<CommerceOfferRow | null>;
|
|
47
|
+
getById(id: string): Promise<CommerceOfferRow | null>;
|
|
48
|
+
listByTask(taskId: string): Promise<CommerceOfferRow[]>;
|
|
49
|
+
applyStatus(id: string, expectedFrom: string, to: string): Promise<CommerceOfferRow | null>;
|
|
50
|
+
}
|
|
51
|
+
export interface CommerceAgreementInsert {
|
|
52
|
+
id: string;
|
|
53
|
+
taskId: string;
|
|
54
|
+
offerId: string;
|
|
55
|
+
buyerWallet: string;
|
|
56
|
+
providerWallet: string;
|
|
57
|
+
agreedAmount: string;
|
|
58
|
+
currency?: string;
|
|
59
|
+
acceptanceCriteria?: JsonValue | null;
|
|
60
|
+
status: string;
|
|
61
|
+
completedAtMs?: number | null;
|
|
62
|
+
payload: JsonValue;
|
|
63
|
+
}
|
|
64
|
+
export interface CommerceAgreementRow extends CommerceAgreementInsert {
|
|
65
|
+
createdAt: string;
|
|
66
|
+
updatedAt: string;
|
|
67
|
+
}
|
|
68
|
+
export interface AgreementStatusPatch {
|
|
69
|
+
completedAtMs?: number;
|
|
70
|
+
}
|
|
71
|
+
export interface CommerceAgreementStore {
|
|
72
|
+
create(a: CommerceAgreementInsert): Promise<CommerceAgreementRow | null>;
|
|
73
|
+
getById(id: string): Promise<CommerceAgreementRow | null>;
|
|
74
|
+
getByTaskId(taskId: string): Promise<CommerceAgreementRow | null>;
|
|
75
|
+
getByOfferId(offerId: string): Promise<CommerceAgreementRow | null>;
|
|
76
|
+
applyStatus(id: string, expectedFrom: string, to: string, patch?: AgreementStatusPatch): Promise<CommerceAgreementRow | null>;
|
|
77
|
+
}
|
|
78
|
+
export interface CommerceSubmissionInsert {
|
|
79
|
+
id: string;
|
|
80
|
+
agreementId: string;
|
|
81
|
+
providerWallet: string;
|
|
82
|
+
summary: string;
|
|
83
|
+
resultUrl?: string | null;
|
|
84
|
+
status: string;
|
|
85
|
+
reviewNote?: string | null;
|
|
86
|
+
submittedAtMs: number;
|
|
87
|
+
reviewedAtMs?: number | null;
|
|
88
|
+
payload: JsonValue;
|
|
89
|
+
}
|
|
90
|
+
export interface CommerceSubmissionRow extends CommerceSubmissionInsert {
|
|
91
|
+
createdAt: string;
|
|
92
|
+
updatedAt: string;
|
|
93
|
+
}
|
|
94
|
+
export interface SubmissionStatusPatch {
|
|
95
|
+
reviewNote?: string;
|
|
96
|
+
reviewedAtMs?: number;
|
|
97
|
+
}
|
|
98
|
+
export interface CommerceSubmissionStore {
|
|
99
|
+
create(s: CommerceSubmissionInsert): Promise<CommerceSubmissionRow | null>;
|
|
100
|
+
getById(id: string): Promise<CommerceSubmissionRow | null>;
|
|
101
|
+
listByAgreement(agreementId: string): Promise<CommerceSubmissionRow[]>;
|
|
102
|
+
applyStatus(id: string, expectedFrom: string, to: string, patch?: SubmissionStatusPatch): Promise<CommerceSubmissionRow | null>;
|
|
103
|
+
}
|
|
104
|
+
export interface CommerceEvidenceInsert {
|
|
105
|
+
id: string;
|
|
106
|
+
submissionId?: string | null;
|
|
107
|
+
agreementId?: string | null;
|
|
108
|
+
type: string;
|
|
109
|
+
url?: string | null;
|
|
110
|
+
hash?: string | null;
|
|
111
|
+
content?: string | null;
|
|
112
|
+
metadata?: JsonValue | null;
|
|
113
|
+
payload: JsonValue;
|
|
114
|
+
}
|
|
115
|
+
export interface CommerceEvidenceRow extends CommerceEvidenceInsert {
|
|
116
|
+
createdAt: string;
|
|
117
|
+
}
|
|
118
|
+
export interface CommerceEvidenceStore {
|
|
119
|
+
append(e: CommerceEvidenceInsert): Promise<CommerceEvidenceRow | null>;
|
|
120
|
+
getById(id: string): Promise<CommerceEvidenceRow | null>;
|
|
121
|
+
listBySubmission(submissionId: string): Promise<CommerceEvidenceRow[]>;
|
|
122
|
+
listByAgreement(agreementId: string): Promise<CommerceEvidenceRow[]>;
|
|
123
|
+
}
|
|
124
|
+
export interface CommerceEventInsert {
|
|
125
|
+
id: string;
|
|
126
|
+
entityType: string;
|
|
127
|
+
entityId: string;
|
|
128
|
+
eventType: string;
|
|
129
|
+
actorWallet?: string | null;
|
|
130
|
+
prevHash: string;
|
|
131
|
+
hash: string;
|
|
132
|
+
payload: JsonValue;
|
|
133
|
+
}
|
|
134
|
+
export interface CommerceEventRow extends CommerceEventInsert {
|
|
135
|
+
eventSeq: string;
|
|
136
|
+
createdAt: string;
|
|
137
|
+
}
|
|
138
|
+
export interface CommerceEventStore {
|
|
139
|
+
append(e: CommerceEventInsert): Promise<CommerceEventRow | null>;
|
|
140
|
+
getById(id: string): Promise<CommerceEventRow | null>;
|
|
141
|
+
latest(): Promise<CommerceEventRow | null>;
|
|
142
|
+
listByEntity(entityType: string, entityId: string): Promise<CommerceEventRow[]>;
|
|
143
|
+
list(opts?: {
|
|
144
|
+
entityType?: string;
|
|
145
|
+
limit?: number;
|
|
146
|
+
}): Promise<CommerceEventRow[]>;
|
|
147
|
+
}
|
|
148
|
+
export interface CommercePaymentInsert {
|
|
149
|
+
id: string;
|
|
150
|
+
agreementId: string;
|
|
151
|
+
mode?: string;
|
|
152
|
+
status: string;
|
|
153
|
+
amount?: string | null;
|
|
154
|
+
currency?: string;
|
|
155
|
+
paymentId?: string | null;
|
|
156
|
+
recordedBy?: string | null;
|
|
157
|
+
recordedAtMs?: number | null;
|
|
158
|
+
verifiedBy?: string | null;
|
|
159
|
+
verifiedAtMs?: number | null;
|
|
160
|
+
note?: string | null;
|
|
161
|
+
payload: JsonValue;
|
|
162
|
+
}
|
|
163
|
+
export interface CommercePaymentRow extends CommercePaymentInsert {
|
|
164
|
+
createdAt: string;
|
|
165
|
+
updatedAt: string;
|
|
166
|
+
}
|
|
167
|
+
export interface PaymentStatusPatch {
|
|
168
|
+
recordedBy?: string;
|
|
169
|
+
recordedAtMs?: number;
|
|
170
|
+
verifiedBy?: string;
|
|
171
|
+
verifiedAtMs?: number;
|
|
172
|
+
paymentId?: string | null;
|
|
173
|
+
note?: string;
|
|
174
|
+
}
|
|
175
|
+
export interface CommercePaymentStore {
|
|
176
|
+
create(p: CommercePaymentInsert): Promise<CommercePaymentRow | null>;
|
|
177
|
+
getById(id: string): Promise<CommercePaymentRow | null>;
|
|
178
|
+
getByAgreement(agreementId: string): Promise<CommercePaymentRow | null>;
|
|
179
|
+
applyStatus(id: string, expectedFrom: string, to: string, patch?: PaymentStatusPatch): Promise<CommercePaymentRow | null>;
|
|
180
|
+
}
|
|
181
|
+
export interface CommerceStores {
|
|
182
|
+
tasks: CommerceTaskStore;
|
|
183
|
+
offers: CommerceOfferStore;
|
|
184
|
+
agreements: CommerceAgreementStore;
|
|
185
|
+
submissions: CommerceSubmissionStore;
|
|
186
|
+
evidence: CommerceEvidenceStore;
|
|
187
|
+
events: CommerceEventStore;
|
|
188
|
+
payments: CommercePaymentStore;
|
|
189
|
+
}
|
|
190
|
+
export declare class InMemoryCommerceTaskStore implements CommerceTaskStore {
|
|
191
|
+
private readonly rows;
|
|
192
|
+
create(t: CommerceTaskInsert): Promise<CommerceTaskRow | null>;
|
|
193
|
+
getById(id: string): Promise<CommerceTaskRow | null>;
|
|
194
|
+
list(opts?: {
|
|
195
|
+
status?: string;
|
|
196
|
+
buyerWallet?: string;
|
|
197
|
+
limit?: number;
|
|
198
|
+
}): Promise<CommerceTaskRow[]>;
|
|
199
|
+
applyStatus(id: string, expectedFrom: string, to: string): Promise<CommerceTaskRow | null>;
|
|
200
|
+
}
|
|
201
|
+
export declare class InMemoryCommerceOfferStore implements CommerceOfferStore {
|
|
202
|
+
private readonly rows;
|
|
203
|
+
create(o: CommerceOfferInsert): Promise<CommerceOfferRow | null>;
|
|
204
|
+
getById(id: string): Promise<CommerceOfferRow | null>;
|
|
205
|
+
listByTask(taskId: string): Promise<CommerceOfferRow[]>;
|
|
206
|
+
applyStatus(id: string, expectedFrom: string, to: string): Promise<CommerceOfferRow | null>;
|
|
207
|
+
}
|
|
208
|
+
export declare class InMemoryCommerceAgreementStore implements CommerceAgreementStore {
|
|
209
|
+
private readonly rows;
|
|
210
|
+
create(a: CommerceAgreementInsert): Promise<CommerceAgreementRow | null>;
|
|
211
|
+
getById(id: string): Promise<CommerceAgreementRow | null>;
|
|
212
|
+
getByTaskId(taskId: string): Promise<CommerceAgreementRow | null>;
|
|
213
|
+
getByOfferId(offerId: string): Promise<CommerceAgreementRow | null>;
|
|
214
|
+
applyStatus(id: string, expectedFrom: string, to: string, patch?: AgreementStatusPatch): Promise<CommerceAgreementRow | null>;
|
|
215
|
+
}
|
|
216
|
+
export declare class InMemoryCommerceSubmissionStore implements CommerceSubmissionStore {
|
|
217
|
+
private readonly rows;
|
|
218
|
+
create(s: CommerceSubmissionInsert): Promise<CommerceSubmissionRow | null>;
|
|
219
|
+
getById(id: string): Promise<CommerceSubmissionRow | null>;
|
|
220
|
+
listByAgreement(agreementId: string): Promise<CommerceSubmissionRow[]>;
|
|
221
|
+
applyStatus(id: string, expectedFrom: string, to: string, patch?: SubmissionStatusPatch): Promise<CommerceSubmissionRow | null>;
|
|
222
|
+
}
|
|
223
|
+
export declare class InMemoryCommerceEvidenceStore implements CommerceEvidenceStore {
|
|
224
|
+
private readonly rows;
|
|
225
|
+
append(e: CommerceEvidenceInsert): Promise<CommerceEvidenceRow | null>;
|
|
226
|
+
getById(id: string): Promise<CommerceEvidenceRow | null>;
|
|
227
|
+
listBySubmission(submissionId: string): Promise<CommerceEvidenceRow[]>;
|
|
228
|
+
listByAgreement(agreementId: string): Promise<CommerceEvidenceRow[]>;
|
|
229
|
+
}
|
|
230
|
+
export declare class InMemoryCommerceEventStore implements CommerceEventStore {
|
|
231
|
+
private readonly rows;
|
|
232
|
+
private seq;
|
|
233
|
+
append(e: CommerceEventInsert): Promise<CommerceEventRow | null>;
|
|
234
|
+
getById(id: string): Promise<CommerceEventRow | null>;
|
|
235
|
+
latest(): Promise<CommerceEventRow | null>;
|
|
236
|
+
listByEntity(entityType: string, entityId: string): Promise<CommerceEventRow[]>;
|
|
237
|
+
list(opts?: {
|
|
238
|
+
entityType?: string;
|
|
239
|
+
limit?: number;
|
|
240
|
+
}): Promise<CommerceEventRow[]>;
|
|
241
|
+
}
|
|
242
|
+
export declare class InMemoryCommercePaymentStore implements CommercePaymentStore {
|
|
243
|
+
private readonly rows;
|
|
244
|
+
create(p: CommercePaymentInsert): Promise<CommercePaymentRow | null>;
|
|
245
|
+
getById(id: string): Promise<CommercePaymentRow | null>;
|
|
246
|
+
getByAgreement(agreementId: string): Promise<CommercePaymentRow | null>;
|
|
247
|
+
applyStatus(id: string, expectedFrom: string, to: string, patch?: PaymentStatusPatch): Promise<CommercePaymentRow | null>;
|
|
248
|
+
}
|
|
249
|
+
export declare function createInMemoryCommerceStores(): CommerceStores;
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
const nowIso = () => new Date().toISOString();
|
|
2
|
+
export class InMemoryCommerceTaskStore {
|
|
3
|
+
rows = new Map();
|
|
4
|
+
async create(t) {
|
|
5
|
+
if (this.rows.has(t.id))
|
|
6
|
+
return null;
|
|
7
|
+
const ts = nowIso();
|
|
8
|
+
const row = {
|
|
9
|
+
...t,
|
|
10
|
+
buyerAgentId: t.buyerAgentId ?? null,
|
|
11
|
+
requirements: t.requirements ?? null,
|
|
12
|
+
budget: t.budget ?? null,
|
|
13
|
+
currency: t.currency ?? "USD",
|
|
14
|
+
expiresAtMs: t.expiresAtMs ?? null,
|
|
15
|
+
createdAt: ts,
|
|
16
|
+
updatedAt: ts,
|
|
17
|
+
};
|
|
18
|
+
this.rows.set(row.id, row);
|
|
19
|
+
return row;
|
|
20
|
+
}
|
|
21
|
+
async getById(id) {
|
|
22
|
+
return this.rows.get(id) ?? null;
|
|
23
|
+
}
|
|
24
|
+
async list(opts) {
|
|
25
|
+
let out = [...this.rows.values()].filter((r) => (opts?.status === undefined || r.status === opts.status) &&
|
|
26
|
+
(opts?.buyerWallet === undefined || r.buyerWallet === opts.buyerWallet));
|
|
27
|
+
out = out.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
28
|
+
if (opts?.limit !== undefined)
|
|
29
|
+
out = out.slice(0, opts.limit);
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
async applyStatus(id, expectedFrom, to) {
|
|
33
|
+
const row = this.rows.get(id);
|
|
34
|
+
if (!row || row.status !== expectedFrom)
|
|
35
|
+
return null;
|
|
36
|
+
const next = { ...row, status: to, updatedAt: nowIso() };
|
|
37
|
+
this.rows.set(id, next);
|
|
38
|
+
return next;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export class InMemoryCommerceOfferStore {
|
|
42
|
+
rows = new Map();
|
|
43
|
+
async create(o) {
|
|
44
|
+
if (this.rows.has(o.id))
|
|
45
|
+
return null;
|
|
46
|
+
const ts = nowIso();
|
|
47
|
+
const row = {
|
|
48
|
+
...o,
|
|
49
|
+
providerAgentId: o.providerAgentId ?? null,
|
|
50
|
+
currency: o.currency ?? "USD",
|
|
51
|
+
eta: o.eta ?? null,
|
|
52
|
+
createdAt: ts,
|
|
53
|
+
updatedAt: ts,
|
|
54
|
+
};
|
|
55
|
+
this.rows.set(row.id, row);
|
|
56
|
+
return row;
|
|
57
|
+
}
|
|
58
|
+
async getById(id) {
|
|
59
|
+
return this.rows.get(id) ?? null;
|
|
60
|
+
}
|
|
61
|
+
async listByTask(taskId) {
|
|
62
|
+
return [...this.rows.values()]
|
|
63
|
+
.filter((r) => r.taskId === taskId)
|
|
64
|
+
.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
|
|
65
|
+
}
|
|
66
|
+
async applyStatus(id, expectedFrom, to) {
|
|
67
|
+
const row = this.rows.get(id);
|
|
68
|
+
if (!row || row.status !== expectedFrom)
|
|
69
|
+
return null;
|
|
70
|
+
const next = { ...row, status: to, updatedAt: nowIso() };
|
|
71
|
+
this.rows.set(id, next);
|
|
72
|
+
return next;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export class InMemoryCommerceAgreementStore {
|
|
76
|
+
rows = new Map();
|
|
77
|
+
async create(a) {
|
|
78
|
+
if (this.rows.has(a.id))
|
|
79
|
+
return null;
|
|
80
|
+
for (const x of this.rows.values()) {
|
|
81
|
+
if (x.taskId === a.taskId || x.offerId === a.offerId)
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const ts = nowIso();
|
|
85
|
+
const row = {
|
|
86
|
+
...a,
|
|
87
|
+
currency: a.currency ?? "USD",
|
|
88
|
+
acceptanceCriteria: a.acceptanceCriteria ?? null,
|
|
89
|
+
completedAtMs: a.completedAtMs ?? null,
|
|
90
|
+
createdAt: ts,
|
|
91
|
+
updatedAt: ts,
|
|
92
|
+
};
|
|
93
|
+
this.rows.set(row.id, row);
|
|
94
|
+
return row;
|
|
95
|
+
}
|
|
96
|
+
async getById(id) {
|
|
97
|
+
return this.rows.get(id) ?? null;
|
|
98
|
+
}
|
|
99
|
+
async getByTaskId(taskId) {
|
|
100
|
+
return [...this.rows.values()].find((r) => r.taskId === taskId) ?? null;
|
|
101
|
+
}
|
|
102
|
+
async getByOfferId(offerId) {
|
|
103
|
+
return [...this.rows.values()].find((r) => r.offerId === offerId) ?? null;
|
|
104
|
+
}
|
|
105
|
+
async applyStatus(id, expectedFrom, to, patch) {
|
|
106
|
+
const row = this.rows.get(id);
|
|
107
|
+
if (!row || row.status !== expectedFrom)
|
|
108
|
+
return null;
|
|
109
|
+
const next = {
|
|
110
|
+
...row,
|
|
111
|
+
status: to,
|
|
112
|
+
...(patch?.completedAtMs !== undefined
|
|
113
|
+
? { completedAtMs: patch.completedAtMs }
|
|
114
|
+
: {}),
|
|
115
|
+
updatedAt: nowIso(),
|
|
116
|
+
};
|
|
117
|
+
this.rows.set(id, next);
|
|
118
|
+
return next;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
export class InMemoryCommerceSubmissionStore {
|
|
122
|
+
rows = new Map();
|
|
123
|
+
async create(s) {
|
|
124
|
+
if (this.rows.has(s.id))
|
|
125
|
+
return null;
|
|
126
|
+
const ts = nowIso();
|
|
127
|
+
const row = {
|
|
128
|
+
...s,
|
|
129
|
+
resultUrl: s.resultUrl ?? null,
|
|
130
|
+
reviewNote: s.reviewNote ?? null,
|
|
131
|
+
reviewedAtMs: s.reviewedAtMs ?? null,
|
|
132
|
+
createdAt: ts,
|
|
133
|
+
updatedAt: ts,
|
|
134
|
+
};
|
|
135
|
+
this.rows.set(row.id, row);
|
|
136
|
+
return row;
|
|
137
|
+
}
|
|
138
|
+
async getById(id) {
|
|
139
|
+
return this.rows.get(id) ?? null;
|
|
140
|
+
}
|
|
141
|
+
async listByAgreement(agreementId) {
|
|
142
|
+
return [...this.rows.values()]
|
|
143
|
+
.filter((r) => r.agreementId === agreementId)
|
|
144
|
+
.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
|
|
145
|
+
}
|
|
146
|
+
async applyStatus(id, expectedFrom, to, patch) {
|
|
147
|
+
const row = this.rows.get(id);
|
|
148
|
+
if (!row || row.status !== expectedFrom)
|
|
149
|
+
return null;
|
|
150
|
+
const next = {
|
|
151
|
+
...row,
|
|
152
|
+
status: to,
|
|
153
|
+
...(patch?.reviewNote !== undefined ? { reviewNote: patch.reviewNote } : {}),
|
|
154
|
+
...(patch?.reviewedAtMs !== undefined
|
|
155
|
+
? { reviewedAtMs: patch.reviewedAtMs }
|
|
156
|
+
: {}),
|
|
157
|
+
updatedAt: nowIso(),
|
|
158
|
+
};
|
|
159
|
+
this.rows.set(id, next);
|
|
160
|
+
return next;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
export class InMemoryCommerceEvidenceStore {
|
|
164
|
+
rows = new Map();
|
|
165
|
+
async append(e) {
|
|
166
|
+
if (this.rows.has(e.id))
|
|
167
|
+
return null;
|
|
168
|
+
const row = {
|
|
169
|
+
...e,
|
|
170
|
+
submissionId: e.submissionId ?? null,
|
|
171
|
+
agreementId: e.agreementId ?? null,
|
|
172
|
+
url: e.url ?? null,
|
|
173
|
+
hash: e.hash ?? null,
|
|
174
|
+
content: e.content ?? null,
|
|
175
|
+
metadata: e.metadata ?? null,
|
|
176
|
+
createdAt: nowIso(),
|
|
177
|
+
};
|
|
178
|
+
this.rows.set(row.id, row);
|
|
179
|
+
return row;
|
|
180
|
+
}
|
|
181
|
+
async getById(id) {
|
|
182
|
+
return this.rows.get(id) ?? null;
|
|
183
|
+
}
|
|
184
|
+
async listBySubmission(submissionId) {
|
|
185
|
+
return [...this.rows.values()]
|
|
186
|
+
.filter((r) => r.submissionId === submissionId)
|
|
187
|
+
.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
|
|
188
|
+
}
|
|
189
|
+
async listByAgreement(agreementId) {
|
|
190
|
+
return [...this.rows.values()]
|
|
191
|
+
.filter((r) => r.agreementId === agreementId)
|
|
192
|
+
.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
export class InMemoryCommerceEventStore {
|
|
196
|
+
rows = new Map();
|
|
197
|
+
seq = 0n;
|
|
198
|
+
async append(e) {
|
|
199
|
+
if (this.rows.has(e.id))
|
|
200
|
+
return null;
|
|
201
|
+
const row = {
|
|
202
|
+
...e,
|
|
203
|
+
actorWallet: e.actorWallet ?? null,
|
|
204
|
+
eventSeq: String(++this.seq),
|
|
205
|
+
createdAt: nowIso(),
|
|
206
|
+
};
|
|
207
|
+
this.rows.set(row.id, row);
|
|
208
|
+
return row;
|
|
209
|
+
}
|
|
210
|
+
async getById(id) {
|
|
211
|
+
return this.rows.get(id) ?? null;
|
|
212
|
+
}
|
|
213
|
+
async latest() {
|
|
214
|
+
let head = null;
|
|
215
|
+
for (const r of this.rows.values()) {
|
|
216
|
+
if (head === null || BigInt(r.eventSeq) > BigInt(head.eventSeq)) {
|
|
217
|
+
head = r;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return head;
|
|
221
|
+
}
|
|
222
|
+
async listByEntity(entityType, entityId) {
|
|
223
|
+
return [...this.rows.values()]
|
|
224
|
+
.filter((r) => r.entityType === entityType && r.entityId === entityId)
|
|
225
|
+
.sort((a, b) => BigInt(a.eventSeq) < BigInt(b.eventSeq)
|
|
226
|
+
? -1
|
|
227
|
+
: BigInt(a.eventSeq) > BigInt(b.eventSeq)
|
|
228
|
+
? 1
|
|
229
|
+
: 0);
|
|
230
|
+
}
|
|
231
|
+
async list(opts) {
|
|
232
|
+
let out = [...this.rows.values()]
|
|
233
|
+
.filter((r) => opts?.entityType === undefined || r.entityType === opts.entityType)
|
|
234
|
+
.sort((a, b) => BigInt(a.eventSeq) > BigInt(b.eventSeq)
|
|
235
|
+
? -1
|
|
236
|
+
: BigInt(a.eventSeq) < BigInt(b.eventSeq)
|
|
237
|
+
? 1
|
|
238
|
+
: 0);
|
|
239
|
+
if (opts?.limit !== undefined)
|
|
240
|
+
out = out.slice(0, opts.limit);
|
|
241
|
+
return out;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
export class InMemoryCommercePaymentStore {
|
|
245
|
+
rows = new Map();
|
|
246
|
+
async create(p) {
|
|
247
|
+
if (this.rows.has(p.id))
|
|
248
|
+
return null;
|
|
249
|
+
for (const x of this.rows.values()) {
|
|
250
|
+
if (x.agreementId === p.agreementId)
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
const ts = nowIso();
|
|
254
|
+
const row = {
|
|
255
|
+
...p,
|
|
256
|
+
mode: p.mode ?? "RECORD_ONLY",
|
|
257
|
+
amount: p.amount ?? null,
|
|
258
|
+
currency: p.currency ?? "USD",
|
|
259
|
+
paymentId: p.paymentId ?? null,
|
|
260
|
+
recordedBy: p.recordedBy ?? null,
|
|
261
|
+
recordedAtMs: p.recordedAtMs ?? null,
|
|
262
|
+
verifiedBy: p.verifiedBy ?? null,
|
|
263
|
+
verifiedAtMs: p.verifiedAtMs ?? null,
|
|
264
|
+
note: p.note ?? null,
|
|
265
|
+
createdAt: ts,
|
|
266
|
+
updatedAt: ts,
|
|
267
|
+
};
|
|
268
|
+
this.rows.set(row.id, row);
|
|
269
|
+
return row;
|
|
270
|
+
}
|
|
271
|
+
async getById(id) {
|
|
272
|
+
return this.rows.get(id) ?? null;
|
|
273
|
+
}
|
|
274
|
+
async getByAgreement(agreementId) {
|
|
275
|
+
return ([...this.rows.values()].find((r) => r.agreementId === agreementId) ??
|
|
276
|
+
null);
|
|
277
|
+
}
|
|
278
|
+
async applyStatus(id, expectedFrom, to, patch) {
|
|
279
|
+
const row = this.rows.get(id);
|
|
280
|
+
if (!row || row.status !== expectedFrom)
|
|
281
|
+
return null;
|
|
282
|
+
const next = {
|
|
283
|
+
...row,
|
|
284
|
+
status: to,
|
|
285
|
+
...(patch?.recordedBy !== undefined ? { recordedBy: patch.recordedBy } : {}),
|
|
286
|
+
...(patch?.recordedAtMs !== undefined
|
|
287
|
+
? { recordedAtMs: patch.recordedAtMs }
|
|
288
|
+
: {}),
|
|
289
|
+
...(patch?.verifiedBy !== undefined ? { verifiedBy: patch.verifiedBy } : {}),
|
|
290
|
+
...(patch?.verifiedAtMs !== undefined
|
|
291
|
+
? { verifiedAtMs: patch.verifiedAtMs }
|
|
292
|
+
: {}),
|
|
293
|
+
...(patch?.paymentId !== undefined ? { paymentId: patch.paymentId } : {}),
|
|
294
|
+
...(patch?.note !== undefined ? { note: patch.note } : {}),
|
|
295
|
+
updatedAt: nowIso(),
|
|
296
|
+
};
|
|
297
|
+
this.rows.set(id, next);
|
|
298
|
+
return next;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
export function createInMemoryCommerceStores() {
|
|
302
|
+
return {
|
|
303
|
+
tasks: new InMemoryCommerceTaskStore(),
|
|
304
|
+
offers: new InMemoryCommerceOfferStore(),
|
|
305
|
+
agreements: new InMemoryCommerceAgreementStore(),
|
|
306
|
+
submissions: new InMemoryCommerceSubmissionStore(),
|
|
307
|
+
evidence: new InMemoryCommerceEvidenceStore(),
|
|
308
|
+
events: new InMemoryCommerceEventStore(),
|
|
309
|
+
payments: new InMemoryCommercePaymentStore(),
|
|
310
|
+
};
|
|
311
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
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";
|
|
2
|
+
import type { AttemptStatusPatch, PaymentAttemptInsert, PaymentAttemptRow, PaymentAttemptStore, PaymentEventInsert, PaymentEventRow, PaymentEventStore, PaymentExceptionInsert, PaymentExceptionRow, PaymentExceptionStore, PaymentInsert, PaymentPriceInsert, PaymentPriceRow, PaymentPriceStore, PaymentProductInsert, PaymentProductRow, PaymentProductStore, PaymentReceiptInsert, PaymentReceiptRow, PaymentReceiptStore, PaymentRow, PaymentStore, PaymentStores, UsageRecordInsert, UsageRecordRow, UsageRecordStore } from "./payment-store.js";
|
|
3
3
|
export declare class PaymentRepository implements PaymentStore {
|
|
4
4
|
private readonly db;
|
|
5
5
|
constructor(db: PrismaDb);
|
|
@@ -24,6 +24,8 @@ export declare class PaymentAttemptRepository implements PaymentAttemptStore {
|
|
|
24
24
|
getByTxHash(transactionHash: string): Promise<PaymentAttemptRow | null>;
|
|
25
25
|
applyStatus(id: string, expectedFrom: string, to: string, _updatedAtMs: number, patch?: AttemptStatusPatch): Promise<PaymentAttemptRow | null>;
|
|
26
26
|
listExpired(nowMs: number): Promise<PaymentAttemptRow[]>;
|
|
27
|
+
listPayable(nowMs: number): Promise<PaymentAttemptRow[]>;
|
|
28
|
+
listRecentTerminal(sinceMs: number): Promise<PaymentAttemptRow[]>;
|
|
27
29
|
}
|
|
28
30
|
export declare class PaymentReceiptRepository implements PaymentReceiptStore {
|
|
29
31
|
private readonly db;
|
|
@@ -63,4 +65,30 @@ export declare class PaymentPriceRepository implements PaymentPriceStore {
|
|
|
63
65
|
activeOnly?: boolean;
|
|
64
66
|
}): Promise<PaymentPriceRow[]>;
|
|
65
67
|
}
|
|
68
|
+
export declare class PaymentExceptionRepository implements PaymentExceptionStore {
|
|
69
|
+
private readonly db;
|
|
70
|
+
constructor(db: PrismaDb);
|
|
71
|
+
private get orm();
|
|
72
|
+
append(e: PaymentExceptionInsert): Promise<PaymentExceptionRow | null>;
|
|
73
|
+
getById(id: string): Promise<PaymentExceptionRow | null>;
|
|
74
|
+
listByPayment(paymentId: string): Promise<PaymentExceptionRow[]>;
|
|
75
|
+
list(opts?: {
|
|
76
|
+
status?: string;
|
|
77
|
+
kind?: string;
|
|
78
|
+
sinceMs?: number;
|
|
79
|
+
limit?: number;
|
|
80
|
+
}): Promise<PaymentExceptionRow[]>;
|
|
81
|
+
resolve(id: string, status: "RESOLVED" | "IGNORED", note?: string): Promise<PaymentExceptionRow | null>;
|
|
82
|
+
}
|
|
83
|
+
export declare class UsageRecordRepository implements UsageRecordStore {
|
|
84
|
+
private readonly db;
|
|
85
|
+
constructor(db: PrismaDb);
|
|
86
|
+
private get orm();
|
|
87
|
+
append(r: UsageRecordInsert): Promise<UsageRecordRow | null>;
|
|
88
|
+
getById(id: string): Promise<UsageRecordRow | null>;
|
|
89
|
+
getByRequestId(requestId: string): Promise<UsageRecordRow | null>;
|
|
90
|
+
listByPayment(paymentId: string): Promise<UsageRecordRow[]>;
|
|
91
|
+
listByAgent(agentId: string, limit?: number): Promise<UsageRecordRow[]>;
|
|
92
|
+
listByResource(resource: string, limit?: number): Promise<UsageRecordRow[]>;
|
|
93
|
+
}
|
|
66
94
|
export declare function createPrismaPaymentStores(db: PrismaDb): PaymentStores;
|