@agentbadge/database 0.6.9 → 0.6.11
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,4 +1,6 @@
|
|
|
1
|
+
import { param } from "@prisma/orm-postgres/relational-core/expression";
|
|
1
2
|
import { isUniqueViolation } from "./commerce-repository.js";
|
|
3
|
+
import { parseReversalCursor } from "./identity-store.js";
|
|
2
4
|
const catchUnique = async (p) => {
|
|
3
5
|
try {
|
|
4
6
|
return await p;
|
|
@@ -38,6 +40,24 @@ const toEntitlementRow = (r) => ({
|
|
|
38
40
|
createdAt: r.createdAt,
|
|
39
41
|
updatedAt: r.updatedAt,
|
|
40
42
|
});
|
|
43
|
+
const ENTITLEMENT_ROW_SPEC = {
|
|
44
|
+
id: "pg/text@1",
|
|
45
|
+
ownerId: "pg/text@1",
|
|
46
|
+
productCode: "pg/text@1",
|
|
47
|
+
sourcePaymentId: { codecId: "pg/text@1", nullable: true },
|
|
48
|
+
status: "pg/text@1",
|
|
49
|
+
validFromMs: "pg/int8@1",
|
|
50
|
+
validUntilMs: { codecId: "pg/int8@1", nullable: true },
|
|
51
|
+
idempotencyKey: "pg/text@1",
|
|
52
|
+
activeSlot: { codecId: "pg/text@1", nullable: true },
|
|
53
|
+
payload: "pg/json@1",
|
|
54
|
+
createdAt: "pg/timestamptz-string@1",
|
|
55
|
+
updatedAt: "pg/timestamptz-string@1",
|
|
56
|
+
};
|
|
57
|
+
const FACT_COUNT_ROW_SPEC = {
|
|
58
|
+
factType: "pg/text@1",
|
|
59
|
+
n: "pg/int8@1",
|
|
60
|
+
};
|
|
41
61
|
const toFactRow = (r) => ({
|
|
42
62
|
id: r.id,
|
|
43
63
|
actorWallet: r.actorWallet,
|
|
@@ -164,24 +184,19 @@ export class EntitlementRepository {
|
|
|
164
184
|
return rows.slice(0, need);
|
|
165
185
|
}
|
|
166
186
|
async listActiveWithSource(limit, afterKey) {
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
.
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
out = out.filter((r) => r.createdAt > cAt || (r.createdAt === cAt && r.id > cId));
|
|
181
|
-
}
|
|
182
|
-
if (limit !== undefined)
|
|
183
|
-
out = out.slice(0, limit);
|
|
184
|
-
return out;
|
|
187
|
+
const lim = param(limit ?? 500, { codecId: "pg/int4@1" });
|
|
188
|
+
const cur = afterKey !== undefined ? parseReversalCursor(afterKey) : null;
|
|
189
|
+
const plan = cur !== null
|
|
190
|
+
? this.db.raw
|
|
191
|
+
.sql `SELECT "id","ownerId","productCode","sourcePaymentId","status","validFromMs","validUntilMs","idempotencyKey","activeSlot","payload","createdAt","updatedAt" FROM "entitlement" WHERE "status" = 'ACTIVE' AND "sourcePaymentId" IS NOT NULL AND ("createdAt","id") > (${param(cur.cAt, { codecId: "pg/timestamptz-string@1" })}, ${param(cur.cId, { codecId: "pg/text@1" })})) ORDER BY "createdAt" ASC, "id" ASC LIMIT ${lim}`
|
|
192
|
+
.returnsRow(ENTITLEMENT_ROW_SPEC)
|
|
193
|
+
.build()
|
|
194
|
+
: this.db.raw
|
|
195
|
+
.sql `SELECT "id","ownerId","productCode","sourcePaymentId","status","validFromMs","validUntilMs","idempotencyKey","activeSlot","payload","createdAt","updatedAt" FROM "entitlement" WHERE "status" = 'ACTIVE' AND "sourcePaymentId" IS NOT NULL ORDER BY "createdAt" ASC, "id" ASC LIMIT ${lim}`
|
|
196
|
+
.returnsRow(ENTITLEMENT_ROW_SPEC)
|
|
197
|
+
.build();
|
|
198
|
+
const rows = await this.db.runtime().query(plan);
|
|
199
|
+
return rows.map(toEntitlementRow);
|
|
185
200
|
}
|
|
186
201
|
async applyStatus(id, expectedFrom, to, payloadPatch) {
|
|
187
202
|
const existing = await this.getById(id);
|
|
@@ -243,10 +258,14 @@ export class ReputationFactRepository {
|
|
|
243
258
|
return (await q.all()).map(toFactRow);
|
|
244
259
|
}
|
|
245
260
|
async countsByActor(actorWallet) {
|
|
246
|
-
const
|
|
261
|
+
const plan = this.db.raw
|
|
262
|
+
.sql `SELECT "factType", COUNT(*) AS "n" FROM "reputationFact" WHERE "actorWallet" = ${param(actorWallet, { codecId: "pg/text@1" })} GROUP BY "factType"`
|
|
263
|
+
.returnsRow(FACT_COUNT_ROW_SPEC)
|
|
264
|
+
.build();
|
|
265
|
+
const rows = (await this.db.runtime().query(plan));
|
|
247
266
|
const out = {};
|
|
248
267
|
for (const r of rows)
|
|
249
|
-
out[r.factType] = (
|
|
268
|
+
out[r.factType] = Number(r.n);
|
|
250
269
|
return out;
|
|
251
270
|
}
|
|
252
271
|
}
|
|
@@ -79,6 +79,10 @@ export interface IdentityStores {
|
|
|
79
79
|
facts: ReputationFactStore;
|
|
80
80
|
atomic<R>(fn: (stores: IdentityStores) => Promise<R>): Promise<R>;
|
|
81
81
|
}
|
|
82
|
+
export declare const parseReversalCursor: (afterKey: string) => {
|
|
83
|
+
cAt: string;
|
|
84
|
+
cId: string;
|
|
85
|
+
} | null;
|
|
82
86
|
export declare class InMemoryActorStore implements ActorStore {
|
|
83
87
|
private readonly rows;
|
|
84
88
|
upsert(a: ActorUpsert): Promise<ActorRow>;
|
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
const nowIso = () => new Date().toISOString();
|
|
2
|
+
const CURSOR_TS_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?(Z|[+-]\d{2}:\d{2})$/;
|
|
3
|
+
export const parseReversalCursor = (afterKey) => {
|
|
4
|
+
const sep = afterKey.indexOf("|");
|
|
5
|
+
if (sep <= 0 || sep === afterKey.length - 1)
|
|
6
|
+
return null;
|
|
7
|
+
const cAt = afterKey.slice(0, sep);
|
|
8
|
+
const cId = afterKey.slice(sep + 1);
|
|
9
|
+
if (!CURSOR_TS_RE.test(cAt))
|
|
10
|
+
return null;
|
|
11
|
+
const parsed = Date.parse(cAt);
|
|
12
|
+
if (Number.isNaN(parsed))
|
|
13
|
+
return null;
|
|
14
|
+
return { cAt, cId };
|
|
15
|
+
};
|
|
2
16
|
const entActiveSlot = (e) => e.status === "ACTIVE" ? `${e.ownerId}|${e.productCode}` : null;
|
|
3
17
|
export class InMemoryActorStore {
|
|
4
18
|
rows = new Map();
|
|
@@ -90,10 +104,11 @@ export class InMemoryEntitlementStore {
|
|
|
90
104
|
out = out.sort((a, b) => a.createdAt.localeCompare(b.createdAt) ||
|
|
91
105
|
a.id.localeCompare(b.id));
|
|
92
106
|
if (afterKey !== undefined) {
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
107
|
+
const cur = parseReversalCursor(afterKey);
|
|
108
|
+
if (cur) {
|
|
109
|
+
out = out.filter((r) => r.createdAt > cur.cAt ||
|
|
110
|
+
(r.createdAt === cur.cAt && r.id > cur.cId));
|
|
111
|
+
}
|
|
97
112
|
}
|
|
98
113
|
if (limit !== undefined)
|
|
99
114
|
out = out.slice(0, limit);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { param } from "@prisma/orm-postgres/relational-core/expression";
|
|
1
2
|
import { TERMINAL_ATTEMPT_STATUSES } from "./payment-store.js";
|
|
2
3
|
const num = (v) => Number(v);
|
|
3
4
|
const numOrNull = (v) => v === null || v === undefined ? null : Number(v);
|
|
@@ -38,6 +39,23 @@ const toAttemptRow = (r) => ({
|
|
|
38
39
|
createdAt: r.createdAt,
|
|
39
40
|
updatedAt: r.updatedAt,
|
|
40
41
|
});
|
|
42
|
+
const ATTEMPT_ROW_SPEC = {
|
|
43
|
+
id: "pg/text@1",
|
|
44
|
+
paymentId: "pg/text@1",
|
|
45
|
+
provider: "pg/text@1",
|
|
46
|
+
network: { codecId: "pg/text@1", nullable: true },
|
|
47
|
+
token: { codecId: "pg/text@1", nullable: true },
|
|
48
|
+
amount: "pg/numeric@1",
|
|
49
|
+
currency: "pg/text@1",
|
|
50
|
+
status: "pg/text@1",
|
|
51
|
+
providerReference: { codecId: "pg/text@1", nullable: true },
|
|
52
|
+
transactionHash: { codecId: "pg/text@1", nullable: true },
|
|
53
|
+
expiresAtMs: { codecId: "pg/int8@1", nullable: true },
|
|
54
|
+
confirmedAtMs: { codecId: "pg/int8@1", nullable: true },
|
|
55
|
+
payload: "pg/json@1",
|
|
56
|
+
createdAt: "pg/timestamptz-string@1",
|
|
57
|
+
updatedAt: "pg/timestamptz-string@1",
|
|
58
|
+
};
|
|
41
59
|
const toReceiptRow = (r) => ({
|
|
42
60
|
id: r.id,
|
|
43
61
|
paymentId: r.paymentId,
|
|
@@ -296,16 +314,12 @@ export class PaymentAttemptRepository {
|
|
|
296
314
|
async listRecentTerminal(sinceMs) {
|
|
297
315
|
const out = [];
|
|
298
316
|
for (const status of TERMINAL_ATTEMPT_STATUSES) {
|
|
299
|
-
const
|
|
300
|
-
.
|
|
301
|
-
.
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
: (numOrNull(r.confirmedAtMs) ?? 0);
|
|
306
|
-
if (stamp >= sinceMs)
|
|
307
|
-
out.push(toAttemptRow(r));
|
|
308
|
-
}
|
|
317
|
+
const plan = this.db.raw
|
|
318
|
+
.sql `SELECT "id","paymentId","provider","network","token","amount","currency","status","providerReference","transactionHash","expiresAtMs","confirmedAtMs","payload","createdAt","updatedAt" FROM "paymentAttempt" WHERE "status" = ${param(status, { codecId: "pg/text@1" })} AND "updatedAt" >= to_timestamp(${param(sinceMs, { codecId: "pg/int8@1" })} / 1000.0) ORDER BY "updatedAt" ASC, "id" ASC`
|
|
319
|
+
.returnsRow(ATTEMPT_ROW_SPEC)
|
|
320
|
+
.build();
|
|
321
|
+
const rows = await this.db.runtime().query(plan);
|
|
322
|
+
out.push(...rows.map(toAttemptRow));
|
|
309
323
|
}
|
|
310
324
|
return out;
|
|
311
325
|
}
|
package/package.json
CHANGED