feat(organization): allow setting a default role for new members

This commit is contained in:
Mauricio Siu
2026-08-11 02:21:33 -06:00
parent 1ccf15f331
commit aaf8d7e12f
10 changed files with 9241 additions and 9 deletions
@@ -1,6 +1,10 @@
import { db } from "@dokploy/server/db";
import { user } from "@dokploy/server/db/schema";
import { eq } from "drizzle-orm";
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) => {
@@ -22,3 +26,35 @@ export const hasValidLicense = async (organizationId: string) => {
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;
};