Initial Commit

This commit is contained in:
Ahmad 2024-02-14 21:30:10 -05:00
commit f3e2f01bd7
No known key found for this signature in database
GPG key ID: 8FD8A93530D182BF
150 changed files with 13612 additions and 0 deletions

86
lib/org-limit.ts Normal file
View file

@ -0,0 +1,86 @@
import { auth } from "@clerk/nextjs";
import { db } from "@/lib/db";
import { MAX_FREE_BOARDS } from "@/constants/boards";
export const incrementAvailableCount = async () => {
const { orgId } = auth();
if (!orgId) {
throw new Error("Unauthorized");
}
const orgLimit = await db.orgLimit.findUnique({
where: { orgId },
});
if (orgLimit) {
await db.orgLimit.update({
where: { orgId },
data: { count: orgLimit.count + 1 },
});
} else {
await db.orgLimit.create({
data: { orgId, count: 1 },
});
}
};
export const decreaseAvailableCount = async () => {
const { orgId } = auth();
if (!orgId) {
throw new Error("Unauthorized");
}
const orgLimit = await db.orgLimit.findUnique({
where: { orgId },
});
if (orgLimit) {
await db.orgLimit.update({
where: { orgId },
data: { count: orgLimit.count > 0 ? orgLimit.count - 1 : 0 },
});
} else {
await db.orgLimit.create({
data: { orgId, count: 1 },
});
}
};
export const hasAvailableCount = async () => {
const { orgId } = auth();
if (!orgId) {
throw new Error("Unauthorized");
}
const orgLimit = await db.orgLimit.findUnique({
where: { orgId },
});
if (!orgLimit || orgLimit.count < MAX_FREE_BOARDS) {
return true;
} else {
return false;
}
};
export const getAvailableCount = async () => {
const { orgId } = auth();
if (!orgId) {
return 0;
}
const orgLimit = await db.orgLimit.findUnique({
where: { orgId },
});
if (!orgLimit) {
return 0;
}
return orgLimit.count;
};