@agentbadge/database 0.6.6 → 0.6.8

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.
@@ -1,3 +1,4 @@
1
+ import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
1
2
  import type { PrismaDb } from "./event-repository.js";
2
3
  import type { ActorRow, ActorStore, ActorUpsert, EntitlementInsert, EntitlementRow, EntitlementStore, IdentityStores, ReputationFactInsert, ReputationFactRow, ReputationFactStore } from "./identity-store.js";
3
4
  export declare class ActorRepository implements ActorStore {
@@ -25,7 +26,8 @@ export declare class EntitlementRepository implements EntitlementStore {
25
26
  limit?: number;
26
27
  }): Promise<EntitlementRow[]>;
27
28
  listExpiring(nowMs: number, limit?: number): Promise<EntitlementRow[]>;
28
- applyStatus(id: string, expectedFrom: string, to: string): Promise<EntitlementRow | null>;
29
+ listActiveWithSource(limit?: number, afterId?: string): Promise<EntitlementRow[]>;
30
+ applyStatus(id: string, expectedFrom: string, to: string, payloadPatch?: Record<string, JsonValue | undefined>): Promise<EntitlementRow | null>;
29
31
  }
30
32
  export declare class ReputationFactRepository implements ReputationFactStore {
31
33
  private readonly db;
@@ -163,15 +163,40 @@ export class EntitlementRepository {
163
163
  .filter((r) => r.validUntilMs != null && r.validUntilMs <= nowMs);
164
164
  return rows.slice(0, need);
165
165
  }
166
- async applyStatus(id, expectedFrom, to) {
166
+ async listActiveWithSource(limit, afterId) {
167
+ const rows = await this.orm
168
+ .where({ status: "ACTIVE" })
169
+ .orderBy((t) => t.createdAt.asc())
170
+ .all();
171
+ let out = rows
172
+ .map(toEntitlementRow)
173
+ .filter((r) => r.sourcePaymentId != null);
174
+ out = out.sort((a, b) => a.createdAt.localeCompare(b.createdAt) ||
175
+ a.id.localeCompare(b.id));
176
+ if (afterId !== undefined) {
177
+ const idx = out.findIndex((r) => r.id === afterId);
178
+ out = idx >= 0 ? out.slice(idx + 1) : out;
179
+ }
180
+ if (limit !== undefined)
181
+ out = out.slice(0, limit);
182
+ return out;
183
+ }
184
+ async applyStatus(id, expectedFrom, to, payloadPatch) {
167
185
  const existing = await this.getById(id);
168
186
  if (!existing || existing.status !== expectedFrom)
169
187
  return null;
170
188
  const slot = entActiveSlot({ ...existing, status: to });
171
- const row = await catchUnique(this.orm.where({ id, status: expectedFrom }).update({
189
+ const update = {
172
190
  status: to,
173
191
  activeSlot: slot,
174
- }));
192
+ };
193
+ if (payloadPatch) {
194
+ const base = existing.payload && typeof existing.payload === "object"
195
+ ? existing.payload
196
+ : {};
197
+ update.payload = { ...base, ...payloadPatch };
198
+ }
199
+ const row = await catchUnique(this.orm.where({ id, status: expectedFrom }).update(update));
175
200
  return row ? toEntitlementRow(row) : null;
176
201
  }
177
202
  }
@@ -48,7 +48,8 @@ export interface EntitlementStore {
48
48
  limit?: number;
49
49
  }): Promise<EntitlementRow[]>;
50
50
  listExpiring(nowMs: number, limit?: number): Promise<EntitlementRow[]>;
51
- applyStatus(id: string, expectedFrom: string, to: string): Promise<EntitlementRow | null>;
51
+ listActiveWithSource(limit?: number, afterId?: string): Promise<EntitlementRow[]>;
52
+ applyStatus(id: string, expectedFrom: string, to: string, payloadPatch?: Record<string, JsonValue | undefined>): Promise<EntitlementRow | null>;
52
53
  }
53
54
  export interface ReputationFactInsert {
54
55
  id: string;
@@ -99,7 +100,8 @@ export declare class InMemoryEntitlementStore implements EntitlementStore {
99
100
  limit?: number;
100
101
  }): Promise<EntitlementRow[]>;
101
102
  listExpiring(nowMs: number, limit?: number): Promise<EntitlementRow[]>;
102
- applyStatus(id: string, expectedFrom: string, to: string): Promise<EntitlementRow | null>;
103
+ listActiveWithSource(limit?: number, afterId?: string): Promise<EntitlementRow[]>;
104
+ applyStatus(id: string, expectedFrom: string, to: string, payloadPatch?: Record<string, JsonValue | undefined>): Promise<EntitlementRow | null>;
103
105
  }
104
106
  export declare class InMemoryReputationFactStore implements ReputationFactStore {
105
107
  private readonly rows;
@@ -14,7 +14,7 @@ export class InMemoryActorStore {
14
14
  did: merge("did"),
15
15
  orgId: merge("orgId"),
16
16
  tier: merge("tier"),
17
- payload: a.payload,
17
+ payload: structuredClone(a.payload),
18
18
  createdAt: existing?.createdAt ?? ts,
19
19
  updatedAt: ts,
20
20
  };
@@ -52,7 +52,7 @@ export class InMemoryEntitlementStore {
52
52
  createdAt: ts,
53
53
  updatedAt: ts,
54
54
  };
55
- this.rows.set(row.id, row);
55
+ this.rows.set(row.id, structuredClone(row));
56
56
  return row;
57
57
  }
58
58
  async getById(id) {
@@ -85,7 +85,19 @@ export class InMemoryEntitlementStore {
85
85
  out = out.slice(0, limit);
86
86
  return out;
87
87
  }
88
- async applyStatus(id, expectedFrom, to) {
88
+ async listActiveWithSource(limit, afterId) {
89
+ let out = [...this.rows.values()].filter((r) => r.status === "ACTIVE" && r.sourcePaymentId != null);
90
+ out = out.sort((a, b) => a.createdAt.localeCompare(b.createdAt) ||
91
+ a.id.localeCompare(b.id));
92
+ if (afterId !== undefined) {
93
+ const idx = out.findIndex((r) => r.id === afterId);
94
+ out = idx >= 0 ? out.slice(idx + 1) : out;
95
+ }
96
+ if (limit !== undefined)
97
+ out = out.slice(0, limit);
98
+ return out;
99
+ }
100
+ async applyStatus(id, expectedFrom, to, payloadPatch) {
89
101
  const row = this.rows.get(id);
90
102
  if (!row || row.status !== expectedFrom)
91
103
  return null;
@@ -99,6 +111,15 @@ export class InMemoryEntitlementStore {
99
111
  activeSlot: slot,
100
112
  updatedAt: nowIso(),
101
113
  };
114
+ if (payloadPatch) {
115
+ const base = next.payload && typeof next.payload === "object"
116
+ ? next.payload
117
+ : {};
118
+ next.payload = {
119
+ ...base,
120
+ ...payloadPatch,
121
+ };
122
+ }
102
123
  this.rows.set(id, next);
103
124
  return next;
104
125
  }
@@ -115,7 +136,7 @@ export class InMemoryReputationFactStore {
115
136
  subjectId: f.subjectId ?? null,
116
137
  createdAt: nowIso(),
117
138
  };
118
- this.rows.set(row.id, row);
139
+ this.rows.set(row.id, structuredClone(row));
119
140
  return row;
120
141
  }
121
142
  async getById(id) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/database",
3
- "version": "0.6.6",
3
+ "version": "0.6.8",
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",