Adds team upgrade banner (#5541)
* Adds team upgrade banner * Update TeamListItem.tsx * Update payments.ts * Copy updates and mobile style fixes * Update packages/trpc/server/routers/viewer/teams.tsx Co-authored-by: alannnc <[email protected]> Co-authored-by: alannnc <[email protected]>
This commit is contained in:
1 parent
112efbbda2
commit
ee620c7fff
9 files changed
+97
-16
No files matched your search
@@ -39,13 +39,14 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!team) {
|
||||
const prevTeam = await prisma.team.findFirstOrThrow({ where: { id } });
|
||||
const metadata = teamMetadataSchema.parse(prevTeam.metadata);
|
||||
if (!metadata?.requestedSlug) throw new HttpError({ statusCode: 400, message: "Missing requestedSlug" });
|
||||
|
||||
/** Legacy teams already have a slug, this will allow them to upgrade as well */
|
||||
const slug = prevTeam.slug || metadata?.requestedSlug;
|
||||
if (!slug) throw new HttpError({ statusCode: 400, message: "Missing team slug" });
|
||||
try {
|
||||
team = await prisma.team.update({
|
||||
/** We save the metadata first to prevent duplicate payments */
|
||||
await prisma.team.update({
|
||||
where: { id },
|
||||
data: {
|
||||
slug: metadata.requestedSlug,
|
||||
metadata: {
|
||||
paymentId: checkoutSession.id,
|
||||
subscriptionId: subscription.id || null,
|
||||
@@ -53,8 +54,10 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
},
|
||||
},
|
||||
});
|
||||
/** Then we try to upgrade the slug, which may fail if a conflict came up since team creation */
|
||||
team = await prisma.team.update({ where: { id }, data: { slug } });
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getRequestedSlugError(error, metadata.requestedSlug);
|
||||
const { message, statusCode } = getRequestedSlugError(error, slug);
|
||||
return res.status(statusCode).json({ message });
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import { getPlaceholderAvatar } from "@calcom/lib/getPlaceholderAvatar";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { RouterOutputs, trpc } from "@calcom/trpc/react";
|
||||
import { Icon } from "@calcom/ui/Icon";
|
||||
import { Avatar } from "@calcom/ui/components";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
import { ButtonGroup } from "@calcom/ui/components/buttonGroup";
|
||||
import ConfirmationDialogContent from "@calcom/ui/v2/core/ConfirmationDialogContent";
|
||||
@@ -21,8 +22,6 @@ import Dropdown, {
|
||||
import { Tooltip } from "@calcom/ui/v2/core/Tooltip";
|
||||
import showToast from "@calcom/ui/v2/core/notifications";
|
||||
|
||||
import Avatar from "@components/ui/Avatar";
|
||||
|
||||
import { TeamRole } from "./TeamPill";
|
||||
|
||||
interface Props {
|
||||
@@ -65,7 +64,7 @@ export default function TeamListItem(props: Props) {
|
||||
const teamInfo = (
|
||||
<div className="flex px-5 py-5">
|
||||
<Avatar
|
||||
size={9}
|
||||
size="sm"
|
||||
imageSrc={getPlaceholderAvatar(team?.logo, team?.name as string)}
|
||||
alt="Team Logo"
|
||||
className="min-h-9 min-w-9 h-9 w-9 rounded-full"
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { useRouter } from "next/router";
|
||||
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { TopBanner } from "@calcom/ui/components";
|
||||
import { showToast } from "@calcom/ui/v2/core";
|
||||
|
||||
export function TeamsUpgradeBanner() {
|
||||
const { t } = useLocale();
|
||||
const router = useRouter();
|
||||
const { data } = trpc.viewer.teams.getUpgradeable.useQuery();
|
||||
const publishTeamMutation = trpc.viewer.teams.publish.useMutation({
|
||||
onSuccess(data) {
|
||||
router.push(data.url);
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast(error.message, "error");
|
||||
},
|
||||
});
|
||||
if (!data) return null;
|
||||
const [membership] = data;
|
||||
if (!membership) return null;
|
||||
return (
|
||||
<TopBanner
|
||||
text={t("team_upgrade_banner_description", { teamName: membership.team.name })}
|
||||
variant="warning"
|
||||
actions={
|
||||
<button
|
||||
className="border-b border-b-black"
|
||||
onClick={() => {
|
||||
publishTeamMutation.mutate({ teamId: membership.team.id });
|
||||
}}>
|
||||
{t("team_upgrade_banner_action")}
|
||||
</button>
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
export { CreateANewTeamForm } from "./CreateANewTeamForm";
|
||||
export { TeamsListing } from "./TeamsListing";
|
||||
export { TeamsUpgradeBanner } from "./TeamsUpgradeBanner";
|
||||
@@ -4,10 +4,28 @@ import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
/** Used to prevent double charges for the same team */
|
||||
export const checkIfTeamPaymentRequired = async ({ teamId = -1 }) => {
|
||||
const team = await prisma.team.findUniqueOrThrow({
|
||||
where: { id: teamId },
|
||||
select: { metadata: true },
|
||||
});
|
||||
const metadata = teamMetadataSchema.parse(team.metadata);
|
||||
/** If there's no paymentId, we need to pay this team */
|
||||
if (!metadata?.paymentId) return { url: null };
|
||||
const checkoutSession = await stripe.checkout.sessions.retrieve(metadata.paymentId);
|
||||
/** If there's a pending session but it isn't paid, we need to pay this team */
|
||||
if (checkoutSession.payment_status === "paid") return { url: null };
|
||||
/** If the session is already paid we return the upgrade URL so team is updated. */
|
||||
return { url: `${WEBAPP_URL}/api/teams/${teamId}/upgrade?session_id=${metadata.paymentId}` };
|
||||
};
|
||||
|
||||
export const purchaseTeamSubscription = async (input: { teamId: number; seats: number; userId: number }) => {
|
||||
const { teamId, seats, userId } = input;
|
||||
const { url } = await checkIfTeamPaymentRequired({ teamId });
|
||||
if (url) return { url };
|
||||
const customer = await getStripeCustomerIdFromUserId(userId);
|
||||
return await stripe.checkout.sessions.create({
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
customer,
|
||||
mode: "subscription",
|
||||
success_url: `${WEBAPP_URL}/api/teams/${teamId}/upgrade?session_id={CHECKOUT_SESSION_ID}`,
|
||||
@@ -30,6 +48,7 @@ export const purchaseTeamSubscription = async (input: { teamId: number; seats: n
|
||||
},
|
||||
},
|
||||
});
|
||||
return { url: session.url };
|
||||
};
|
||||
|
||||
export const cancelTeamSubscriptionFromStripe = async (teamId: number) => {
|
||||
|
||||
Reference in new issue
Block a user