fix(web): workflow org visibility issues

This commit is contained in:
BennyKok
2024-01-01 23:51:12 +08:00
parent 8190740abf
commit 40d9060fda
4 changed files with 42 additions and 3 deletions
+27 -2
View File
@@ -1,10 +1,35 @@
import { db } from "@/db/db";
import { workflowTable, workflowVersionTable } from "@/db/schema";
import { desc, eq } from "drizzle-orm";
import { auth } from "@clerk/nextjs";
import { and, desc, eq, isNull } from "drizzle-orm";
export async function findFirstTableWithVersion(workflow_id: string) {
const { userId, orgId } = auth();
if (!userId) throw new Error("No auth");
return await db.query.workflowTable.findFirst({
with: { versions: { orderBy: desc(workflowVersionTable.version) } },
where: eq(workflowTable.id, workflow_id),
where: and(
eq(workflowTable.id, workflow_id),
orgId
? eq(workflowTable.org_id, orgId)
: and(eq(workflowTable.user_id, userId), isNull(workflowTable.org_id))
),
});
}
export function findWorkflowById(workflow_id: string) {
const { userId, orgId } = auth();
if (!userId) throw new Error("No auth");
return db.query.workflowTable.findFirst({
columns: {
id: true,
},
where: and(
eq(workflowTable.id, workflow_id),
orgId
? eq(workflowTable.org_id, orgId)
: and(eq(workflowTable.user_id, userId), isNull(workflowTable.org_id))
),
});
}