@agentbadge/database 0.6.4 → 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 +2296 -817
- package/dist/repositories/commerce-repository.d.ts +2 -0
- package/dist/repositories/commerce-repository.js +69 -33
- package/dist/repositories/commerce-store.d.ts +4 -0
- package/dist/repositories/commerce-store.js +35 -9
- package/dist/repositories/event-repository.d.ts +1 -0
- package/dist/repositories/event-repository.js +30 -2
- 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/dist/store.d.ts +2 -0
- package/dist/store.js +3 -0
- package/package.json +1 -1
|
@@ -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;
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { isUniqueViolation } from "./commerce-repository.js";
|
|
2
|
+
const catchUnique = async (p) => {
|
|
3
|
+
try {
|
|
4
|
+
return await p;
|
|
5
|
+
}
|
|
6
|
+
catch (err) {
|
|
7
|
+
if (isUniqueViolation(err))
|
|
8
|
+
return null;
|
|
9
|
+
throw err;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const numOrNull = (v) => v == null ? null : Number(v);
|
|
13
|
+
const bigOrNull = (n) => n == null ? null : BigInt(n);
|
|
14
|
+
const toActorRow = (r) => ({
|
|
15
|
+
wallet: r.wallet,
|
|
16
|
+
type: r.type,
|
|
17
|
+
agentId: r.agentId,
|
|
18
|
+
passportTokenId: r.passportTokenId,
|
|
19
|
+
did: r.did,
|
|
20
|
+
orgId: r.orgId,
|
|
21
|
+
tier: r.tier,
|
|
22
|
+
payload: r.payload,
|
|
23
|
+
createdAt: r.createdAt,
|
|
24
|
+
updatedAt: r.updatedAt,
|
|
25
|
+
});
|
|
26
|
+
const entActiveSlot = (e) => e.status === "ACTIVE" ? `${e.ownerId}|${e.productCode}` : null;
|
|
27
|
+
const toEntitlementRow = (r) => ({
|
|
28
|
+
id: r.id,
|
|
29
|
+
ownerId: r.ownerId,
|
|
30
|
+
productCode: r.productCode,
|
|
31
|
+
sourcePaymentId: r.sourcePaymentId,
|
|
32
|
+
status: r.status,
|
|
33
|
+
validFromMs: Number(r.validFromMs),
|
|
34
|
+
validUntilMs: numOrNull(r.validUntilMs),
|
|
35
|
+
idempotencyKey: r.idempotencyKey,
|
|
36
|
+
activeSlot: r.activeSlot,
|
|
37
|
+
payload: r.payload,
|
|
38
|
+
createdAt: r.createdAt,
|
|
39
|
+
updatedAt: r.updatedAt,
|
|
40
|
+
});
|
|
41
|
+
const toFactRow = (r) => ({
|
|
42
|
+
id: r.id,
|
|
43
|
+
actorWallet: r.actorWallet,
|
|
44
|
+
factType: r.factType,
|
|
45
|
+
subjectId: r.subjectId,
|
|
46
|
+
sourceRef: r.sourceRef,
|
|
47
|
+
dedupeKey: r.dedupeKey,
|
|
48
|
+
atMs: Number(r.atMs),
|
|
49
|
+
payload: r.payload,
|
|
50
|
+
createdAt: r.createdAt,
|
|
51
|
+
});
|
|
52
|
+
export class ActorRepository {
|
|
53
|
+
db;
|
|
54
|
+
constructor(db) {
|
|
55
|
+
this.db = db;
|
|
56
|
+
}
|
|
57
|
+
get orm() {
|
|
58
|
+
return this.db.orm.public.Actor;
|
|
59
|
+
}
|
|
60
|
+
async upsert(a) {
|
|
61
|
+
const update = {
|
|
62
|
+
type: a.type,
|
|
63
|
+
payload: a.payload,
|
|
64
|
+
};
|
|
65
|
+
for (const key of [
|
|
66
|
+
"agentId",
|
|
67
|
+
"passportTokenId",
|
|
68
|
+
"did",
|
|
69
|
+
"orgId",
|
|
70
|
+
"tier",
|
|
71
|
+
]) {
|
|
72
|
+
if (a[key] !== undefined)
|
|
73
|
+
update[key] = a[key];
|
|
74
|
+
}
|
|
75
|
+
const row = await this.orm.upsert({
|
|
76
|
+
create: {
|
|
77
|
+
wallet: a.wallet,
|
|
78
|
+
type: a.type,
|
|
79
|
+
agentId: a.agentId ?? null,
|
|
80
|
+
passportTokenId: a.passportTokenId ?? null,
|
|
81
|
+
did: a.did ?? null,
|
|
82
|
+
orgId: a.orgId ?? null,
|
|
83
|
+
tier: a.tier ?? null,
|
|
84
|
+
payload: a.payload,
|
|
85
|
+
},
|
|
86
|
+
update: update,
|
|
87
|
+
conflictOn: { wallet: a.wallet },
|
|
88
|
+
});
|
|
89
|
+
return toActorRow(row);
|
|
90
|
+
}
|
|
91
|
+
async getById(wallet) {
|
|
92
|
+
const r = await this.orm.where({ wallet }).first();
|
|
93
|
+
return r ? toActorRow(r) : null;
|
|
94
|
+
}
|
|
95
|
+
async list(opts) {
|
|
96
|
+
let q = this.orm.orderBy((t) => t.createdAt.desc());
|
|
97
|
+
if (opts?.type !== undefined)
|
|
98
|
+
q = q.where({ type: opts.type });
|
|
99
|
+
if (opts?.limit !== undefined)
|
|
100
|
+
q = q.limit(opts.limit);
|
|
101
|
+
return (await q.all()).map(toActorRow);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export class EntitlementRepository {
|
|
105
|
+
db;
|
|
106
|
+
constructor(db) {
|
|
107
|
+
this.db = db;
|
|
108
|
+
}
|
|
109
|
+
get orm() {
|
|
110
|
+
return this.db.orm.public.Entitlement;
|
|
111
|
+
}
|
|
112
|
+
async grant(e) {
|
|
113
|
+
if (await this.getById(e.id))
|
|
114
|
+
return null;
|
|
115
|
+
if (await this.getByIdempotencyKey(e.idempotencyKey))
|
|
116
|
+
return null;
|
|
117
|
+
const row = await catchUnique(this.orm.create({
|
|
118
|
+
id: e.id,
|
|
119
|
+
ownerId: e.ownerId,
|
|
120
|
+
productCode: e.productCode,
|
|
121
|
+
sourcePaymentId: e.sourcePaymentId ?? null,
|
|
122
|
+
status: e.status,
|
|
123
|
+
validFromMs: BigInt(e.validFromMs),
|
|
124
|
+
validUntilMs: bigOrNull(e.validUntilMs),
|
|
125
|
+
idempotencyKey: e.idempotencyKey,
|
|
126
|
+
activeSlot: entActiveSlot(e),
|
|
127
|
+
payload: e.payload,
|
|
128
|
+
}));
|
|
129
|
+
return row ? toEntitlementRow(row) : null;
|
|
130
|
+
}
|
|
131
|
+
async getById(id) {
|
|
132
|
+
const r = await this.orm.where({ id }).first();
|
|
133
|
+
return r ? toEntitlementRow(r) : null;
|
|
134
|
+
}
|
|
135
|
+
async getByIdempotencyKey(key) {
|
|
136
|
+
const r = await this.orm.where({ idempotencyKey: key }).first();
|
|
137
|
+
return r ? toEntitlementRow(r) : null;
|
|
138
|
+
}
|
|
139
|
+
async getActive(ownerId, productCode) {
|
|
140
|
+
const r = await this.orm
|
|
141
|
+
.where({ ownerId, productCode, status: "ACTIVE" })
|
|
142
|
+
.first();
|
|
143
|
+
return r ? toEntitlementRow(r) : null;
|
|
144
|
+
}
|
|
145
|
+
async listByOwner(ownerId, opts) {
|
|
146
|
+
let q = this.orm.where({ ownerId }).orderBy((t) => t.createdAt.desc());
|
|
147
|
+
if (opts?.status !== undefined)
|
|
148
|
+
q = q.where({ status: opts.status });
|
|
149
|
+
if (opts?.productCode !== undefined)
|
|
150
|
+
q = q.where({ productCode: opts.productCode });
|
|
151
|
+
if (opts?.limit !== undefined)
|
|
152
|
+
q = q.limit(opts.limit);
|
|
153
|
+
return (await q.all()).map(toEntitlementRow);
|
|
154
|
+
}
|
|
155
|
+
async listExpiring(nowMs, limit) {
|
|
156
|
+
const need = limit ?? 50;
|
|
157
|
+
const rows = (await this.orm
|
|
158
|
+
.where({ status: "ACTIVE" })
|
|
159
|
+
.orderBy((t) => t.validUntilMs.asc())
|
|
160
|
+
.limit(need * 4)
|
|
161
|
+
.all())
|
|
162
|
+
.map(toEntitlementRow)
|
|
163
|
+
.filter((r) => r.validUntilMs != null && r.validUntilMs <= nowMs);
|
|
164
|
+
return rows.slice(0, need);
|
|
165
|
+
}
|
|
166
|
+
async applyStatus(id, expectedFrom, to) {
|
|
167
|
+
const existing = await this.getById(id);
|
|
168
|
+
if (!existing || existing.status !== expectedFrom)
|
|
169
|
+
return null;
|
|
170
|
+
const slot = entActiveSlot({ ...existing, status: to });
|
|
171
|
+
const row = await catchUnique(this.orm.where({ id, status: expectedFrom }).update({
|
|
172
|
+
status: to,
|
|
173
|
+
activeSlot: slot,
|
|
174
|
+
}));
|
|
175
|
+
return row ? toEntitlementRow(row) : null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
export class ReputationFactRepository {
|
|
179
|
+
db;
|
|
180
|
+
constructor(db) {
|
|
181
|
+
this.db = db;
|
|
182
|
+
}
|
|
183
|
+
get orm() {
|
|
184
|
+
return this.db.orm.public.ReputationFact;
|
|
185
|
+
}
|
|
186
|
+
async append(f) {
|
|
187
|
+
if (await this.getById(f.id))
|
|
188
|
+
return null;
|
|
189
|
+
const dup = await this.orm.where({ dedupeKey: f.dedupeKey }).first();
|
|
190
|
+
if (dup)
|
|
191
|
+
return null;
|
|
192
|
+
const row = await catchUnique(this.orm.create({
|
|
193
|
+
id: f.id,
|
|
194
|
+
actorWallet: f.actorWallet,
|
|
195
|
+
factType: f.factType,
|
|
196
|
+
subjectId: f.subjectId ?? null,
|
|
197
|
+
sourceRef: f.sourceRef,
|
|
198
|
+
dedupeKey: f.dedupeKey,
|
|
199
|
+
atMs: BigInt(f.atMs),
|
|
200
|
+
payload: f.payload,
|
|
201
|
+
}));
|
|
202
|
+
return row ? toFactRow(row) : null;
|
|
203
|
+
}
|
|
204
|
+
async getById(id) {
|
|
205
|
+
const r = await this.orm.where({ id }).first();
|
|
206
|
+
return r ? toFactRow(r) : null;
|
|
207
|
+
}
|
|
208
|
+
async listByActor(actorWallet, opts) {
|
|
209
|
+
let q = this.orm
|
|
210
|
+
.where({ actorWallet })
|
|
211
|
+
.orderBy((t) => t.createdAt.desc());
|
|
212
|
+
if (opts?.factType !== undefined)
|
|
213
|
+
q = q.where({ factType: opts.factType });
|
|
214
|
+
if (opts?.limit !== undefined)
|
|
215
|
+
q = q.limit(opts.limit);
|
|
216
|
+
return (await q.all()).map(toFactRow);
|
|
217
|
+
}
|
|
218
|
+
async countsByActor(actorWallet) {
|
|
219
|
+
const rows = await this.orm.where({ actorWallet }).all();
|
|
220
|
+
const out = {};
|
|
221
|
+
for (const r of rows)
|
|
222
|
+
out[r.factType] = (out[r.factType] ?? 0) + 1;
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
export function createPrismaIdentityStores(db) {
|
|
227
|
+
const bundle = (client) => ({
|
|
228
|
+
actors: new ActorRepository(client),
|
|
229
|
+
entitlements: new EntitlementRepository(client),
|
|
230
|
+
facts: new ReputationFactRepository(client),
|
|
231
|
+
atomic: outerAtomic,
|
|
232
|
+
});
|
|
233
|
+
async function outerAtomic(fn) {
|
|
234
|
+
for (let attempt = 0;; attempt++) {
|
|
235
|
+
try {
|
|
236
|
+
return await db.transaction((tx) => fn(bundle(tx)));
|
|
237
|
+
}
|
|
238
|
+
catch (err) {
|
|
239
|
+
if (attempt === 0 && isUniqueViolation(err))
|
|
240
|
+
continue;
|
|
241
|
+
throw err;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return bundle(db);
|
|
246
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
|
|
2
|
+
export interface ActorUpsert {
|
|
3
|
+
wallet: string;
|
|
4
|
+
type: string;
|
|
5
|
+
agentId?: string | null;
|
|
6
|
+
passportTokenId?: string | null;
|
|
7
|
+
did?: string | null;
|
|
8
|
+
orgId?: string | null;
|
|
9
|
+
tier?: string | null;
|
|
10
|
+
payload: JsonValue;
|
|
11
|
+
}
|
|
12
|
+
export interface ActorRow extends ActorUpsert {
|
|
13
|
+
createdAt: string;
|
|
14
|
+
updatedAt: string;
|
|
15
|
+
}
|
|
16
|
+
export interface ActorStore {
|
|
17
|
+
upsert(a: ActorUpsert): Promise<ActorRow>;
|
|
18
|
+
getById(wallet: string): Promise<ActorRow | null>;
|
|
19
|
+
list(opts?: {
|
|
20
|
+
type?: string;
|
|
21
|
+
limit?: number;
|
|
22
|
+
}): Promise<ActorRow[]>;
|
|
23
|
+
}
|
|
24
|
+
export interface EntitlementInsert {
|
|
25
|
+
id: string;
|
|
26
|
+
ownerId: string;
|
|
27
|
+
productCode: string;
|
|
28
|
+
sourcePaymentId?: string | null;
|
|
29
|
+
status: string;
|
|
30
|
+
validFromMs: number;
|
|
31
|
+
validUntilMs?: number | null;
|
|
32
|
+
idempotencyKey: string;
|
|
33
|
+
payload: JsonValue;
|
|
34
|
+
}
|
|
35
|
+
export interface EntitlementRow extends EntitlementInsert {
|
|
36
|
+
activeSlot: string | null;
|
|
37
|
+
createdAt: string;
|
|
38
|
+
updatedAt: string;
|
|
39
|
+
}
|
|
40
|
+
export interface EntitlementStore {
|
|
41
|
+
grant(e: EntitlementInsert): Promise<EntitlementRow | null>;
|
|
42
|
+
getById(id: string): Promise<EntitlementRow | null>;
|
|
43
|
+
getByIdempotencyKey(key: string): Promise<EntitlementRow | null>;
|
|
44
|
+
getActive(ownerId: string, productCode: string): Promise<EntitlementRow | null>;
|
|
45
|
+
listByOwner(ownerId: string, opts?: {
|
|
46
|
+
status?: string;
|
|
47
|
+
productCode?: string;
|
|
48
|
+
limit?: number;
|
|
49
|
+
}): Promise<EntitlementRow[]>;
|
|
50
|
+
listExpiring(nowMs: number, limit?: number): Promise<EntitlementRow[]>;
|
|
51
|
+
applyStatus(id: string, expectedFrom: string, to: string): Promise<EntitlementRow | null>;
|
|
52
|
+
}
|
|
53
|
+
export interface ReputationFactInsert {
|
|
54
|
+
id: string;
|
|
55
|
+
actorWallet: string;
|
|
56
|
+
factType: string;
|
|
57
|
+
subjectId?: string | null;
|
|
58
|
+
sourceRef: string;
|
|
59
|
+
dedupeKey: string;
|
|
60
|
+
atMs: number;
|
|
61
|
+
payload: JsonValue;
|
|
62
|
+
}
|
|
63
|
+
export interface ReputationFactRow extends ReputationFactInsert {
|
|
64
|
+
createdAt: string;
|
|
65
|
+
}
|
|
66
|
+
export interface ReputationFactStore {
|
|
67
|
+
append(f: ReputationFactInsert): Promise<ReputationFactRow | null>;
|
|
68
|
+
getById(id: string): Promise<ReputationFactRow | null>;
|
|
69
|
+
listByActor(actorWallet: string, opts?: {
|
|
70
|
+
factType?: string;
|
|
71
|
+
limit?: number;
|
|
72
|
+
}): Promise<ReputationFactRow[]>;
|
|
73
|
+
countsByActor(actorWallet: string): Promise<Record<string, number>>;
|
|
74
|
+
}
|
|
75
|
+
export interface IdentityStores {
|
|
76
|
+
actors: ActorStore;
|
|
77
|
+
entitlements: EntitlementStore;
|
|
78
|
+
facts: ReputationFactStore;
|
|
79
|
+
atomic<R>(fn: (stores: IdentityStores) => Promise<R>): Promise<R>;
|
|
80
|
+
}
|
|
81
|
+
export declare class InMemoryActorStore implements ActorStore {
|
|
82
|
+
private readonly rows;
|
|
83
|
+
upsert(a: ActorUpsert): Promise<ActorRow>;
|
|
84
|
+
getById(wallet: string): Promise<ActorRow | null>;
|
|
85
|
+
list(opts?: {
|
|
86
|
+
type?: string;
|
|
87
|
+
limit?: number;
|
|
88
|
+
}): Promise<ActorRow[]>;
|
|
89
|
+
}
|
|
90
|
+
export declare class InMemoryEntitlementStore implements EntitlementStore {
|
|
91
|
+
private readonly rows;
|
|
92
|
+
grant(e: EntitlementInsert): Promise<EntitlementRow | null>;
|
|
93
|
+
getById(id: string): Promise<EntitlementRow | null>;
|
|
94
|
+
getByIdempotencyKey(key: string): Promise<EntitlementRow | null>;
|
|
95
|
+
getActive(ownerId: string, productCode: string): Promise<EntitlementRow | null>;
|
|
96
|
+
listByOwner(ownerId: string, opts?: {
|
|
97
|
+
status?: string;
|
|
98
|
+
productCode?: string;
|
|
99
|
+
limit?: number;
|
|
100
|
+
}): Promise<EntitlementRow[]>;
|
|
101
|
+
listExpiring(nowMs: number, limit?: number): Promise<EntitlementRow[]>;
|
|
102
|
+
applyStatus(id: string, expectedFrom: string, to: string): Promise<EntitlementRow | null>;
|
|
103
|
+
}
|
|
104
|
+
export declare class InMemoryReputationFactStore implements ReputationFactStore {
|
|
105
|
+
private readonly rows;
|
|
106
|
+
append(f: ReputationFactInsert): Promise<ReputationFactRow | null>;
|
|
107
|
+
getById(id: string): Promise<ReputationFactRow | null>;
|
|
108
|
+
listByActor(actorWallet: string, opts?: {
|
|
109
|
+
factType?: string;
|
|
110
|
+
limit?: number;
|
|
111
|
+
}): Promise<ReputationFactRow[]>;
|
|
112
|
+
countsByActor(actorWallet: string): Promise<Record<string, number>>;
|
|
113
|
+
}
|
|
114
|
+
export declare function createInMemoryIdentityStores(): IdentityStores;
|