Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions packages/common/src/services/access/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
import { invalidateMultiple } from '@op/cache';
import { db } from '@op/db/client';
import {
accessRolePermissionsOnAccessZones,
accessRoles,
organizationUserToAccessRoles,
profileUserToAccessRoles,
profileUsers,
} from '@op/db/schema';
import { permission, toBitField } from 'access-zones';
import { and, eq } from 'drizzle-orm';

import { CommonError, NotFoundError } from '../../utils';
import { assertProfileAdmin } from '../assert';

export async function invalidateProfileUserCacheForRole(roleId: string) {
const affectedUsers = await db
.select({
profileId: profileUsers.profileId,
authUserId: profileUsers.authUserId,
})
.from(profileUserToAccessRoles)
.innerJoin(
profileUsers,
eq(profileUserToAccessRoles.profileUserId, profileUsers.id),
)
.where(eq(profileUserToAccessRoles.accessRoleId, roleId));

if (affectedUsers.length > 0) {
await invalidateMultiple({
type: 'profileUser',
paramsList: affectedUsers.map((u) => [u.profileId, u.authUserId]),
});
}
}

export type Permissions = {
admin: boolean;
create: boolean;
Expand Down Expand Up @@ -152,6 +176,8 @@ export async function updateRolePermissions({
});
}

await invalidateProfileUserCacheForRole(roleId);

return role;
}

Expand Down Expand Up @@ -180,6 +206,9 @@ export async function deleteRole({

await assertProfileAdmin(user, role.profileId);

// Invalidate before delete (cascade will remove the join rows we query)
await invalidateProfileUserCacheForRole(roleId);

// Delete the role (cascade will handle permissions)
await db.delete(accessRoles).where(eq(accessRoles.id, roleId));

Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/services/decision/decisionRoles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { permission, toBitField } from 'access-zones';
import { eq } from 'drizzle-orm';

import { CommonError, NotFoundError } from '../../utils';
import { invalidateProfileUserCacheForRole } from '../access/permissions';
import { assertProfileAdmin } from '../assert';
import {
type DecisionRolePermissions,
Expand Down Expand Up @@ -191,5 +192,7 @@ export async function updateDecisionRoles({
});
}

await invalidateProfileUserCacheForRole(roleId);

return { roleId, decisionPermissions };
}
6 changes: 6 additions & 0 deletions packages/common/src/services/profile/updateProfileUserRole.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { invalidate } from '@op/cache';
import { and, db, eq, inArray } from '@op/db/client';
import { profileUserToAccessRoles } from '@op/db/schema';
import type { User } from '@op/supabase/lib';
Expand Down Expand Up @@ -102,6 +103,11 @@ export const updateProfileUserRoles = async ({
});
}

await invalidate({
type: 'profileUser',
params: [targetProfileId, targetProfileUser.authUserId],
});

// Fetch and return the updated profile user with full relations
const updatedProfileUser = await getProfileUserWithRelations(profileUserId);
if (!updatedProfileUser) {
Expand Down
Loading