349 lines
13 KiB
TypeScript
349 lines
13 KiB
TypeScript
import { useTranslation } from "react-i18next";
|
|
import { ActionFunctionArgs, Link, LoaderFunctionArgs, useLoaderData } from "react-router";
|
|
import { adminGetAllTenantsWithUsage, createTenantUser, TenantSimple, TenantWithUsage, updateTenantDeactivated } from "~/utils/db/tenants.db.server";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import TenantsTable from "~/components/core/tenants/TenantsTable";
|
|
import { getUserHasPermission } from "~/utils/helpers/PermissionsHelper";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { getFiltersFromCurrentUrl, getPaginationFromCurrentUrl } from "~/utils/helpers/RowPaginationHelper";
|
|
import InputFilters from "~/components/ui/input/InputFilters";
|
|
import { FilterablePropertyDto } from "~/application/dtos/data/FilterablePropertyDto";
|
|
import { adminGetAllUsersNames } from "~/utils/db/users.db.server";
|
|
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { useActionData, useLocation, useNavigation, useSubmit } from "react-router";
|
|
import ActionResultModal from "~/components/ui/modals/ActionResultModal";
|
|
import type Stripe from "stripe";
|
|
import { getStripeInvoices, isStripeTestMode } from "~/utils/stripe.server";
|
|
import { createMetrics } from "~/modules/metrics/services/.server/MetricTracker";
|
|
import { serverTimingHeaders } from "~/modules/metrics/utils/defaultHeaders";
|
|
import { getAllTenantTypes } from "~/utils/db/tenants/tenantTypes.db.server";
|
|
import EditPageLayout from "~/components/ui/layouts/EditPageLayout";
|
|
import SlideOverWideEmpty from "~/components/ui/slideOvers/SlideOverWideEmpty";
|
|
import { useAppOrAdminData } from "~/utils/data/useAppOrAdminData";
|
|
import { EntityWithDetails, findEntityByName } from "~/utils/db/entities/entities.db.server";
|
|
import { TenantsApi } from "~/utils/api/.server/TenantsApi";
|
|
import { getAllRoles } from "~/utils/db/permissions/roles.db.server";
|
|
import { TenantUserType } from "~/application/enums/tenants/TenantUserType";
|
|
import InputCheckboxWithDescription from "~/components/ui/input/InputCheckboxWithDescription";
|
|
import FormGroup from "~/components/ui/forms/FormGroup";
|
|
import InputText, { RefInputText } from "~/components/ui/input/InputText";
|
|
import RowProperties from "~/components/entities/rows/RowProperties";
|
|
import UrlUtils from "~/utils/app/UrlUtils";
|
|
import { getExistingSlug } from "~/utils/services/tenantService";
|
|
import DeactivateTenantModal from "~/components/core/tenants/DeactivateTenantModal";
|
|
import { v2MetaFunction } from "~/utils/compat/v2MetaFunction";
|
|
import { Button } from "~/components/ui/button";
|
|
import { validateCSRFToken } from "~/utils/session.server";
|
|
import { appendCSRFToken } from "~/utils/csrf";
|
|
import { mapWithConcurrency } from "@toolctrl/foundation";
|
|
export { serverTimingHeaders as headers };
|
|
|
|
const ADMIN_TENANT_INVOICE_HYDRATION_CONCURRENCY = 3;
|
|
const ADMIN_TENANT_INVOICE_LIST_LIMIT = 10;
|
|
|
|
export const meta: v2MetaFunction<LoaderData> = ({ data }) => [{ title: data?.title }];
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
items: TenantWithUsage[];
|
|
filterableProperties: FilterablePropertyDto[];
|
|
pagination: PaginationDto;
|
|
tenantInvoices?: Stripe.Invoice[];
|
|
includeInvoices: boolean;
|
|
isStripeTest: boolean;
|
|
tenantSettingsEntity: EntityWithDetails | null;
|
|
};
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { time, getServerTimingHeader } = await createMetrics({ request, params }, "admin.accounts");
|
|
await time(verifyUserHasPermission(request, "admin.accounts.view"), "verifyUserHasPermission");
|
|
let { t } = await time(getTranslations(request), "getTranslations");
|
|
const [userOptions, tenantTypes] = await Promise.all([
|
|
time(adminGetAllUsersNames(), "adminGetAllUsersNames"),
|
|
time(getAllTenantTypes(), "getAllTenantTypes"),
|
|
]);
|
|
|
|
const filterableProperties = [
|
|
{ name: "name", title: "models.tenant.name" },
|
|
{ name: "slug", title: "models.tenant.slug" },
|
|
{
|
|
name: "userId",
|
|
title: "models.user.object",
|
|
manual: true,
|
|
options: userOptions.map((item) => {
|
|
return {
|
|
value: item.id,
|
|
name: item.email,
|
|
};
|
|
}),
|
|
},
|
|
{
|
|
name: "typeId",
|
|
title: t("shared.type"),
|
|
manual: true,
|
|
options: [
|
|
{ value: "null", name: t("admin.accounts.filters.defaultType") },
|
|
...tenantTypes.map((item) => {
|
|
return {
|
|
value: item.id,
|
|
name: t(item.title),
|
|
};
|
|
}),
|
|
],
|
|
},
|
|
];
|
|
const filters = getFiltersFromCurrentUrl(request, filterableProperties);
|
|
const urlSearchParams = new URL(request.url).searchParams;
|
|
const currentPagination = getPaginationFromCurrentUrl(urlSearchParams);
|
|
const { items, pagination } = await time(adminGetAllTenantsWithUsage(filters, currentPagination), "adminGetAllTenantsWithUsage");
|
|
const includeInvoices = ["1", "true"].includes((urlSearchParams.get("includeInvoices") ?? "").toLowerCase());
|
|
|
|
let tenantInvoices: Stripe.Invoice[] | undefined;
|
|
if (includeInvoices) {
|
|
tenantInvoices = [];
|
|
await time(
|
|
mapWithConcurrency(items, ADMIN_TENANT_INVOICE_HYDRATION_CONCURRENCY, async (item) => {
|
|
if (item.subscription?.stripeCustomerId) {
|
|
const invoices = await getStripeInvoices(item.subscription.stripeCustomerId, { limit: ADMIN_TENANT_INVOICE_LIST_LIMIT });
|
|
tenantInvoices?.push(...invoices);
|
|
}
|
|
}),
|
|
"getStripeInvoices[adminTenantList]"
|
|
);
|
|
}
|
|
|
|
const data: LoaderData = {
|
|
title: `${t("models.tenant.plural")} | ${process.env.APP_NAME}`,
|
|
items: items.sort((a, b) => (a.createdAt > b.createdAt ? -1 : 1)),
|
|
filterableProperties,
|
|
pagination,
|
|
tenantInvoices,
|
|
includeInvoices,
|
|
isStripeTest: isStripeTestMode(),
|
|
tenantSettingsEntity: await findEntityByName({ tenantId: null, name: "tenantSettings" }),
|
|
};
|
|
return Response.json(data, { headers: getServerTimingHeader() });
|
|
};
|
|
|
|
type ActionData = {
|
|
error?: string;
|
|
success?: string;
|
|
createdTenantId?: string;
|
|
};
|
|
export const action = async ({ request }: ActionFunctionArgs) => {
|
|
await verifyUserHasPermission(request, "admin.accounts.view");
|
|
const { t } = await getTranslations(request);
|
|
|
|
await validateCSRFToken(request);
|
|
const form = await request.formData();
|
|
const action = form.get("action")?.toString();
|
|
const id = form.get("id")?.toString();
|
|
if (action === "deactivate" && id) {
|
|
const deactivatedReason = form.get("reason")?.toString();
|
|
if (!deactivatedReason) {
|
|
return Response.json({ error: t("admin.accounts.actions.missingDeactivationReason") }, { status: 400 });
|
|
}
|
|
await updateTenantDeactivated(id, {
|
|
active: false,
|
|
deactivatedReason,
|
|
});
|
|
return Response.json({ success: t("admin.accounts.actions.tenantDeactivated") });
|
|
} else if (action === "activate" && id) {
|
|
await updateTenantDeactivated(id, {
|
|
active: true,
|
|
deactivatedReason: null,
|
|
});
|
|
return Response.json({});
|
|
} else if (action === "create") {
|
|
await verifyUserHasPermission(request, "admin.accounts.create");
|
|
const name = form.get("name")?.toString() ?? "";
|
|
const slug = form.get("slug")?.toString() ?? "";
|
|
const existingSlug = await getExistingSlug(slug);
|
|
if (!slug || existingSlug) {
|
|
return Response.json({ error: t("shared.slugTaken") }, { status: 400 });
|
|
}
|
|
const { tenant, user } = await TenantsApi.create({ request, form, name, slug });
|
|
const addMySelf = Boolean(form.get("addMySelf"));
|
|
if (addMySelf) {
|
|
const roles = await getAllRoles("app");
|
|
await createTenantUser(
|
|
{
|
|
tenantId: tenant.id,
|
|
userId: user.id,
|
|
type: TenantUserType.OWNER,
|
|
},
|
|
roles
|
|
);
|
|
}
|
|
const data: ActionData = {
|
|
createdTenantId: tenant.id,
|
|
};
|
|
return data;
|
|
} else {
|
|
return Response.json({ error: t("shared.invalidForm") }, { status: 400 });
|
|
}
|
|
};
|
|
|
|
export default function AdminTenantsRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const actionData = useActionData<ActionData>();
|
|
const appOrAdminData = useAppOrAdminData();
|
|
const { t } = useTranslation();
|
|
const submit = useSubmit();
|
|
const navigation = useNavigation();
|
|
const location = useLocation();
|
|
|
|
const [deactivatingTenant, setDeactivatingTenant] = useState<TenantWithUsage>();
|
|
const [creatingNewAccount, setCreatingNewAccount] = useState(false);
|
|
const invoiceToggleHref = buildIncludeInvoicesHref(location.search, !data.includeInvoices);
|
|
|
|
useEffect(() => {
|
|
if (actionData?.createdTenantId) {
|
|
setCreatingNewAccount(false);
|
|
}
|
|
}, [actionData]);
|
|
|
|
function onToggleActive(item: TenantWithUsage) {
|
|
if (!item.deactivatedReason) {
|
|
setDeactivatingTenant(item);
|
|
} else {
|
|
onConfirmedToggleActive(item, "", true);
|
|
}
|
|
}
|
|
|
|
function onConfirmedToggleActive(value: TenantSimple, reason: string, activate: boolean) {
|
|
const form = new FormData();
|
|
appendCSRFToken(form);
|
|
form.set("action", !activate ? "deactivate" : "activate");
|
|
form.set("reason", reason ?? "");
|
|
form.set("id", value.id);
|
|
submit(form, {
|
|
method: "post",
|
|
});
|
|
|
|
setDeactivatingTenant(undefined);
|
|
}
|
|
|
|
return (
|
|
<EditPageLayout
|
|
title={t("models.tenant.plural")}
|
|
buttons={
|
|
<>
|
|
<InputFilters size="sm" filters={data.filterableProperties} />
|
|
<Button type="button" variant="outline" size="sm" asChild>
|
|
<Link to={invoiceToggleHref}>
|
|
{t(data.includeInvoices ? "shared.hide" : "shared.show")} {t("app.subscription.invoices.title")}
|
|
</Link>
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
variant="default"
|
|
size="sm"
|
|
disabled={!getUserHasPermission(appOrAdminData, "admin.accounts.create")}
|
|
onClick={() => setCreatingNewAccount(true)}
|
|
>
|
|
{t("shared.new")}
|
|
</Button>
|
|
</>
|
|
}
|
|
>
|
|
<TenantsTable
|
|
items={data.items}
|
|
pagination={data.pagination}
|
|
tenantInvoices={data.tenantInvoices}
|
|
isStripeTest={data.isStripeTest}
|
|
actions={[
|
|
{
|
|
renderTitle: (i) => (!i.deactivatedReason ? t("shared.deactivate") : t("shared.activate")),
|
|
onClick: (_idx, item) => onToggleActive(item),
|
|
disabled: () => navigation.state === "submitting",
|
|
renderIsDestructive: (i) => !i.deactivatedReason,
|
|
},
|
|
]}
|
|
/>
|
|
|
|
<DeactivateTenantModal
|
|
open={!!deactivatingTenant}
|
|
onClose={() => setDeactivatingTenant(undefined)}
|
|
item={deactivatingTenant}
|
|
onConfirm={(item, reason) => onConfirmedToggleActive(item, reason, false)}
|
|
/>
|
|
|
|
<SlideOverWideEmpty
|
|
title={t("admin.accounts.create.title")}
|
|
open={creatingNewAccount}
|
|
onClose={() => {
|
|
setCreatingNewAccount(false);
|
|
}}
|
|
className="sm:max-w-sm"
|
|
overflowYScroll={true}
|
|
>
|
|
<div className="-mx-1 -mt-3">
|
|
<div className="space-y-4">
|
|
<CreateTenantForm tenantSettingsEntity={data.tenantSettingsEntity} />
|
|
</div>
|
|
</div>
|
|
</SlideOverWideEmpty>
|
|
|
|
<ActionResultModal actionData={actionData} />
|
|
</EditPageLayout>
|
|
);
|
|
}
|
|
|
|
function buildIncludeInvoicesHref(search: string, includeInvoices: boolean): string {
|
|
const params = new URLSearchParams(search);
|
|
if (includeInvoices) {
|
|
params.set("includeInvoices", "true");
|
|
} else {
|
|
params.delete("includeInvoices");
|
|
}
|
|
const nextSearch = params.toString();
|
|
return nextSearch ? `?${nextSearch}` : "?";
|
|
}
|
|
|
|
function CreateTenantForm({ tenantSettingsEntity }: { tenantSettingsEntity: EntityWithDetails | null }) {
|
|
const { t } = useTranslation();
|
|
|
|
const [name, setName] = useState("");
|
|
const [slug, setSlug] = useState("");
|
|
const [addMySelf, setAddMySelf] = useState(true);
|
|
|
|
useEffect(() => {
|
|
setSlug(UrlUtils.slugify(name));
|
|
}, [name]);
|
|
|
|
const firstInput = useRef<RefInputText>(null);
|
|
useEffect(() => {
|
|
setTimeout(() => {
|
|
firstInput.current?.input.current?.focus();
|
|
}, 100);
|
|
}, []);
|
|
|
|
return (
|
|
<FormGroup
|
|
labels={{
|
|
create: t("shared.create"),
|
|
}}
|
|
withErrorModal={false}
|
|
>
|
|
<div className="space-y-3">
|
|
<InputText ref={firstInput} autoFocus name="name" title={t("shared.name")} value={name} setValue={setName} required />
|
|
<InputText name="slug" title={t("shared.slug")} value={slug} setValue={setSlug} required onBlur={() => setSlug(UrlUtils.slugify(slug))} />
|
|
{tenantSettingsEntity && (
|
|
<div className="col-span-6 sm:col-span-6">
|
|
<RowProperties entity={tenantSettingsEntity} item={null} />
|
|
</div>
|
|
)}
|
|
<InputCheckboxWithDescription
|
|
name="addMySelf"
|
|
title={t("admin.accounts.create.addMyselfAsOwner")}
|
|
description={t("admin.accounts.create.addMyselfAsOwnerDescription")}
|
|
value={addMySelf}
|
|
setValue={setAddMySelf}
|
|
/>
|
|
</div>
|
|
</FormGroup>
|
|
);
|
|
}
|