Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 18 additions & 33 deletions apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getTenantId } from "@/lib/auth";
import { db } from "@/lib/db";
import { notFound, redirect } from "next/navigation";
import { notFound } from "next/navigation";
import { Navigation } from "./navigation";
import { RoleClient } from "./settings-client";
import type { NestedPermissions } from "./settings-client";
Expand Down Expand Up @@ -44,58 +44,43 @@ function sortNestedPermissions(nested: NestedPermissions) {
export default async function RolePage(props: Props) {
const tenantId = getTenantId();

// Get workspace with active permissions only
const workspace = await db.query.workspaces.findFirst({
where: (table, { eq, isNull }) => eq(table.tenantId, tenantId) && isNull(table.deletedAtM),
const role = await db.query.roles.findFirst({
where: (table, { eq }) => eq(table.id, props.params.roleId),
with: {
roles: {
where: (table, { eq }) => eq(table.id, props.params.roleId),
permissions: {
with: {
permission: true,
},
},
workspace: {
with: {
permissions: true,
keys: {
with: {
key: {
columns: {
deletedAtM: true,
},
},
},
},
},
},
permissions: {
keys: {
with: {
roles: true,
key: true,
},
orderBy: (table, { asc }) => [asc(table.name)],
},
},
});

if (!workspace) {
return redirect("/new");
if (!role || !role.workspace) {
return notFound();
}

const role = workspace.roles.at(0);

if (!role) {
if (role.workspace.tenantId !== tenantId) {
return notFound();
}

// Filter out soft-deleted keys
const activeKeys = role.keys?.filter(({ key }) => key.deletedAtM === null) || [];

// Get all active permissions in the workspace
const activePermissions = workspace.permissions;
const permissions = role.workspace.permissions;

// Filter role's permissions to only include active ones
const activeRolePermissionIds = new Set(
role.permissions
.filter((rp) => activePermissions.some((p) => p.id === rp.permissionId))
.filter((rp) => permissions.some((p) => p.id === rp.permissionId))
.map((rp) => rp.permissionId),
);

const sortedPermissions = activePermissions.sort((a, b) => {
const sortedPermissions = permissions.sort((a, b) => {
const aParts = a.name.split(".");
const bParts = b.name.split(".");
if (aParts.length !== bParts.length) {
Expand Down Expand Up @@ -139,7 +124,7 @@ export default async function RolePage(props: Props) {
...role,
permissions: role.permissions.filter((p) => activeRolePermissionIds.has(p.permissionId)),
}}
activeKeys={activeKeys}
activeKeys={role.keys.filter(({ key }) => key.deletedAtM === null)}
sortedNestedPermissions={sortedNestedPermissions}
/>
</div>
Expand Down
Loading