Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/routes/app.$tenant/settings/subscription.tsx
T

171 lines
7.6 KiB
TypeScript

import { useEffect } from "react";
import { ActionFunction, LoaderFunctionArgs, redirect, useLoaderData } from "react-router";
import { useActionData } from "react-router";
import {
createCustomerPortalSession,
createStripeSetupSession,
deleteStripePaymentMethod,
getStripeCustomer,
getStripeInvoices,
getStripePaymentIntents,
getStripePaymentMethods,
getStripeUpcomingInvoices,
} from "~/utils/stripe.server";
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
import { getUser } from "~/utils/db/users.db.server";
import { getTranslations } from "~/locale/i18next.server";
import type Stripe from "stripe";
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
import { getOrPersistTenantSubscription, getTenantSubscription } from "~/utils/db/tenantSubscriptions.db.server";
import { getTenant } from "~/utils/db/tenants.db.server";
import { cancelTenantSubscription, getPlanFeaturesUsage } from "~/utils/services/.server/subscriptionService";
import { PlanFeatureUsageDto } from "~/application/dtos/subscriptions/PlanFeatureUsageDto";
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
import { serverTimingHeaders } from "~/modules/metrics/utils/defaultHeaders";
import { promiseHash } from "~/utils/promises/promiseHash";
import { createMetrics } from "~/modules/metrics/services/.server/MetricTracker";
import EditPageLayout from "~/components/ui/layouts/EditPageLayout";
import SubscriptionSettings from "~/modules/users/components/SubscriptionSettings";
import toast from "react-hot-toast";
import { getUserHasPermission } from "~/utils/helpers/PermissionsHelper";
import { useAppData } from "~/utils/data/useAppData";
import { v2MetaFunction } from "~/utils/compat/v2MetaFunction";
export { serverTimingHeaders as headers };
type LoaderData = {
title: string;
customer: Stripe.Customer | Stripe.DeletedCustomer | null;
myInvoices: Stripe.Invoice[];
myPayments: Stripe.PaymentIntent[];
myFeatures: PlanFeatureUsageDto[];
myUpcomingInvoices: Stripe.Invoice[];
myPaymentMethods: Stripe.PaymentMethod[];
};
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const { time, getServerTimingHeader } = await createMetrics({ request, params }, "app.$tenant.settings.subscription");
let { t } = await getTranslations(request);
const tenantId = await time(getTenantIdFromUrl(params), "getTenantIdFromUrl");
await time(verifyUserHasPermission(request, "app.settings.subscription.view", tenantId), "verifyUserHasPermission");
const userInfoPromise = time(getUserInfo(request), "getUserInfo");
const userPromise = userInfoPromise.then((userInfo) => time(getUser(userInfo.userId), "getUser"));
const tenantPromise = time(getTenant(tenantId), "getTenant");
const canViewInvoicesPromise = time(
verifyUserHasPermission(request, "app.settings.subscription.invoices.view", tenantId).then(
() => true,
() => false
),
"verifyInvoicePermission"
);
const [user, tenant, canViewInvoices] = await Promise.all([userPromise, tenantPromise, canViewInvoicesPromise]);
if (!user) {
return badRequest({ error: t("shared.invalidUser") });
}
if (!tenant) {
return badRequest({ error: t("settings.subscription.errors.invalidTenant", { tenantId }) });
}
const tenantSubscription = await time(getOrPersistTenantSubscription(tenantId), "getOrPersistTenantSubscription");
const { customer, myInvoices, myPayments, myUpcomingInvoices, myPaymentMethods, myFeatures } = await time(
promiseHash({
customer: canViewInvoices ? getStripeCustomer(tenantSubscription.stripeCustomerId) : Promise.resolve(null),
myInvoices: canViewInvoices ? (getStripeInvoices(tenantSubscription.stripeCustomerId) ?? []) : Promise.resolve([] as Stripe.Invoice[]),
myPayments: canViewInvoices
? (getStripePaymentIntents(tenantSubscription.stripeCustomerId, "succeeded") ?? [])
: Promise.resolve([] as Stripe.PaymentIntent[]),
myUpcomingInvoices: canViewInvoices
? getStripeUpcomingInvoices(tenantSubscription.products.filter((f) => f.stripeSubscriptionId).flatMap((p) => p.stripeSubscriptionId!))
: Promise.resolve([] as Stripe.Invoice[]),
myPaymentMethods: canViewInvoices ? getStripePaymentMethods(tenantSubscription.stripeCustomerId) : Promise.resolve([] as Stripe.PaymentMethod[]),
myFeatures: getPlanFeaturesUsage(tenantId, tenantSubscription),
}),
"subscription[getStripeData]"
);
const data: LoaderData = {
title: `${t("app.navbar.subscription")} | ${process.env.APP_NAME}`,
customer,
myFeatures,
myInvoices,
myPayments,
myUpcomingInvoices,
myPaymentMethods,
};
return Response.json(data, { headers: getServerTimingHeader() });
};
type ActionData = {
error?: string;
success?: string;
};
const badRequest = (data: ActionData) => Response.json(data, { status: 400 });
export const action: ActionFunction = async ({ request, params }) => {
const { t } = await getTranslations(request);
const tenantId = await getTenantIdFromUrl(params);
await verifyUserHasPermission(request, "app.settings.subscription.update", tenantId);
const userInfo = await getUserInfo(request);
const tenantSubscription = await getTenantSubscription(tenantId);
await validateCSRFToken(request);
const form = await request.formData();
const action = form.get("action")?.toString();
if (!tenantSubscription || !tenantSubscription?.stripeCustomerId) {
return badRequest({
error: t("settings.subscription.errors.invalidStripeCustomer"),
});
} else if (action === "cancel") {
await verifyUserHasPermission(request, "app.settings.subscription.delete", tenantId);
const tenantSubscriptionProductId = form.get("tenant-subscription-product-id")?.toString() ?? "";
try {
await cancelTenantSubscription(tenantSubscriptionProductId, { t, tenantId, userId: userInfo.userId });
return Response.json({ success: t("settings.subscription.success.cancelled") });
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
return Response.json({ error: t("shared.unexpectedError") }, { status: 400 });
}
} else if (action === "add-payment-method") {
const session = await createStripeSetupSession(request, tenantSubscription.stripeCustomerId);
return redirect(session?.url ?? "");
} else if (action === "delete-payment-method") {
await deleteStripePaymentMethod(form.get("id")?.toString() ?? "", tenantSubscription.stripeCustomerId);
return Response.json({});
} else if (action === "open-customer-portal") {
const session = await createCustomerPortalSession(request, tenantSubscription.stripeCustomerId);
if (!session?.url) {
return badRequest({ error: t("settings.subscription.errors.customerPortalUnavailable") });
}
return redirect(session.url);
}
};
export const meta: v2MetaFunction<typeof loader> = ({ data }) => (data && "title" in data ? [{ title: data.title }] : []);
export default function SubscriptionRoute() {
const data = useLoaderData<LoaderData>();
const actionData = useActionData<ActionData>();
const appData = useAppData();
useEffect(() => {
if (actionData?.error) {
toast.error(actionData.error);
} else if (actionData?.success) {
toast.success(actionData.success);
}
}, [actionData]);
return (
<EditPageLayout className="mb-12">
<SubscriptionSettings
{...data}
mySubscription={appData.mySubscription}
currentTenant={appData.currentTenant}
permissions={{
viewInvoices: getUserHasPermission(appData, "app.settings.subscription.invoices.view") && data.myInvoices.length > 0,
}}
/>
</EditPageLayout>
);
}