Skip to content

Commit 15bde4c

Browse files
authored
[Feature] Implement “Get My Permissions” api service (#4000)
* add my role permissions API service * Add a global jotai atom that holds the role permissions of the connected user * add ai bot suggestions * add ai bot suggestions * add ai bot suggestions
1 parent b6169bc commit 15bde4c

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

apps/web/core/components/layouts/app/init-state.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
useEmployee
1818
} from '@/core/hooks/organizations';
1919
import { useWorkspaces, useCurrentOrg } from '@/core/hooks/auth';
20-
import { useRoles } from '@/core/hooks/roles';
20+
import { useRolePermissions, useRoles } from '@/core/hooks/roles';
2121
import {
2222
useTaskStatistics,
2323
useAutoAssignTask,
@@ -52,6 +52,7 @@ function InitState() {
5252
const { firstLoadData: firstLoadAutoAssignTask } = useAutoAssignTask();
5353
const { firstLoadRolesData } = useRoles();
5454
const { firstLoadTaskStatusesData, loadTaskStatuses: loadTaskStatusesData } = useTaskStatus();
55+
const { firstLoadMyRolePermissionsData } = useRolePermissions();
5556

5657
// Load workspaces data on app initialization
5758
const { firstLoadWorkspacesData } = useWorkspaces();
@@ -102,6 +103,7 @@ function InitState() {
102103
firstLoadDailyPlanData();
103104
firstLoadDataEmployee();
104105
firstLoadRolesData();
106+
firstLoadMyRolePermissionsData();
105107
// --------------
106108

107109
getTimerStatus();

apps/web/core/hooks/roles/use-role-permissions.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
import { rolePermissionsFormatedState, rolePermissionsState } from '@/core/stores';
1+
import { myRolePermissionsState, rolePermissionsFormatedState, rolePermissionsState } from '@/core/stores';
22
import { useCallback } from 'react';
33
import { useAtom } from 'jotai';
44
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
55
import cloneDeep from 'lodash/cloneDeep';
66
import { rolePermissionService } from '@/core/services/client/api/roles/role-permission.service';
77
import { TRolePermission } from '@/core/types/schemas/role/role-permission-schema';
88
import { queryKeys } from '@/core/query/keys';
9-
import { useConditionalUpdateEffect } from '../common';
9+
import { useConditionalUpdateEffect, useFirstLoad } from '../common';
10+
import { getTenantIdCookie } from '@/core/lib/helpers/cookies';
1011

1112
export const useRolePermissions = (roleId?: string) => {
1213
const [rolePermissions, setRolePermissions] = useAtom(rolePermissionsState);
14+
const [myRolePermissions, setMyRolePermissions] = useAtom(myRolePermissionsState);
1315
const [rolePermissionsFormated, setRolePermissionsFormated] = useAtom(rolePermissionsFormatedState);
16+
const { firstLoadData: firstLoadMyRolePermissionsData } = useFirstLoad();
1417
const queryClient = useQueryClient();
18+
const tenantId = getTenantIdCookie();
1519

1620
// Query for fetching role permissions
1721
const rolePermissionsQuery = useQuery({
@@ -23,6 +27,12 @@ export const useRolePermissions = (roleId?: string) => {
2327
enabled: !!roleId
2428
});
2529

30+
const myRolePermissionsQuery = useQuery({
31+
queryKey: queryKeys.roles.myPermissions(tenantId),
32+
queryFn: () => rolePermissionService.getMyRolePermissions(),
33+
enabled: !!tenantId
34+
});
35+
2636
// Mutation for updating role permissions
2737
const updateRolePermissionMutation = useMutation({
2838
mutationFn: rolePermissionService.updateRolePermission,
@@ -63,6 +73,16 @@ export const useRolePermissions = (roleId?: string) => {
6373
Boolean(rolePermissions?.length)
6474
);
6575

76+
useConditionalUpdateEffect(
77+
() => {
78+
if (myRolePermissionsQuery.data?.items?.length) {
79+
setMyRolePermissions(myRolePermissionsQuery.data.items);
80+
}
81+
},
82+
[myRolePermissionsQuery.data],
83+
Boolean(myRolePermissions?.length)
84+
);
85+
6686
// For backward compatibility
6787
const getRolePermissions = useCallback(
6888
(id: string) => {
@@ -71,12 +91,24 @@ export const useRolePermissions = (roleId?: string) => {
7191
[queryClient]
7292
);
7393

94+
const getMyRolePermissions = useCallback(() => {
95+
queryClient.invalidateQueries({ queryKey: queryKeys.roles.myPermissions(tenantId) });
96+
}, [queryClient, tenantId]);
97+
98+
const handleFirstLoad = useCallback(async () => {
99+
await myRolePermissionsQuery.refetch();
100+
firstLoadMyRolePermissionsData();
101+
}, [firstLoadMyRolePermissionsData, myRolePermissionsQuery]);
102+
74103
return {
75104
loading: rolePermissionsQuery.isLoading,
76105
rolePermissions,
77106
getRolePermissions,
78107
updateRolePermission: updateRolePermissionMutation.mutate,
79108
updateRolePermissionLoading: updateRolePermissionMutation.isPending,
80-
rolePermissionsFormated
109+
rolePermissionsFormated,
110+
myRolePermissions,
111+
getMyRolePermissions,
112+
firstLoadMyRolePermissionsData: handleFirstLoad
81113
};
82114
};

apps/web/core/query/keys/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ export const queryKeys = {
111111
detail: (roleId: string | undefined | null) => ['roles', ...(roleId ? [roleId] : [])] as const,
112112
permissions: (roleId: string | undefined | null) =>
113113
['roles', ...(roleId ? [roleId] : []), 'permissions'] as const,
114-
users: (roleId: string | undefined | null) => ['roles', ...(roleId ? [roleId] : []), 'users'] as const
114+
users: (roleId: string | undefined | null) => ['roles', ...(roleId ? [roleId] : []), 'users'] as const,
115+
myPermissions: (tenantId: string | undefined | null) =>
116+
['roles', 'my-permissions', ...(tenantId ? [tenantId] : [])] as const
115117
},
116118
permissions: {
117119
all: ['permissions'] as const,

apps/web/core/services/client/api/roles/role-permission.service.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ class RolePermissionService extends APIService {
8080
throw error;
8181
}
8282
};
83+
84+
/**
85+
* Get my role permissions
86+
*
87+
* @returns Promise<PaginationResponse<TRolePermission>> - Validated role permissions for current user
88+
* @throws ValidationError if response data doesn't match schema
89+
*/
90+
91+
getMyRolePermissions = async () => {
92+
try {
93+
const response = await this.get<PaginationResponse<TRolePermission>>('/role-permissions/me');
94+
95+
// Validate the response data using Zod schema
96+
return validatePaginationResponse(rolePermissionSchema, response.data, 'getMyRolePermissions API response');
97+
} catch (error) {
98+
if (error instanceof ZodValidationError) {
99+
this.logger.error(
100+
'Get my role permissions validation failed:',
101+
{
102+
message: error.message,
103+
issues: error.issues
104+
},
105+
'RolePermissionService'
106+
);
107+
}
108+
throw error;
109+
}
110+
};
83111
}
84112

85113
export const rolePermissionService = new RolePermissionService(GAUZY_API_BASE_SERVER_URL.value);

apps/web/core/stores/auth/role-permissions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { TRolePermission } from '@/core/types/schemas/role/role-permission-schem
22
import { atom } from 'jotai';
33

44
export const rolePermissionsState = atom<TRolePermission[]>([]);
5+
export const myRolePermissionsState = atom<TRolePermission[]>([]);
56

67
export const rolePermissionsFormatedState = atom<{
78
[key: string]: TRolePermission;
89
}>({});
10+
11+
export const myPermissions = atom<string[]>((get) => {
12+
const myRolePermissions = get(myRolePermissionsState);
13+
return myRolePermissions.map((item) => item.permission);
14+
});

0 commit comments

Comments
 (0)