mail-server serves apps/mail/dashboard at /dashboard/ (tower-http fs,
DASHBOARD_DIR, mock/ and MOUNT.md never served). New account-scoped,
Resend-shaped endpoints the dashboard reads: GET /emails/{id}/events
(status history with timestamps and a one-line detail from the stored
payload), GET /emails/{id}/webhook-deliveries and
GET /webhooks/{id}/deliveries (delivery log joined with event + endpoint),
GET /emails?test=true|false and GET /suppressions?q= server filters.
GET /billing now carries description, next_plan, upgrade_label,
current_period_end/renews_at; POST /billing/checkout accepts success_url
(own origin only) and returns url. mail-auth: a bad, used or expired link
opened in a browser redirects to <redirect_to>#error=invalid_token (or a
plain HTML page when the token was never ours); the dashboard shows the
message on the login form. Dashboard uses the server-side test filter,
portal via POST /billing/portal, docs updated.
Verified live against local Postgres mail_e2e + fake relay + webhook
listener: curl of every endpoint (404 for another account's ids, 401
without auth, 422 on test=maybe), and a headless-browser pass through the
real magic-link login, email timeline, webhook delivery log, usage card
and the expired-link redirect.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
96 lines
5.5 KiB
JavaScript
96 lines
5.5 KiB
JavaScript
// Usage and plan: quota bars from GET /usage (+ GET /billing when it exists),
|
|
// upgrade via POST /billing/checkout (handles "not built yet" gracefully).
|
|
|
|
import * as api from "../api.js";
|
|
import * as S from "../session.js";
|
|
import { ApiError } from "../api.js";
|
|
import { badge, button, el, errorBox, fullTime, pageHead, table } from "../ui.js";
|
|
|
|
const fmt = (n) => (n === null || n === undefined ? "-" : Number(n).toLocaleString());
|
|
|
|
function quota(used, limit, labelText) {
|
|
if (!limit) return el("div", el("div.row", { style: "justify-content:space-between" }, el("span", labelText), el("span.muted", `${fmt(used)} sent, no cap`)));
|
|
const pct = Math.min(100, Math.round((used / limit) * 100));
|
|
return el("div",
|
|
el("div.row", { style: "justify-content:space-between" }, el("span", labelText), el("span.muted", `${fmt(used)} of ${fmt(limit)}`)),
|
|
el(`div.quota${used > limit ? ".over" : ""}`, el("div", { style: `width:${pct}%` })),
|
|
el("div.small.muted", `${pct}% used`));
|
|
}
|
|
|
|
export async function usageView(ctx) {
|
|
const today = new Date().toISOString().slice(0, 10);
|
|
const [u, b] = await Promise.all([api.usage(), api.billing()]);
|
|
const acct = ctx.account || {};
|
|
const plan = b?.plan?.name || b?.plan || acct.plan || "free";
|
|
const limits = b?.limits || u.limits || {};
|
|
const todayRow = (u.days || []).find((d) => d.day === today) || { emails_sent: 0, emails_test: 0 };
|
|
const test = S.isTest();
|
|
const monthUsed = test ? u.totals?.emails_test : u.totals?.emails_sent;
|
|
const dayUsed = test ? todayRow.emails_test : todayRow.emails_sent;
|
|
const msg = el("div");
|
|
|
|
const upgrade = button(b?.upgrade_label || "Upgrade", async () => {
|
|
msg.replaceChildren();
|
|
try {
|
|
const r = await api.checkout(b?.next_plan || "pro");
|
|
if (r?.url || r?.checkout_url) location.href = r.url || r.checkout_url;
|
|
else msg.append(el("p.help", "Checkout started."));
|
|
} catch (e) {
|
|
if (e instanceof ApiError && (e.missing || e.status === 503)) msg.append(el("p.help", "Upgrades are not available yet. Email support and we raise your limits by hand."));
|
|
else msg.append(errorBox(e));
|
|
}
|
|
});
|
|
|
|
// Portal links are minted on demand (POST /billing/portal) and expire, so
|
|
// the server only tells us whether one is available.
|
|
const portal = b?.portal_url
|
|
? el("a.btn.secondary", { href: b.portal_url }, "Billing portal")
|
|
: b?.portal_available
|
|
? button("Billing portal", async () => {
|
|
msg.replaceChildren();
|
|
try { const r = await api.billingPortal(); if (r?.link) location.href = r.link; } catch (e) { msg.append(errorBox(e)); }
|
|
}, "btn secondary")
|
|
: null;
|
|
// Only hide the button when the server positively says there is no plan above this one.
|
|
const showUpgrade = !(b && b.next_plan === null && b.checkout_available);
|
|
|
|
const planCard = el("div.card",
|
|
el("div.row", { style: "justify-content:space-between" }, el("h2", { style: "margin:0" }, "Plan"), badge(acct.status || "active")),
|
|
el("div.stat", { style: "margin:10px 0 14px" }, el("b", String(plan)), el("span", b?.description || (limits.emails_per_month ? `${fmt(limits.emails_per_month)} emails per month` : "Operator-set limits"))),
|
|
b?.renews_at || b?.current_period_end ? el("p.small.muted", `Renews ${fullTime(b.renews_at || b.current_period_end)}`) : null,
|
|
el("div.row", showUpgrade ? upgrade : el("span.muted", "You are on the top plan."), portal),
|
|
msg,
|
|
b === null ? el("p.help", { style: "margin-top:10px" }, "Billing is read from your account limits until the billing service is live.") : null,
|
|
b && b.checkout_available === false ? el("p.help", { style: "margin-top:10px" }, "Checkout is not enabled on this server yet. Email support to change plans.") : null);
|
|
|
|
const quotaCard = el("div.card",
|
|
el("h2", test ? "Test sends this month" : "Quota"),
|
|
test ? el("div.testbanner", el("b", "Test mode"), el("span.muted", "Test sends never count toward your quota.")) : null,
|
|
el("div", { style: "display:grid;gap:16px" },
|
|
quota(monthUsed || 0, test ? null : limits.emails_per_month, `This month (${u.from} to ${u.to})`),
|
|
quota(dayUsed || 0, test ? null : limits.emails_per_day, "Today")));
|
|
|
|
const stats = el("div.stats",
|
|
el("div.card", el("div.stat", el("b", fmt(u.totals?.emails_sent)), el("span", "Live emails this month"))),
|
|
el("div.card", el("div.stat", el("b", fmt(u.totals?.recipients)), el("span", "Recipients"))),
|
|
el("div.card", el("div.stat", el("b", fmt(u.totals?.emails_test)), el("span", "Test emails"))));
|
|
|
|
const days = (u.days || []).slice().reverse();
|
|
const daily = el("div.card.tight", table(["Day", { label: "Live", num: true }, { label: "Test", num: true }, { label: "Recipients", num: true }],
|
|
days.map((d) => el("tr", el("td", d.day), el("td.num", fmt(d.emails_sent)), el("td.num.muted", fmt(d.emails_test)), el("td.num.muted", fmt(d.recipients)))),
|
|
"No sends this month yet."));
|
|
|
|
let audit = null;
|
|
try { audit = await api.auditLog(); } catch { audit = null; }
|
|
const auditCard = audit ? el("div.card.tight", table(["When", "Who", "Action", "Target"],
|
|
(audit.data || []).slice(0, 20).map((a) => el("tr", el("td.muted", fullTime(a.created_at)), el("td", a.actor?.label || a.actor?.type), el("td", a.action), el("td.mono.small.muted", a.target?.id || ""))),
|
|
"No changes recorded yet.")) : null;
|
|
|
|
return el("div",
|
|
pageHead("Usage and plan", "Live sends count toward your limits. Test sends do not."),
|
|
stats,
|
|
el("div.grid2", quotaCard, planCard),
|
|
el("h2", "Daily"), daily,
|
|
audit ? [el("h2", { style: "margin-top:20px" }, "Recent changes"), auditCard] : null);
|
|
}
|