forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds trial banner and in-app upgrade (calcom#1402)
* WIP trial banner * Fixes days left count * Defers stripe loading until needed * Fixes auth issues * Checkout fixes * Allows for signup testing * Debugging checkout * Adds tests for trial banner Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
baa7e86
commit 4cd7a4c
Showing
11 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import dayjs from "dayjs"; | ||
|
||
import { TRIAL_LIMIT_DAYS } from "@lib/config/constants"; | ||
import { useLocale } from "@lib/hooks/useLocale"; | ||
|
||
import { useMeQuery } from "@components/Shell"; | ||
import Button from "@components/ui/Button"; | ||
|
||
const TrialBanner = () => { | ||
const { t } = useLocale(); | ||
const query = useMeQuery(); | ||
const user = query.data; | ||
|
||
if (!user || user.plan !== "TRIAL") return null; | ||
|
||
const trialDaysLeft = dayjs(user.createdDate) | ||
.add(TRIAL_LIMIT_DAYS + 1, "day") | ||
.diff(dayjs(), "day"); | ||
|
||
return ( | ||
<div | ||
className="p-4 m-4 text-sm font-medium text-center text-gray-600 bg-yellow-200 rounded-md" | ||
data-testid="trial-banner"> | ||
<div className="mb-2 text-left">{t("trial_days_left", { days: trialDaysLeft })}</div> | ||
<Button | ||
href="/api/upgrade" | ||
color="minimal" | ||
className="justify-center w-full border-2 border-gray-600 hover:bg-yellow-100"> | ||
{t("upgrade_now")} | ||
</Button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TrialBanner; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export const BASE_URL = process.env.BASE_URL || `https://${process.env.VERCEL_URL}`; | ||
export const WEBSITE_URL = process.env.NEXT_PUBLIC_APP_URL || "https://cal.com"; | ||
export const IS_PRODUCTION = process.env.NODE_ENV === "production"; | ||
export const TRIAL_LIMIT_DAYS = 14; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Prisma } from "@prisma/client"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { getSession } from "@lib/auth"; | ||
import { WEBSITE_URL } from "@lib/config/constants"; | ||
import { HttpError as HttpCode } from "@lib/core/http/error"; | ||
import prisma from "@lib/prisma"; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const session = await getSession({ req }); | ||
if (!session?.user?.id) { | ||
return res.status(401).json({ message: "Not authenticated" }); | ||
} | ||
|
||
if (req.method !== "GET") { | ||
throw new HttpCode({ statusCode: 405, message: "Method Not Allowed" }); | ||
} | ||
|
||
const user = await prisma.user.findUnique({ | ||
rejectOnNotFound: true, | ||
where: { | ||
id: session.user.id, | ||
}, | ||
select: { | ||
email: true, | ||
metadata: true, | ||
}, | ||
}); | ||
|
||
try { | ||
const response = await fetch(`${WEBSITE_URL}/api/upgrade`, { | ||
method: "POST", | ||
credentials: "include", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
stripeCustomerId: (user.metadata as Prisma.JsonObject)?.stripeCustomerId, | ||
email: user.email, | ||
fromApp: true, | ||
}), | ||
}); | ||
const data = await response.json(); | ||
|
||
res.redirect(303, data.url); | ||
} catch (error) { | ||
console.error(`error`, error); | ||
res.redirect(303, req.headers.origin || "/"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { expect, test } from "@playwright/test"; | ||
|
||
// Using logged in state from globalSteup | ||
test.use({ storageState: "playwright/artifacts/trialStorageState.json" }); | ||
|
||
test("Trial banner should be visible to TRIAL users", async ({ page }) => { | ||
// Try to go homepage | ||
await page.goto("/"); | ||
// It should redirect you to the event-types page | ||
await page.waitForSelector("[data-testid=event-types]"); | ||
|
||
await expect(page.locator(`[data-testid=trial-banner]`)).toBeVisible(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters