mirror of https://github.com/buster-so/buster.git
invite users working again
This commit is contained in:
parent
ba159f056c
commit
67f90048da
|
@ -2,11 +2,11 @@ use anyhow::{Context, Result};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use database::{
|
use database::{
|
||||||
self,
|
self,
|
||||||
enums::{UserOrganizationRole, UserOrganizationStatus, SharingSetting},
|
enums::{SharingSetting, UserOrganizationRole, UserOrganizationStatus},
|
||||||
models::{User, UserToOrganization},
|
models::{User, UserToOrganization},
|
||||||
|
pool::get_pg_pool,
|
||||||
schema::{users, users_to_organizations},
|
schema::{users, users_to_organizations},
|
||||||
};
|
};
|
||||||
use diesel::prelude::*;
|
|
||||||
use diesel_async::{AsyncPgConnection, RunQueryDsl};
|
use diesel_async::{AsyncPgConnection, RunQueryDsl};
|
||||||
use middleware::AuthenticatedUser;
|
use middleware::AuthenticatedUser;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
@ -17,7 +17,6 @@ use uuid::Uuid;
|
||||||
pub async fn invite_user_handler(
|
pub async fn invite_user_handler(
|
||||||
inviting_user: &AuthenticatedUser,
|
inviting_user: &AuthenticatedUser,
|
||||||
emails: Vec<String>,
|
emails: Vec<String>,
|
||||||
conn: &mut AsyncPgConnection, // Accept the connection directly
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let organization_id = inviting_user
|
let organization_id = inviting_user
|
||||||
.organizations
|
.organizations
|
||||||
|
@ -42,7 +41,7 @@ pub async fn invite_user_handler(
|
||||||
|
|
||||||
// 2. Create User struct instance using the generated ID and attributes
|
// 2. Create User struct instance using the generated ID and attributes
|
||||||
let user_to_insert = User {
|
let user_to_insert = User {
|
||||||
id: new_user_id, // Use the generated ID
|
id: new_user_id, // Use the generated ID
|
||||||
email: email.clone(), // Use the original email variable again or the cloned one
|
email: email.clone(), // Use the original email variable again or the cloned one
|
||||||
name: None,
|
name: None,
|
||||||
config: json!({}),
|
config: json!({}),
|
||||||
|
@ -52,23 +51,35 @@ pub async fn invite_user_handler(
|
||||||
avatar_url: None,
|
avatar_url: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut conn = match get_pg_pool().get().await {
|
||||||
|
Ok(mut conn) => conn,
|
||||||
|
Err(e) => {
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 3. Insert user
|
// 3. Insert user
|
||||||
diesel::insert_into(users::table)
|
match diesel::insert_into(users::table)
|
||||||
.values(&user_to_insert)
|
.values(&user_to_insert)
|
||||||
.execute(conn)
|
.execute(&mut conn)
|
||||||
.await
|
.await
|
||||||
.context("Failed to insert new user")?;
|
{
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(e) => {
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 4. Create UserToOrganization struct instance
|
// 4. Create UserToOrganization struct instance
|
||||||
let user_org_to_insert = UserToOrganization {
|
let user_org_to_insert = UserToOrganization {
|
||||||
user_id: new_user_id, // Use the generated ID
|
user_id: new_user_id, // Use the generated ID
|
||||||
organization_id,
|
organization_id,
|
||||||
role: assigned_role, // Use the role variable
|
role: assigned_role, // Use the role variable
|
||||||
sharing_setting: SharingSetting::None, // Default setting
|
sharing_setting: SharingSetting::None, // Default setting
|
||||||
edit_sql: false, // Default permission
|
edit_sql: false, // Default permission
|
||||||
upload_csv: false, // Default permission
|
upload_csv: false, // Default permission
|
||||||
export_assets: false, // Default permission
|
export_assets: false, // Default permission
|
||||||
email_slack_enabled: false, // Default setting
|
email_slack_enabled: false, // Default setting
|
||||||
created_at: now,
|
created_at: now,
|
||||||
updated_at: now,
|
updated_at: now,
|
||||||
deleted_at: None,
|
deleted_at: None,
|
||||||
|
@ -79,11 +90,16 @@ pub async fn invite_user_handler(
|
||||||
};
|
};
|
||||||
|
|
||||||
// 5. Insert user organization mapping
|
// 5. Insert user organization mapping
|
||||||
diesel::insert_into(users_to_organizations::table)
|
match diesel::insert_into(users_to_organizations::table)
|
||||||
.values(&user_org_to_insert)
|
.values(&user_org_to_insert)
|
||||||
.execute(conn)
|
.execute(&mut conn)
|
||||||
.await
|
.await
|
||||||
.context("Failed to map user to organization")?;
|
{
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(e) => {
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -1 +1,3 @@
|
||||||
pub mod invite_user_handler;
|
pub mod invite_user_handler;
|
||||||
|
|
||||||
|
pub use invite_user_handler::*;
|
|
@ -1,11 +1,12 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use axum::{Extension, Json};
|
use axum::{Extension, Json};
|
||||||
|
use handlers::users::invite_user_handler;
|
||||||
|
|
||||||
use crate::routes::rest::ApiResponse;
|
use crate::routes::rest::ApiResponse;
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use database::enums::UserOrganizationRole;
|
|
||||||
use middleware::AuthenticatedUser;
|
use middleware::AuthenticatedUser;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use tracing::error;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct InviteUsersRequest {
|
pub struct InviteUsersRequest {
|
||||||
|
@ -16,5 +17,16 @@ pub async fn invite_users(
|
||||||
Extension(user): Extension<AuthenticatedUser>,
|
Extension(user): Extension<AuthenticatedUser>,
|
||||||
Json(body): Json<InviteUsersRequest>,
|
Json(body): Json<InviteUsersRequest>,
|
||||||
) -> Result<ApiResponse<()>, (StatusCode, &'static str)> {
|
) -> Result<ApiResponse<()>, (StatusCode, &'static str)> {
|
||||||
Ok(ApiResponse::NoContent)
|
let result = invite_user_handler(&user, body.emails).await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(_) => Ok(ApiResponse::NoContent),
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to invite users: {}", e);
|
||||||
|
Err((
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"Failed to process invitation request",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue