feat(admin): billing tab to monitor subscriptions and sales (#952)

* feat(admin): add a Billing tab for subscriptions and sales

The admin can now follow subscriptions and sales without opening Stripe:
- Summary tiles: active subscriptions (monthly/annual), MRR with annual
  plans counted as 1/12, sales and revenue for 7d/30d/90d/12m/all time,
  all-time sales, subscriptions cancelling at period end, and refunds
  and disputes.
- A stacked chart of sales per day, week or month by plan family.
- A by-plan table: active grants, period and all-time sales and revenue.
- A paginated list of grants with plan, Stripe status, active/expired/
  revoked and user filters, linked to the admin user, repository or
  conference.
- One row of webhook health: pending, failed, oldest pending event, last
  error and unreviewed payment reversals.

A sale is one checkout or subscription, so a paper pack's three grants
count once. Revenue is each sale's Stripe list price, resolved once per
price and cached in memory. Currencies are never added together. Without
Stripe the page shows counts and marks revenue as unavailable.

GET /api/admin/billing/summary and /grants sit behind the admin check.
Grants get a { purchasedAt, _id } index for the list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: preserve payment history in admin billing totals

* fix: scope billing history and preserve original sale prices

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas DurieuxandClaude Opus 5.5 authored and GitHub committed 2026-11-13 14:37:29 +01:00
1 parent 476f25e264
commit 511dcd7067
25 files changed
+1371 -59

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
import * as express from "express";
import { AsyncCache } from "../../core/async-cache";
import { BILLING_PERIODS, billingGrants, billingSummary, billingWebhookHealth } from "../../core/billing/report";
import UserModel from "../../core/model/users/users.model";
import { handleError } from "./route-utils";
// Mounted under the admin router, after its administrator check.
const router = express.Router();
const summaryCache = new AsyncCache<Awaited<ReturnType<typeof billingSummary>>>(30_000, 8);
async function usernames(ids: string[]) {
const users = await UserModel.find({ _id: { $in: ids.filter(id => /^[a-f0-9]{24}$/.test(id)) } }).select("username").lean();
return new Map(users.map(u => [String(u._id), u.username as string]));
}
router.get("/summary", async (req, res) => {
try {
const period = typeof req.query.period === "string" && req.query.period in BILLING_PERIODS ? req.query.period : "30d";
const [summary, webhooks] = await Promise.all([summaryCache.get(period, () => billingSummary(period)), billingWebhookHealth()]);
res.set("Cache-Control", "no-store").json({ ...summary, webhooks });
} catch (error) { handleError(error, res, req); }
});
router.get("/grants", async (req, res) => {
try {
res.set("Cache-Control", "no-store").json(await billingGrants({ ...req.query, owners: null }, usernames));
} catch (error) { handleError(error, res, req); }
});
export default router;
+2
View File
@@ -21,6 +21,7 @@ import User from "../../core/User";
import { ensureAuthenticated } from "./connection";
import { handleError, getUser, isOwnerOrAdmin, getRepo } from "./route-utils";
import adminTokensRouter from "./admin-tokens";
import adminBillingRouter from "./admin-billing";
import { octokit, getToken } from "../../core/GitHubUtils";
import {
createLogger,
@@ -83,6 +84,7 @@ router.use(
);
router.use("/tokens", adminTokensRouter);
router.use("/billing", adminBillingRouter);
router.get("/performance", async (req, res) => {
res.set("Cache-Control", "no-store");