Spaces:
Sleeping
Sleeping
| import { RoleFeature, RoleFeatureCreateRequest, RoleFeatureUpdateRequest } from '../types'; | |
| import apiClient from '@/lib/api/axios-instance'; | |
| import { createHeaders } from '@/lib/api'; | |
| import { API_BASE_URL } from '../config'; | |
| import axios from 'axios'; | |
| // Helper interfaces to match the backend API format | |
| interface BackendRoleFeatureRequest { | |
| roleFeatureId: number; | |
| roleId: number; | |
| featureId: number; | |
| permissionId: number; | |
| } | |
| // RoleFeature API service | |
| export const roleFeatureApi = { | |
| getAll: async (): Promise<RoleFeature[]> => { | |
| const response = await apiClient.get(`/api/RoleFeature`); | |
| if (!response.data) { | |
| throw new Error('Failed to fetch role features'); | |
| } | |
| const data = response.data; | |
| // Convert backend format to frontend format | |
| return data.map((item: any) => ({ | |
| ...item, | |
| permissionIds: item.permissionId !== undefined ? [item.permissionId] : [] | |
| })); | |
| }, | |
| getById: async (id: number): Promise<RoleFeature> => { | |
| const response = await apiClient.get(`/api/RoleFeature/${id}`); | |
| if (!response.data) { | |
| throw new Error(`Failed to fetch role feature with ID ${id}`); | |
| } | |
| const data = response.data; | |
| // Convert backend format to frontend format | |
| return { | |
| ...data, | |
| permissionIds: data.permissionId !== undefined ? [data.permissionId] : [] | |
| }; | |
| }, | |
| getByRoleId: async (roleId: number): Promise<RoleFeature[]> => { | |
| const response = await apiClient.get(`/api/RoleFeature/GetByRoleID/${roleId}`); | |
| if (!response.data) { | |
| throw new Error(`Failed to fetch role features for role ID ${roleId}`); | |
| } | |
| const data = response.data; | |
| // Convert backend format to frontend format | |
| return data.map((item: any) => ({ | |
| ...item, | |
| permissionIds: item.permissionId !== undefined ? [item.permissionId] : [] | |
| })); | |
| }, | |
| create: async (roleFeature: RoleFeatureCreateRequest): Promise<RoleFeature> => { | |
| // If we have multiple permissions, create multiple role features | |
| if (roleFeature.permissionIds && roleFeature.permissionIds.length > 0) { | |
| // For the first permission, create the role feature | |
| const firstPermission = roleFeature.permissionIds[0]; | |
| const backendRequest: BackendRoleFeatureRequest = { | |
| roleFeatureId: roleFeature.roleFeatureId, | |
| roleId: roleFeature.roleId, | |
| featureId: roleFeature.featureId, | |
| permissionId: firstPermission | |
| }; | |
| const response = await apiClient.post(`/api/RoleFeature`, backendRequest); | |
| if (!response.data) { | |
| throw new Error('Failed to create role feature'); | |
| } | |
| const createdRoleFeature = response.data; | |
| // For additional permissions, create additional role features | |
| if (roleFeature.permissionIds.length > 1) { | |
| const additionalPermissions = roleFeature.permissionIds.slice(1); | |
| for (const permissionId of additionalPermissions) { | |
| const additionalRequest: BackendRoleFeatureRequest = { | |
| roleFeatureId: 0, // Let backend generate new ID | |
| roleId: roleFeature.roleId, | |
| featureId: roleFeature.featureId, | |
| permissionId: permissionId | |
| }; | |
| try { | |
| await apiClient.post(`/api/RoleFeature`, additionalRequest); | |
| } catch (error) { | |
| console.error("Error creating additional permission:", error); | |
| } | |
| } | |
| } | |
| // Return the created role feature with all permissions | |
| return { | |
| ...createdRoleFeature, | |
| permissionIds: roleFeature.permissionIds | |
| }; | |
| } else { | |
| throw new Error('Cannot create role feature without permissions'); | |
| } | |
| }, | |
| update: async (id: number, roleFeature: RoleFeatureUpdateRequest): Promise<void> => { | |
| // For update, we'll update the existing record with the first permission | |
| if (roleFeature.permissionIds && roleFeature.permissionIds.length > 0) { | |
| const firstPermission = roleFeature.permissionIds[0]; | |
| const backendRequest: BackendRoleFeatureRequest = { | |
| roleFeatureId: roleFeature.roleFeatureId, | |
| roleId: roleFeature.roleId, | |
| featureId: roleFeature.featureId, | |
| permissionId: firstPermission | |
| }; | |
| const response = await apiClient.put(`/api/RoleFeature/${id}`, backendRequest); | |
| if (response.status >= 400) { | |
| throw new Error(`Failed to update role feature with ID ${id}`); | |
| } | |
| // For additional permissions, create new role features | |
| if (roleFeature.permissionIds.length > 1) { | |
| const additionalPermissions = roleFeature.permissionIds.slice(1); | |
| for (const permissionId of additionalPermissions) { | |
| const additionalRequest: BackendRoleFeatureRequest = { | |
| roleFeatureId: 0, // Let backend generate new ID | |
| roleId: roleFeature.roleId, | |
| featureId: roleFeature.featureId, | |
| permissionId: permissionId | |
| }; | |
| try { | |
| await apiClient.post(`/api/RoleFeature`, additionalRequest); | |
| } catch (error) { | |
| console.error("Error creating additional permission:", error); | |
| } | |
| } | |
| } | |
| } else { | |
| throw new Error('Cannot update role feature without permissions'); | |
| } | |
| }, | |
| delete: async (id: number): Promise<void> => { | |
| const response = await apiClient.delete(`/api/RoleFeature/${id}`); | |
| if (response.status >= 400) { | |
| throw new Error(`Failed to delete role feature with ID ${id}`); | |
| } | |
| }, | |
| }; |