@@ -9,7 +9,7 @@ export function roleRoutes(fastify: FastifyInstance) {
99 fastify . post (
1010 "/api/v1/role/create" ,
1111 {
12- preHandler : requirePermission ( [ ' role::create' ] ) ,
12+ preHandler : requirePermission ( [ " role::create" ] ) ,
1313 } ,
1414 async ( request : FastifyRequest , reply : FastifyReply ) => {
1515 const user = await checkSession ( request ) ;
@@ -20,9 +20,9 @@ export function roleRoutes(fastify: FastifyInstance) {
2020 } ) ;
2121
2222 if ( existingRole ) {
23- return reply . status ( 400 ) . send ( {
24- message : "Role already exists" ,
25- success : false
23+ return reply . status ( 400 ) . send ( {
24+ message : "Role already exists" ,
25+ success : false ,
2626 } ) ;
2727 }
2828
@@ -50,28 +50,34 @@ export function roleRoutes(fastify: FastifyInstance) {
5050 fastify . get (
5151 "/api/v1/roles/all" ,
5252 {
53- preHandler : requirePermission ( [ ' role::read' ] ) ,
53+ preHandler : requirePermission ( [ " role::read" ] ) ,
5454 } ,
5555 async ( request : FastifyRequest , reply : FastifyReply ) => {
5656 const roles = await prisma . role . findMany ( {
5757 include : {
58- users : true ,
58+ users : false ,
59+ } ,
60+ } ) ;
61+
62+ const active = await prisma . config . findFirst ( {
63+ select : {
64+ roles_active : true ,
5965 } ,
6066 } ) ;
6167
62- reply . status ( 200 ) . send ( { roles, success : true } ) ;
68+ reply . status ( 200 ) . send ( { roles, success : true , roles_active : active } ) ;
6369 }
6470 ) ;
6571
6672 // Get role by ID
6773 fastify . get (
6874 "/api/v1/role/:id" ,
6975 {
70- preHandler : requirePermission ( [ ' role::read' ] ) ,
76+ preHandler : requirePermission ( [ " role::read" ] ) ,
7177 } ,
7278 async ( request : FastifyRequest , reply : FastifyReply ) => {
7379 const { id } : any = request . params ;
74-
80+
7581 const role = await prisma . role . findUnique ( {
7682 where : { id } ,
7783 include : {
@@ -80,9 +86,9 @@ export function roleRoutes(fastify: FastifyInstance) {
8086 } ) ;
8187
8288 if ( ! role ) {
83- return reply . status ( 404 ) . send ( {
84- message : "Role not found" ,
85- success : false
89+ return reply . status ( 404 ) . send ( {
90+ message : "Role not found" ,
91+ success : false ,
8692 } ) ;
8793 }
8894
@@ -94,11 +100,12 @@ export function roleRoutes(fastify: FastifyInstance) {
94100 fastify . put (
95101 "/api/v1/role/:id/update" ,
96102 {
97- preHandler : requirePermission ( [ ' role::update' ] ) ,
103+ preHandler : requirePermission ( [ " role::update" ] ) ,
98104 } ,
99105 async ( request : FastifyRequest , reply : FastifyReply ) => {
100106 const { id } : any = request . params ;
101- const { name, description, permissions, isDefault, users } : any = request . body ;
107+ const { name, description, permissions, isDefault, users } : any =
108+ request . body ;
102109
103110 try {
104111 const updatedRole = await prisma . role . update ( {
@@ -110,17 +117,19 @@ export function roleRoutes(fastify: FastifyInstance) {
110117 isDefault,
111118 updatedAt : new Date ( ) ,
112119 users : {
113- set : Array . isArray ( users ) ? users . map ( userId => ( { id : userId } ) ) : [ { id : users } ] , // Ensure users is an array of objects with unique IDs when updating
120+ set : Array . isArray ( users )
121+ ? users . map ( ( userId ) => ( { id : userId } ) )
122+ : [ { id : users } ] , // Ensure users is an array of objects with unique IDs when updating
114123 } ,
115124 } ,
116125 } ) ;
117126
118127 reply . status ( 200 ) . send ( { role : updatedRole , success : true } ) ;
119128 } catch ( error : any ) {
120- if ( error . code === ' P2025' ) {
121- return reply . status ( 404 ) . send ( {
122- message : "Role not found" ,
123- success : false
129+ if ( error . code === " P2025" ) {
130+ return reply . status ( 404 ) . send ( {
131+ message : "Role not found" ,
132+ success : false ,
124133 } ) ;
125134 }
126135 throw error ;
@@ -132,22 +141,22 @@ export function roleRoutes(fastify: FastifyInstance) {
132141 fastify . delete (
133142 "/api/v1/role/:id/delete" ,
134143 {
135- preHandler : requirePermission ( [ ' role::delete' ] ) ,
144+ preHandler : requirePermission ( [ " role::delete" ] ) ,
136145 } ,
137146 async ( request : FastifyRequest , reply : FastifyReply ) => {
138147 const { id } : any = request . params ;
139-
148+
140149 try {
141150 await prisma . role . delete ( {
142151 where : { id } ,
143152 } ) ;
144153
145154 reply . status ( 200 ) . send ( { success : true } ) ;
146155 } catch ( error : any ) {
147- if ( error . code === ' P2025' ) {
148- return reply . status ( 404 ) . send ( {
149- message : "Role not found" ,
150- success : false
156+ if ( error . code === " P2025" ) {
157+ return reply . status ( 404 ) . send ( {
158+ message : "Role not found" ,
159+ success : false ,
151160 } ) ;
152161 }
153162 throw error ;
@@ -159,7 +168,7 @@ export function roleRoutes(fastify: FastifyInstance) {
159168 fastify . post (
160169 "/api/v1/role/assign" ,
161170 {
162- preHandler : requirePermission ( [ ' role::update' ] ) ,
171+ preHandler : requirePermission ( [ " role::update" ] ) ,
163172 } ,
164173 async ( request : FastifyRequest , reply : FastifyReply ) => {
165174 const { userId, roleId } : any = request . body ;
@@ -179,10 +188,10 @@ export function roleRoutes(fastify: FastifyInstance) {
179188
180189 reply . status ( 200 ) . send ( { user : updatedUser , success : true } ) ;
181190 } catch ( error : any ) {
182- if ( error . code === ' P2025' ) {
183- return reply . status ( 404 ) . send ( {
184- message : "User or Role not found" ,
185- success : false
191+ if ( error . code === " P2025" ) {
192+ return reply . status ( 404 ) . send ( {
193+ message : "User or Role not found" ,
194+ success : false ,
186195 } ) ;
187196 }
188197 throw error ;
@@ -194,7 +203,7 @@ export function roleRoutes(fastify: FastifyInstance) {
194203 fastify . post (
195204 "/api/v1/role/remove" ,
196205 {
197- // preHandler: requirePermission(['role::remove']),
206+ // preHandler: requirePermission(['role::remove']),
198207 } ,
199208 async ( request : FastifyRequest , reply : FastifyReply ) => {
200209 const { userId, roleId } : any = request . body ;
@@ -214,10 +223,10 @@ export function roleRoutes(fastify: FastifyInstance) {
214223
215224 reply . status ( 200 ) . send ( { user : updatedUser , success : true } ) ;
216225 } catch ( error : any ) {
217- if ( error . code === ' P2025' ) {
218- return reply . status ( 404 ) . send ( {
219- message : "User or Role not found" ,
220- success : false
226+ if ( error . code === " P2025" ) {
227+ return reply . status ( 404 ) . send ( {
228+ message : "User or Role not found" ,
229+ success : false ,
221230 } ) ;
222231 }
223232 throw error ;
0 commit comments