11use aide:: NoApi ;
22use axum:: Json ;
33use axum:: extract:: Query ;
4- use axum:: { extract :: State , http:: StatusCode , response:: IntoResponse , response:: Response } ;
4+ use axum:: { http:: StatusCode , response:: IntoResponse , response:: Response } ;
55use schemars:: JsonSchema ;
66use serde:: { Deserialize , Serialize } ;
77
88use crate :: db_query;
99use crate :: models:: import_job:: ImportJob ;
10- use crate :: models:: user:: User ;
11- use crate :: state:: AppState ;
12- use crate :: utils:: extractors:: AuthenticatedUser ;
10+ use crate :: models:: import_job:: ImportJobWithUser ;
11+ use crate :: utils:: extractors:: { AuthenticatedUser , DbConnection } ;
1312
1413#[ derive( Deserialize , JsonSchema ) ]
1514pub struct ImportsQuery {
@@ -23,23 +22,6 @@ fn default_limit() -> i64 {
2322 50
2423}
2524
26- #[ derive( Serialize , JsonSchema ) ]
27- pub struct ImportJobWithUser {
28- pub id : i64 ,
29- pub user_id : i32 ,
30- pub user_name : Option < String > ,
31- pub user_avatar_url : Option < String > ,
32- pub status : String ,
33- pub imported_count : Option < i64 > ,
34- pub processed_count : Option < i64 > ,
35- pub request_count : Option < i32 > ,
36- pub start_date : Option < String > ,
37- pub time_taken : Option < f64 > ,
38- pub error_message : Option < String > ,
39- pub created_at : String ,
40- pub updated_at : String ,
41- }
42-
4325#[ derive( Serialize , JsonSchema ) ]
4426pub struct AdminImportsResponse {
4527 pub imports : Vec < ImportJobWithUser > ,
@@ -49,55 +31,27 @@ pub struct AdminImportsResponse {
4931}
5032
5133pub async fn admin_imports (
52- State ( app_state) : State < AppState > ,
5334 Query ( query) : Query < ImportsQuery > ,
5435 NoApi ( AuthenticatedUser ( current_user) ) : NoApi < AuthenticatedUser > ,
36+ NoApi ( DbConnection ( mut conn) ) : NoApi < DbConnection > ,
5537) -> Result < Json < AdminImportsResponse > , Response > {
56- if !current_user. is_admin ( ) {
38+ if !current_user. is_owner ( ) {
5739 return Err ( ( StatusCode :: FORBIDDEN , "No permission" ) . into_response ( ) ) ;
5840 }
5941
6042 let limit = query. limit . clamp ( 1 , 100 ) ;
6143 let offset = query. offset . max ( 0 ) ;
6244
6345 let total = db_query ! (
64- ImportJob :: count_all( & app_state . db_pool ) ,
46+ ImportJob :: count_all( & mut conn ) ,
6547 "Failed to count import jobs"
6648 ) ;
6749
68- let jobs = db_query ! (
69- ImportJob :: get_all ( & app_state . db_pool , limit, offset) ,
50+ let imports = db_query ! (
51+ ImportJob :: get_all_with_users ( & mut conn , limit, offset) ,
7052 "Failed to fetch import jobs"
7153 ) ;
7254
73- let user_ids: Vec < i32 > = jobs. iter ( ) . map ( |j| j. user_id ) . collect ( ) ;
74- let users = db_query ! (
75- User :: get_by_ids( & app_state. db_pool, & user_ids) ,
76- "Failed to fetch users"
77- ) ;
78-
79- let imports: Vec < ImportJobWithUser > = jobs
80- . into_iter ( )
81- . map ( |job| {
82- let user = users. iter ( ) . find ( |u| u. id == job. user_id ) ;
83- ImportJobWithUser {
84- id : job. id ,
85- user_id : job. user_id ,
86- user_name : user. map ( |u| u. name . clone ( ) ) ,
87- user_avatar_url : user. map ( |u| u. avatar_url . clone ( ) ) ,
88- status : job. status ,
89- imported_count : job. imported_count ,
90- processed_count : job. processed_count ,
91- request_count : job. request_count ,
92- start_date : job. start_date ,
93- time_taken : job. time_taken ,
94- error_message : job. error_message ,
95- created_at : job. created_at . to_rfc3339 ( ) ,
96- updated_at : job. updated_at . to_rfc3339 ( ) ,
97- }
98- } )
99- . collect ( ) ;
100-
10155 Ok ( Json ( AdminImportsResponse {
10256 imports,
10357 total,
0 commit comments