Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+245
@@ -0,0 +1,245 @@
|
||||
import { buildAdminBillingVM, type AdminBillingRow } from "@hivegrid/view-models";
|
||||
import { ActionFunctionArgs, Form, LoaderFunctionArgs, MetaFunction, useActionData, useLoaderData, useNavigation } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import ButtonPrimary from "~/components/ui/buttons/ButtonPrimary";
|
||||
import ButtonSecondary from "~/components/ui/buttons/ButtonSecondary";
|
||||
import EditPageLayout from "~/components/ui/layouts/EditPageLayout";
|
||||
import ServerError from "~/components/ui/errors/ServerError";
|
||||
import { useRootData } from "~/utils/data/useRootData";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
||||
import { getUser } from "~/utils/db/users.db.server";
|
||||
import {
|
||||
listAdminBillingRows,
|
||||
requestManualBillingSync,
|
||||
setTenantBillingPlanSeats,
|
||||
AdminBillingError,
|
||||
type BillingSyncProvider,
|
||||
} from "~/services/admin/billing-admin.server";
|
||||
import { AdminActionInputError } from "~/services/admin/admin-action-audit.server";
|
||||
|
||||
type LoaderData = {
|
||||
title: string;
|
||||
vm: ReturnType<typeof buildAdminBillingVM>;
|
||||
};
|
||||
|
||||
type ActionData = {
|
||||
success?: string;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
||||
|
||||
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
||||
await verifyUserHasPermission(request, "admin.account.subscription");
|
||||
const { t } = await getTranslations(request);
|
||||
const vm = buildAdminBillingVM({ rows: await listAdminBillingRows() });
|
||||
return {
|
||||
title: `${t("admin.billing.title")} | ${process.env.APP_NAME}`,
|
||||
vm,
|
||||
} satisfies LoaderData;
|
||||
};
|
||||
|
||||
function badRequest(data: ActionData) {
|
||||
return Response.json(data, { status: 400 });
|
||||
}
|
||||
|
||||
function billingErrorKey(error: unknown): string {
|
||||
if (error instanceof AdminBillingError || error instanceof AdminActionInputError) return error.i18nKey;
|
||||
return "admin.billing.errors.UNKNOWN";
|
||||
}
|
||||
|
||||
export const action = async ({ request }: ActionFunctionArgs) => {
|
||||
await verifyUserHasPermission(request, "admin.account.subscription");
|
||||
const { t } = await getTranslations(request);
|
||||
await validateCSRFToken(request);
|
||||
|
||||
const form = await request.formData();
|
||||
const action = form.get("action")?.toString() ?? "";
|
||||
const tenantId = form.get("tenantId")?.toString() ?? "";
|
||||
const userInfo = await getUserInfo(request);
|
||||
const actor = await getUser(userInfo.userId);
|
||||
|
||||
try {
|
||||
if (action === "set-plan-seats") {
|
||||
await setTenantBillingPlanSeats({
|
||||
tenantId,
|
||||
planCode: form.get("planCode")?.toString() ?? "",
|
||||
seatQuantity: form.get("seatQuantity")?.toString() ?? "",
|
||||
reason: form.get("reason")?.toString() ?? "",
|
||||
actorUserId: userInfo.userId,
|
||||
actorEmail: actor?.email ?? null,
|
||||
request,
|
||||
});
|
||||
return Response.json({ success: t("admin.billing.actions.planSeatsUpdated") } satisfies ActionData);
|
||||
}
|
||||
|
||||
if (action === "request-sync") {
|
||||
const provider = form.get("provider")?.toString() as BillingSyncProvider;
|
||||
if (provider !== "lago" && provider !== "mollie") {
|
||||
return badRequest({ error: t("shared.invalidForm") });
|
||||
}
|
||||
await requestManualBillingSync({
|
||||
tenantId,
|
||||
provider,
|
||||
actorUserId: userInfo.userId,
|
||||
actorEmail: actor?.email ?? null,
|
||||
request,
|
||||
});
|
||||
return Response.json({ success: t("admin.billing.actions.syncRequested", { provider: t(`admin.billing.providers.${provider}`) }) } satisfies ActionData);
|
||||
}
|
||||
|
||||
return badRequest({ error: t("shared.invalidForm") });
|
||||
} catch (error) {
|
||||
return badRequest({ error: t(billingErrorKey(error)) });
|
||||
}
|
||||
};
|
||||
|
||||
function FieldValue({ label, value }: { label: string; value: string | number | null }) {
|
||||
return (
|
||||
<div className="space-y-1">
|
||||
<div className="text-muted-foreground text-xs font-medium uppercase">{label}</div>
|
||||
<div className="text-foreground text-sm break-all">{value || "-"}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function SyncButtons({ row, csrf }: { row: AdminBillingRow; csrf?: string }) {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{(["lago", "mollie"] as const).map((provider) => (
|
||||
<Form key={provider} method="post">
|
||||
<input type="hidden" name="csrf" value={csrf ?? ""} />
|
||||
<input type="hidden" name="action" value="request-sync" />
|
||||
<input type="hidden" name="tenantId" value={row.tenantId} />
|
||||
<input type="hidden" name="provider" value={provider} />
|
||||
<ButtonSecondary type="submit" className="h-8 px-3 text-xs">
|
||||
{t("admin.billing.actions.requestProviderSync", { provider: t(`admin.billing.providers.${provider}`) })}
|
||||
</ButtonSecondary>
|
||||
</Form>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function PlanSeatForm({ row, csrf, loading }: { row: AdminBillingRow; csrf?: string; loading: boolean }) {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<Form method="post" className="grid gap-2 md:grid-cols-[minmax(120px,1fr)_96px_minmax(180px,1.5fr)_auto]">
|
||||
<input type="hidden" name="csrf" value={csrf ?? ""} />
|
||||
<input type="hidden" name="action" value="set-plan-seats" />
|
||||
<input type="hidden" name="tenantId" value={row.tenantId} />
|
||||
<label className="text-muted-foreground space-y-1 text-xs font-medium">
|
||||
{t("admin.billing.fields.planCode")}
|
||||
<input
|
||||
className="border-border bg-background text-foreground h-9 w-full rounded-md border px-3 text-sm"
|
||||
name="planCode"
|
||||
defaultValue={row.planCode ?? ""}
|
||||
placeholder={t("admin.billing.placeholders.planCode")}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label className="text-muted-foreground space-y-1 text-xs font-medium">
|
||||
{t("admin.billing.fields.seats")}
|
||||
<input
|
||||
className="border-border bg-background text-foreground h-9 w-full rounded-md border px-3 text-sm"
|
||||
name="seatQuantity"
|
||||
type="number"
|
||||
min={1}
|
||||
defaultValue={row.seatQuantity ?? 1}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label className="text-muted-foreground space-y-1 text-xs font-medium">
|
||||
{t("admin.billing.fields.reason")}
|
||||
<input
|
||||
className="border-border bg-background text-foreground h-9 w-full rounded-md border px-3 text-sm"
|
||||
name="reason"
|
||||
placeholder={t("admin.billing.placeholders.reason")}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<div className="flex items-end">
|
||||
<ButtonPrimary type="submit" disabled={loading} className="h-9 px-3 text-xs">
|
||||
{t("admin.billing.actions.saveOverride")}
|
||||
</ButtonPrimary>
|
||||
</div>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
|
||||
function BillingRowCard({ row, csrf, loading }: { row: AdminBillingRow; csrf?: string; loading: boolean }) {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<div className="border-border bg-background rounded-md border p-4">
|
||||
<div className="flex flex-wrap items-start justify-between gap-3">
|
||||
<div>
|
||||
<div className="text-foreground text-base font-semibold">{row.tenantName}</div>
|
||||
<div className="text-muted-foreground text-sm">{row.tenantSlug}</div>
|
||||
</div>
|
||||
<div className="bg-muted text-foreground rounded-full px-3 py-1 text-xs font-medium">{t(row.statusLabelKey)}</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid gap-4 md:grid-cols-4">
|
||||
<FieldValue label={t("admin.billing.fields.planCode")} value={row.planCode} />
|
||||
<FieldValue label={t("admin.billing.fields.seats")} value={row.seatQuantity} />
|
||||
<FieldValue label={t("admin.billing.fields.lastInvoice")} value={row.lastInvoiceExternalId} />
|
||||
<FieldValue label={t("admin.billing.fields.lastPayment")} value={row.lastPaymentExternalId} />
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid gap-4 md:grid-cols-3">
|
||||
<FieldValue label={t("admin.billing.fields.readOnlyReason")} value={t(row.readOnlyReasonKey)} />
|
||||
<FieldValue label={t("admin.billing.fields.recoveryHint")} value={t(row.recoveryHintKey)} />
|
||||
<FieldValue label={t("admin.billing.fields.providers")} value={t(row.providerSummaryKey)} />
|
||||
</div>
|
||||
|
||||
<div className="border-border mt-4 border-t pt-4">
|
||||
<PlanSeatForm row={row} csrf={csrf} loading={loading} />
|
||||
</div>
|
||||
<div className="mt-3">
|
||||
<SyncButtons row={row} csrf={csrf} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function AdminBillingRoute() {
|
||||
const { t } = useTranslation();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const actionData = useActionData<ActionData>();
|
||||
const navigation = useNavigation();
|
||||
const rootData = useRootData();
|
||||
const loading = navigation.state === "submitting";
|
||||
const rows = data.vm.status === "ready" ? data.vm.data.rows : [];
|
||||
|
||||
return (
|
||||
<EditPageLayout title={t("admin.billing.title")} fullWidth>
|
||||
<div className="space-y-4">
|
||||
<div className="border-border bg-surface-neutral-primary rounded-md border p-4">
|
||||
<div className="text-muted-foreground text-sm">{t("admin.billing.description")}</div>
|
||||
{actionData?.success && <div className="mt-3 rounded-md bg-green-50 px-3 py-2 text-sm text-green-800">{actionData.success}</div>}
|
||||
{actionData?.error && <div className="mt-3 rounded-md bg-red-50 px-3 py-2 text-sm text-red-800">{actionData.error}</div>}
|
||||
</div>
|
||||
|
||||
{data.vm.status === "empty" ? (
|
||||
<div className="border-border bg-background rounded-md border p-6">
|
||||
<div className="font-medium">{t("admin.billing.empty.headline")}</div>
|
||||
<div className="text-muted-foreground mt-1 text-sm">{t("admin.billing.empty.body")}</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{rows.map((row) => (
|
||||
<BillingRowCard key={row.tenantId} row={row} csrf={rootData.csrf} loading={loading} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</EditPageLayout>
|
||||
);
|
||||
}
|
||||
|
||||
export function ErrorBoundary() {
|
||||
return <ServerError />;
|
||||
}
|
||||
Reference in new issue
Block a user