89 lines
3.9 KiB
TypeScript
89 lines
3.9 KiB
TypeScript
import { buildAdminBillingVM } from "@hivegrid/view-models";
|
|
import { ActionFunctionArgs, LoaderFunctionArgs, MetaFunction, useActionData, useLoaderData, useNavigation } from "react-router";
|
|
import { AdminBillingPage } from "~/components/screens/admin/AdminBillingPage";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { AdminActionInputError } from "~/services/admin/admin-action-audit.server";
|
|
import { AdminBillingError, listAdminBillingRows, requestManualBillingSync, setTenantBillingPlanSeats, type BillingSyncProvider } from "~/services/admin/billing-admin.server";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import { getUser } from "~/utils/db/users.db.server";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
|
import { runWithRlsContext } from "@hivegrid/db/rls-context.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);
|
|
return { title: `${t("admin.billing.title")} | ${process.env.APP_NAME}`, vm: buildAdminBillingVM({ rows: await listAdminBillingRows() }) } 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 actionName = form.get("action")?.toString() ?? "";
|
|
const tenantId = form.get("tenantId")?.toString() ?? "";
|
|
const userInfo = await getUserInfo(request);
|
|
const actor = await getUser(userInfo.userId);
|
|
|
|
try {
|
|
if (actionName === "set-plan-seats") {
|
|
await runWithRlsContext({ tenantId }, () =>
|
|
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 (actionName === "request-sync") {
|
|
const provider = form.get("provider")?.toString() as BillingSyncProvider;
|
|
if (provider !== "lago" && provider !== "mollie") return badRequest({ error: t("shared.invalidForm") });
|
|
await runWithRlsContext({ tenantId }, () =>
|
|
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)) });
|
|
}
|
|
};
|
|
|
|
export default function AdminBillingRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const rootData = useRootData();
|
|
return (
|
|
<AdminBillingPage
|
|
vm={data.vm}
|
|
csrf={rootData.csrf ?? ""}
|
|
submitting={useNavigation().state === "submitting"}
|
|
feedback={useActionData<ActionData>()}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|