test: get upgradeable test (#15261)

* Add test for defaeult handler

* tests: add tests for getUsersTopbanner-getUpgradeable

* Import router to use userId
This commit is contained in:
sean-brydon authored and GitHub committed 2024-05-30 12:28:40 +00:00
1 parent 344454e574
commit 793c0c60e3
4 files changed
+144 -11

No files matched your search

@@ -35,7 +35,7 @@ const checkInvalidGoogleCalendarCredentials = async ({ ctx }: Props) => {
};
export const getUserTopBannersHandler = async ({ ctx }: Props) => {
const upgradeableTeamMememberships = getUpgradeableHandler({ ctx });
const upgradeableTeamMememberships = getUpgradeableHandler({ userId: ctx.user.id });
const upgradeableOrgMememberships = checkIfOrgNeedsUpgradeHandler({ ctx });
const shouldEmailVerify = shouldVerifyEmailHandler({ ctx });
const isInvalidCalendarCredential = checkInvalidGoogleCalendarCredentials({ ctx });
@@ -102,12 +102,12 @@ export const viewerTeamsRouter = router({
return handler(opts);
}),
/** This is a temporal endpoint so we can progressively upgrade teams to the new billing system. */
getUpgradeable: authedProcedure.query(async (opts) => {
getUpgradeable: authedProcedure.query(async ({ ctx }) => {
const handler = await importHandler(
namespaced("getUpgradeable"),
() => import("./getUpgradeable.handler")
);
return handler(opts);
return handler({ userId: ctx.user.id });
}),
listMembers: authedProcedure.input(ZListMembersInputSchema).query(async (opts) => {
const handler = await importHandler(namespaced("listMembers"), () => import("./listMembers.handler"));
@@ -2,22 +2,19 @@ import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
import { prisma } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
type GetUpgradeableOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
userId: number;
};
export const getUpgradeableHandler = async ({ ctx }: GetUpgradeableOptions) => {
export const getUpgradeableHandler = async ({ userId }: GetUpgradeableOptions) => {
if (!IS_TEAM_BILLING_ENABLED) return [];
// Get all teams/orgs where the user is an owner
let teams = await prisma.membership.findMany({
where: {
user: {
id: ctx.user.id,
id: userId,
},
role: MembershipRole.OWNER,
team: {
@@ -37,8 +34,8 @@ export const getUpgradeableHandler = async ({ ctx }: GetUpgradeableOptions) => {
teams = teams.filter((m) => {
const metadata = teamMetadataSchema.safeParse(m.team.metadata);
if (metadata.success && metadata.data?.subscriptionId) return false;
if (m.team.isOrganization) return false; // We also dont return ORGs as it will be handled in OrgUpgradeBanner
if (m.team.children.length > 0) return false; // We also dont return ORGs as it will be handled in OrgUpgradeBanner
if (m.team.isOrganization) return false; // We also don't return ORGs as it will be handled in OrgUpgradeBanner
if (m.team.children.length > 0) return false; // We also don't return ORGs as it will be handled in OrgUpgradeBanner
return true;
});
return teams;
@@ -0,0 +1,136 @@
import prismaMock from "../../../../../../tests/libs/__mocks__/prismaMock";
import { describe, it, expect, beforeEach, vi } from "vitest";
import { MembershipRole } from "@calcom/prisma/enums";
import getUpgradeableHandler from "./getUpgradeable.handler";
vi.mock("@calcom/lib/constants", () => ({
IS_TEAM_BILLING_ENABLED: true,
}));
describe("getUpgradeableHandler", () => {
const ctx = {
userId: 1137,
};
beforeEach(() => {
// Reset all mocks before each test
vi.clearAllMocks();
});
it("should return teams without subscriptionId in their metadata", async () => {
// Mock the return value of prisma.membership.findMany
prismaMock.membership.findMany.mockResolvedValue([
{
team: {
metadata: {},
isOrganization: false,
children: [],
},
user: { id: 1137 },
role: MembershipRole.OWNER,
},
{
team: {
metadata: { subscriptionId: "123" },
isOrganization: false,
children: [],
},
user: { id: 1137 },
role: MembershipRole.OWNER,
},
]);
const result = await getUpgradeableHandler(ctx);
expect(result).toEqual([
{
team: {
metadata: {},
isOrganization: false,
children: [],
},
user: { id: 1137 },
role: MembershipRole.OWNER,
},
]);
});
it("should return teams that are not organizations", async () => {
// Mock the return value of prisma.membership.findMany
prismaMock.membership.findMany.mockResolvedValue([
{
team: {
metadata: {},
isOrganization: true,
children: [],
},
user: { id: 1137 },
role: MembershipRole.OWNER,
},
{
team: {
metadata: {},
isOrganization: false,
children: [],
},
user: { id: 1137 },
role: MembershipRole.OWNER,
},
]);
const result = await getUpgradeableHandler(ctx);
expect(result).toEqual([
{
team: {
metadata: {},
isOrganization: false,
children: [],
},
user: { id: 1137 },
role: MembershipRole.OWNER,
},
]);
});
it("should return teams without children", async () => {
// Mock the return value of prisma.membership.findMany
prismaMock.membership.findMany.mockResolvedValue([
{
team: {
metadata: {},
isOrganization: false,
children: [{ id: "child-id-1" }],
},
user: { id: 1137 },
role: MembershipRole.OWNER,
},
{
team: {
metadata: {},
isOrganization: false,
children: [],
},
user: { id: 1137 },
role: MembershipRole.OWNER,
},
]);
const result = await getUpgradeableHandler(ctx);
expect(result).toEqual([
{
team: {
metadata: {},
isOrganization: false,
children: [],
},
user: { id: 1137 },
role: MembershipRole.OWNER,
},
]);
});
});