@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.
- package/dist/database.d.ts +8 -0
- package/dist/database.js +14 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +6 -0
- package/dist/prisma/contract.json +5912 -2456
- 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 +37 -0
- package/dist/repositories/agent-survey-repository.js +82 -0
- package/dist/repositories/archive-cursor-repository.d.ts +8 -0
- package/dist/repositories/archive-cursor-repository.js +38 -0
- package/dist/repositories/archive-cursor-store.d.ts +21 -0
- package/dist/repositories/archive-cursor-store.js +16 -0
- package/dist/repositories/forensics-edge-repository.d.ts +4 -0
- package/dist/repositories/forensics-edge-repository.js +8 -0
- package/dist/repositories/forensics-store.d.ts +8 -0
- package/dist/repositories/forensics-store.js +7 -0
- package/dist/repositories/payment-repository.d.ts +94 -0
- package/dist/repositories/payment-repository.js +624 -0
- package/dist/repositories/payment-store.d.ts +291 -0
- package/dist/repositories/payment-store.js +374 -0
- package/package.json +2 -2
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
|
|
2
|
+
import type { Models } from "../prisma/contract.d.js";
|
|
3
|
+
import type { PrismaDb } from "./event-repository.js";
|
|
4
|
+
type Row = Models.public_AgentRequest;
|
|
5
|
+
type QaRow = Models.public_AgentQAPair;
|
|
6
|
+
export interface AgentRequestUpsert {
|
|
7
|
+
id: string;
|
|
8
|
+
state: string;
|
|
9
|
+
kind?: string | null;
|
|
10
|
+
payload?: JsonValue | null;
|
|
11
|
+
fingerprint: JsonValue;
|
|
12
|
+
fpHash: string;
|
|
13
|
+
agentId?: string | null;
|
|
14
|
+
verified?: boolean;
|
|
15
|
+
response?: JsonValue | null;
|
|
16
|
+
spamFlag?: boolean;
|
|
17
|
+
idempotencyKey?: string | null;
|
|
18
|
+
expiresAtMs: number;
|
|
19
|
+
createdAtMs: number;
|
|
20
|
+
respondedAtMs?: number | null;
|
|
21
|
+
}
|
|
22
|
+
export interface AgentQAPairUpsert {
|
|
23
|
+
id: string;
|
|
24
|
+
questionHash: string;
|
|
25
|
+
question: string;
|
|
26
|
+
response: JsonValue;
|
|
27
|
+
sourceRequestId: string;
|
|
28
|
+
createdAtMs: number;
|
|
29
|
+
}
|
|
30
|
+
export declare class AgentRequestRepository {
|
|
31
|
+
private readonly db;
|
|
32
|
+
constructor(db: PrismaDb);
|
|
33
|
+
put(r: AgentRequestUpsert): Promise<Row>;
|
|
34
|
+
get(id: string): Promise<Row | null>;
|
|
35
|
+
findByIdempotency(idempotencyKey: string, fpHash: string): Promise<Row | null>;
|
|
36
|
+
listByState(opts?: {
|
|
37
|
+
state?: string;
|
|
38
|
+
sinceMs?: number;
|
|
39
|
+
limit?: number;
|
|
40
|
+
}): Promise<Row[]>;
|
|
41
|
+
transition(id: string, from: string, to: string, patch?: {
|
|
42
|
+
response?: JsonValue | null;
|
|
43
|
+
respondedAtMs?: number | null;
|
|
44
|
+
spamFlag?: boolean;
|
|
45
|
+
state?: never;
|
|
46
|
+
}): Promise<Row | null>;
|
|
47
|
+
setSpamFlag(id: string, spamFlag: boolean): Promise<Row | null>;
|
|
48
|
+
listExpired(nowMs: number, limit?: number): Promise<Row[]>;
|
|
49
|
+
listPurgeable(cutoffMs: number, limit?: number): Promise<Row[]>;
|
|
50
|
+
purgePayload(id: string): Promise<boolean>;
|
|
51
|
+
delete(id: string): Promise<boolean>;
|
|
52
|
+
upsertQaPair(qa: AgentQAPairUpsert): Promise<QaRow>;
|
|
53
|
+
getQaByHash(questionHash: string): Promise<QaRow | null>;
|
|
54
|
+
searchQa(substring: string, limit?: number): Promise<QaRow[]>;
|
|
55
|
+
}
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
const toBig = (n) => BigInt(n);
|
|
2
|
+
const toBigOrNull = (n) => n == null ? null : BigInt(n);
|
|
3
|
+
const REQ_STATES_TERMINAL = ["completed", "failed"];
|
|
4
|
+
export class AgentRequestRepository {
|
|
5
|
+
db;
|
|
6
|
+
constructor(db) {
|
|
7
|
+
this.db = db;
|
|
8
|
+
}
|
|
9
|
+
async put(r) {
|
|
10
|
+
return this.db.orm.public.AgentRequest.upsert({
|
|
11
|
+
create: {
|
|
12
|
+
id: r.id,
|
|
13
|
+
state: r.state,
|
|
14
|
+
kind: r.kind ?? null,
|
|
15
|
+
payload: r.payload ?? null,
|
|
16
|
+
fingerprint: r.fingerprint,
|
|
17
|
+
fpHash: r.fpHash,
|
|
18
|
+
agentId: r.agentId ?? null,
|
|
19
|
+
verified: r.verified ?? false,
|
|
20
|
+
response: r.response ?? null,
|
|
21
|
+
spamFlag: r.spamFlag ?? false,
|
|
22
|
+
idempotencyKey: r.idempotencyKey ?? null,
|
|
23
|
+
expiresAtMs: toBig(r.expiresAtMs),
|
|
24
|
+
createdAtMs: toBig(r.createdAtMs),
|
|
25
|
+
respondedAtMs: toBigOrNull(r.respondedAtMs),
|
|
26
|
+
},
|
|
27
|
+
update: {
|
|
28
|
+
state: r.state,
|
|
29
|
+
kind: r.kind ?? null,
|
|
30
|
+
payload: r.payload ?? null,
|
|
31
|
+
fingerprint: r.fingerprint,
|
|
32
|
+
agentId: r.agentId ?? null,
|
|
33
|
+
verified: r.verified ?? false,
|
|
34
|
+
response: r.response ?? null,
|
|
35
|
+
spamFlag: r.spamFlag ?? false,
|
|
36
|
+
respondedAtMs: toBigOrNull(r.respondedAtMs),
|
|
37
|
+
},
|
|
38
|
+
conflictOn: { id: r.id },
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
async get(id) {
|
|
42
|
+
return this.db.orm.public.AgentRequest.where({ id }).first();
|
|
43
|
+
}
|
|
44
|
+
async findByIdempotency(idempotencyKey, fpHash) {
|
|
45
|
+
return this.db.orm.public.AgentRequest.where({
|
|
46
|
+
idempotencyKey,
|
|
47
|
+
fpHash,
|
|
48
|
+
}).first();
|
|
49
|
+
}
|
|
50
|
+
async listByState(opts) {
|
|
51
|
+
let q = this.db.orm.public.AgentRequest.orderBy((r) => r.createdAtMs.desc());
|
|
52
|
+
if (opts?.state !== undefined)
|
|
53
|
+
q = q.where({ state: opts.state });
|
|
54
|
+
const rows = await q.limit(opts?.limit ?? 500).all();
|
|
55
|
+
return opts?.sinceMs === undefined
|
|
56
|
+
? rows
|
|
57
|
+
: rows.filter((r) => Number(r.createdAtMs) >= opts.sinceMs);
|
|
58
|
+
}
|
|
59
|
+
async transition(id, from, to, patch) {
|
|
60
|
+
const row = await this.db.orm.public.AgentRequest.where({
|
|
61
|
+
id,
|
|
62
|
+
state: from,
|
|
63
|
+
}).update({
|
|
64
|
+
state: to,
|
|
65
|
+
...(patch?.response !== undefined ? { response: patch.response } : {}),
|
|
66
|
+
...(patch?.respondedAtMs !== undefined
|
|
67
|
+
? { respondedAtMs: toBigOrNull(patch.respondedAtMs) }
|
|
68
|
+
: {}),
|
|
69
|
+
...(patch?.spamFlag !== undefined ? { spamFlag: patch.spamFlag } : {}),
|
|
70
|
+
});
|
|
71
|
+
return row;
|
|
72
|
+
}
|
|
73
|
+
async setSpamFlag(id, spamFlag) {
|
|
74
|
+
return this.db.orm.public.AgentRequest.where({ id }).update({
|
|
75
|
+
spamFlag,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async listExpired(nowMs, limit = 500) {
|
|
79
|
+
const rows = await this.db.orm.public.AgentRequest.orderBy((r) => r.expiresAtMs.asc())
|
|
80
|
+
.limit(limit)
|
|
81
|
+
.all();
|
|
82
|
+
return rows.filter((r) => Number(r.expiresAtMs) < nowMs);
|
|
83
|
+
}
|
|
84
|
+
async listPurgeable(cutoffMs, limit = 500) {
|
|
85
|
+
const out = [];
|
|
86
|
+
for (const state of REQ_STATES_TERMINAL) {
|
|
87
|
+
const rows = await this.db.orm.public.AgentRequest.where({
|
|
88
|
+
state,
|
|
89
|
+
})
|
|
90
|
+
.orderBy((r) => r.createdAtMs.asc())
|
|
91
|
+
.limit(limit)
|
|
92
|
+
.all();
|
|
93
|
+
for (const r of rows) {
|
|
94
|
+
if (r.payload !== null && Number(r.createdAtMs) < cutoffMs) {
|
|
95
|
+
out.push(r);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
async purgePayload(id) {
|
|
102
|
+
const row = await this.db.orm.public.AgentRequest.where({
|
|
103
|
+
id,
|
|
104
|
+
}).update({ payload: null });
|
|
105
|
+
return row !== null;
|
|
106
|
+
}
|
|
107
|
+
async delete(id) {
|
|
108
|
+
return ((await this.db.orm.public.AgentRequest.where({ id }).delete()) !==
|
|
109
|
+
null);
|
|
110
|
+
}
|
|
111
|
+
async upsertQaPair(qa) {
|
|
112
|
+
const existing = await this.db.orm.public.AgentQAPair.where({
|
|
113
|
+
questionHash: qa.questionHash,
|
|
114
|
+
}).first();
|
|
115
|
+
if (existing) {
|
|
116
|
+
const updated = await this.db.orm.public.AgentQAPair.where({
|
|
117
|
+
id: existing.id,
|
|
118
|
+
}).update({ uses: existing.uses + 1 });
|
|
119
|
+
return updated ?? existing;
|
|
120
|
+
}
|
|
121
|
+
return this.db.orm.public.AgentQAPair.create({
|
|
122
|
+
id: qa.id,
|
|
123
|
+
questionHash: qa.questionHash,
|
|
124
|
+
question: qa.question,
|
|
125
|
+
response: qa.response,
|
|
126
|
+
sourceRequestId: qa.sourceRequestId,
|
|
127
|
+
uses: 0,
|
|
128
|
+
createdAtMs: BigInt(qa.createdAtMs),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
async getQaByHash(questionHash) {
|
|
132
|
+
return this.db.orm.public.AgentQAPair.where({
|
|
133
|
+
questionHash,
|
|
134
|
+
}).first();
|
|
135
|
+
}
|
|
136
|
+
async searchQa(substring, limit = 50) {
|
|
137
|
+
const needle = substring.toLowerCase();
|
|
138
|
+
const rows = await this.db.orm.public.AgentQAPair.orderBy((r) => r.createdAtMs.desc())
|
|
139
|
+
.limit(500)
|
|
140
|
+
.all();
|
|
141
|
+
return rows
|
|
142
|
+
.filter((r) => r.question.toLowerCase().includes(needle))
|
|
143
|
+
.slice(0, limit);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
|
|
2
|
+
import type { Models } from "../prisma/contract.d.js";
|
|
3
|
+
import type { PrismaDb } from "./event-repository.js";
|
|
4
|
+
type SurveyRow = Models.public_AgentSurvey;
|
|
5
|
+
type AnswerRow = Models.public_SurveyAnswer;
|
|
6
|
+
export interface AgentSurveyUpsert {
|
|
7
|
+
id: string;
|
|
8
|
+
title: string;
|
|
9
|
+
questions: JsonValue;
|
|
10
|
+
active?: boolean;
|
|
11
|
+
closesAtMs?: number | null;
|
|
12
|
+
createdAtMs: number;
|
|
13
|
+
}
|
|
14
|
+
export interface SurveyAnswerUpsert {
|
|
15
|
+
id: string;
|
|
16
|
+
surveyId: string;
|
|
17
|
+
agentId?: string | null;
|
|
18
|
+
fpHash?: string | null;
|
|
19
|
+
answers: JsonValue;
|
|
20
|
+
rated?: boolean;
|
|
21
|
+
createdAtMs: number;
|
|
22
|
+
}
|
|
23
|
+
export declare class AgentSurveyRepository {
|
|
24
|
+
private readonly db;
|
|
25
|
+
constructor(db: PrismaDb);
|
|
26
|
+
putSurvey(s: AgentSurveyUpsert): Promise<SurveyRow>;
|
|
27
|
+
getSurvey(id: string): Promise<SurveyRow | null>;
|
|
28
|
+
listOpenSurveys(nowMs: number, limit?: number): Promise<SurveyRow[]>;
|
|
29
|
+
setSurveyActive(id: string, active: boolean): Promise<SurveyRow | null>;
|
|
30
|
+
putAnswer(a: SurveyAnswerUpsert): Promise<AnswerRow>;
|
|
31
|
+
getAnswer(id: string): Promise<AnswerRow | null>;
|
|
32
|
+
findAnswerByAgent(surveyId: string, agentId: string): Promise<AnswerRow | null>;
|
|
33
|
+
findAnswerByFp(surveyId: string, fpHash: string): Promise<AnswerRow | null>;
|
|
34
|
+
listAnswers(surveyId: string, limit?: number): Promise<AnswerRow[]>;
|
|
35
|
+
markRated(id: string): Promise<boolean>;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const toBigOrNull = (n) => n == null ? null : BigInt(n);
|
|
2
|
+
export class AgentSurveyRepository {
|
|
3
|
+
db;
|
|
4
|
+
constructor(db) {
|
|
5
|
+
this.db = db;
|
|
6
|
+
}
|
|
7
|
+
async putSurvey(s) {
|
|
8
|
+
return this.db.orm.public.AgentSurvey.upsert({
|
|
9
|
+
create: {
|
|
10
|
+
id: s.id,
|
|
11
|
+
title: s.title,
|
|
12
|
+
questions: s.questions,
|
|
13
|
+
active: s.active ?? true,
|
|
14
|
+
closesAtMs: toBigOrNull(s.closesAtMs),
|
|
15
|
+
createdAtMs: BigInt(s.createdAtMs),
|
|
16
|
+
},
|
|
17
|
+
update: {
|
|
18
|
+
title: s.title,
|
|
19
|
+
questions: s.questions,
|
|
20
|
+
active: s.active ?? true,
|
|
21
|
+
closesAtMs: toBigOrNull(s.closesAtMs),
|
|
22
|
+
},
|
|
23
|
+
conflictOn: { id: s.id },
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
async getSurvey(id) {
|
|
27
|
+
return this.db.orm.public.AgentSurvey.where({ id }).first();
|
|
28
|
+
}
|
|
29
|
+
async listOpenSurveys(nowMs, limit = 50) {
|
|
30
|
+
const rows = await this.db.orm.public.AgentSurvey.where({
|
|
31
|
+
active: true,
|
|
32
|
+
})
|
|
33
|
+
.orderBy((s) => s.createdAtMs.desc())
|
|
34
|
+
.limit(limit)
|
|
35
|
+
.all();
|
|
36
|
+
return rows.filter((s) => s.closesAtMs === null || Number(s.closesAtMs) > nowMs);
|
|
37
|
+
}
|
|
38
|
+
async setSurveyActive(id, active) {
|
|
39
|
+
return this.db.orm.public.AgentSurvey.where({ id }).update({
|
|
40
|
+
active,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async putAnswer(a) {
|
|
44
|
+
return this.db.orm.public.SurveyAnswer.create({
|
|
45
|
+
id: a.id,
|
|
46
|
+
surveyId: a.surveyId,
|
|
47
|
+
agentId: a.agentId ?? null,
|
|
48
|
+
fpHash: a.fpHash ?? null,
|
|
49
|
+
answers: a.answers,
|
|
50
|
+
rated: a.rated ?? false,
|
|
51
|
+
createdAtMs: BigInt(a.createdAtMs),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
async getAnswer(id) {
|
|
55
|
+
return this.db.orm.public.SurveyAnswer.where({ id }).first();
|
|
56
|
+
}
|
|
57
|
+
async findAnswerByAgent(surveyId, agentId) {
|
|
58
|
+
return this.db.orm.public.SurveyAnswer.where({
|
|
59
|
+
surveyId,
|
|
60
|
+
agentId,
|
|
61
|
+
}).first();
|
|
62
|
+
}
|
|
63
|
+
async findAnswerByFp(surveyId, fpHash) {
|
|
64
|
+
const rows = await this.db.orm.public.SurveyAnswer.where({
|
|
65
|
+
surveyId,
|
|
66
|
+
}).all();
|
|
67
|
+
return rows.find((r) => r.fpHash === fpHash && r.agentId === null) ?? null;
|
|
68
|
+
}
|
|
69
|
+
async listAnswers(surveyId, limit = 500) {
|
|
70
|
+
return this.db.orm.public.SurveyAnswer.where({ surveyId })
|
|
71
|
+
.orderBy((a) => a.createdAtMs.desc())
|
|
72
|
+
.limit(limit)
|
|
73
|
+
.all();
|
|
74
|
+
}
|
|
75
|
+
async markRated(id) {
|
|
76
|
+
const row = await this.db.orm.public.SurveyAnswer.where({
|
|
77
|
+
id,
|
|
78
|
+
rated: false,
|
|
79
|
+
}).update({ rated: true });
|
|
80
|
+
return row !== null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -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,94 @@
|
|
|
1
|
+
import type { PrismaDb } from "./event-repository.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
|
+
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
|
+
listPayable(nowMs: number): Promise<PaymentAttemptRow[]>;
|
|
28
|
+
listRecentTerminal(sinceMs: number): Promise<PaymentAttemptRow[]>;
|
|
29
|
+
}
|
|
30
|
+
export declare class PaymentReceiptRepository implements PaymentReceiptStore {
|
|
31
|
+
private readonly db;
|
|
32
|
+
constructor(db: PrismaDb);
|
|
33
|
+
private get orm();
|
|
34
|
+
append(r: PaymentReceiptInsert): Promise<PaymentReceiptRow | null>;
|
|
35
|
+
getById(id: string): Promise<PaymentReceiptRow | null>;
|
|
36
|
+
listByPayment(paymentId: string): Promise<PaymentReceiptRow[]>;
|
|
37
|
+
getByExternalRef(provider: string, externalReference: string): Promise<PaymentReceiptRow | null>;
|
|
38
|
+
}
|
|
39
|
+
export declare class PaymentEventRepository implements PaymentEventStore {
|
|
40
|
+
private readonly db;
|
|
41
|
+
constructor(db: PrismaDb);
|
|
42
|
+
private get orm();
|
|
43
|
+
append(e: PaymentEventInsert): Promise<PaymentEventRow | null>;
|
|
44
|
+
listByPayment(paymentId: string): Promise<PaymentEventRow[]>;
|
|
45
|
+
listByAttempt(attemptId: string): Promise<PaymentEventRow[]>;
|
|
46
|
+
}
|
|
47
|
+
export declare class PaymentProductRepository implements PaymentProductStore {
|
|
48
|
+
private readonly db;
|
|
49
|
+
constructor(db: PrismaDb);
|
|
50
|
+
private get orm();
|
|
51
|
+
upsert(p: PaymentProductInsert): Promise<PaymentProductRow>;
|
|
52
|
+
getById(id: string): Promise<PaymentProductRow | null>;
|
|
53
|
+
getByCode(code: string): Promise<PaymentProductRow | null>;
|
|
54
|
+
list(opts?: {
|
|
55
|
+
activeOnly?: boolean;
|
|
56
|
+
}): Promise<PaymentProductRow[]>;
|
|
57
|
+
}
|
|
58
|
+
export declare class PaymentPriceRepository implements PaymentPriceStore {
|
|
59
|
+
private readonly db;
|
|
60
|
+
constructor(db: PrismaDb);
|
|
61
|
+
private get orm();
|
|
62
|
+
create(p: PaymentPriceInsert): Promise<PaymentPriceRow | null>;
|
|
63
|
+
getById(id: string): Promise<PaymentPriceRow | null>;
|
|
64
|
+
listByProduct(productId: string, opts?: {
|
|
65
|
+
activeOnly?: boolean;
|
|
66
|
+
}): Promise<PaymentPriceRow[]>;
|
|
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
|
+
}
|
|
94
|
+
export declare function createPrismaPaymentStores(db: PrismaDb): PaymentStores;
|