-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #408 from mfts/feat/subscription-plan
feat: check team limits
- Loading branch information
Showing
13 changed files
with
240 additions
and
23 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,40 @@ | ||
The Papermark Commercial License (the “Commercial License”) | ||
Copyright (c) 2024-present Papermark, Inc | ||
|
||
With regard to the Papermark Software: | ||
|
||
This software and associated documentation files (the "Software") may only be | ||
used in production, if you (and any entity that you represent) have agreed to, | ||
and are in compliance with, an agreement governing | ||
the use of the Software, as mutually agreed by you and Papermark, Inc ("Papermark"), | ||
and otherwise have a valid Papermark Enterprise Edition subscription ("Commercial Subscription"). | ||
Subject to the foregoing sentence, you are free to modify this Software and publish patches to the Software. | ||
You agree that Papermark and/or its licensors (as applicable) retain all right, title and interest in | ||
and to all such modifications and/or patches, and all such modifications and/or | ||
patches may only be used, copied, modified, displayed, distributed, or otherwise | ||
exploited with a valid Commercial Subscription for the correct number of hosts. | ||
Notwithstanding the foregoing, you may copy and modify the Software for development | ||
and testing purposes, without requiring a subscription. You agree that Papermark and/or | ||
its licensors (as applicable) retain all right, title and interest in and to all such | ||
modifications. You are not granted any other rights beyond what is expressly stated herein. | ||
Subject to the foregoing, it is forbidden to copy, merge, publish, distribute, sublicense, | ||
and/or sell the Software. | ||
|
||
This Commercial License applies only to the part of this Software that is not distributed under | ||
the AGPLv3 license. Any part of this Software distributed under the MIT license or which | ||
is served client-side as an image, font, cascading stylesheet (CSS), file which produces | ||
or is compiled, arranged, augmented, or combined into client-side JavaScript, in whole or | ||
in part, is copyrighted under the AGPLv3 license. The full text of this Commercial License shall | ||
be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
For all third party components incorporated into the Papermark Software, those | ||
components are licensed under the original license provided by the owner of the | ||
applicable component. |
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,32 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { getServerSession } from "next-auth"; | ||
import { CustomUser } from "@/lib/types"; | ||
import { getLimits } from "@/ee/limits/server"; | ||
import { authOptions } from "@/pages/api/auth/[...nextauth]"; | ||
|
||
export default async function handle( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
) { | ||
if (req.method === "GET") { | ||
// GET /api/:teamId/limits | ||
const session = await getServerSession(req, res, authOptions); | ||
if (!session) { | ||
return res.status(401).end("Unauthorized"); | ||
} | ||
|
||
const { teamId } = req.query as { teamId: string }; | ||
const userId = (session.user as CustomUser).id; | ||
|
||
try { | ||
const limits = await getLimits({ teamId, userId }); | ||
|
||
return res.status(200).json(limits); | ||
} catch (error) { | ||
return res.status(500).json((error as Error).message); | ||
} | ||
} else { | ||
res.setHeader("Allow", ["GET"]); | ||
return res.status(405).end(`Method ${req.method} Not Allowed`); | ||
} | ||
} |
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,49 @@ | ||
import prisma from "@/lib/prisma"; | ||
import { z } from "zod"; | ||
|
||
export async function getLimits({ | ||
teamId, | ||
userId, | ||
}: { | ||
teamId: string; | ||
userId: string; | ||
}) { | ||
const team = await prisma.team.findUnique({ | ||
where: { | ||
id: teamId, | ||
users: { | ||
some: { | ||
userId: userId, | ||
}, | ||
}, | ||
}, | ||
select: { | ||
plan: true, | ||
limits: true, | ||
}, | ||
}); | ||
|
||
if (!team) { | ||
throw new Error("Team not found"); | ||
} | ||
|
||
// parse the limits json with zod and return the limits | ||
// {datarooms: 1, users: 1, domains: 1, customDomainOnPro: boolean, customDomainInDataroom: boolean} | ||
|
||
const configSchema = z.object({ | ||
datarooms: z.number(), | ||
users: z.number(), | ||
domains: z.number(), | ||
customDomainOnPro: z.boolean(), | ||
customDomainInDataroom: z.boolean(), | ||
}); | ||
|
||
try { | ||
const parsedData = configSchema.parse(team.limits); | ||
|
||
return parsedData; | ||
} catch (error) { | ||
// if no limits set, then return null and don't block the team | ||
return null; | ||
} | ||
} |
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,30 @@ | ||
import { useTeam } from "@/context/team-context"; | ||
import useSWR from "swr"; | ||
import { fetcher } from "@/lib/utils"; | ||
|
||
export type LimitProps = { | ||
datarooms: number; | ||
users: number; | ||
domains: number; | ||
customDomainOnPro: boolean; | ||
customDomainInDataroom: boolean; | ||
}; | ||
|
||
export function useLimits() { | ||
const teamInfo = useTeam(); | ||
const teamId = teamInfo?.currentTeam?.id; | ||
|
||
const { data, error } = useSWR<LimitProps | null>( | ||
teamId && `/api/teams/${teamId}/limits`, | ||
fetcher, | ||
{ | ||
dedupingInterval: 30000, | ||
}, | ||
); | ||
|
||
return { | ||
limits: data, | ||
error, | ||
loading: !data && !error, | ||
}; | ||
} |
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,3 @@ | ||
import { useLimits } from "@/ee/limits/swr-handler"; | ||
|
||
export default useLimits; |
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,3 @@ | ||
import limitsHandler from "@/ee/limits/handler"; | ||
|
||
export default limitsHandler; |
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
3 changes: 3 additions & 0 deletions
3
prisma/migrations/20240511000000_add_team_limits/migration.sql
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,3 @@ | ||
-- AlterTable | ||
ALTER TABLE "Team" ADD COLUMN "limits" JSONB; | ||
|
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