-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d33aa0a
commit 7c65bfe
Showing
10 changed files
with
153 additions
and
29 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
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
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,57 @@ | ||
import { auth } from "@clerk/nextjs"; | ||
|
||
import prismadb from "@/lib/prismadb"; | ||
import { MAX_FREE_COUNTS } from "@/constants"; | ||
|
||
export const incrementApiLimit = async () => { | ||
const { userId } = auth(); | ||
|
||
if (!userId) return | ||
|
||
const userApiLimit = await prismadb.userApiLimit.findUnique({ | ||
where: { userId: userId }, | ||
}); | ||
|
||
if (userApiLimit) { | ||
await prismadb.userApiLimit.update({ | ||
where: { userId: userId }, | ||
data: { count: userApiLimit.count + 1 }, | ||
}); | ||
} else { | ||
await prismadb.userApiLimit.create({ | ||
data: { userId: userId, count: 1 }, | ||
}); | ||
} | ||
}; | ||
|
||
export const checkApiLimit = async () => { | ||
const { userId } = auth(); | ||
|
||
if (!userId) return false | ||
|
||
const userApiLimit = await prismadb.userApiLimit.findUnique({ | ||
where: { userId: userId }, | ||
}); | ||
|
||
if (!userApiLimit || userApiLimit.count < MAX_FREE_COUNTS) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
export const getApiLimitCount = async () => { | ||
const { userId } = auth(); | ||
|
||
if (!userId) return 0 | ||
|
||
const userApiLimit = await prismadb.userApiLimit.findUnique({ | ||
where: { | ||
userId | ||
} | ||
}); | ||
|
||
if (!userApiLimit) return 0; | ||
|
||
return userApiLimit.count; | ||
}; |
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,10 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
declare global { | ||
var prisma: PrismaClient | undefined | ||
} | ||
|
||
const prismadb = globalThis.prisma || new PrismaClient() | ||
if (process.env.NODE_ENV !== "production") globalThis.prisma = prismadb | ||
|
||
export default prismadb; |
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,28 @@ | ||
// This is your Prisma schema file, | ||
// learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model UserApiLimit { | ||
id String @id @default(cuid()) | ||
userId String @unique | ||
count Int @default(0) | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
} | ||
|
||
model UserSubscription { | ||
id String @id @default(cuid()) | ||
userId String @unique | ||
stripeCustomerId String? @unique @map(name: "stripe_customer_id") | ||
stripeSubscriptionId String? @unique @map(name: "stripe_subscription_id") | ||
stripePriceId String? @map(name: "stripe_price_id") | ||
stripeCurrentPeriodEnd DateTime? @map(name: "stripe_current_period_end") | ||
} |
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 |
---|---|---|
|
@@ -322,6 +322,23 @@ | |
picocolors "^1.0.0" | ||
tslib "^2.6.0" | ||
|
||
"@prisma/client@^5.0.0": | ||
version "5.0.0" | ||
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-5.0.0.tgz#9f0cd4164f4ffddb28bb1811c27eb7fa1e01a087" | ||
integrity sha512-XlO5ELNAQ7rV4cXIDJUNBEgdLwX3pjtt9Q/RHqDpGf43szpNJx2hJnggfFs7TKNx0cOFsl6KJCSfqr5duEU/bQ== | ||
dependencies: | ||
"@prisma/engines-version" "4.17.0-26.6b0aef69b7cdfc787f822ecd7cdc76d5f1991584" | ||
|
||
"@prisma/engines-version@4.17.0-26.6b0aef69b7cdfc787f822ecd7cdc76d5f1991584": | ||
version "4.17.0-26.6b0aef69b7cdfc787f822ecd7cdc76d5f1991584" | ||
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-4.17.0-26.6b0aef69b7cdfc787f822ecd7cdc76d5f1991584.tgz#b36eda5620872d3fac810c302a7e46cf41daa033" | ||
integrity sha512-HHiUF6NixsldsP3JROq07TYBLEjXFKr6PdH8H4gK/XAoTmIplOJBCgrIUMrsRAnEuGyRoRLXKXWUb943+PFoKQ== | ||
|
||
"@prisma/[email protected]": | ||
version "5.0.0" | ||
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-5.0.0.tgz#5249650eabe77c458c90f2be97d8210353c2e22e" | ||
integrity sha512-kyT/8fd0OpWmhAU5YnY7eP31brW1q1YrTGoblWrhQJDiN/1K+Z8S1kylcmtjqx5wsUGcP1HBWutayA/jtyt+sg== | ||
|
||
"@radix-ui/[email protected]": | ||
version "1.0.1" | ||
resolved "https://registry.yarnpkg.com/@radix-ui/number/-/number-1.0.1.tgz#644161a3557f46ed38a042acf4a770e826021674" | ||
|
@@ -3086,6 +3103,13 @@ prelude-ls@^1.2.1: | |
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396" | ||
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g== | ||
|
||
prisma@^5.0.0: | ||
version "5.0.0" | ||
resolved "https://registry.yarnpkg.com/prisma/-/prisma-5.0.0.tgz#f6571c46dc2478172cb7bc1bb62d74026a2c2630" | ||
integrity sha512-KYWk83Fhi1FH59jSpavAYTt2eoMVW9YKgu8ci0kuUnt6Dup5Qy47pcB4/TLmiPAbhGrxxSz7gsSnJcCmkyPANA== | ||
dependencies: | ||
"@prisma/engines" "5.0.0" | ||
|
||
prop-types@^15.0.0, prop-types@^15.8.1: | ||
version "15.8.1" | ||
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5" | ||
|