2024-03-21 21:15:27 +00:00
|
|
|
import { auth } from '@clerk/nextjs/server';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
2024-02-16 01:49:19 +00:00
|
|
|
import { db } from '@/lib/db';
|
|
|
|
import { MAX_FREE_BOARDS } from '@/constants/boards';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
|
|
|
export const incrementAvailableCount = async () => {
|
|
|
|
const { orgId } = auth();
|
|
|
|
|
|
|
|
if (!orgId) {
|
2024-02-16 01:49:19 +00:00
|
|
|
throw new Error('Unauthorized');
|
2024-02-15 02:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-02-16 01:49:19 +00:00
|
|
|
throw new Error('Unauthorized');
|
2024-02-15 02:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-02-16 01:49:19 +00:00
|
|
|
throw new Error('Unauthorized');
|
2024-02-15 02:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|