2025-03-26 13:06:34 +07:00
use std ::error ::Error ;
2025-12-01 00:20:42 +07:00
use imphnen_libs ::postgres ::{ PostgresConfig , PostgresConnection };
use imphnen_entities ::seaorm ::auth ::roles ::{ RoleBuilder , Entity as RoleEntity };
use sea_orm ::{ ActiveModelTrait , ActiveValue ::Set , EntityTrait };
use uuid ::Uuid ;
use chrono ::Utc ; // Added chrono
2025-03-26 13:06:34 +07:00
#[tokio::main]
async fn main () -> Result < (), Box < dyn Error >> {
2025-12-01 00:20:42 +07:00
let config = PostgresConfig ::from_env () ? ;
let pg_conn = PostgresConnection ::new ( config ). await ? ;
let db = & pg_conn . conn ;
2025-03-26 13:06:34 +07:00
let roles = vec! [
(
"50133429-f4b1-4249-9f97-7b86e6ee9d86" ,
"Staf" ,
Some ( "2025-02-24T16:52:27.630453+00" ),
Some ( "2025-02-24T16:52:27.630461+00" ),
),
(
"5713cb37-dc02-4e87-8048-d7a41d352059" ,
2025-04-01 20:00:00 +07:00
"User" ,
2025-10-11 23:23:51 +07:00
Some ( "2025-02-28T14:53:58.576688+00" ),
2025-03-26 13:06:34 +07:00
Some ( "2025-02-28T14:53:58.576688+00" ),
),
(
"60f1aeb7-dad2-4e06-bcb5-be1ba510c906" ,
"Staff Aktivasi User" ,
Some ( "2025-02-20T02:47:09.660640+00" ),
Some ( "2025-02-20T02:48:30.083283+00" ),
),
(
"6d4fea5d-4a08-4b8a-9782-f2ab2183dcf0" ,
"Admin Pembayaran" ,
Some ( "2025-01-29T05:39:28.562667+00" ),
Some ( "2025-03-12T22:56:29.597416+00" ),
),
(
"f6b03f25-e416-4893-ac88-caaa690afb07" ,
"Admin" ,
2025-10-11 23:23:51 +07:00
Some ( "2025-02-22T15:38:39.868306+00" ),
2025-03-26 13:06:34 +07:00
Some ( "2025-02-22T15:38:39.868306+00" ),
),
2025-07-21 21:29:04 +07:00
(
"3b9f8c4e-6a2d-4f8a-9a12-2d6f8b3c4e5a" ,
"Mentor" ,
2025-10-11 23:23:51 +07:00
Some ( "2025-07-06T10:00:00.000000+00" ),
2025-07-21 21:29:04 +07:00
Some ( "2025-07-06T10:00:00.000000+00" ),
),
2025-03-26 13:06:34 +07:00
];
2025-12-01 00:20:42 +07:00
for ( id , name , _created_at_str , _updated_at_str ) in roles { // Renamed to avoid conflict
let uuid = Uuid ::parse_str ( id ). unwrap_or_else ( | _ | Uuid ::new_v4 ());
// Check if role already exists
let existing = RoleEntity ::find_by_id ( uuid ). one ( db ). await ? ;
if existing . is_some () {
println! ( "ℹ ️ Skipping (already exists): {name} " );
continue ;
}
// Delete existing by id to avoid duplicates (original logic, replaced by existence check)
// let _ = pg_conn.execute(sea_orm::Statement::from_string(db.get_database_backend(), format!("DELETE FROM app_roles WHERE id = '{}'", uuid))).await.ok();
let role_model = RoleBuilder ::new ()
. name ( name . to_string ())
. description ( "System generated role" . to_string ())
. permissions ( vec! [])
. is_default ( false )
. build () ? ;
let mut role_model = role_model ;
role_model . id = Set ( uuid );
role_model . is_system_role = Set ( true ); // Set the missing field
role_model . created_at = Set ( Utc ::now ()); // Set created_at
role_model . updated_at = Set ( Utc ::now ()); // Set updated_at
role_model . insert ( db ). await ? ;
2025-07-21 21:29:04 +07:00
println! ( "✅ Inserted role: {name} " );
2025-03-26 13:06:34 +07:00
}
2025-05-17 02:08:59 +07:00
println! ( "✅ All Roles seeded" );
2025-03-26 13:06:34 +07:00
Ok (())
2025-12-01 00:20:42 +07:00
}