Files
loki/scripts/grant-plan.ts
CatoandClaude Fable 5.1 95c40a9cd8 refactor: rename the product from FleetCrown to Loki (#695)
The product, its assistant, the repo, the host and every unit on the box now
share one name. 638 files, ~3,000 mentions: UI copy, config, env keys
(FLEETCROWN_* -> LOKI_*), systemd units (fleetcrown-* -> loki-*), docs,
articles, the deploy workflow other repos reuse, and nine file names.

- Three article slugs carried the old name and had been shared; permanent
  redirects keep those links alive (next.config.ts).
- test-no-retired-name.sh now also forbids the old product name across the
  whole repo, so the rename cannot creep back through templates and examples.
- ASSISTANT_STACK collapses the product and the assistant into one entry,
  because they are now the same name.

Infra was cut over ahead of this merge: /opt/loki, loki-* units, the `loki`
database and role, loki.orangecat.ch (old host 308s to it; /api, /widget,
/embed keep proxying), GitHub repos bitbaum/loki and bitbaum/loki-releases.
The Resend sending domain stays fleetcrown.orangecat.ch until the new domain
is verified in DNS.


Claude-Session: https://claude.ai/code/session_016YJVAanfj8keVSXyZnTemh

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-13 16:13:10 -07:00

82 lines
2.8 KiB
TypeScript

/**
* Manual plan grant — the Phase-0 OrangeCat-rail entitlement path.
*
* Until the OC→FC payment webhook exists (Phase 1), a Loki tier bought in
* Bitcoin through OrangeCat is granted by hand with this: confirm the BTC payment
* settled on the OC side, then run this to flip `users.plan` — the SAME write
* (updateUserBilling) that /api/orangecat/entitlement performs, so a hand grant
* and an automated one are indistinguishable downstream. (This used to say "the
* same write the Stripe webhook does"; that rail was removed in #508.) Records
* the grant in debug_logs for an audit trail.
*
* npx tsx scripts/grant-plan.ts <userId> <free|personal|pro|team> "<reason/oc-payment-ref>"
*
* Reversible: re-run with `free` to downgrade. Point DATABASE_URL at the target
* (a prod tunnel for a live grant).
*/
import { getUserById, updateUserBilling } from "@/db/queries/users";
import { countActiveProjects } from "@/db/queries/user-projects";
import { getProjectLimit, isUnlimitedProjects } from "@/lib/plan";
import { logDebug } from "@/db/queries/debug-logs";
import { PLAN_VALUES, type Plan } from "@/db/schema/users";
function limitLabel(plan: Plan): string {
return isUnlimitedProjects(plan) ? "Unlimited" : String(getProjectLimit(plan));
}
async function main() {
const [userId, plan, reason] = process.argv.slice(2);
if (!userId || !plan) {
console.error('Usage: grant-plan.ts <userId> <free|personal|pro|team> "<reason>"');
process.exit(2);
}
if (!(PLAN_VALUES as readonly string[]).includes(plan)) {
console.error(`Invalid plan "${plan}". One of: ${PLAN_VALUES.join(", ")}`);
process.exit(2);
}
const before = await getUserById(userId);
if (!before) {
console.error(`No user ${userId}`);
process.exit(1);
}
const count = await countActiveProjects(userId).catch(() => 0);
console.log(`user ${userId} (${before.email ?? "?"})`);
console.log(
` BEFORE: plan=${before.plan} status=${before.planStatus ?? "—"} projectLimit=${limitLabel(before.plan)} (using ${count})`,
);
const targetPlan = plan as Plan;
const updated = await updateUserBilling(userId, {
plan: targetPlan,
planStatus: targetPlan === "free" ? "canceled" : "active",
});
if (!updated) {
console.error("update failed");
process.exit(1);
}
await logDebug({
source: "manual-grant",
level: "info",
message: `plan ${before.plan} → ${targetPlan}`,
meta: {
userId,
from: before.plan,
to: targetPlan,
reason: reason ?? null,
rail: "orangecat-btc",
},
}).catch(() => {});
console.log(
` AFTER: plan=${updated.plan} status=${updated.planStatus} projectLimit=${limitLabel(updated.plan)}`,
);
console.log(` reason: ${reason ?? "(none)"}`);
process.exit(0);
}
main().catch((e) => {
console.error(e instanceof Error ? e.message : e);
process.exit(1);
});