-
Notifications
You must be signed in to change notification settings - Fork 1
/
api-limit.ts
57 lines (43 loc) · 1.25 KB
/
api-limit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
};