@agentbadge/database 0.6.3 → 0.6.5
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 +2 -0
- package/dist/database.js +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/prisma/contract.json +3757 -1872
- package/dist/repositories/agent-survey-repository.d.ts +3 -0
- package/dist/repositories/agent-survey-repository.js +8 -0
- package/dist/repositories/commerce-repository.d.ts +77 -0
- package/dist/repositories/commerce-repository.js +528 -0
- package/dist/repositories/commerce-store.d.ts +253 -0
- package/dist/repositories/commerce-store.js +337 -0
- package/dist/repositories/event-repository.d.ts +1 -0
- package/dist/repositories/event-repository.js +30 -2
- package/dist/store.d.ts +2 -0
- package/dist/store.js +3 -0
- package/package.json +1 -1
|
@@ -16,8 +16,10 @@ export interface SurveyAnswerUpsert {
|
|
|
16
16
|
surveyId: string;
|
|
17
17
|
agentId?: string | null;
|
|
18
18
|
fpHash?: string | null;
|
|
19
|
+
softFpHash?: string | null;
|
|
19
20
|
answers: JsonValue;
|
|
20
21
|
rated?: boolean;
|
|
22
|
+
flag?: string | null;
|
|
21
23
|
createdAtMs: number;
|
|
22
24
|
}
|
|
23
25
|
export declare class AgentSurveyRepository {
|
|
@@ -31,6 +33,7 @@ export declare class AgentSurveyRepository {
|
|
|
31
33
|
getAnswer(id: string): Promise<AnswerRow | null>;
|
|
32
34
|
findAnswerByAgent(surveyId: string, agentId: string): Promise<AnswerRow | null>;
|
|
33
35
|
findAnswerByFp(surveyId: string, fpHash: string): Promise<AnswerRow | null>;
|
|
36
|
+
findAnswerBySoftFp(surveyId: string, softFpHash: string): Promise<AnswerRow | null>;
|
|
34
37
|
listAnswers(surveyId: string, limit?: number): Promise<AnswerRow[]>;
|
|
35
38
|
markRated(id: string): Promise<boolean>;
|
|
36
39
|
}
|
|
@@ -46,8 +46,10 @@ export class AgentSurveyRepository {
|
|
|
46
46
|
surveyId: a.surveyId,
|
|
47
47
|
agentId: a.agentId ?? null,
|
|
48
48
|
fpHash: a.fpHash ?? null,
|
|
49
|
+
softFpHash: a.softFpHash ?? null,
|
|
49
50
|
answers: a.answers,
|
|
50
51
|
rated: a.rated ?? false,
|
|
52
|
+
flag: a.flag ?? null,
|
|
51
53
|
createdAtMs: BigInt(a.createdAtMs),
|
|
52
54
|
});
|
|
53
55
|
}
|
|
@@ -66,6 +68,12 @@ export class AgentSurveyRepository {
|
|
|
66
68
|
}).all();
|
|
67
69
|
return rows.find((r) => r.fpHash === fpHash && r.agentId === null) ?? null;
|
|
68
70
|
}
|
|
71
|
+
async findAnswerBySoftFp(surveyId, softFpHash) {
|
|
72
|
+
const rows = await this.db.orm.public.SurveyAnswer.where({
|
|
73
|
+
surveyId,
|
|
74
|
+
}).all();
|
|
75
|
+
return rows.find((r) => r.softFpHash === softFpHash) ?? null;
|
|
76
|
+
}
|
|
69
77
|
async listAnswers(surveyId, limit = 500) {
|
|
70
78
|
return this.db.orm.public.SurveyAnswer.where({ surveyId })
|
|
71
79
|
.orderBy((a) => a.createdAtMs.desc())
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { PrismaDb } from "./event-repository.js";
|
|
2
|
+
import type { AgreementStatusPatch, CommerceAgreementInsert, CommerceAgreementRow, CommerceAgreementStore, CommerceEvidenceInsert, CommerceEvidenceRow, CommerceEvidenceStore, CommerceEventInsert, CommerceEventRow, CommerceEventStore, CommerceOfferInsert, CommerceOfferRow, CommerceOfferStore, CommercePaymentInsert, CommercePaymentRow, CommercePaymentStore, CommerceStores, CommerceSubmissionInsert, CommerceSubmissionRow, CommerceSubmissionStore, CommerceTaskInsert, CommerceTaskRow, CommerceTaskStore, PaymentStatusPatch, SubmissionStatusPatch } from "./commerce-store.js";
|
|
3
|
+
export declare const isUniqueViolation: (err: unknown) => boolean;
|
|
4
|
+
export declare class CommerceTaskRepository implements CommerceTaskStore {
|
|
5
|
+
private readonly db;
|
|
6
|
+
constructor(db: PrismaDb);
|
|
7
|
+
private get orm();
|
|
8
|
+
create(t: CommerceTaskInsert): Promise<CommerceTaskRow | null>;
|
|
9
|
+
getById(id: string): Promise<CommerceTaskRow | null>;
|
|
10
|
+
list(opts?: {
|
|
11
|
+
status?: string;
|
|
12
|
+
buyerWallet?: string;
|
|
13
|
+
visibleTo?: string;
|
|
14
|
+
limit?: number;
|
|
15
|
+
}): Promise<CommerceTaskRow[]>;
|
|
16
|
+
applyStatus(id: string, expectedFrom: string, to: string): Promise<CommerceTaskRow | null>;
|
|
17
|
+
}
|
|
18
|
+
export declare class CommerceOfferRepository implements CommerceOfferStore {
|
|
19
|
+
private readonly db;
|
|
20
|
+
constructor(db: PrismaDb);
|
|
21
|
+
private get orm();
|
|
22
|
+
create(o: CommerceOfferInsert): Promise<CommerceOfferRow | null>;
|
|
23
|
+
getById(id: string): Promise<CommerceOfferRow | null>;
|
|
24
|
+
listByTask(taskId: string): Promise<CommerceOfferRow[]>;
|
|
25
|
+
applyStatus(id: string, expectedFrom: string, to: string): Promise<CommerceOfferRow | null>;
|
|
26
|
+
}
|
|
27
|
+
export declare class CommerceAgreementRepository implements CommerceAgreementStore {
|
|
28
|
+
private readonly db;
|
|
29
|
+
constructor(db: PrismaDb);
|
|
30
|
+
private get orm();
|
|
31
|
+
create(a: CommerceAgreementInsert): Promise<CommerceAgreementRow | null>;
|
|
32
|
+
getById(id: string): Promise<CommerceAgreementRow | null>;
|
|
33
|
+
getByTaskId(taskId: string): Promise<CommerceAgreementRow | null>;
|
|
34
|
+
getByOfferId(offerId: string): Promise<CommerceAgreementRow | null>;
|
|
35
|
+
applyStatus(id: string, expectedFrom: string, to: string, patch?: AgreementStatusPatch): Promise<CommerceAgreementRow | null>;
|
|
36
|
+
}
|
|
37
|
+
export declare class CommerceSubmissionRepository implements CommerceSubmissionStore {
|
|
38
|
+
private readonly db;
|
|
39
|
+
constructor(db: PrismaDb);
|
|
40
|
+
private get orm();
|
|
41
|
+
create(s: CommerceSubmissionInsert): Promise<CommerceSubmissionRow | null>;
|
|
42
|
+
getById(id: string): Promise<CommerceSubmissionRow | null>;
|
|
43
|
+
listByAgreement(agreementId: string): Promise<CommerceSubmissionRow[]>;
|
|
44
|
+
applyStatus(id: string, expectedFrom: string, to: string, patch?: SubmissionStatusPatch): Promise<CommerceSubmissionRow | null>;
|
|
45
|
+
}
|
|
46
|
+
export declare class CommerceEvidenceRepository implements CommerceEvidenceStore {
|
|
47
|
+
private readonly db;
|
|
48
|
+
constructor(db: PrismaDb);
|
|
49
|
+
private get orm();
|
|
50
|
+
append(e: CommerceEvidenceInsert): Promise<CommerceEvidenceRow | null>;
|
|
51
|
+
getById(id: string): Promise<CommerceEvidenceRow | null>;
|
|
52
|
+
listBySubmission(submissionId: string): Promise<CommerceEvidenceRow[]>;
|
|
53
|
+
listByAgreement(agreementId: string): Promise<CommerceEvidenceRow[]>;
|
|
54
|
+
}
|
|
55
|
+
export declare class CommerceEventRepository implements CommerceEventStore {
|
|
56
|
+
private readonly db;
|
|
57
|
+
constructor(db: PrismaDb);
|
|
58
|
+
private get orm();
|
|
59
|
+
append(e: CommerceEventInsert): Promise<CommerceEventRow | null>;
|
|
60
|
+
getById(id: string): Promise<CommerceEventRow | null>;
|
|
61
|
+
latest(): Promise<CommerceEventRow | null>;
|
|
62
|
+
listByEntity(entityType: string, entityId: string): Promise<CommerceEventRow[]>;
|
|
63
|
+
list(opts?: {
|
|
64
|
+
entityType?: string;
|
|
65
|
+
limit?: number;
|
|
66
|
+
}): Promise<CommerceEventRow[]>;
|
|
67
|
+
}
|
|
68
|
+
export declare class CommercePaymentRepository implements CommercePaymentStore {
|
|
69
|
+
private readonly db;
|
|
70
|
+
constructor(db: PrismaDb);
|
|
71
|
+
private get orm();
|
|
72
|
+
create(p: CommercePaymentInsert): Promise<CommercePaymentRow | null>;
|
|
73
|
+
getById(id: string): Promise<CommercePaymentRow | null>;
|
|
74
|
+
getByAgreement(agreementId: string): Promise<CommercePaymentRow | null>;
|
|
75
|
+
applyStatus(id: string, expectedFrom: string, to: string, patch?: PaymentStatusPatch): Promise<CommercePaymentRow | null>;
|
|
76
|
+
}
|
|
77
|
+
export declare function createPrismaCommerceStores(db: PrismaDb): CommerceStores;
|
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
export const isUniqueViolation = (err) => typeof err === "object" &&
|
|
2
|
+
err !== null &&
|
|
3
|
+
err.sqlState === "23505";
|
|
4
|
+
async function catchUnique(p) {
|
|
5
|
+
try {
|
|
6
|
+
return await p;
|
|
7
|
+
}
|
|
8
|
+
catch (err) {
|
|
9
|
+
if (isUniqueViolation(err))
|
|
10
|
+
return null;
|
|
11
|
+
throw err;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const dec = (v) => String(v);
|
|
15
|
+
const decOrNull = (v) => v === null || v === undefined ? null : String(v);
|
|
16
|
+
const numOrNull = (v) => v === null || v === undefined ? null : Number(v);
|
|
17
|
+
const toTaskRow = (r) => ({
|
|
18
|
+
id: r.id,
|
|
19
|
+
buyerWallet: r.buyerWallet,
|
|
20
|
+
buyerAgentId: r.buyerAgentId,
|
|
21
|
+
title: r.title,
|
|
22
|
+
description: r.description,
|
|
23
|
+
requirements: r.requirements,
|
|
24
|
+
budget: decOrNull(r.budget),
|
|
25
|
+
currency: r.currency,
|
|
26
|
+
expiresAtMs: numOrNull(r.expiresAtMs),
|
|
27
|
+
status: r.status,
|
|
28
|
+
payload: r.payload,
|
|
29
|
+
createdAt: r.createdAt,
|
|
30
|
+
updatedAt: r.updatedAt,
|
|
31
|
+
});
|
|
32
|
+
const toOfferRow = (r) => ({
|
|
33
|
+
id: r.id,
|
|
34
|
+
taskId: r.taskId,
|
|
35
|
+
providerWallet: r.providerWallet,
|
|
36
|
+
providerAgentId: r.providerAgentId,
|
|
37
|
+
price: dec(r.price),
|
|
38
|
+
currency: r.currency,
|
|
39
|
+
eta: r.eta,
|
|
40
|
+
proposal: r.proposal,
|
|
41
|
+
status: r.status,
|
|
42
|
+
payload: r.payload,
|
|
43
|
+
createdAt: r.createdAt,
|
|
44
|
+
updatedAt: r.updatedAt,
|
|
45
|
+
});
|
|
46
|
+
const toAgreementRow = (r) => ({
|
|
47
|
+
id: r.id,
|
|
48
|
+
taskId: r.taskId,
|
|
49
|
+
offerId: r.offerId,
|
|
50
|
+
buyerWallet: r.buyerWallet,
|
|
51
|
+
providerWallet: r.providerWallet,
|
|
52
|
+
agreedAmount: dec(r.agreedAmount),
|
|
53
|
+
currency: r.currency,
|
|
54
|
+
acceptanceCriteria: r.acceptanceCriteria,
|
|
55
|
+
status: r.status,
|
|
56
|
+
completedAtMs: numOrNull(r.completedAtMs),
|
|
57
|
+
payload: r.payload,
|
|
58
|
+
createdAt: r.createdAt,
|
|
59
|
+
updatedAt: r.updatedAt,
|
|
60
|
+
});
|
|
61
|
+
const toSubmissionRow = (r) => ({
|
|
62
|
+
id: r.id,
|
|
63
|
+
agreementId: r.agreementId,
|
|
64
|
+
providerWallet: r.providerWallet,
|
|
65
|
+
summary: r.summary,
|
|
66
|
+
resultUrl: r.resultUrl,
|
|
67
|
+
status: r.status,
|
|
68
|
+
reviewNote: r.reviewNote,
|
|
69
|
+
submittedAtMs: Number(r.submittedAtMs),
|
|
70
|
+
reviewedAtMs: numOrNull(r.reviewedAtMs),
|
|
71
|
+
payload: r.payload,
|
|
72
|
+
createdAt: r.createdAt,
|
|
73
|
+
updatedAt: r.updatedAt,
|
|
74
|
+
});
|
|
75
|
+
const toEvidenceRow = (r) => ({
|
|
76
|
+
id: r.id,
|
|
77
|
+
submissionId: r.submissionId,
|
|
78
|
+
agreementId: r.agreementId,
|
|
79
|
+
type: r.type,
|
|
80
|
+
url: r.url,
|
|
81
|
+
hash: r.hash,
|
|
82
|
+
content: r.content,
|
|
83
|
+
metadata: r.metadata,
|
|
84
|
+
payload: r.payload,
|
|
85
|
+
createdAt: r.createdAt,
|
|
86
|
+
});
|
|
87
|
+
const toEventRow = (r) => ({
|
|
88
|
+
id: r.id,
|
|
89
|
+
entityType: r.entityType,
|
|
90
|
+
entityId: r.entityId,
|
|
91
|
+
eventType: r.eventType,
|
|
92
|
+
actorWallet: r.actorWallet,
|
|
93
|
+
prevHash: r.prevHash,
|
|
94
|
+
hash: r.hash,
|
|
95
|
+
payload: r.payload,
|
|
96
|
+
eventSeq: String(r.eventSeq),
|
|
97
|
+
createdAt: r.createdAt,
|
|
98
|
+
});
|
|
99
|
+
const toPaymentRow = (r) => ({
|
|
100
|
+
id: r.id,
|
|
101
|
+
agreementId: r.agreementId,
|
|
102
|
+
mode: r.mode,
|
|
103
|
+
status: r.status,
|
|
104
|
+
amount: decOrNull(r.amount),
|
|
105
|
+
currency: r.currency,
|
|
106
|
+
paymentId: r.paymentId,
|
|
107
|
+
recordedBy: r.recordedBy,
|
|
108
|
+
recordedAtMs: numOrNull(r.recordedAtMs),
|
|
109
|
+
verifiedBy: r.verifiedBy,
|
|
110
|
+
verifiedAtMs: numOrNull(r.verifiedAtMs),
|
|
111
|
+
note: r.note,
|
|
112
|
+
payload: r.payload,
|
|
113
|
+
createdAt: r.createdAt,
|
|
114
|
+
updatedAt: r.updatedAt,
|
|
115
|
+
});
|
|
116
|
+
export class CommerceTaskRepository {
|
|
117
|
+
db;
|
|
118
|
+
constructor(db) {
|
|
119
|
+
this.db = db;
|
|
120
|
+
}
|
|
121
|
+
get orm() {
|
|
122
|
+
return this.db.orm.public.CommerceTask;
|
|
123
|
+
}
|
|
124
|
+
async create(t) {
|
|
125
|
+
if (await this.getById(t.id))
|
|
126
|
+
return null;
|
|
127
|
+
const row = await catchUnique(this.orm.create({
|
|
128
|
+
id: t.id,
|
|
129
|
+
buyerWallet: t.buyerWallet,
|
|
130
|
+
buyerAgentId: t.buyerAgentId ?? null,
|
|
131
|
+
title: t.title,
|
|
132
|
+
description: t.description,
|
|
133
|
+
requirements: t.requirements ?? null,
|
|
134
|
+
budget: t.budget != null
|
|
135
|
+
? t.budget
|
|
136
|
+
: null,
|
|
137
|
+
currency: t.currency ?? "USD",
|
|
138
|
+
expiresAtMs: t.expiresAtMs != null
|
|
139
|
+
? BigInt(t.expiresAtMs)
|
|
140
|
+
: null,
|
|
141
|
+
status: t.status,
|
|
142
|
+
payload: t.payload,
|
|
143
|
+
}));
|
|
144
|
+
return row ? toTaskRow(row) : null;
|
|
145
|
+
}
|
|
146
|
+
async getById(id) {
|
|
147
|
+
const r = await this.orm.where({ id }).first();
|
|
148
|
+
return r ? toTaskRow(r) : null;
|
|
149
|
+
}
|
|
150
|
+
async list(opts) {
|
|
151
|
+
let q = this.orm.orderBy((t) => t.createdAt.desc());
|
|
152
|
+
if (opts?.status !== undefined && opts?.buyerWallet !== undefined) {
|
|
153
|
+
q = q.where({ status: opts.status, buyerWallet: opts.buyerWallet });
|
|
154
|
+
}
|
|
155
|
+
else if (opts?.status !== undefined) {
|
|
156
|
+
q = q.where({ status: opts.status });
|
|
157
|
+
}
|
|
158
|
+
else if (opts?.buyerWallet !== undefined) {
|
|
159
|
+
q = q.where({ buyerWallet: opts.buyerWallet });
|
|
160
|
+
}
|
|
161
|
+
const need = opts?.limit !== undefined ? opts.limit : undefined;
|
|
162
|
+
if (need !== undefined)
|
|
163
|
+
q = q.limit(need * 4);
|
|
164
|
+
let rows = (await q.all()).map(toTaskRow);
|
|
165
|
+
if (opts?.visibleTo !== undefined) {
|
|
166
|
+
const viewer = opts.visibleTo;
|
|
167
|
+
rows = rows.filter((r) => r.status !== "DRAFT" || r.buyerWallet === viewer);
|
|
168
|
+
}
|
|
169
|
+
if (need !== undefined)
|
|
170
|
+
rows = rows.slice(0, need);
|
|
171
|
+
return rows;
|
|
172
|
+
}
|
|
173
|
+
async applyStatus(id, expectedFrom, to) {
|
|
174
|
+
const row = await this.orm
|
|
175
|
+
.where({ id, status: expectedFrom })
|
|
176
|
+
.update({ status: to });
|
|
177
|
+
return row ? toTaskRow(row) : null;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
export class CommerceOfferRepository {
|
|
181
|
+
db;
|
|
182
|
+
constructor(db) {
|
|
183
|
+
this.db = db;
|
|
184
|
+
}
|
|
185
|
+
get orm() {
|
|
186
|
+
return this.db.orm.public.CommerceOffer;
|
|
187
|
+
}
|
|
188
|
+
async create(o) {
|
|
189
|
+
if (await this.getById(o.id))
|
|
190
|
+
return null;
|
|
191
|
+
const row = await catchUnique(this.orm.create({
|
|
192
|
+
id: o.id,
|
|
193
|
+
taskId: o.taskId,
|
|
194
|
+
providerWallet: o.providerWallet,
|
|
195
|
+
providerAgentId: o.providerAgentId ?? null,
|
|
196
|
+
price: o.price,
|
|
197
|
+
currency: o.currency ?? "USD",
|
|
198
|
+
eta: o.eta ?? null,
|
|
199
|
+
proposal: o.proposal,
|
|
200
|
+
status: o.status,
|
|
201
|
+
payload: o.payload,
|
|
202
|
+
}));
|
|
203
|
+
return row ? toOfferRow(row) : null;
|
|
204
|
+
}
|
|
205
|
+
async getById(id) {
|
|
206
|
+
const r = await this.orm.where({ id }).first();
|
|
207
|
+
return r ? toOfferRow(r) : null;
|
|
208
|
+
}
|
|
209
|
+
async listByTask(taskId) {
|
|
210
|
+
const rows = await this.orm
|
|
211
|
+
.orderBy((o) => o.createdAt.asc())
|
|
212
|
+
.where({ taskId })
|
|
213
|
+
.all();
|
|
214
|
+
return rows.map(toOfferRow);
|
|
215
|
+
}
|
|
216
|
+
async applyStatus(id, expectedFrom, to) {
|
|
217
|
+
const row = await this.orm
|
|
218
|
+
.where({ id, status: expectedFrom })
|
|
219
|
+
.update({ status: to });
|
|
220
|
+
return row ? toOfferRow(row) : null;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
export class CommerceAgreementRepository {
|
|
224
|
+
db;
|
|
225
|
+
constructor(db) {
|
|
226
|
+
this.db = db;
|
|
227
|
+
}
|
|
228
|
+
get orm() {
|
|
229
|
+
return this.db.orm.public.CommerceAgreement;
|
|
230
|
+
}
|
|
231
|
+
async create(a) {
|
|
232
|
+
if (await this.getById(a.id))
|
|
233
|
+
return null;
|
|
234
|
+
if (await this.getByTaskId(a.taskId))
|
|
235
|
+
return null;
|
|
236
|
+
if (await this.getByOfferId(a.offerId))
|
|
237
|
+
return null;
|
|
238
|
+
const row = await catchUnique(this.orm.create({
|
|
239
|
+
id: a.id,
|
|
240
|
+
taskId: a.taskId,
|
|
241
|
+
offerId: a.offerId,
|
|
242
|
+
buyerWallet: a.buyerWallet,
|
|
243
|
+
providerWallet: a.providerWallet,
|
|
244
|
+
agreedAmount: a.agreedAmount,
|
|
245
|
+
currency: a.currency ?? "USD",
|
|
246
|
+
acceptanceCriteria: a.acceptanceCriteria ?? null,
|
|
247
|
+
status: a.status,
|
|
248
|
+
completedAtMs: a.completedAtMs != null
|
|
249
|
+
? BigInt(a.completedAtMs)
|
|
250
|
+
: null,
|
|
251
|
+
payload: a.payload,
|
|
252
|
+
}));
|
|
253
|
+
return row ? toAgreementRow(row) : null;
|
|
254
|
+
}
|
|
255
|
+
async getById(id) {
|
|
256
|
+
const r = await this.orm.where({ id }).first();
|
|
257
|
+
return r ? toAgreementRow(r) : null;
|
|
258
|
+
}
|
|
259
|
+
async getByTaskId(taskId) {
|
|
260
|
+
const r = await this.orm.where({ taskId }).first();
|
|
261
|
+
return r ? toAgreementRow(r) : null;
|
|
262
|
+
}
|
|
263
|
+
async getByOfferId(offerId) {
|
|
264
|
+
const r = await this.orm.where({ offerId }).first();
|
|
265
|
+
return r ? toAgreementRow(r) : null;
|
|
266
|
+
}
|
|
267
|
+
async applyStatus(id, expectedFrom, to, patch) {
|
|
268
|
+
const row = await this.orm
|
|
269
|
+
.where({ id, status: expectedFrom })
|
|
270
|
+
.update({
|
|
271
|
+
status: to,
|
|
272
|
+
...(patch?.completedAtMs !== undefined
|
|
273
|
+
? {
|
|
274
|
+
completedAtMs: BigInt(patch.completedAtMs),
|
|
275
|
+
}
|
|
276
|
+
: {}),
|
|
277
|
+
});
|
|
278
|
+
return row ? toAgreementRow(row) : null;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
export class CommerceSubmissionRepository {
|
|
282
|
+
db;
|
|
283
|
+
constructor(db) {
|
|
284
|
+
this.db = db;
|
|
285
|
+
}
|
|
286
|
+
get orm() {
|
|
287
|
+
return this.db.orm.public.CommerceSubmission;
|
|
288
|
+
}
|
|
289
|
+
async create(s) {
|
|
290
|
+
if (await this.getById(s.id))
|
|
291
|
+
return null;
|
|
292
|
+
const row = await catchUnique(this.orm.create({
|
|
293
|
+
id: s.id,
|
|
294
|
+
agreementId: s.agreementId,
|
|
295
|
+
providerWallet: s.providerWallet,
|
|
296
|
+
summary: s.summary,
|
|
297
|
+
resultUrl: s.resultUrl ?? null,
|
|
298
|
+
status: s.status,
|
|
299
|
+
reviewNote: s.reviewNote ?? null,
|
|
300
|
+
submittedAtMs: BigInt(s.submittedAtMs),
|
|
301
|
+
reviewedAtMs: s.reviewedAtMs != null
|
|
302
|
+
? BigInt(s.reviewedAtMs)
|
|
303
|
+
: null,
|
|
304
|
+
payload: s.payload,
|
|
305
|
+
}));
|
|
306
|
+
return row ? toSubmissionRow(row) : null;
|
|
307
|
+
}
|
|
308
|
+
async getById(id) {
|
|
309
|
+
const r = await this.orm.where({ id }).first();
|
|
310
|
+
return r ? toSubmissionRow(r) : null;
|
|
311
|
+
}
|
|
312
|
+
async listByAgreement(agreementId) {
|
|
313
|
+
const rows = await this.orm
|
|
314
|
+
.orderBy((s) => s.createdAt.asc())
|
|
315
|
+
.where({ agreementId })
|
|
316
|
+
.all();
|
|
317
|
+
return rows.map(toSubmissionRow);
|
|
318
|
+
}
|
|
319
|
+
async applyStatus(id, expectedFrom, to, patch) {
|
|
320
|
+
const row = await this.orm
|
|
321
|
+
.where({ id, status: expectedFrom })
|
|
322
|
+
.update({
|
|
323
|
+
status: to,
|
|
324
|
+
...(patch?.reviewNote !== undefined
|
|
325
|
+
? { reviewNote: patch.reviewNote }
|
|
326
|
+
: {}),
|
|
327
|
+
...(patch?.reviewedAtMs !== undefined
|
|
328
|
+
? {
|
|
329
|
+
reviewedAtMs: BigInt(patch.reviewedAtMs),
|
|
330
|
+
}
|
|
331
|
+
: {}),
|
|
332
|
+
});
|
|
333
|
+
return row ? toSubmissionRow(row) : null;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
export class CommerceEvidenceRepository {
|
|
337
|
+
db;
|
|
338
|
+
constructor(db) {
|
|
339
|
+
this.db = db;
|
|
340
|
+
}
|
|
341
|
+
get orm() {
|
|
342
|
+
return this.db.orm.public.CommerceEvidence;
|
|
343
|
+
}
|
|
344
|
+
async append(e) {
|
|
345
|
+
if (await this.getById(e.id))
|
|
346
|
+
return null;
|
|
347
|
+
const row = await catchUnique(this.orm.create({
|
|
348
|
+
id: e.id,
|
|
349
|
+
submissionId: e.submissionId ?? null,
|
|
350
|
+
agreementId: e.agreementId ?? null,
|
|
351
|
+
type: e.type,
|
|
352
|
+
url: e.url ?? null,
|
|
353
|
+
hash: e.hash ?? null,
|
|
354
|
+
content: e.content ?? null,
|
|
355
|
+
metadata: e.metadata ?? null,
|
|
356
|
+
payload: e.payload,
|
|
357
|
+
}));
|
|
358
|
+
return row ? toEvidenceRow(row) : null;
|
|
359
|
+
}
|
|
360
|
+
async getById(id) {
|
|
361
|
+
const r = await this.orm.where({ id }).first();
|
|
362
|
+
return r ? toEvidenceRow(r) : null;
|
|
363
|
+
}
|
|
364
|
+
async listBySubmission(submissionId) {
|
|
365
|
+
const rows = await this.orm
|
|
366
|
+
.orderBy((e) => e.createdAt.asc())
|
|
367
|
+
.where({ submissionId })
|
|
368
|
+
.all();
|
|
369
|
+
return rows.map(toEvidenceRow);
|
|
370
|
+
}
|
|
371
|
+
async listByAgreement(agreementId) {
|
|
372
|
+
const rows = await this.orm
|
|
373
|
+
.orderBy((e) => e.createdAt.asc())
|
|
374
|
+
.where({ agreementId })
|
|
375
|
+
.all();
|
|
376
|
+
return rows.map(toEvidenceRow);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
export class CommerceEventRepository {
|
|
380
|
+
db;
|
|
381
|
+
constructor(db) {
|
|
382
|
+
this.db = db;
|
|
383
|
+
}
|
|
384
|
+
get orm() {
|
|
385
|
+
return this.db.orm.public.CommerceEvent;
|
|
386
|
+
}
|
|
387
|
+
async append(e) {
|
|
388
|
+
if (await this.getById(e.id))
|
|
389
|
+
return null;
|
|
390
|
+
const row = await catchUnique(this.orm.create({
|
|
391
|
+
id: e.id,
|
|
392
|
+
entityType: e.entityType,
|
|
393
|
+
entityId: e.entityId,
|
|
394
|
+
eventType: e.eventType,
|
|
395
|
+
actorWallet: e.actorWallet ?? null,
|
|
396
|
+
prevHash: e.prevHash,
|
|
397
|
+
hash: e.hash,
|
|
398
|
+
payload: e.payload,
|
|
399
|
+
}));
|
|
400
|
+
return row ? toEventRow(row) : null;
|
|
401
|
+
}
|
|
402
|
+
async getById(id) {
|
|
403
|
+
const r = await this.orm.where({ id }).first();
|
|
404
|
+
return r ? toEventRow(r) : null;
|
|
405
|
+
}
|
|
406
|
+
async latest() {
|
|
407
|
+
const r = await this.orm
|
|
408
|
+
.orderBy((e) => e.eventSeq.desc())
|
|
409
|
+
.limit(1)
|
|
410
|
+
.first();
|
|
411
|
+
return r ? toEventRow(r) : null;
|
|
412
|
+
}
|
|
413
|
+
async listByEntity(entityType, entityId) {
|
|
414
|
+
const rows = await this.orm
|
|
415
|
+
.orderBy((e) => e.eventSeq.asc())
|
|
416
|
+
.where({ entityType, entityId })
|
|
417
|
+
.all();
|
|
418
|
+
return rows.map(toEventRow);
|
|
419
|
+
}
|
|
420
|
+
async list(opts) {
|
|
421
|
+
let q = this.orm.orderBy((e) => e.eventSeq.desc());
|
|
422
|
+
if (opts?.entityType !== undefined) {
|
|
423
|
+
q = q.where({ entityType: opts.entityType });
|
|
424
|
+
}
|
|
425
|
+
if (opts?.limit !== undefined)
|
|
426
|
+
q = q.limit(opts.limit);
|
|
427
|
+
return (await q.all()).map(toEventRow);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
export class CommercePaymentRepository {
|
|
431
|
+
db;
|
|
432
|
+
constructor(db) {
|
|
433
|
+
this.db = db;
|
|
434
|
+
}
|
|
435
|
+
get orm() {
|
|
436
|
+
return this.db.orm.public.CommercePayment;
|
|
437
|
+
}
|
|
438
|
+
async create(p) {
|
|
439
|
+
if (await this.getById(p.id))
|
|
440
|
+
return null;
|
|
441
|
+
if (await this.getByAgreement(p.agreementId))
|
|
442
|
+
return null;
|
|
443
|
+
const row = await catchUnique(this.orm.create({
|
|
444
|
+
id: p.id,
|
|
445
|
+
agreementId: p.agreementId,
|
|
446
|
+
mode: p.mode ?? "RECORD_ONLY",
|
|
447
|
+
status: p.status,
|
|
448
|
+
amount: p.amount != null
|
|
449
|
+
? p.amount
|
|
450
|
+
: null,
|
|
451
|
+
currency: p.currency ?? "USD",
|
|
452
|
+
paymentId: p.paymentId ?? null,
|
|
453
|
+
recordedBy: p.recordedBy ?? null,
|
|
454
|
+
recordedAtMs: p.recordedAtMs != null
|
|
455
|
+
? BigInt(p.recordedAtMs)
|
|
456
|
+
: null,
|
|
457
|
+
verifiedBy: p.verifiedBy ?? null,
|
|
458
|
+
verifiedAtMs: p.verifiedAtMs != null
|
|
459
|
+
? BigInt(p.verifiedAtMs)
|
|
460
|
+
: null,
|
|
461
|
+
note: p.note ?? null,
|
|
462
|
+
payload: p.payload,
|
|
463
|
+
}));
|
|
464
|
+
return row ? toPaymentRow(row) : null;
|
|
465
|
+
}
|
|
466
|
+
async getById(id) {
|
|
467
|
+
const r = await this.orm.where({ id }).first();
|
|
468
|
+
return r ? toPaymentRow(r) : null;
|
|
469
|
+
}
|
|
470
|
+
async getByAgreement(agreementId) {
|
|
471
|
+
const r = await this.orm.where({ agreementId }).first();
|
|
472
|
+
return r ? toPaymentRow(r) : null;
|
|
473
|
+
}
|
|
474
|
+
async applyStatus(id, expectedFrom, to, patch) {
|
|
475
|
+
const row = await this.orm
|
|
476
|
+
.where({ id, status: expectedFrom })
|
|
477
|
+
.update({
|
|
478
|
+
status: to,
|
|
479
|
+
...(patch?.recordedBy !== undefined
|
|
480
|
+
? { recordedBy: patch.recordedBy }
|
|
481
|
+
: {}),
|
|
482
|
+
...(patch?.recordedAtMs !== undefined
|
|
483
|
+
? {
|
|
484
|
+
recordedAtMs: BigInt(patch.recordedAtMs),
|
|
485
|
+
}
|
|
486
|
+
: {}),
|
|
487
|
+
...(patch?.verifiedBy !== undefined
|
|
488
|
+
? { verifiedBy: patch.verifiedBy }
|
|
489
|
+
: {}),
|
|
490
|
+
...(patch?.verifiedAtMs !== undefined
|
|
491
|
+
? {
|
|
492
|
+
verifiedAtMs: BigInt(patch.verifiedAtMs),
|
|
493
|
+
}
|
|
494
|
+
: {}),
|
|
495
|
+
...(patch?.paymentId !== undefined
|
|
496
|
+
? { paymentId: patch.paymentId }
|
|
497
|
+
: {}),
|
|
498
|
+
...(patch?.amount !== undefined ? { amount: patch.amount } : {}),
|
|
499
|
+
...(patch?.note !== undefined ? { note: patch.note } : {}),
|
|
500
|
+
});
|
|
501
|
+
return row ? toPaymentRow(row) : null;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
export function createPrismaCommerceStores(db) {
|
|
505
|
+
const bundle = (client) => ({
|
|
506
|
+
tasks: new CommerceTaskRepository(client),
|
|
507
|
+
offers: new CommerceOfferRepository(client),
|
|
508
|
+
agreements: new CommerceAgreementRepository(client),
|
|
509
|
+
submissions: new CommerceSubmissionRepository(client),
|
|
510
|
+
evidence: new CommerceEvidenceRepository(client),
|
|
511
|
+
events: new CommerceEventRepository(client),
|
|
512
|
+
payments: new CommercePaymentRepository(client),
|
|
513
|
+
atomic: outerAtomic,
|
|
514
|
+
});
|
|
515
|
+
async function outerAtomic(fn) {
|
|
516
|
+
for (let attempt = 0;; attempt++) {
|
|
517
|
+
try {
|
|
518
|
+
return await db.transaction((tx) => fn(bundle(tx)));
|
|
519
|
+
}
|
|
520
|
+
catch (err) {
|
|
521
|
+
if (attempt === 0 && isUniqueViolation(err))
|
|
522
|
+
continue;
|
|
523
|
+
throw err;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return bundle(db);
|
|
528
|
+
}
|