mirror of
https://github.com/andwati/dokploy.git
synced 2026-10-24 05:52:19 +00:00
61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
import { db } from "@dokploy/server/db";
|
|
import {
|
|
organization,
|
|
organizationRole,
|
|
user,
|
|
} from "@dokploy/server/db/schema";
|
|
import { and, eq } from "drizzle-orm";
|
|
import { getOrganizationOwnerId } from "./sso";
|
|
|
|
export const hasValidLicense = async (organizationId: string) => {
|
|
const ownerId = await getOrganizationOwnerId(organizationId);
|
|
|
|
if (!ownerId) {
|
|
return false;
|
|
}
|
|
|
|
const currentUser = await db.query.user.findFirst({
|
|
where: eq(user.id, ownerId),
|
|
columns: {
|
|
enableEnterpriseFeatures: true,
|
|
isValidEnterpriseLicense: true,
|
|
},
|
|
});
|
|
return !!(
|
|
currentUser?.enableEnterpriseFeatures &&
|
|
currentUser?.isValidEnterpriseLicense
|
|
);
|
|
};
|
|
|
|
export const resolveOrganizationDefaultRole = async (
|
|
organizationId: string,
|
|
) => {
|
|
const org = await db.query.organization.findFirst({
|
|
where: eq(organization.id, organizationId),
|
|
columns: { defaultRole: true },
|
|
});
|
|
const defaultRole = org?.defaultRole;
|
|
|
|
if (!defaultRole || defaultRole === "owner") {
|
|
return "member";
|
|
}
|
|
|
|
if (defaultRole === "admin" || defaultRole === "member") {
|
|
return defaultRole;
|
|
}
|
|
|
|
const customRole = await db.query.organizationRole.findFirst({
|
|
where: and(
|
|
eq(organizationRole.organizationId, organizationId),
|
|
eq(organizationRole.role, defaultRole),
|
|
),
|
|
columns: { id: true },
|
|
});
|
|
|
|
if (!customRole || !(await hasValidLicense(organizationId))) {
|
|
return "member";
|
|
}
|
|
|
|
return defaultRole;
|
|
};
|