@agentbadge/database 0.6.5 → 0.6.6
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 +4 -0
- package/dist/database.js +8 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/prisma/contract.json +2289 -817
- package/dist/repositories/finops-repository.d.ts +46 -0
- package/dist/repositories/finops-repository.js +208 -0
- package/dist/repositories/finops-store.d.ts +130 -0
- package/dist/repositories/finops-store.js +151 -0
- package/dist/repositories/identity-repository.d.ts +42 -0
- package/dist/repositories/identity-repository.js +246 -0
- package/dist/repositories/identity-store.d.ts +114 -0
- package/dist/repositories/identity-store.js +170 -0
- package/package.json +1 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { PrismaDb } from "./event-repository.js";
|
|
2
|
+
import type { AdminActionInsert, AdminActionRow, AdminActionStore, FinopsStores, ReconciliationExceptionInsert, ReconciliationExceptionRow, ReconciliationExceptionStore, RefundIntentInsert, RefundIntentRow, RefundIntentStore } from "./finops-store.js";
|
|
3
|
+
export declare class AdminActionRepository implements AdminActionStore {
|
|
4
|
+
private readonly db;
|
|
5
|
+
constructor(db: PrismaDb);
|
|
6
|
+
private get orm();
|
|
7
|
+
append(a: AdminActionInsert): Promise<AdminActionRow | null>;
|
|
8
|
+
getById(id: string): Promise<AdminActionRow | null>;
|
|
9
|
+
listBySubject(subjectType: string, subjectId: string): Promise<AdminActionRow[]>;
|
|
10
|
+
list(opts?: {
|
|
11
|
+
action?: string;
|
|
12
|
+
subjectType?: string;
|
|
13
|
+
operator?: string;
|
|
14
|
+
sinceMs?: number;
|
|
15
|
+
limit?: number;
|
|
16
|
+
}): Promise<AdminActionRow[]>;
|
|
17
|
+
}
|
|
18
|
+
export declare class RefundIntentRepository implements RefundIntentStore {
|
|
19
|
+
private readonly db;
|
|
20
|
+
constructor(db: PrismaDb);
|
|
21
|
+
private get orm();
|
|
22
|
+
create(r: RefundIntentInsert): Promise<RefundIntentRow | null>;
|
|
23
|
+
getById(id: string): Promise<RefundIntentRow | null>;
|
|
24
|
+
listByPayment(paymentId: string): Promise<RefundIntentRow[]>;
|
|
25
|
+
list(opts?: {
|
|
26
|
+
status?: string;
|
|
27
|
+
limit?: number;
|
|
28
|
+
}): Promise<RefundIntentRow[]>;
|
|
29
|
+
complete(id: string, txHash: string, completedBy: string, completedAtMs: number): Promise<RefundIntentRow | null>;
|
|
30
|
+
}
|
|
31
|
+
export declare class ReconciliationExceptionRepository implements ReconciliationExceptionStore {
|
|
32
|
+
private readonly db;
|
|
33
|
+
constructor(db: PrismaDb);
|
|
34
|
+
private get orm();
|
|
35
|
+
append(e: ReconciliationExceptionInsert): Promise<ReconciliationExceptionRow | null>;
|
|
36
|
+
getById(id: string): Promise<ReconciliationExceptionRow | null>;
|
|
37
|
+
list(opts?: {
|
|
38
|
+
status?: string;
|
|
39
|
+
type?: string;
|
|
40
|
+
subjectRef?: string;
|
|
41
|
+
sinceMs?: number;
|
|
42
|
+
limit?: number;
|
|
43
|
+
}): Promise<ReconciliationExceptionRow[]>;
|
|
44
|
+
resolve(id: string, resolution: string, resolvedBy: string, resolvedAtMs: number): Promise<ReconciliationExceptionRow | null>;
|
|
45
|
+
}
|
|
46
|
+
export declare function createPrismaFinopsStores(db: PrismaDb): FinopsStores;
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
const num = (v) => Number(v);
|
|
2
|
+
const numOrNull = (v) => v === null || v === undefined ? null : Number(v);
|
|
3
|
+
const dec = (v) => String(v);
|
|
4
|
+
const toActionRow = (r) => ({
|
|
5
|
+
id: r.id,
|
|
6
|
+
action: r.action,
|
|
7
|
+
subjectType: r.subjectType,
|
|
8
|
+
subjectId: r.subjectId,
|
|
9
|
+
operator: r.operator,
|
|
10
|
+
evidence: r.evidence ?? undefined,
|
|
11
|
+
createdAtMs: num(r.createdAtMs),
|
|
12
|
+
createdAt: r.createdAt,
|
|
13
|
+
});
|
|
14
|
+
const toRefundRow = (r) => ({
|
|
15
|
+
id: r.id,
|
|
16
|
+
paymentId: r.paymentId,
|
|
17
|
+
payee: r.payee,
|
|
18
|
+
amount: dec(r.amount),
|
|
19
|
+
currency: r.currency,
|
|
20
|
+
reason: r.reason,
|
|
21
|
+
evidence: r.evidence ?? undefined,
|
|
22
|
+
status: r.status,
|
|
23
|
+
txHash: r.txHash,
|
|
24
|
+
operator: r.operator,
|
|
25
|
+
createdAtMs: num(r.createdAtMs),
|
|
26
|
+
completedAtMs: numOrNull(r.completedAtMs),
|
|
27
|
+
completedBy: r.completedBy,
|
|
28
|
+
payload: r.payload ?? undefined,
|
|
29
|
+
createdAt: r.createdAt,
|
|
30
|
+
updatedAt: r.updatedAt,
|
|
31
|
+
});
|
|
32
|
+
const toExceptionRow = (r) => ({
|
|
33
|
+
id: r.id,
|
|
34
|
+
type: r.type,
|
|
35
|
+
subjectRef: r.subjectRef,
|
|
36
|
+
details: r.details && typeof r.details === "object"
|
|
37
|
+
? r.details
|
|
38
|
+
: undefined,
|
|
39
|
+
status: r.status,
|
|
40
|
+
resolution: r.resolution,
|
|
41
|
+
resolvedBy: r.resolvedBy,
|
|
42
|
+
resolvedAtMs: numOrNull(r.resolvedAtMs),
|
|
43
|
+
createdAtMs: num(r.createdAtMs),
|
|
44
|
+
createdAt: r.createdAt,
|
|
45
|
+
});
|
|
46
|
+
export class AdminActionRepository {
|
|
47
|
+
db;
|
|
48
|
+
constructor(db) {
|
|
49
|
+
this.db = db;
|
|
50
|
+
}
|
|
51
|
+
get orm() {
|
|
52
|
+
return this.db.orm.public.AdminAction;
|
|
53
|
+
}
|
|
54
|
+
async append(a) {
|
|
55
|
+
const existing = await this.orm.where({ id: a.id }).first();
|
|
56
|
+
if (existing)
|
|
57
|
+
return null;
|
|
58
|
+
const row = await this.orm.create({
|
|
59
|
+
id: a.id,
|
|
60
|
+
action: a.action,
|
|
61
|
+
subjectType: a.subjectType,
|
|
62
|
+
subjectId: a.subjectId,
|
|
63
|
+
operator: a.operator,
|
|
64
|
+
evidence: (a.evidence ?? null),
|
|
65
|
+
createdAtMs: BigInt(a.createdAtMs),
|
|
66
|
+
});
|
|
67
|
+
return toActionRow(row);
|
|
68
|
+
}
|
|
69
|
+
async getById(id) {
|
|
70
|
+
const r = await this.orm.where({ id }).first();
|
|
71
|
+
return r ? toActionRow(r) : null;
|
|
72
|
+
}
|
|
73
|
+
async listBySubject(subjectType, subjectId) {
|
|
74
|
+
const rows = await this.orm
|
|
75
|
+
.orderBy((e) => e.createdAtMs.asc())
|
|
76
|
+
.where({ subjectType, subjectId })
|
|
77
|
+
.all();
|
|
78
|
+
return rows.map(toActionRow);
|
|
79
|
+
}
|
|
80
|
+
async list(opts = {}) {
|
|
81
|
+
const rows = await this.orm
|
|
82
|
+
.orderBy((e) => e.createdAtMs.desc())
|
|
83
|
+
.all();
|
|
84
|
+
let out = rows
|
|
85
|
+
.map(toActionRow)
|
|
86
|
+
.filter((r) => (opts.action === undefined || r.action === opts.action) &&
|
|
87
|
+
(opts.subjectType === undefined ||
|
|
88
|
+
r.subjectType === opts.subjectType) &&
|
|
89
|
+
(opts.operator === undefined || r.operator === opts.operator) &&
|
|
90
|
+
(opts.sinceMs === undefined || r.createdAtMs >= opts.sinceMs));
|
|
91
|
+
if (opts.limit !== undefined)
|
|
92
|
+
out = out.slice(0, opts.limit);
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export class RefundIntentRepository {
|
|
97
|
+
db;
|
|
98
|
+
constructor(db) {
|
|
99
|
+
this.db = db;
|
|
100
|
+
}
|
|
101
|
+
get orm() {
|
|
102
|
+
return this.db.orm.public.RefundIntent;
|
|
103
|
+
}
|
|
104
|
+
async create(r) {
|
|
105
|
+
const existing = await this.orm.where({ id: r.id }).first();
|
|
106
|
+
if (existing)
|
|
107
|
+
return null;
|
|
108
|
+
const row = await this.orm.create({
|
|
109
|
+
id: r.id,
|
|
110
|
+
paymentId: r.paymentId,
|
|
111
|
+
payee: r.payee,
|
|
112
|
+
amount: r.amount,
|
|
113
|
+
currency: r.currency ?? "USDC",
|
|
114
|
+
reason: r.reason,
|
|
115
|
+
evidence: (r.evidence ?? null),
|
|
116
|
+
status: r.status,
|
|
117
|
+
operator: r.operator,
|
|
118
|
+
createdAtMs: BigInt(r.createdAtMs),
|
|
119
|
+
payload: (r.payload ?? null),
|
|
120
|
+
});
|
|
121
|
+
return toRefundRow(row);
|
|
122
|
+
}
|
|
123
|
+
async getById(id) {
|
|
124
|
+
const r = await this.orm.where({ id }).first();
|
|
125
|
+
return r ? toRefundRow(r) : null;
|
|
126
|
+
}
|
|
127
|
+
async listByPayment(paymentId) {
|
|
128
|
+
const rows = await this.orm
|
|
129
|
+
.orderBy((e) => e.createdAtMs.desc())
|
|
130
|
+
.where({ paymentId })
|
|
131
|
+
.all();
|
|
132
|
+
return rows.map(toRefundRow);
|
|
133
|
+
}
|
|
134
|
+
async list(opts = {}) {
|
|
135
|
+
const q = this.orm.orderBy((e) => e.createdAtMs.desc());
|
|
136
|
+
const scoped = opts.status !== undefined ? q.where({ status: opts.status }) : q;
|
|
137
|
+
const rows = opts.limit !== undefined
|
|
138
|
+
? await scoped.limit(opts.limit).all()
|
|
139
|
+
: await scoped.all();
|
|
140
|
+
return rows.map(toRefundRow);
|
|
141
|
+
}
|
|
142
|
+
async complete(id, txHash, completedBy, completedAtMs) {
|
|
143
|
+
const row = await this.orm.where({ id, status: "OPEN" }).update({
|
|
144
|
+
status: "COMPLETED",
|
|
145
|
+
txHash,
|
|
146
|
+
completedBy,
|
|
147
|
+
completedAtMs: BigInt(completedAtMs),
|
|
148
|
+
});
|
|
149
|
+
return row ? toRefundRow(row) : null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export class ReconciliationExceptionRepository {
|
|
153
|
+
db;
|
|
154
|
+
constructor(db) {
|
|
155
|
+
this.db = db;
|
|
156
|
+
}
|
|
157
|
+
get orm() {
|
|
158
|
+
return this.db.orm.public.ReconciliationException;
|
|
159
|
+
}
|
|
160
|
+
async append(e) {
|
|
161
|
+
const existing = await this.orm.where({ id: e.id }).first();
|
|
162
|
+
if (existing)
|
|
163
|
+
return null;
|
|
164
|
+
const row = await this.orm.create({
|
|
165
|
+
id: e.id,
|
|
166
|
+
type: e.type,
|
|
167
|
+
subjectRef: e.subjectRef,
|
|
168
|
+
details: (e.details ?? null),
|
|
169
|
+
status: e.status,
|
|
170
|
+
createdAtMs: BigInt(e.createdAtMs),
|
|
171
|
+
});
|
|
172
|
+
return toExceptionRow(row);
|
|
173
|
+
}
|
|
174
|
+
async getById(id) {
|
|
175
|
+
const r = await this.orm.where({ id }).first();
|
|
176
|
+
return r ? toExceptionRow(r) : null;
|
|
177
|
+
}
|
|
178
|
+
async list(opts = {}) {
|
|
179
|
+
const rows = await this.orm
|
|
180
|
+
.orderBy((e) => e.createdAtMs.desc())
|
|
181
|
+
.all();
|
|
182
|
+
let out = rows
|
|
183
|
+
.map(toExceptionRow)
|
|
184
|
+
.filter((r) => (opts.status === undefined || r.status === opts.status) &&
|
|
185
|
+
(opts.type === undefined || r.type === opts.type) &&
|
|
186
|
+
(opts.subjectRef === undefined || r.subjectRef === opts.subjectRef) &&
|
|
187
|
+
(opts.sinceMs === undefined || r.createdAtMs >= opts.sinceMs));
|
|
188
|
+
if (opts.limit !== undefined)
|
|
189
|
+
out = out.slice(0, opts.limit);
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
async resolve(id, resolution, resolvedBy, resolvedAtMs) {
|
|
193
|
+
const row = await this.orm.where({ id, status: "OPEN" }).update({
|
|
194
|
+
status: "RESOLVED",
|
|
195
|
+
resolution,
|
|
196
|
+
resolvedBy,
|
|
197
|
+
resolvedAtMs: BigInt(resolvedAtMs),
|
|
198
|
+
});
|
|
199
|
+
return row ? toExceptionRow(row) : null;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
export function createPrismaFinopsStores(db) {
|
|
203
|
+
return {
|
|
204
|
+
adminActions: new AdminActionRepository(db),
|
|
205
|
+
refundIntents: new RefundIntentRepository(db),
|
|
206
|
+
exceptions: new ReconciliationExceptionRepository(db),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
|
|
2
|
+
export declare const ADMIN_ACTIONS: readonly ["REVIEW", "RETRY_VERIFY", "MARK_EXTERNAL_REVIEWED", "REFUND", "RESOLVE_EXCEPTION"];
|
|
3
|
+
export type AdminActionKind = (typeof ADMIN_ACTIONS)[number];
|
|
4
|
+
export interface AdminActionInsert {
|
|
5
|
+
id: string;
|
|
6
|
+
action: string;
|
|
7
|
+
subjectType: string;
|
|
8
|
+
subjectId: string;
|
|
9
|
+
operator: string;
|
|
10
|
+
evidence?: JsonValue;
|
|
11
|
+
createdAtMs: number;
|
|
12
|
+
}
|
|
13
|
+
export interface AdminActionRow extends AdminActionInsert {
|
|
14
|
+
createdAt: string;
|
|
15
|
+
}
|
|
16
|
+
export interface AdminActionStore {
|
|
17
|
+
append(a: AdminActionInsert): Promise<AdminActionRow | null>;
|
|
18
|
+
getById(id: string): Promise<AdminActionRow | null>;
|
|
19
|
+
listBySubject(subjectType: string, subjectId: string): Promise<AdminActionRow[]>;
|
|
20
|
+
list(opts?: {
|
|
21
|
+
action?: string;
|
|
22
|
+
subjectType?: string;
|
|
23
|
+
operator?: string;
|
|
24
|
+
sinceMs?: number;
|
|
25
|
+
limit?: number;
|
|
26
|
+
}): Promise<AdminActionRow[]>;
|
|
27
|
+
}
|
|
28
|
+
export interface RefundIntentInsert {
|
|
29
|
+
id: string;
|
|
30
|
+
paymentId: string;
|
|
31
|
+
payee: string;
|
|
32
|
+
amount: string;
|
|
33
|
+
currency?: string;
|
|
34
|
+
reason: string;
|
|
35
|
+
evidence?: JsonValue;
|
|
36
|
+
status: string;
|
|
37
|
+
operator: string;
|
|
38
|
+
createdAtMs: number;
|
|
39
|
+
payload?: JsonValue;
|
|
40
|
+
}
|
|
41
|
+
export interface RefundIntentRow extends RefundIntentInsert {
|
|
42
|
+
txHash?: string | null;
|
|
43
|
+
completedAtMs?: number | null;
|
|
44
|
+
completedBy?: string | null;
|
|
45
|
+
createdAt: string;
|
|
46
|
+
updatedAt: string;
|
|
47
|
+
}
|
|
48
|
+
export interface RefundIntentStore {
|
|
49
|
+
create(r: RefundIntentInsert): Promise<RefundIntentRow | null>;
|
|
50
|
+
getById(id: string): Promise<RefundIntentRow | null>;
|
|
51
|
+
listByPayment(paymentId: string): Promise<RefundIntentRow[]>;
|
|
52
|
+
list(opts?: {
|
|
53
|
+
status?: string;
|
|
54
|
+
limit?: number;
|
|
55
|
+
}): Promise<RefundIntentRow[]>;
|
|
56
|
+
complete(id: string, txHash: string, completedBy: string, completedAtMs: number): Promise<RefundIntentRow | null>;
|
|
57
|
+
}
|
|
58
|
+
export declare const RECONCILIATION_EXCEPTION_TYPES: readonly ["AMOUNT_MISMATCH", "MISSING_TX", "ORPHAN_TX", "REVERTED"];
|
|
59
|
+
export type ReconciliationExceptionType = (typeof RECONCILIATION_EXCEPTION_TYPES)[number];
|
|
60
|
+
export declare const EXCEPTION_RESOLUTIONS: readonly ["MATCHED", "IGNORED", "MANUAL_ADJUSTED", "REFUNDED"];
|
|
61
|
+
export type ExceptionResolution = (typeof EXCEPTION_RESOLUTIONS)[number];
|
|
62
|
+
export interface ReconciliationExceptionInsert {
|
|
63
|
+
id: string;
|
|
64
|
+
type: string;
|
|
65
|
+
subjectRef: string;
|
|
66
|
+
details?: Record<string, unknown>;
|
|
67
|
+
status: string;
|
|
68
|
+
createdAtMs: number;
|
|
69
|
+
}
|
|
70
|
+
export interface ReconciliationExceptionRow extends ReconciliationExceptionInsert {
|
|
71
|
+
resolution?: string | null;
|
|
72
|
+
resolvedBy?: string | null;
|
|
73
|
+
resolvedAtMs?: number | null;
|
|
74
|
+
createdAt?: string;
|
|
75
|
+
}
|
|
76
|
+
export interface ReconciliationExceptionStore {
|
|
77
|
+
append(e: ReconciliationExceptionInsert): Promise<ReconciliationExceptionRow | null>;
|
|
78
|
+
getById(id: string): Promise<ReconciliationExceptionRow | null>;
|
|
79
|
+
list(opts?: {
|
|
80
|
+
status?: string;
|
|
81
|
+
type?: string;
|
|
82
|
+
subjectRef?: string;
|
|
83
|
+
sinceMs?: number;
|
|
84
|
+
limit?: number;
|
|
85
|
+
}): Promise<ReconciliationExceptionRow[]>;
|
|
86
|
+
resolve(id: string, resolution: string, resolvedBy: string, resolvedAtMs: number): Promise<ReconciliationExceptionRow | null>;
|
|
87
|
+
}
|
|
88
|
+
export interface FinopsStores {
|
|
89
|
+
adminActions: AdminActionStore;
|
|
90
|
+
refundIntents: RefundIntentStore;
|
|
91
|
+
exceptions: ReconciliationExceptionStore;
|
|
92
|
+
}
|
|
93
|
+
export declare class InMemoryAdminActionStore implements AdminActionStore {
|
|
94
|
+
private readonly rows;
|
|
95
|
+
append(a: AdminActionInsert): Promise<AdminActionRow | null>;
|
|
96
|
+
getById(id: string): Promise<AdminActionRow | null>;
|
|
97
|
+
listBySubject(subjectType: string, subjectId: string): Promise<AdminActionRow[]>;
|
|
98
|
+
list(opts?: {
|
|
99
|
+
action?: string;
|
|
100
|
+
subjectType?: string;
|
|
101
|
+
operator?: string;
|
|
102
|
+
sinceMs?: number;
|
|
103
|
+
limit?: number;
|
|
104
|
+
}): Promise<AdminActionRow[]>;
|
|
105
|
+
}
|
|
106
|
+
export declare class InMemoryRefundIntentStore implements RefundIntentStore {
|
|
107
|
+
private readonly rows;
|
|
108
|
+
create(r: RefundIntentInsert): Promise<RefundIntentRow | null>;
|
|
109
|
+
getById(id: string): Promise<RefundIntentRow | null>;
|
|
110
|
+
listByPayment(paymentId: string): Promise<RefundIntentRow[]>;
|
|
111
|
+
list(opts?: {
|
|
112
|
+
status?: string;
|
|
113
|
+
limit?: number;
|
|
114
|
+
}): Promise<RefundIntentRow[]>;
|
|
115
|
+
complete(id: string, txHash: string, completedBy: string, completedAtMs: number): Promise<RefundIntentRow | null>;
|
|
116
|
+
}
|
|
117
|
+
export declare class InMemoryReconciliationExceptionStore implements ReconciliationExceptionStore {
|
|
118
|
+
private readonly rows;
|
|
119
|
+
append(e: ReconciliationExceptionInsert): Promise<ReconciliationExceptionRow | null>;
|
|
120
|
+
getById(id: string): Promise<ReconciliationExceptionRow | null>;
|
|
121
|
+
list(opts?: {
|
|
122
|
+
status?: string;
|
|
123
|
+
type?: string;
|
|
124
|
+
subjectRef?: string;
|
|
125
|
+
sinceMs?: number;
|
|
126
|
+
limit?: number;
|
|
127
|
+
}): Promise<ReconciliationExceptionRow[]>;
|
|
128
|
+
resolve(id: string, resolution: string, resolvedBy: string, resolvedAtMs: number): Promise<ReconciliationExceptionRow | null>;
|
|
129
|
+
}
|
|
130
|
+
export declare function createInMemoryFinopsStores(): FinopsStores;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
export const ADMIN_ACTIONS = [
|
|
2
|
+
"REVIEW",
|
|
3
|
+
"RETRY_VERIFY",
|
|
4
|
+
"MARK_EXTERNAL_REVIEWED",
|
|
5
|
+
"REFUND",
|
|
6
|
+
"RESOLVE_EXCEPTION",
|
|
7
|
+
];
|
|
8
|
+
export const RECONCILIATION_EXCEPTION_TYPES = [
|
|
9
|
+
"AMOUNT_MISMATCH",
|
|
10
|
+
"MISSING_TX",
|
|
11
|
+
"ORPHAN_TX",
|
|
12
|
+
"REVERTED",
|
|
13
|
+
];
|
|
14
|
+
export const EXCEPTION_RESOLUTIONS = [
|
|
15
|
+
"MATCHED",
|
|
16
|
+
"IGNORED",
|
|
17
|
+
"MANUAL_ADJUSTED",
|
|
18
|
+
"REFUNDED",
|
|
19
|
+
];
|
|
20
|
+
const nowIso = () => new Date().toISOString();
|
|
21
|
+
export class InMemoryAdminActionStore {
|
|
22
|
+
rows = new Map();
|
|
23
|
+
async append(a) {
|
|
24
|
+
if (this.rows.has(a.id))
|
|
25
|
+
return null;
|
|
26
|
+
const row = {
|
|
27
|
+
...a,
|
|
28
|
+
createdAt: nowIso(),
|
|
29
|
+
};
|
|
30
|
+
this.rows.set(row.id, row);
|
|
31
|
+
return row;
|
|
32
|
+
}
|
|
33
|
+
async getById(id) {
|
|
34
|
+
return this.rows.get(id) ?? null;
|
|
35
|
+
}
|
|
36
|
+
async listBySubject(subjectType, subjectId) {
|
|
37
|
+
return [...this.rows.values()]
|
|
38
|
+
.filter((r) => r.subjectType === subjectType && r.subjectId === subjectId)
|
|
39
|
+
.sort((a, b) => a.createdAtMs - b.createdAtMs);
|
|
40
|
+
}
|
|
41
|
+
async list(opts = {}) {
|
|
42
|
+
let out = [...this.rows.values()].filter((r) => (opts.action === undefined || r.action === opts.action) &&
|
|
43
|
+
(opts.subjectType === undefined ||
|
|
44
|
+
r.subjectType === opts.subjectType) &&
|
|
45
|
+
(opts.operator === undefined || r.operator === opts.operator) &&
|
|
46
|
+
(opts.sinceMs === undefined || r.createdAtMs >= opts.sinceMs));
|
|
47
|
+
out.sort((a, b) => b.createdAtMs - a.createdAtMs);
|
|
48
|
+
if (opts.limit !== undefined)
|
|
49
|
+
out = out.slice(0, opts.limit);
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export class InMemoryRefundIntentStore {
|
|
54
|
+
rows = new Map();
|
|
55
|
+
async create(r) {
|
|
56
|
+
if (this.rows.has(r.id))
|
|
57
|
+
return null;
|
|
58
|
+
const ts = nowIso();
|
|
59
|
+
const row = {
|
|
60
|
+
...r,
|
|
61
|
+
currency: r.currency ?? "USDC",
|
|
62
|
+
txHash: null,
|
|
63
|
+
completedAtMs: null,
|
|
64
|
+
completedBy: null,
|
|
65
|
+
createdAt: ts,
|
|
66
|
+
updatedAt: ts,
|
|
67
|
+
};
|
|
68
|
+
this.rows.set(row.id, row);
|
|
69
|
+
return row;
|
|
70
|
+
}
|
|
71
|
+
async getById(id) {
|
|
72
|
+
return this.rows.get(id) ?? null;
|
|
73
|
+
}
|
|
74
|
+
async listByPayment(paymentId) {
|
|
75
|
+
return [...this.rows.values()]
|
|
76
|
+
.filter((r) => r.paymentId === paymentId)
|
|
77
|
+
.sort((a, b) => b.createdAtMs - a.createdAtMs);
|
|
78
|
+
}
|
|
79
|
+
async list(opts = {}) {
|
|
80
|
+
let out = [...this.rows.values()].filter((r) => opts.status === undefined || r.status === opts.status);
|
|
81
|
+
out.sort((a, b) => b.createdAtMs - a.createdAtMs);
|
|
82
|
+
if (opts.limit !== undefined)
|
|
83
|
+
out = out.slice(0, opts.limit);
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
async complete(id, txHash, completedBy, completedAtMs) {
|
|
87
|
+
const row = this.rows.get(id);
|
|
88
|
+
if (!row || row.status !== "OPEN")
|
|
89
|
+
return null;
|
|
90
|
+
const next = {
|
|
91
|
+
...row,
|
|
92
|
+
status: "COMPLETED",
|
|
93
|
+
txHash,
|
|
94
|
+
completedBy,
|
|
95
|
+
completedAtMs,
|
|
96
|
+
updatedAt: nowIso(),
|
|
97
|
+
};
|
|
98
|
+
this.rows.set(id, next);
|
|
99
|
+
return next;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
export class InMemoryReconciliationExceptionStore {
|
|
103
|
+
rows = new Map();
|
|
104
|
+
async append(e) {
|
|
105
|
+
if (this.rows.has(e.id))
|
|
106
|
+
return null;
|
|
107
|
+
const row = {
|
|
108
|
+
...e,
|
|
109
|
+
resolution: null,
|
|
110
|
+
resolvedBy: null,
|
|
111
|
+
resolvedAtMs: null,
|
|
112
|
+
createdAt: nowIso(),
|
|
113
|
+
};
|
|
114
|
+
this.rows.set(row.id, row);
|
|
115
|
+
return row;
|
|
116
|
+
}
|
|
117
|
+
async getById(id) {
|
|
118
|
+
return this.rows.get(id) ?? null;
|
|
119
|
+
}
|
|
120
|
+
async list(opts = {}) {
|
|
121
|
+
let out = [...this.rows.values()].filter((r) => (opts.status === undefined || r.status === opts.status) &&
|
|
122
|
+
(opts.type === undefined || r.type === opts.type) &&
|
|
123
|
+
(opts.subjectRef === undefined || r.subjectRef === opts.subjectRef) &&
|
|
124
|
+
(opts.sinceMs === undefined || r.createdAtMs >= opts.sinceMs));
|
|
125
|
+
out.sort((a, b) => b.createdAtMs - a.createdAtMs);
|
|
126
|
+
if (opts.limit !== undefined)
|
|
127
|
+
out = out.slice(0, opts.limit);
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
async resolve(id, resolution, resolvedBy, resolvedAtMs) {
|
|
131
|
+
const row = this.rows.get(id);
|
|
132
|
+
if (!row || row.status !== "OPEN")
|
|
133
|
+
return null;
|
|
134
|
+
const next = {
|
|
135
|
+
...row,
|
|
136
|
+
status: "RESOLVED",
|
|
137
|
+
resolution,
|
|
138
|
+
resolvedBy,
|
|
139
|
+
resolvedAtMs,
|
|
140
|
+
};
|
|
141
|
+
this.rows.set(id, next);
|
|
142
|
+
return next;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export function createInMemoryFinopsStores() {
|
|
146
|
+
return {
|
|
147
|
+
adminActions: new InMemoryAdminActionStore(),
|
|
148
|
+
refundIntents: new InMemoryRefundIntentStore(),
|
|
149
|
+
exceptions: new InMemoryReconciliationExceptionStore(),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { PrismaDb } from "./event-repository.js";
|
|
2
|
+
import type { ActorRow, ActorStore, ActorUpsert, EntitlementInsert, EntitlementRow, EntitlementStore, IdentityStores, ReputationFactInsert, ReputationFactRow, ReputationFactStore } from "./identity-store.js";
|
|
3
|
+
export declare class ActorRepository implements ActorStore {
|
|
4
|
+
private readonly db;
|
|
5
|
+
constructor(db: PrismaDb);
|
|
6
|
+
private get orm();
|
|
7
|
+
upsert(a: ActorUpsert): Promise<ActorRow>;
|
|
8
|
+
getById(wallet: string): Promise<ActorRow | null>;
|
|
9
|
+
list(opts?: {
|
|
10
|
+
type?: string;
|
|
11
|
+
limit?: number;
|
|
12
|
+
}): Promise<ActorRow[]>;
|
|
13
|
+
}
|
|
14
|
+
export declare class EntitlementRepository implements EntitlementStore {
|
|
15
|
+
private readonly db;
|
|
16
|
+
constructor(db: PrismaDb);
|
|
17
|
+
private get orm();
|
|
18
|
+
grant(e: EntitlementInsert): Promise<EntitlementRow | null>;
|
|
19
|
+
getById(id: string): Promise<EntitlementRow | null>;
|
|
20
|
+
getByIdempotencyKey(key: string): Promise<EntitlementRow | null>;
|
|
21
|
+
getActive(ownerId: string, productCode: string): Promise<EntitlementRow | null>;
|
|
22
|
+
listByOwner(ownerId: string, opts?: {
|
|
23
|
+
status?: string;
|
|
24
|
+
productCode?: string;
|
|
25
|
+
limit?: number;
|
|
26
|
+
}): Promise<EntitlementRow[]>;
|
|
27
|
+
listExpiring(nowMs: number, limit?: number): Promise<EntitlementRow[]>;
|
|
28
|
+
applyStatus(id: string, expectedFrom: string, to: string): Promise<EntitlementRow | null>;
|
|
29
|
+
}
|
|
30
|
+
export declare class ReputationFactRepository implements ReputationFactStore {
|
|
31
|
+
private readonly db;
|
|
32
|
+
constructor(db: PrismaDb);
|
|
33
|
+
private get orm();
|
|
34
|
+
append(f: ReputationFactInsert): Promise<ReputationFactRow | null>;
|
|
35
|
+
getById(id: string): Promise<ReputationFactRow | null>;
|
|
36
|
+
listByActor(actorWallet: string, opts?: {
|
|
37
|
+
factType?: string;
|
|
38
|
+
limit?: number;
|
|
39
|
+
}): Promise<ReputationFactRow[]>;
|
|
40
|
+
countsByActor(actorWallet: string): Promise<Record<string, number>>;
|
|
41
|
+
}
|
|
42
|
+
export declare function createPrismaIdentityStores(db: PrismaDb): IdentityStores;
|