diff --git a/front/lib/plans/subscription.ts b/front/lib/plans/subscription.ts index 6e2ea5e4a21f0..06a6735e63907 100644 --- a/front/lib/plans/subscription.ts +++ b/front/lib/plans/subscription.ts @@ -160,7 +160,7 @@ export const internalInviteWorkspaceToEnterprisePlan = async ({ const model = await front_sequelize.transaction(async (t) => { if (invitation) { await PlanInvitation.destroy({ - where: { id: workspace.id, consumedAt: null }, + where: { workspaceId: workspace.id, consumedAt: null }, transaction: t, }); } diff --git a/front/pages/api/upgrade-enterprise/[secret].ts b/front/pages/api/upgrade-enterprise/[secret].ts deleted file mode 100644 index b8aeff98c7196..0000000000000 --- a/front/pages/api/upgrade-enterprise/[secret].ts +++ /dev/null @@ -1,57 +0,0 @@ -import { NextApiRequest, NextApiResponse } from "next"; - -import { Workspace } from "@app/lib/models"; -import { PlanInvitation } from "@app/lib/models/plan"; -import { internalUpgradeWorkspace } from "@app/lib/plans/subscription"; -import { withLogging } from "@app/logger/withlogging"; - -async function handler( - req: NextApiRequest, - res: NextApiResponse<{ checkoutUrl: string }> -) { - const token = req.query.secret; - - if (!token || typeof token !== "string") { - res.status(404).end(); - } - - const planInvite = await PlanInvitation.findOne({ - where: { - secret: token, - }, - }); - - if (!planInvite) { - return res.status(404).end(); - } - - const workspace = await Workspace.findOne({ - where: { - id: planInvite.workspaceId, - }, - }); - - if (!workspace) { - return res.status(404).end(); - } - - switch (req.method) { - case "POST": - const checkoutUrl = await internalUpgradeWorkspace({ - workspaceSId: workspace.sId, - }); - if (!checkoutUrl) { - throw new Error("No checkout URL found"); - } - res.status(200).json({ - checkoutUrl, - }); - return; - - default: - res.status(405).end(); - return; - } -} - -export default withLogging(handler); diff --git a/front/pages/upgrade-enterprise/[secret].tsx b/front/pages/upgrade-enterprise/[secret].tsx index 3610e4b68cd44..829860069cf06 100644 --- a/front/pages/upgrade-enterprise/[secret].tsx +++ b/front/pages/upgrade-enterprise/[secret].tsx @@ -1,16 +1,12 @@ import { GetServerSideProps, InferGetServerSidePropsType } from "next"; -import { useEffect } from "react"; -import OnboardingLayout from "@app/components/sparkle/OnboardingLayout"; import { Workspace } from "@app/lib/models"; import { PlanInvitation } from "@app/lib/models/plan"; +import { internalUpgradeWorkspace } from "@app/lib/plans/subscription"; -const { GA_TRACKING_ID = "" } = process.env; - -export const getServerSideProps: GetServerSideProps<{ - secret: string; - gaTrackingId: string; -}> = async (context) => { +export const getServerSideProps: GetServerSideProps = async ( + context +) => { const token = context.params?.secret as string; if (!token) { return { @@ -42,43 +38,26 @@ export const getServerSideProps: GetServerSideProps<{ }; } + const checkoutUrl = await internalUpgradeWorkspace({ + workspaceSId: workspace.sId, + }); + if (!checkoutUrl) { + return { + notFound: true, + }; + } + return { - props: { - secret: token, - gaTrackingId: GA_TRACKING_ID, + redirect: { + destination: checkoutUrl, + permanent: false, }, }; }; -export default function Welcome({ - secret, - gaTrackingId, -}: InferGetServerSidePropsType) { - useEffect(() => { - async function redirect() { - const res = await fetch(`/api/upgrade-enterprise/${secret}`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - }); - if (!res.ok) { - window.location.href = "/"; - return; - } - - const data = await res.json(); - if (data.checkoutUrl) { - window.location.href = data.checkoutUrl; - } - } - - void redirect(); - }, [secret]); - - return ( - -
- - ); +export default function Welcome( + _props: InferGetServerSidePropsType +) { + void _props; + return null; } diff --git a/front/pages/w/[wId]/subscription/index.tsx b/front/pages/w/[wId]/subscription/index.tsx index a46cbebc44232..29f106280c4c1 100644 --- a/front/pages/w/[wId]/subscription/index.tsx +++ b/front/pages/w/[wId]/subscription/index.tsx @@ -48,7 +48,7 @@ export const getServerSideProps: GetServerSideProps<{ }; } - const planInvitation = await getPlanInvitation({ workspaceId: owner.sId }); + const planInvitation = await getPlanInvitation({ workspaceSId: owner.sId }); return { props: {