@agentbadge/database 0.5.1 → 0.5.2

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.
@@ -2,6 +2,7 @@ import { type PrismaDb } from "./repositories/event-repository.js";
2
2
  import { AgentWalletRepository } from "./repositories/agent-wallet-repository.js";
3
3
  import { SpendLedgerRepository } from "./repositories/spend-ledger-repository.js";
4
4
  import { SpendAlertRepository } from "./repositories/spend-alert-repository.js";
5
+ import { SpendApprovalRepository } from "./repositories/spend-approval-repository.js";
5
6
  import { DelegateRepository } from "./repositories/delegate-repository.js";
6
7
  import { MarketplaceRepository } from "./repositories/marketplace-repository.js";
7
8
  import { EaasVerdictRepository } from "./repositories/eaas-verdict-repository.js";
@@ -23,6 +24,7 @@ export interface Database {
23
24
  agentWallets: AgentWalletRepository | null;
24
25
  spendLedger: SpendLedgerRepository | null;
25
26
  spendAlerts: SpendAlertRepository | null;
27
+ spendApprovals: SpendApprovalRepository | null;
26
28
  delegates: DelegateRepository | null;
27
29
  marketplace: MarketplaceRepository | null;
28
30
  eaasVerdicts: EaasVerdictRepository | null;
package/dist/database.js CHANGED
@@ -6,6 +6,7 @@ import { ChatSubscriptionRepository } from "./repositories/chat-subscription-rep
6
6
  import { AgentWalletRepository } from "./repositories/agent-wallet-repository.js";
7
7
  import { SpendLedgerRepository } from "./repositories/spend-ledger-repository.js";
8
8
  import { SpendAlertRepository } from "./repositories/spend-alert-repository.js";
9
+ import { SpendApprovalRepository } from "./repositories/spend-approval-repository.js";
9
10
  import { DelegateRepository } from "./repositories/delegate-repository.js";
10
11
  import { MarketplaceRepository } from "./repositories/marketplace-repository.js";
11
12
  import { EaasVerdictRepository } from "./repositories/eaas-verdict-repository.js";
@@ -27,6 +28,7 @@ export function createDatabase(config) {
27
28
  agentWallets: null,
28
29
  spendLedger: null,
29
30
  spendAlerts: null,
31
+ spendApprovals: null,
30
32
  delegates: null,
31
33
  marketplace: null,
32
34
  eaasVerdicts: null,
@@ -48,6 +50,7 @@ export function createDatabase(config) {
48
50
  agentWallets: new AgentWalletRepository(db),
49
51
  spendLedger: new SpendLedgerRepository(db),
50
52
  spendAlerts: new SpendAlertRepository(db),
53
+ spendApprovals: new SpendApprovalRepository(db),
51
54
  delegates: new DelegateRepository(db),
52
55
  marketplace: new MarketplaceRepository(db),
53
56
  eaasVerdicts: new EaasVerdictRepository(db),
package/dist/index.d.ts CHANGED
@@ -17,6 +17,8 @@ export { SpendLedgerRepository } from "./repositories/spend-ledger-repository.js
17
17
  export type { SpendEventInsert } from "./repositories/spend-ledger-repository.js";
18
18
  export { SpendAlertRepository } from "./repositories/spend-alert-repository.js";
19
19
  export type { SpendAlertInsert } from "./repositories/spend-alert-repository.js";
20
+ export { SpendApprovalRepository } from "./repositories/spend-approval-repository.js";
21
+ export type { SpendApprovalUpsert } from "./repositories/spend-approval-repository.js";
20
22
  export { DelegateRepository } from "./repositories/delegate-repository.js";
21
23
  export type { DelegateRecordUpsert } from "./repositories/delegate-repository.js";
22
24
  export { MarketplaceRepository } from "./repositories/marketplace-repository.js";
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ export { VenueRepository } from "./repositories/venue-repository.js";
10
10
  export { AgentWalletRepository } from "./repositories/agent-wallet-repository.js";
11
11
  export { SpendLedgerRepository } from "./repositories/spend-ledger-repository.js";
12
12
  export { SpendAlertRepository } from "./repositories/spend-alert-repository.js";
13
+ export { SpendApprovalRepository } from "./repositories/spend-approval-repository.js";
13
14
  export { DelegateRepository } from "./repositories/delegate-repository.js";
14
15
  export { MarketplaceRepository } from "./repositories/marketplace-repository.js";
15
16
  export { EaasVerdictRepository } from "./repositories/eaas-verdict-repository.js";
@@ -56,6 +56,10 @@
56
56
  "model": "ScanResult",
57
57
  "namespace": "public"
58
58
  },
59
+ "spendApproval": {
60
+ "model": "SpendApproval",
61
+ "namespace": "public"
62
+ },
59
63
  "spendEvent": {
60
64
  "model": "SpendEvent",
61
65
  "namespace": "public"
@@ -100,6 +104,20 @@
100
104
  "kind": "scalar"
101
105
  }
102
106
  },
107
+ "allowedKinds": {
108
+ "nullable": true,
109
+ "type": {
110
+ "codecId": "pg/json@1",
111
+ "kind": "scalar"
112
+ }
113
+ },
114
+ "approvalAboveUsd": {
115
+ "nullable": true,
116
+ "type": {
117
+ "codecId": "pg/numeric@1",
118
+ "kind": "scalar"
119
+ }
120
+ },
103
121
  "createdAt": {
104
122
  "nullable": false,
105
123
  "type": {
@@ -128,6 +146,20 @@
128
146
  "kind": "scalar"
129
147
  }
130
148
  },
149
+ "maxAmountPerHour": {
150
+ "nullable": true,
151
+ "type": {
152
+ "codecId": "pg/numeric@1",
153
+ "kind": "scalar"
154
+ }
155
+ },
156
+ "maxTxPerHour": {
157
+ "nullable": true,
158
+ "type": {
159
+ "codecId": "pg/int4@1",
160
+ "kind": "scalar"
161
+ }
162
+ },
131
163
  "payload": {
132
164
  "nullable": false,
133
165
  "type": {
@@ -142,6 +174,27 @@
142
174
  "kind": "scalar"
143
175
  }
144
176
  },
177
+ "suspended": {
178
+ "nullable": false,
179
+ "type": {
180
+ "codecId": "pg/bool@1",
181
+ "kind": "scalar"
182
+ }
183
+ },
184
+ "suspendedAt": {
185
+ "nullable": true,
186
+ "type": {
187
+ "codecId": "pg/int8@1",
188
+ "kind": "scalar"
189
+ }
190
+ },
191
+ "suspendedBy": {
192
+ "nullable": true,
193
+ "type": {
194
+ "codecId": "pg/text@1",
195
+ "kind": "scalar"
196
+ }
197
+ },
145
198
  "updatedAt": {
146
199
  "nullable": false,
147
200
  "type": {
@@ -169,6 +222,12 @@
169
222
  "agentId": {
170
223
  "column": "agentId"
171
224
  },
225
+ "allowedKinds": {
226
+ "column": "allowedKinds"
227
+ },
228
+ "approvalAboveUsd": {
229
+ "column": "approvalAboveUsd"
230
+ },
172
231
  "createdAt": {
173
232
  "column": "createdAt"
174
233
  },
@@ -181,12 +240,27 @@
181
240
  "label": {
182
241
  "column": "label"
183
242
  },
243
+ "maxAmountPerHour": {
244
+ "column": "maxAmountPerHour"
245
+ },
246
+ "maxTxPerHour": {
247
+ "column": "maxTxPerHour"
248
+ },
184
249
  "payload": {
185
250
  "column": "payload"
186
251
  },
187
252
  "registeredBy": {
188
253
  "column": "registeredBy"
189
254
  },
255
+ "suspended": {
256
+ "column": "suspended"
257
+ },
258
+ "suspendedAt": {
259
+ "column": "suspendedAt"
260
+ },
261
+ "suspendedBy": {
262
+ "column": "suspendedBy"
263
+ },
190
264
  "updatedAt": {
191
265
  "column": "updatedAt"
192
266
  },
@@ -1300,6 +1374,167 @@
1300
1374
  "table": "scanResult"
1301
1375
  }
1302
1376
  },
1377
+ "SpendApproval": {
1378
+ "fields": {
1379
+ "amountUsd": {
1380
+ "nullable": false,
1381
+ "type": {
1382
+ "codecId": "pg/numeric@1",
1383
+ "kind": "scalar"
1384
+ }
1385
+ },
1386
+ "consumedAtMs": {
1387
+ "nullable": true,
1388
+ "type": {
1389
+ "codecId": "pg/int8@1",
1390
+ "kind": "scalar"
1391
+ }
1392
+ },
1393
+ "createdAt": {
1394
+ "nullable": false,
1395
+ "type": {
1396
+ "codecId": "pg/timestamptz-string@1",
1397
+ "kind": "scalar"
1398
+ }
1399
+ },
1400
+ "createdAtMs": {
1401
+ "nullable": false,
1402
+ "type": {
1403
+ "codecId": "pg/int8@1",
1404
+ "kind": "scalar"
1405
+ }
1406
+ },
1407
+ "decidedAtMs": {
1408
+ "nullable": true,
1409
+ "type": {
1410
+ "codecId": "pg/int8@1",
1411
+ "kind": "scalar"
1412
+ }
1413
+ },
1414
+ "decidedBy": {
1415
+ "nullable": true,
1416
+ "type": {
1417
+ "codecId": "pg/text@1",
1418
+ "kind": "scalar"
1419
+ }
1420
+ },
1421
+ "expiresAtMs": {
1422
+ "nullable": false,
1423
+ "type": {
1424
+ "codecId": "pg/int8@1",
1425
+ "kind": "scalar"
1426
+ }
1427
+ },
1428
+ "id": {
1429
+ "nullable": false,
1430
+ "type": {
1431
+ "codecId": "pg/text@1",
1432
+ "kind": "scalar"
1433
+ }
1434
+ },
1435
+ "kind": {
1436
+ "nullable": false,
1437
+ "type": {
1438
+ "codecId": "pg/text@1",
1439
+ "kind": "scalar"
1440
+ }
1441
+ },
1442
+ "payload": {
1443
+ "nullable": false,
1444
+ "type": {
1445
+ "codecId": "pg/json@1",
1446
+ "kind": "scalar"
1447
+ }
1448
+ },
1449
+ "refId": {
1450
+ "nullable": false,
1451
+ "type": {
1452
+ "codecId": "pg/text@1",
1453
+ "kind": "scalar"
1454
+ }
1455
+ },
1456
+ "state": {
1457
+ "nullable": false,
1458
+ "type": {
1459
+ "codecId": "pg/text@1",
1460
+ "kind": "scalar"
1461
+ }
1462
+ },
1463
+ "updatedAt": {
1464
+ "nullable": false,
1465
+ "type": {
1466
+ "codecId": "pg/timestamptz-string@1",
1467
+ "kind": "scalar"
1468
+ }
1469
+ },
1470
+ "venueId": {
1471
+ "nullable": true,
1472
+ "type": {
1473
+ "codecId": "pg/text@1",
1474
+ "kind": "scalar"
1475
+ }
1476
+ },
1477
+ "wallet": {
1478
+ "nullable": false,
1479
+ "type": {
1480
+ "codecId": "pg/text@1",
1481
+ "kind": "scalar"
1482
+ }
1483
+ }
1484
+ },
1485
+ "relations": {},
1486
+ "storage": {
1487
+ "fields": {
1488
+ "amountUsd": {
1489
+ "column": "amountUsd"
1490
+ },
1491
+ "consumedAtMs": {
1492
+ "column": "consumedAtMs"
1493
+ },
1494
+ "createdAt": {
1495
+ "column": "createdAt"
1496
+ },
1497
+ "createdAtMs": {
1498
+ "column": "createdAtMs"
1499
+ },
1500
+ "decidedAtMs": {
1501
+ "column": "decidedAtMs"
1502
+ },
1503
+ "decidedBy": {
1504
+ "column": "decidedBy"
1505
+ },
1506
+ "expiresAtMs": {
1507
+ "column": "expiresAtMs"
1508
+ },
1509
+ "id": {
1510
+ "column": "id"
1511
+ },
1512
+ "kind": {
1513
+ "column": "kind"
1514
+ },
1515
+ "payload": {
1516
+ "column": "payload"
1517
+ },
1518
+ "refId": {
1519
+ "column": "refId"
1520
+ },
1521
+ "state": {
1522
+ "column": "state"
1523
+ },
1524
+ "updatedAt": {
1525
+ "column": "updatedAt"
1526
+ },
1527
+ "venueId": {
1528
+ "column": "venueId"
1529
+ },
1530
+ "wallet": {
1531
+ "column": "wallet"
1532
+ }
1533
+ },
1534
+ "namespaceId": "public",
1535
+ "table": "spendApproval"
1536
+ }
1537
+ },
1303
1538
  "SpendEvent": {
1304
1539
  "fields": {
1305
1540
  "amountUsd": {
@@ -1684,6 +1919,16 @@
1684
1919
  "nativeType": "text",
1685
1920
  "nullable": true
1686
1921
  },
1922
+ "allowedKinds": {
1923
+ "codecId": "pg/json@1",
1924
+ "nativeType": "json",
1925
+ "nullable": true
1926
+ },
1927
+ "approvalAboveUsd": {
1928
+ "codecId": "pg/numeric@1",
1929
+ "nativeType": "numeric",
1930
+ "nullable": true
1931
+ },
1687
1932
  "createdAt": {
1688
1933
  "codecId": "pg/timestamptz-string@1",
1689
1934
  "default": {
@@ -1708,6 +1953,16 @@
1708
1953
  "nativeType": "text",
1709
1954
  "nullable": false
1710
1955
  },
1956
+ "maxAmountPerHour": {
1957
+ "codecId": "pg/numeric@1",
1958
+ "nativeType": "numeric",
1959
+ "nullable": true
1960
+ },
1961
+ "maxTxPerHour": {
1962
+ "codecId": "pg/int4@1",
1963
+ "nativeType": "int4",
1964
+ "nullable": true
1965
+ },
1711
1966
  "payload": {
1712
1967
  "codecId": "pg/json@1",
1713
1968
  "nativeType": "json",
@@ -1718,6 +1973,25 @@
1718
1973
  "nativeType": "text",
1719
1974
  "nullable": false
1720
1975
  },
1976
+ "suspended": {
1977
+ "codecId": "pg/bool@1",
1978
+ "default": {
1979
+ "kind": "literal",
1980
+ "value": false
1981
+ },
1982
+ "nativeType": "bool",
1983
+ "nullable": false
1984
+ },
1985
+ "suspendedAt": {
1986
+ "codecId": "pg/int8@1",
1987
+ "nativeType": "int8",
1988
+ "nullable": true
1989
+ },
1990
+ "suspendedBy": {
1991
+ "codecId": "pg/text@1",
1992
+ "nativeType": "text",
1993
+ "nullable": true
1994
+ },
1721
1995
  "updatedAt": {
1722
1996
  "codecId": "pg/timestamptz-string@1",
1723
1997
  "nativeType": "timestamptz",
@@ -2617,6 +2891,120 @@
2617
2891
  },
2618
2892
  "uniques": []
2619
2893
  },
2894
+ "spendApproval": {
2895
+ "columns": {
2896
+ "amountUsd": {
2897
+ "codecId": "pg/numeric@1",
2898
+ "nativeType": "numeric",
2899
+ "nullable": false
2900
+ },
2901
+ "consumedAtMs": {
2902
+ "codecId": "pg/int8@1",
2903
+ "nativeType": "int8",
2904
+ "nullable": true
2905
+ },
2906
+ "createdAt": {
2907
+ "codecId": "pg/timestamptz-string@1",
2908
+ "default": {
2909
+ "expression": "now()",
2910
+ "kind": "function"
2911
+ },
2912
+ "nativeType": "timestamptz",
2913
+ "nullable": false
2914
+ },
2915
+ "createdAtMs": {
2916
+ "codecId": "pg/int8@1",
2917
+ "nativeType": "int8",
2918
+ "nullable": false
2919
+ },
2920
+ "decidedAtMs": {
2921
+ "codecId": "pg/int8@1",
2922
+ "nativeType": "int8",
2923
+ "nullable": true
2924
+ },
2925
+ "decidedBy": {
2926
+ "codecId": "pg/text@1",
2927
+ "nativeType": "text",
2928
+ "nullable": true
2929
+ },
2930
+ "expiresAtMs": {
2931
+ "codecId": "pg/int8@1",
2932
+ "nativeType": "int8",
2933
+ "nullable": false
2934
+ },
2935
+ "id": {
2936
+ "codecId": "pg/text@1",
2937
+ "nativeType": "text",
2938
+ "nullable": false
2939
+ },
2940
+ "kind": {
2941
+ "codecId": "pg/text@1",
2942
+ "nativeType": "text",
2943
+ "nullable": false
2944
+ },
2945
+ "payload": {
2946
+ "codecId": "pg/json@1",
2947
+ "nativeType": "json",
2948
+ "nullable": false
2949
+ },
2950
+ "refId": {
2951
+ "codecId": "pg/text@1",
2952
+ "nativeType": "text",
2953
+ "nullable": false
2954
+ },
2955
+ "state": {
2956
+ "codecId": "pg/text@1",
2957
+ "default": {
2958
+ "kind": "literal",
2959
+ "value": "pending"
2960
+ },
2961
+ "nativeType": "text",
2962
+ "nullable": false
2963
+ },
2964
+ "updatedAt": {
2965
+ "codecId": "pg/timestamptz-string@1",
2966
+ "nativeType": "timestamptz",
2967
+ "nullable": false
2968
+ },
2969
+ "venueId": {
2970
+ "codecId": "pg/text@1",
2971
+ "nativeType": "text",
2972
+ "nullable": true
2973
+ },
2974
+ "wallet": {
2975
+ "codecId": "pg/text@1",
2976
+ "nativeType": "text",
2977
+ "nullable": false
2978
+ }
2979
+ },
2980
+ "foreignKeys": [],
2981
+ "indexes": [
2982
+ {
2983
+ "columns": [
2984
+ "wallet",
2985
+ "createdAtMs"
2986
+ ],
2987
+ "name": "spendApproval_wallet_createdAtMs_idx_70458d62",
2988
+ "prefix": "spendApproval_wallet_createdAtMs_idx",
2989
+ "unique": false
2990
+ },
2991
+ {
2992
+ "columns": [
2993
+ "wallet",
2994
+ "state"
2995
+ ],
2996
+ "name": "spendApproval_wallet_state_idx_68707ee0",
2997
+ "prefix": "spendApproval_wallet_state_idx",
2998
+ "unique": false
2999
+ }
3000
+ ],
3001
+ "primaryKey": {
3002
+ "columns": [
3003
+ "id"
3004
+ ]
3005
+ },
3006
+ "uniques": []
3007
+ },
2620
3008
  "spendEvent": {
2621
3009
  "columns": {
2622
3010
  "amountUsd": {
@@ -2921,10 +3309,10 @@
2921
3309
  "kind": "postgres-schema"
2922
3310
  }
2923
3311
  },
2924
- "storageHash": "0f835062304d587ab0193284ffd553d6e21cc9287c5c6034050dcec9548b5fa9"
3312
+ "storageHash": "f6ed86f749de40634b0e1d780fc73776b5156edd40dc9839cf1d94826cce653b"
2925
3313
  },
2926
3314
  "execution": {
2927
- "executionHash": "17c77dc31dfeba93bb11fbb0b8d0d156d9f58ee8d3db073359617bf0abcca5b7",
3315
+ "executionHash": "5a12f7c955315a204fddb1c746399171f30156749652d8cb192fc8f373c7640a",
2928
3316
  "mutations": {
2929
3317
  "defaults": [
2930
3318
  {
@@ -3151,6 +3539,21 @@
3151
3539
  "table": "scanResult"
3152
3540
  }
3153
3541
  },
3542
+ {
3543
+ "onCreate": {
3544
+ "id": "timestampNow",
3545
+ "kind": "generator"
3546
+ },
3547
+ "onUpdate": {
3548
+ "id": "timestampNow",
3549
+ "kind": "generator"
3550
+ },
3551
+ "ref": {
3552
+ "column": "updatedAt",
3553
+ "namespace": "public",
3554
+ "table": "spendApproval"
3555
+ }
3556
+ },
3154
3557
  {
3155
3558
  "onCreate": {
3156
3559
  "id": "timestampNow",
@@ -10,6 +10,13 @@ export interface AgentWalletUpsert {
10
10
  kind: string;
11
11
  registeredBy: string;
12
12
  active?: boolean;
13
+ suspended?: boolean;
14
+ suspendedAt?: number | null;
15
+ suspendedBy?: string | null;
16
+ approvalAboveUsd?: number | null;
17
+ maxTxPerHour?: number | null;
18
+ maxAmountPerHour?: number | null;
19
+ allowedKinds?: JsonValue | null;
13
20
  payload: JsonValue;
14
21
  }
15
22
  export declare class AgentWalletRepository {
@@ -1,3 +1,5 @@
1
+ const decOrNull = (n) => n == null ? null : String(n);
2
+ const bigOrNull = (n) => n == null ? null : BigInt(n);
1
3
  export class AgentWalletRepository {
2
4
  db;
3
5
  constructor(db) {
@@ -13,6 +15,13 @@ export class AgentWalletRepository {
13
15
  kind: w.kind,
14
16
  registeredBy: w.registeredBy,
15
17
  active: w.active ?? true,
18
+ suspended: w.suspended ?? false,
19
+ suspendedAt: bigOrNull(w.suspendedAt),
20
+ suspendedBy: w.suspendedBy ?? null,
21
+ approvalAboveUsd: decOrNull(w.approvalAboveUsd),
22
+ maxTxPerHour: w.maxTxPerHour ?? null,
23
+ maxAmountPerHour: decOrNull(w.maxAmountPerHour),
24
+ allowedKinds: w.allowedKinds ?? null,
16
25
  payload: w.payload,
17
26
  },
18
27
  update: {
@@ -22,6 +31,25 @@ export class AgentWalletRepository {
22
31
  kind: w.kind,
23
32
  registeredBy: w.registeredBy,
24
33
  ...(w.active !== undefined ? { active: w.active } : {}),
34
+ ...(w.suspended !== undefined ? { suspended: w.suspended } : {}),
35
+ ...(w.suspendedAt !== undefined
36
+ ? { suspendedAt: bigOrNull(w.suspendedAt) }
37
+ : {}),
38
+ ...(w.suspendedBy !== undefined
39
+ ? { suspendedBy: w.suspendedBy }
40
+ : {}),
41
+ ...(w.approvalAboveUsd !== undefined
42
+ ? { approvalAboveUsd: decOrNull(w.approvalAboveUsd) }
43
+ : {}),
44
+ ...(w.maxTxPerHour !== undefined
45
+ ? { maxTxPerHour: w.maxTxPerHour }
46
+ : {}),
47
+ ...(w.maxAmountPerHour !== undefined
48
+ ? { maxAmountPerHour: decOrNull(w.maxAmountPerHour) }
49
+ : {}),
50
+ ...(w.allowedKinds !== undefined
51
+ ? { allowedKinds: w.allowedKinds }
52
+ : {}),
25
53
  payload: w.payload,
26
54
  },
27
55
  conflictOn: { address: w.address.toLowerCase() },
@@ -0,0 +1,32 @@
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_SpendApproval;
5
+ export interface SpendApprovalUpsert {
6
+ id: string;
7
+ wallet: string;
8
+ venueId?: string | null;
9
+ amountUsd: number;
10
+ kind: string;
11
+ refId: string;
12
+ state: string;
13
+ decidedBy?: string | null;
14
+ decidedAtMs?: number | null;
15
+ consumedAtMs?: number | null;
16
+ expiresAtMs: number;
17
+ createdAtMs: number;
18
+ payload: JsonValue;
19
+ }
20
+ export declare class SpendApprovalRepository {
21
+ private readonly db;
22
+ constructor(db: PrismaDb);
23
+ put(a: SpendApprovalUpsert): Promise<Row>;
24
+ get(id: string): Promise<Row | null>;
25
+ listByWallet(wallet: string, state?: string): Promise<Row[]>;
26
+ listAll(limit?: number): Promise<Row[]>;
27
+ decide(id: string, to: "approved" | "rejected", actor: string, decidedAtMs: number, expiresAtMs?: number, payload?: JsonValue): Promise<boolean>;
28
+ consume(id: string, consumedAtMs: number, payload?: JsonValue): Promise<boolean>;
29
+ expire(id: string, payload?: JsonValue): Promise<boolean>;
30
+ listPending(): Promise<Row[]>;
31
+ }
32
+ export {};
@@ -0,0 +1,89 @@
1
+ const toBig = (n) => BigInt(n);
2
+ const toBigOrNull = (n) => n == null ? null : BigInt(n);
3
+ export class SpendApprovalRepository {
4
+ db;
5
+ constructor(db) {
6
+ this.db = db;
7
+ }
8
+ async put(a) {
9
+ return this.db.orm.public.SpendApproval.upsert({
10
+ create: {
11
+ id: a.id,
12
+ wallet: a.wallet.toLowerCase(),
13
+ venueId: a.venueId ?? null,
14
+ amountUsd: String(a.amountUsd),
15
+ kind: a.kind,
16
+ refId: a.refId,
17
+ state: a.state,
18
+ decidedBy: a.decidedBy ?? null,
19
+ decidedAtMs: toBigOrNull(a.decidedAtMs),
20
+ consumedAtMs: toBigOrNull(a.consumedAtMs),
21
+ expiresAtMs: toBig(a.expiresAtMs),
22
+ createdAtMs: toBig(a.createdAtMs),
23
+ payload: a.payload,
24
+ },
25
+ update: {
26
+ state: a.state,
27
+ decidedBy: a.decidedBy ?? null,
28
+ decidedAtMs: toBigOrNull(a.decidedAtMs),
29
+ consumedAtMs: toBigOrNull(a.consumedAtMs),
30
+ expiresAtMs: toBig(a.expiresAtMs),
31
+ payload: a.payload,
32
+ },
33
+ conflictOn: { id: a.id },
34
+ });
35
+ }
36
+ async get(id) {
37
+ return this.db.orm.public.SpendApproval.where({ id }).first();
38
+ }
39
+ async listByWallet(wallet, state) {
40
+ let q = this.db.orm.public.SpendApproval.orderBy((a) => a.createdAtMs.desc()).where({ wallet: wallet.toLowerCase() });
41
+ if (state !== undefined)
42
+ q = q.where({ state });
43
+ return q.all();
44
+ }
45
+ async listAll(limit = 1_000) {
46
+ return this.db.orm.public.SpendApproval.orderBy((a) => a.createdAtMs.desc())
47
+ .limit(limit)
48
+ .all();
49
+ }
50
+ async decide(id, to, actor, decidedAtMs, expiresAtMs, payload) {
51
+ const row = await this.db.orm.public.SpendApproval.where({
52
+ id,
53
+ state: "pending",
54
+ }).update({
55
+ state: to,
56
+ decidedBy: actor,
57
+ decidedAtMs: toBig(decidedAtMs),
58
+ ...(expiresAtMs !== undefined ? { expiresAtMs: toBig(expiresAtMs) } : {}),
59
+ ...(payload !== undefined ? { payload } : {}),
60
+ });
61
+ return row !== null;
62
+ }
63
+ async consume(id, consumedAtMs, payload) {
64
+ const row = await this.db.orm.public.SpendApproval.where({
65
+ id,
66
+ state: "approved",
67
+ }).update({
68
+ state: "consumed",
69
+ consumedAtMs: toBig(consumedAtMs),
70
+ ...(payload !== undefined ? { payload } : {}),
71
+ });
72
+ return row !== null;
73
+ }
74
+ async expire(id, payload) {
75
+ const row = await this.db.orm.public.SpendApproval.where({
76
+ id,
77
+ state: "pending",
78
+ }).update({
79
+ state: "expired",
80
+ ...(payload !== undefined ? { payload } : {}),
81
+ });
82
+ return row !== null;
83
+ }
84
+ async listPending() {
85
+ return this.db.orm.public.SpendApproval.where({
86
+ state: "pending",
87
+ }).all();
88
+ }
89
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/database",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Shared database layer for AgentBadge — Prisma ORM 8 client, repositories, store abstraction",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",