11import { ApiResponse } from './ApiResponses' ;
22import { BASE_API_URL } from '../Enums' ;
33
4- export async function getPermissionRequest ( type , token ) {
4+ export async function getPermissionRequest ( type , userId , token ) {
55 const status = new ApiResponse ( ) ;
66 const url = new URL ( '/api/PermissionRequest/get' , BASE_API_URL ) ;
77 url . searchParams . append ( 'type' , type ) ;
8+ if ( userId ) {
9+ url . searchParams . append ( 'userId' , userId ) ;
10+ }
811
912 try {
1013 const res = await fetch ( url . toString ( ) , {
@@ -15,11 +18,11 @@ export async function getPermissionRequest(type, token) {
1518
1619 if ( res . ok ) {
1720 const data = await res . json ( ) ;
18- status . responseData = data ;
19- } else if ( res . status === 404 ) {
20- status . responseData = null ;
21+ // API returns an array, return first item or null
22+ status . responseData = Array . isArray ( data ) && data . length > 0 ? data [ 0 ] : null ;
2123 } else {
2224 status . error = true ;
25+ status . responseData = null ;
2326 }
2427 } catch ( err ) {
2528 status . responseData = err ;
@@ -43,6 +46,38 @@ export async function createPermissionRequest(type, token) {
4346 body : JSON . stringify ( { type } ) ,
4447 } ) ;
4548
49+ if ( res . ok ) {
50+ // API returns 200 with no body, so we just mark success
51+ status . responseData = true ;
52+ } else if ( res . status === 409 ) {
53+ // CONFLICT - duplicate request
54+ status . error = true ;
55+ status . responseData = 'Request already exists' ;
56+ } else {
57+ status . error = true ;
58+ }
59+ } catch ( err ) {
60+ status . responseData = err ;
61+ status . error = true ;
62+ }
63+
64+ return status ;
65+ }
66+
67+ export async function getAllPermissionRequests ( type , token ) {
68+ const status = new ApiResponse ( ) ;
69+ const url = new URL ( '/api/PermissionRequest/get' , BASE_API_URL ) ;
70+ if ( type ) {
71+ url . searchParams . append ( 'type' , type ) ;
72+ }
73+
74+ try {
75+ const res = await fetch ( url . toString ( ) , {
76+ headers : {
77+ Authorization : `Bearer ${ token } ` ,
78+ } ,
79+ } ) ;
80+
4681 if ( res . ok ) {
4782 const data = await res . json ( ) ;
4883 status . responseData = data ;
@@ -57,3 +92,57 @@ export async function createPermissionRequest(type, token) {
5792 return status ;
5893}
5994
95+ export async function approvePermissionRequest ( type , id , token ) {
96+ const status = new ApiResponse ( ) ;
97+ const url = new URL ( '/api/PermissionRequest/approve' , BASE_API_URL ) ;
98+
99+ try {
100+ const res = await fetch ( url . toString ( ) , {
101+ method : 'POST' ,
102+ headers : {
103+ 'Content-Type' : 'application/json' ,
104+ Authorization : `Bearer ${ token } ` ,
105+ } ,
106+ body : JSON . stringify ( { type, _id : id } ) ,
107+ } ) ;
108+
109+ if ( res . ok ) {
110+ status . responseData = true ;
111+ } else {
112+ status . error = true ;
113+ }
114+ } catch ( err ) {
115+ status . responseData = err ;
116+ status . error = true ;
117+ }
118+
119+ return status ;
120+ }
121+
122+ export async function deletePermissionRequest ( type , id , token ) {
123+ const status = new ApiResponse ( ) ;
124+ const url = new URL ( '/api/PermissionRequest/delete' , BASE_API_URL ) ;
125+
126+ try {
127+ const res = await fetch ( url . toString ( ) , {
128+ method : 'POST' ,
129+ headers : {
130+ 'Content-Type' : 'application/json' ,
131+ Authorization : `Bearer ${ token } ` ,
132+ } ,
133+ body : JSON . stringify ( { type, _id : id } ) ,
134+ } ) ;
135+
136+ if ( res . ok ) {
137+ status . responseData = true ;
138+ } else {
139+ status . error = true ;
140+ }
141+ } catch ( err ) {
142+ status . responseData = err ;
143+ status . error = true ;
144+ }
145+
146+ return status ;
147+ }
148+
0 commit comments