mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
feat(ai): update AI settings to use organization-based access control
- Refactor AI-related schemas and services to use organizationId instead of adminId - Update AI router to check organization-level permissions - Modify AI settings creation and retrieval to work with organization context - Adjust server-side props and access checks for AI settings
This commit is contained in:
@@ -3,8 +3,7 @@ import { boolean, pgTable, text } from "drizzle-orm/pg-core";
|
||||
import { createInsertSchema } from "drizzle-zod";
|
||||
import { nanoid } from "nanoid";
|
||||
import { z } from "zod";
|
||||
import { admins } from "./admin";
|
||||
|
||||
import { organization } from "./account";
|
||||
export const ai = pgTable("ai", {
|
||||
aiId: text("aiId")
|
||||
.notNull()
|
||||
@@ -15,18 +14,18 @@ export const ai = pgTable("ai", {
|
||||
apiKey: text("apiKey").notNull(),
|
||||
model: text("model").notNull(),
|
||||
isEnabled: boolean("isEnabled").notNull().default(true),
|
||||
adminId: text("adminId")
|
||||
organizationId: text("organizationId")
|
||||
.notNull()
|
||||
.references(() => admins.adminId, { onDelete: "cascade" }), // Admin ID who created the AI settings
|
||||
.references(() => organization.id, { onDelete: "cascade" }), // Admin ID who created the AI settings
|
||||
createdAt: text("createdAt")
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date().toISOString()),
|
||||
});
|
||||
|
||||
export const aiRelations = relations(ai, ({ one }) => ({
|
||||
admin: one(admins, {
|
||||
fields: [ai.adminId],
|
||||
references: [admins.adminId],
|
||||
organization: one(organization, {
|
||||
fields: [ai.organizationId],
|
||||
references: [organization.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
@@ -53,7 +52,7 @@ export const apiUpdateAi = createSchema
|
||||
.extend({
|
||||
aiId: z.string().min(1),
|
||||
})
|
||||
.omit({ adminId: true });
|
||||
.omit({ organizationId: true });
|
||||
|
||||
export const deploySuggestionSchema = z.object({
|
||||
projectId: z.string().min(1),
|
||||
|
||||
@@ -28,6 +28,9 @@ export const findUserById = async (userId: string) => {
|
||||
export const findOrganizationById = async (organizationId: string) => {
|
||||
const organizationResult = await db.query.organization.findFirst({
|
||||
where: eq(organization.id, organizationId),
|
||||
with: {
|
||||
owner: true,
|
||||
},
|
||||
});
|
||||
return organizationResult;
|
||||
};
|
||||
|
||||
@@ -6,12 +6,12 @@ import { generateObject } from "ai";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { IS_CLOUD } from "../constants";
|
||||
import { findAdminById } from "./admin";
|
||||
import { findServerById } from "./server";
|
||||
import { findOrganizationById } from "./admin";
|
||||
|
||||
export const getAiSettingsByAdminId = async (adminId: string) => {
|
||||
export const getAiSettingsByOrganizationId = async (organizationId: string) => {
|
||||
const aiSettings = await db.query.ai.findMany({
|
||||
where: eq(ai.adminId, adminId),
|
||||
where: eq(ai.organizationId, organizationId),
|
||||
orderBy: desc(ai.createdAt),
|
||||
});
|
||||
return aiSettings;
|
||||
@@ -30,14 +30,14 @@ export const getAiSettingById = async (aiId: string) => {
|
||||
return aiSetting;
|
||||
};
|
||||
|
||||
export const saveAiSettings = async (adminId: string, settings: any) => {
|
||||
export const saveAiSettings = async (organizationId: string, settings: any) => {
|
||||
const aiId = settings.aiId;
|
||||
|
||||
return db
|
||||
.insert(ai)
|
||||
.values({
|
||||
aiId,
|
||||
adminId,
|
||||
organizationId,
|
||||
...settings,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
@@ -53,14 +53,14 @@ export const deleteAiSettings = async (aiId: string) => {
|
||||
};
|
||||
|
||||
interface Props {
|
||||
adminId: string;
|
||||
organizationId: string;
|
||||
aiId: string;
|
||||
input: string;
|
||||
serverId?: string | undefined;
|
||||
}
|
||||
|
||||
export const suggestVariants = async ({
|
||||
adminId,
|
||||
organizationId,
|
||||
aiId,
|
||||
input,
|
||||
serverId,
|
||||
@@ -79,8 +79,8 @@ export const suggestVariants = async ({
|
||||
|
||||
let ip = "";
|
||||
if (!IS_CLOUD) {
|
||||
const admin = await findAdminById(adminId);
|
||||
ip = admin?.serverIp || "";
|
||||
const organization = await findOrganizationById(organizationId);
|
||||
ip = organization?.owner.serverIp || "";
|
||||
}
|
||||
|
||||
if (serverId) {
|
||||
|
||||
Reference in New Issue
Block a user