Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
dzlk17 committed Nov 4, 2024
1 parent 709b44e commit f61d142
Show file tree
Hide file tree
Showing 20 changed files with 38 additions and 51 deletions.
4 changes: 3 additions & 1 deletion database/src/tables/team/table_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ impl FromRow<'_, PgRow> for Team {
fn from_row(row: &sqlx::postgres::PgRow) -> std::result::Result<Self, sqlx::Error> {
Ok(Team {
team_id: row.get("team_id"),
grafana_id: row.get("grafana_id"),
grafana_id: row
.try_get::<Option<String>, _>("grafana_id")?
.unwrap_or_default(),
team_name: row.get("team_name"),
personal: row.get("personal"),
subscription: row.get("subscription"),
Expand Down
3 changes: 1 addition & 2 deletions database/src/tables/team/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ impl Db {
}

pub async fn clear_all_grafana_ids(&self) -> Result<(), DbError> {
let query_body =
format!("UPDATE {TEAM_TABLE_NAME} SET grafana_id = NULL WHERE deactivated_at IS NULL",);
let query_body = format!("UPDATE {TEAM_TABLE_NAME} SET grafana_id = NULL",);

let query_result = query(&query_body).execute(&self.connection_pool).await;

Expand Down
4 changes: 2 additions & 2 deletions database/src/tables/user_app_privileges/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Db {
// 17.10.2024 Hubert: "If this ever breaks, I will write comments."
let query = format!(
"WITH RelevantTeams AS (
SELECT DISTINCT t.team_id, t.team_name, t.personal, t.subscription,
SELECT DISTINCT t.team_id, t.grafana_id, t.team_name, t.personal, t.subscription,
t.registration_timestamp, gu.email AS team_admin_email,
gu.user_id AS team_admin_id, t.deactivated_at,
CASE
Expand All @@ -127,7 +127,7 @@ impl Db {
JOIN {USERS_TABLE_NAME} gu ON t.team_admin_id = gu.user_id
WHERE (t.team_admin_id = $1 OR uap.user_id = $1) AND t.deactivated_at IS NULL
)
SELECT rt.team_id, rt.team_name, rt.personal, rt.subscription, rt.registration_timestamp,
SELECT rt.team_id, rt.grafana_id, rt.team_name, rt.personal, rt.subscription, rt.registration_timestamp,
rt.team_admin_email, rt.team_admin_id, ra.app_id, ra.app_name, ra.whitelisted_domains,
ra.ack_public_keys, ra.registration_timestamp AS app_registration_timestamp,
uap.user_id, uap.privilege_level, uap.creation_timestamp AS privilege_creation_timestamp,
Expand Down
13 changes: 5 additions & 8 deletions server/src/bin/grafana_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn main() {
};
for team in teams {
// create teams
match handle_grafana_create_new_team(
let grafana_id = match handle_grafana_create_new_team(
&grafana_client_conf,
&team.team_admin_id,
&team.team_name,
Expand All @@ -48,6 +48,7 @@ async fn main() {
if let Err(err) = db.update_grafana_id(&team.team_id, &id.to_string()).await {
panic!("Failed to update grafana id in database: {:?}", err);
}
id.to_string()
}
Err(err) => {
panic!("Failed to create team in grafana: {:?}", err);
Expand Down Expand Up @@ -75,12 +76,8 @@ async fn main() {
};
for (_, user_email) in users_emails {
// add users to teams
match handle_grafana_add_user_to_team(
&grafana_client_conf,
&team.grafana_id,
&user_email,
)
.await
match handle_grafana_add_user_to_team(&grafana_client_conf, &grafana_id, &user_email)
.await
{
Ok(id) => id,
Err(err) => {
Expand All @@ -102,7 +99,7 @@ async fn main() {
&grafana_client_conf,
&app_id,
&app_name,
&team.grafana_id,
&grafana_id,
)
.await
{
Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/accept_team_invite.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
grafana_utils::add_user_to_team::handle_grafana_add_user_to_team,
utils::{custom_validate_team_id, validate_request},
utils::{custom_validate_uuid, validate_request},
};
use crate::{
env::is_env_production, middlewares::auth_middleware::UserId,
Expand All @@ -19,7 +19,7 @@ use ts_rs::TS;
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpAcceptTeamInviteRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/cancel_team_user_invite.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::{custom_validate_team_id, validate_request};
use super::utils::{custom_validate_uuid, validate_request};
use crate::{
middlewares::auth_middleware::UserId,
structs::cloud::{api_cloud_errors::CloudApiErrors, team_invite::TeamInvite},
Expand All @@ -15,7 +15,7 @@ use ts_rs::TS;
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpCancelTeamUserInviteRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
#[garde(email)]
pub user_email: String,
Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/cancel_user_team_invite.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::{custom_validate_team_id, validate_request};
use super::utils::{custom_validate_uuid, validate_request};
use crate::{
middlewares::auth_middleware::UserId, structs::cloud::api_cloud_errors::CloudApiErrors,
};
Expand All @@ -14,7 +14,7 @@ use ts_rs::TS;
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpCancelUserTeamInviteRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/change_user_privileges.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::{custom_validate_team_id, custom_validate_uuid, validate_request};
use super::utils::{custom_validate_uuid, validate_request};
use crate::{
middlewares::auth_middleware::UserId,
structs::cloud::{
Expand Down Expand Up @@ -36,7 +36,7 @@ pub struct PrivilegeChange {
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpChangeUsersPrivilegesRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
#[garde(dive)]
pub privileges_changes: Vec<PrivilegeChange>,
Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/delete_team.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::{custom_validate_team_id, validate_request};
use super::utils::{custom_validate_uuid, validate_request};
use crate::{
env::is_env_production,
http::cloud::grafana_utils::delete_team::handle_grafana_delete_team,
Expand All @@ -18,7 +18,7 @@ use ts_rs::TS;
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpDeleteTeamRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/get_team_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::custom_validate_team_id;
use super::utils::custom_validate_uuid;
use crate::{
middlewares::auth_middleware::UserId,
structs::cloud::{
Expand All @@ -24,7 +24,7 @@ use ts_rs::TS;
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpGetTeamMetadataRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/get_team_user_invites.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::{custom_validate_team_id, validate_request};
use super::utils::{custom_validate_uuid, validate_request};
use crate::{
middlewares::auth_middleware::UserId,
structs::cloud::{api_cloud_errors::CloudApiErrors, team_invite::TeamInvite},
Expand All @@ -19,7 +19,7 @@ use ts_rs::TS;
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpGetTeamUserInvitesRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/get_team_users_privileges.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::custom_validate_team_id;
use super::utils::custom_validate_uuid;
use crate::{
middlewares::auth_middleware::UserId,
structs::cloud::{api_cloud_errors::CloudApiErrors, team_user_privilege::TeamUserPrivilege},
Expand All @@ -19,7 +19,7 @@ use ts_rs::TS;
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpGetTeamUsersPrivilegesRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
}

Expand Down
1 change: 1 addition & 0 deletions server/src/http/cloud/get_user_joined_teams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub async fn get_user_joined_teams(
// Parse joined team
let joined_team = JoinedTeam {
team_id: team.team_id.clone(),
grafana_id: team.grafana_id,
team_name: team.team_name,
created_at: team.registration_timestamp,
creator_email: admin_email,
Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/invite_user_to_team.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::{custom_validate_team_id, validate_request};
use super::utils::{custom_validate_uuid, validate_request};
use crate::{
mailer::{
mail_requests::{SendEmailRequest, TeamInviteNotification},
Expand All @@ -21,7 +21,7 @@ use ts_rs::TS;
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpInviteUserToTeamRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
#[garde(email)]
pub user_email: String,
Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/leave_team.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
grafana_utils::remove_user_from_the_team::handle_grafana_remove_user_from_team,
utils::{custom_validate_team_id, validate_request},
utils::{custom_validate_uuid, validate_request},
};
use crate::{
env::is_env_production,
Expand All @@ -24,7 +24,7 @@ use ts_rs::TS;
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpLeaveTeamRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
#[garde(alphanumeric)]
pub device: String,
Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/register_new_app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
grafana_utils::create_new_app::handle_grafana_create_new_app,
utils::{custom_validate_name, custom_validate_team_id, validate_request},
utils::{custom_validate_name, custom_validate_uuid, validate_request},
};
use crate::{
env::is_env_production, middlewares::auth_middleware::UserId,
Expand All @@ -23,7 +23,7 @@ use uuid7::uuid7;
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpRegisterNewAppRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
#[garde(custom(custom_validate_name))]
pub app_name: String,
Expand Down
2 changes: 1 addition & 1 deletion server/src/http/cloud/register_new_team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub async fn register_new_team(
}

return Ok(Json(HttpRegisterNewTeamResponse {
team_id: grafana_team_id.to_string(),
team_id: team_id.to_string(),
}));
}
Err(err) => {
Expand Down
4 changes: 2 additions & 2 deletions server/src/http/cloud/remove_user_from_team.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
grafana_utils::remove_user_from_the_team::handle_grafana_remove_user_from_team,
utils::{custom_validate_team_id, validate_request},
utils::{custom_validate_uuid, validate_request},
};
use crate::{
env::is_env_production,
Expand All @@ -24,7 +24,7 @@ use ts_rs::TS;
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct HttpRemoveUserFromTeamRequest {
#[garde(custom(custom_validate_team_id))]
#[garde(custom(custom_validate_uuid))]
pub team_id: String,
#[garde(email)]
pub user_email: String,
Expand Down
13 changes: 0 additions & 13 deletions server/src/http/cloud/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,6 @@ pub fn custom_validate_uuid(string_uuid: &String, _context: &()) -> garde::Resul
Ok(())
}

pub fn custom_validate_team_id(string_id: &String, _context: &()) -> garde::Result {
// For now we are using i64 returned from grafana as team_id, hopefully this will be changed to UUID
match i64::from_str(string_id) {
Ok(id) => {
if id < 0 {
return Err(garde::Error::new("Invalid ID format".to_string()));
}
}
Err(_) => return Err(garde::Error::new("Invalid ID format".to_string())),
}
Ok(())
}

pub fn custom_validate_name(name: &String, _context: &()) -> garde::Result {
NAME_REGEX
.is_match(name)
Expand Down
1 change: 1 addition & 0 deletions server/src/structs/cloud/joined_team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub type TeamId = String;
#[serde(rename_all = "camelCase")]
pub struct JoinedTeam {
pub team_id: TeamId,
pub grafana_id: String,
pub team_name: String,
pub creator_email: String,
pub created_at: DateTime<Utc>,
Expand Down

0 comments on commit f61d142

Please sign in to comment.