@secondlayer/shared 6.5.0 → 6.7.0
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/README.md +9 -12
- package/dist/src/db/index.js.map +2 -2
- package/dist/src/db/queries/chain-reorgs.js.map +2 -2
- package/dist/src/index.d.ts +29 -42
- package/dist/src/index.js +94 -103
- package/dist/src/index.js.map +4 -5
- package/dist/src/schemas/index.d.ts +29 -42
- package/dist/src/schemas/index.js +94 -103
- package/dist/src/schemas/index.js.map +3 -4
- package/package.json +1 -29
- package/dist/src/db/queries/account-spend-caps.d.ts +0 -729
- package/dist/src/db/queries/account-spend-caps.js +0 -59
- package/dist/src/db/queries/account-spend-caps.js.map +0 -10
- package/dist/src/db/queries/account-usage.d.ts +0 -741
- package/dist/src/db/queries/account-usage.js +0 -266
- package/dist/src/db/queries/account-usage.js.map +0 -11
- package/dist/src/db/queries/accounts.d.ts +0 -741
- package/dist/src/db/queries/accounts.js +0 -86
- package/dist/src/db/queries/accounts.js.map +0 -10
- package/dist/src/db/queries/projects.d.ts +0 -722
- package/dist/src/db/queries/projects.js +0 -47
- package/dist/src/db/queries/projects.js.map +0 -10
- package/dist/src/db/queries/usage.d.ts +0 -731
- package/dist/src/db/queries/usage.js +0 -109
- package/dist/src/db/queries/usage.js.map +0 -10
- package/dist/src/pricing.d.ts +0 -60
- package/dist/src/pricing.js +0 -164
- package/dist/src/pricing.js.map +0 -10
- package/dist/src/schemas/accounts.d.ts +0 -14
- package/dist/src/schemas/accounts.js +0 -29
- package/dist/src/schemas/accounts.js.map +0 -10
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { createRequire } from "node:module";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __returnValue = (v) => v;
|
|
4
|
-
function __exportSetter(name, newValue) {
|
|
5
|
-
this[name] = __returnValue.bind(null, newValue);
|
|
6
|
-
}
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, {
|
|
10
|
-
get: all[name],
|
|
11
|
-
enumerable: true,
|
|
12
|
-
configurable: true,
|
|
13
|
-
set: __exportSetter.bind(all, name)
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
// src/db/queries/account-spend-caps.ts
|
|
18
|
-
async function getCaps(db, accountId) {
|
|
19
|
-
const row = await db.selectFrom("account_spend_caps").selectAll().where("account_id", "=", accountId).executeTakeFirst();
|
|
20
|
-
return row ?? null;
|
|
21
|
-
}
|
|
22
|
-
async function upsertCaps(db, accountId, patch) {
|
|
23
|
-
const insert = {
|
|
24
|
-
account_id: accountId,
|
|
25
|
-
monthly_cap_cents: patch.monthly_cap_cents ?? null,
|
|
26
|
-
compute_cap_cents: patch.compute_cap_cents ?? null,
|
|
27
|
-
storage_cap_cents: patch.storage_cap_cents ?? null,
|
|
28
|
-
alert_threshold_pct: patch.alert_threshold_pct ?? 80,
|
|
29
|
-
alert_sent_at: patch.alert_sent_at ?? null,
|
|
30
|
-
frozen_at: patch.frozen_at ?? null
|
|
31
|
-
};
|
|
32
|
-
return db.insertInto("account_spend_caps").values(insert).onConflict((oc) => oc.column("account_id").doUpdateSet({
|
|
33
|
-
...patch,
|
|
34
|
-
updated_at: new Date
|
|
35
|
-
})).returningAll().executeTakeFirstOrThrow();
|
|
36
|
-
}
|
|
37
|
-
async function freezeAccount(db, accountId) {
|
|
38
|
-
await upsertCaps(db, accountId, { frozen_at: new Date });
|
|
39
|
-
}
|
|
40
|
-
async function clearFreeze(db, accountId) {
|
|
41
|
-
await upsertCaps(db, accountId, {
|
|
42
|
-
frozen_at: null,
|
|
43
|
-
alert_sent_at: null
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
async function listFrozenAccountIds(db) {
|
|
47
|
-
const rows = await db.selectFrom("account_spend_caps").select("account_id").where("frozen_at", "is not", null).execute();
|
|
48
|
-
return new Set(rows.map((r) => r.account_id));
|
|
49
|
-
}
|
|
50
|
-
export {
|
|
51
|
-
upsertCaps,
|
|
52
|
-
listFrozenAccountIds,
|
|
53
|
-
getCaps,
|
|
54
|
-
freezeAccount,
|
|
55
|
-
clearFreeze
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
//# debugId=61F9CA6C05B3563A64756E2164756E21
|
|
59
|
-
//# sourceMappingURL=account-spend-caps.js.map
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/db/queries/account-spend-caps.ts"],
|
|
4
|
-
"sourcesContent": [
|
|
5
|
-
"import type { Kysely } from \"kysely\";\nimport type {\n\tAccountSpendCap,\n\tDatabase,\n\tInsertAccountSpendCap,\n\tUpdateAccountSpendCap,\n} from \"../types.ts\";\n\n/**\n * Spend-cap state for an account. Both the metering crons (check + set\n * frozen_at) and the dashboard (read + update caps) call through here.\n */\n\nexport async function getCaps(\n\tdb: Kysely<Database>,\n\taccountId: string,\n): Promise<AccountSpendCap | null> {\n\tconst row = await db\n\t\t.selectFrom(\"account_spend_caps\")\n\t\t.selectAll()\n\t\t.where(\"account_id\", \"=\", accountId)\n\t\t.executeTakeFirst();\n\treturn row ?? null;\n}\n\n/**\n * Upsert semantics: row is created on first write (default threshold\n * 80%), subsequent writes PATCH. `updated_at` is always bumped.\n */\nexport async function upsertCaps(\n\tdb: Kysely<Database>,\n\taccountId: string,\n\tpatch: Omit<UpdateAccountSpendCap, \"account_id\" | \"updated_at\">,\n): Promise<AccountSpendCap> {\n\tconst insert: InsertAccountSpendCap = {\n\t\taccount_id: accountId,\n\t\tmonthly_cap_cents: patch.monthly_cap_cents ?? null,\n\t\tcompute_cap_cents: patch.compute_cap_cents ?? null,\n\t\tstorage_cap_cents: patch.storage_cap_cents ?? null,\n\t\talert_threshold_pct: patch.alert_threshold_pct ?? 80,\n\t\talert_sent_at: patch.alert_sent_at ?? null,\n\t\tfrozen_at: patch.frozen_at ?? null,\n\t};\n\n\treturn db\n\t\t.insertInto(\"account_spend_caps\")\n\t\t.values(insert)\n\t\t.onConflict((oc) =>\n\t\t\toc.column(\"account_id\").doUpdateSet({\n\t\t\t\t...patch,\n\t\t\t\tupdated_at: new Date(),\n\t\t\t}),\n\t\t)\n\t\t.returningAll()\n\t\t.executeTakeFirstOrThrow();\n}\n\n/** Mark an account frozen at the current time (cap just tripped). */\nexport async function freezeAccount(\n\tdb: Kysely<Database>,\n\taccountId: string,\n): Promise<void> {\n\tawait upsertCaps(db, accountId, { frozen_at: new Date() });\n}\n\n/**\n * Clear the frozen + alert state — called on `invoice.paid` webhook at\n * cycle rollover (new billing period starts fresh) OR when the user\n * explicitly raises their cap above current usage.\n */\nexport async function clearFreeze(\n\tdb: Kysely<Database>,\n\taccountId: string,\n): Promise<void> {\n\tawait upsertCaps(db, accountId, {\n\t\tfrozen_at: null,\n\t\talert_sent_at: null,\n\t});\n}\n\n/** Is this account currently cap-frozen? Bulk-checked by metering crons. */\nexport async function listFrozenAccountIds(\n\tdb: Kysely<Database>,\n): Promise<Set<string>> {\n\tconst rows = await db\n\t\t.selectFrom(\"account_spend_caps\")\n\t\t.select(\"account_id\")\n\t\t.where(\"frozen_at\", \"is not\", null)\n\t\t.execute();\n\treturn new Set(rows.map((r) => r.account_id));\n}\n"
|
|
6
|
-
],
|
|
7
|
-
"mappings": ";;;;;;;;;;;;;;;;;AAaA,eAAsB,OAAO,CAC5B,IACA,WACkC;AAAA,EAClC,MAAM,MAAM,MAAM,GAChB,WAAW,oBAAoB,EAC/B,UAAU,EACV,MAAM,cAAc,KAAK,SAAS,EAClC,iBAAiB;AAAA,EACnB,OAAO,OAAO;AAAA;AAOf,eAAsB,UAAU,CAC/B,IACA,WACA,OAC2B;AAAA,EAC3B,MAAM,SAAgC;AAAA,IACrC,YAAY;AAAA,IACZ,mBAAmB,MAAM,qBAAqB;AAAA,IAC9C,mBAAmB,MAAM,qBAAqB;AAAA,IAC9C,mBAAmB,MAAM,qBAAqB;AAAA,IAC9C,qBAAqB,MAA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