@@ -10,8 +10,9 @@ use crate::{
1010 } ,
1111 dashboard,
1212 dtos:: {
13- AdminDashboardResponse , HospitalActionRequest , SpecialistActionRequest , UserFilters ,
14- UserListResponse , AdminPatientProfileResponse , AdminUserProfileResponse ,
13+ AdminDashboardResponse , ConfigActionRequest , ConfigResponse , HospitalActionRequest ,
14+ SpecialistActionRequest , UserFilters , UserListResponse , AdminPatientProfileResponse ,
15+ AdminUserProfileResponse ,
1516 } ,
1617 } ,
1718 error:: AppError ,
@@ -278,3 +279,55 @@ pub async fn get_user_profile(
278279
279280 Ok ( ApiResponse :: success ( response) )
280281}
282+
283+ /// Update application configuration (Admin only)
284+ ///
285+ /// Allows admins to update system-wide settings like reward points and conversion rates
286+ #[ utoipa:: path(
287+ put,
288+ path = "/api/admin/configs/{key}" ,
289+ request_body = ConfigActionRequest ,
290+ responses(
291+ ( status = 200 , body = ApiResponse <ConfigResponse >) ,
292+ ( status = 401 , description = "Unauthorized" ) ,
293+ ( status = 403 , description = "Forbidden - Admin only" ) ,
294+ ( status = 404 , description = "Config key not found" ) ,
295+ ( status = 500 )
296+ ) ,
297+ tag = "admin" ,
298+ security( ( "bearer_auth" = [ ] ) )
299+ ) ]
300+ pub async fn update_app_config (
301+ State ( state) : State < AppState > ,
302+ Extension ( current_admin) : Extension < User > ,
303+ Path ( key) : Path < String > ,
304+ Json ( payload) : Json < crate :: admin:: dtos:: ConfigActionRequest > ,
305+ ) -> Result < ApiResponse < crate :: admin:: dtos:: ConfigResponse > , AppError > {
306+ use crate :: schema:: app_configs;
307+ use diesel:: prelude:: * ;
308+
309+ // Ensure user is admin
310+ if !matches ! ( current_admin. role, crate :: utils:: enums:: Role :: Admin ) {
311+ return Err ( AppError :: Unauthorized (
312+ "Only administrators can perform this action" . into ( ) ,
313+ ) ) ;
314+ }
315+
316+ let mut conn = state. pool . get ( ) ?;
317+
318+ let updated_config = diesel:: update ( app_configs:: table. filter ( app_configs:: key. eq ( & key) ) )
319+ . set ( (
320+ app_configs:: value. eq ( payload. value ) ,
321+ app_configs:: description. eq ( payload. description ) ,
322+ app_configs:: updated_at. eq ( chrono:: Utc :: now ( ) ) ,
323+ ) )
324+ . get_result :: < crate :: models:: AppConfig > ( & mut conn)
325+ . map_err ( |_| AppError :: NotFound ( format ! ( "Config key '{}' not found" , key) ) ) ?;
326+
327+ Ok ( ApiResponse :: success ( crate :: admin:: dtos:: ConfigResponse {
328+ key : updated_config. key ,
329+ value : updated_config. value ,
330+ description : updated_config. description ,
331+ updated_at : updated_config. updated_at ,
332+ } ) )
333+ }
0 commit comments