Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
dzlk17 committed Oct 13, 2024
1 parent d962cdc commit 6e4cbf5
Show file tree
Hide file tree
Showing 31 changed files with 234 additions and 132 deletions.
1 change: 0 additions & 1 deletion database/migrations/0002_team.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ CREATE TABLE team(
subscription subscription,
team_admin_id TEXT NOT NULL,
registration_timestamp TIMESTAMPTZ NOT NULL,
active BOOLEAN NOT NULL,
deactivated_at TIMESTAMPTZ,
PRIMARY KEY (team_name, team_admin_id)
);
1 change: 0 additions & 1 deletion database/migrations/0004_registered_apps.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ CREATE TABLE registered_apps(
whitelisted_domains TEXT [] NOT NULL,
ack_public_keys TEXT [] NOT NULL,
registration_timestamp TIMESTAMPTZ NOT NULL,
active BOOLEAN NOT NULL,
deactivated_at TIMESTAMPTZ
);

Expand Down
4 changes: 2 additions & 2 deletions database/src/tables/registered_app/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ impl Db {
&self,
app_id: &String,
) -> Result<Option<DbRegisteredApp>, DbError> {
let query = format!("SELECT * FROM {REGISTERED_APPS_TABLE_NAME} WHERE app_id = $1 AND active = true");
let query = format!("SELECT * FROM {REGISTERED_APPS_TABLE_NAME} WHERE app_id = $1 AND deactivated_at IS NULL");
let typed_query = query_as::<_, DbRegisteredApp>(&query);

return typed_query
Expand All @@ -23,7 +23,7 @@ impl Db {
team_id: &String,
) -> Result<Option<DbRegisteredApp>, DbError> {
let query = format!(
"SELECT * FROM {REGISTERED_APPS_TABLE_NAME} WHERE app_name = $1 AND team_id = $2 AND active = true"
"SELECT * FROM {REGISTERED_APPS_TABLE_NAME} WHERE app_name = $1 AND team_id = $2 AND deactivated_at IS NULL"
);
let typed_query = query_as::<_, DbRegisteredApp>(&query);

Expand Down
4 changes: 1 addition & 3 deletions database/src/tables/registered_app/table_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sqlx::{

pub const REGISTERED_APPS_TABLE_NAME: &str = "registered_apps";
pub const REGISTERED_APPS_KEYS: &str =
"team_id, app_id, app_name, whitelisted_domains, ack_public_keys, registration_timestamp, active, deactivated_at";
"team_id, app_id, app_name, whitelisted_domains, ack_public_keys, registration_timestamp, deactivated_at";

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DbRegisteredApp {
Expand All @@ -16,7 +16,6 @@ pub struct DbRegisteredApp {
pub whitelisted_domains: Vec<String>,
pub ack_public_keys: Vec<String>,
pub registration_timestamp: DateTime<Utc>,
pub active: bool,
pub deactivated_at: Option<DateTime<Utc>>,
}

Expand All @@ -29,7 +28,6 @@ impl FromRow<'_, PgRow> for DbRegisteredApp {
whitelisted_domains: row.get("whitelisted_domains"),
ack_public_keys: row.get("ack_public_keys"),
registration_timestamp: row.get("registration_timestamp"),
active: row.get("active"),
deactivated_at: row.get("deactivated_at"),
})
}
Expand Down
12 changes: 5 additions & 7 deletions database/src/tables/registered_app/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sqlx::{query, Transaction};
impl Db {
pub async fn register_new_app(&self, app: &DbRegisteredApp) -> Result<(), DbError> {
let query_body = format!(
"INSERT INTO {REGISTERED_APPS_TABLE_NAME} ({REGISTERED_APPS_KEYS}) VALUES ($1, $2, $3, $4, $5, $6, $7, NULL)",
"INSERT INTO {REGISTERED_APPS_TABLE_NAME} ({REGISTERED_APPS_KEYS}) VALUES ($1, $2, $3, $4, $5, $6, NULL)",
);

let query_result = query(&query_body)
Expand All @@ -15,7 +15,6 @@ impl Db {
.bind(&app.whitelisted_domains)
.bind(&app.ack_public_keys)
.bind(&app.registration_timestamp)
.bind(&app.active)
.execute(&self.connection_pool)
.await;

Expand All @@ -31,7 +30,7 @@ impl Db {
app: &DbRegisteredApp,
) -> Result<(), DbError> {
let query_body = format!(
"INSERT INTO {REGISTERED_APPS_TABLE_NAME} ({}) VALUES ($1, $2, $3, $4, $5, $6, $7, NULL)",
"INSERT INTO {REGISTERED_APPS_TABLE_NAME} ({}) VALUES ($1, $2, $3, $4, $5, $6, NULL)",
REGISTERED_APPS_KEYS
);

Expand All @@ -42,7 +41,6 @@ impl Db {
.bind(&app.whitelisted_domains)
.bind(&app.ack_public_keys)
.bind(&app.registration_timestamp)
.bind(&app.active)
.execute(&mut **tx)
.await;

Expand All @@ -59,7 +57,7 @@ impl Db {
domain: &str,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {REGISTERED_APPS_TABLE_NAME} SET whitelisted_domains = array_append(whitelisted_domains, $1) WHERE app_id = $2 AND active = true",
"UPDATE {REGISTERED_APPS_TABLE_NAME} SET whitelisted_domains = array_append(whitelisted_domains, $1) WHERE app_id = $2 AND deactivated_at IS NULL",
);

let query_result = query(&query_body)
Expand All @@ -81,7 +79,7 @@ impl Db {
domain: &str,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {REGISTERED_APPS_TABLE_NAME} SET whitelisted_domains = array_remove(whitelisted_domains, $1) WHERE app_id = $2 AND active = true",
"UPDATE {REGISTERED_APPS_TABLE_NAME} SET whitelisted_domains = array_remove(whitelisted_domains, $1) WHERE app_id = $2 AND deactivated_at IS NULL",
);

let query_result = query(&query_body)
Expand All @@ -102,7 +100,7 @@ impl Db {
app_id: &str,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {REGISTERED_APPS_TABLE_NAME} SET active = false, deactivated_at = $1 WHERE app_id = $2 AND active = true",
"UPDATE {REGISTERED_APPS_TABLE_NAME} SET deactivated_at = $1 WHERE app_id = $2 AND deactivated_at IS NULL",
);

let query_result = query(&query_body)
Expand Down
11 changes: 8 additions & 3 deletions database/src/tables/sessions/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ impl Db {
return Err(err);
}

tx.commit().await.unwrap();

if let Err(err) = tx.commit().await {
error!("Failed to commit transaction: {:?}", err);
return Err(err).map_err(|err| err.into());
}
Ok(())
}

Expand Down Expand Up @@ -230,7 +232,10 @@ impl Db {
return Err(err);
}

tx.commit().await.unwrap();
if let Err(err) = tx.commit().await {
error!("Failed to commit transaction: {:?}", err);
return Err(err).map_err(|err| err.into());
}

Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions database/src/tables/team/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Db {
tx: Option<&mut Transaction<'_, sqlx::Postgres>>,
team_id: &String,
) -> Result<Option<Team>, DbError> {
let query = format!("SELECT * FROM {TEAM_TABLE_NAME} WHERE team_id = $1 AND active = true");
let query = format!("SELECT * FROM {TEAM_TABLE_NAME} WHERE team_id = $1 AND deactivated_at IS NULL");
let typed_query = query_as::<_, Team>(&query);

match tx {
Expand Down Expand Up @@ -39,7 +39,7 @@ impl Db {
let query = format!(
"SELECT r.* FROM {REGISTERED_APPS_TABLE_NAME} r
INNER JOIN team t ON r.team_id = t.team_id
WHERE t.team_id = $1 AND r.active = true AND t.active = true
WHERE t.team_id = $1 AND r.deactivated_at IS NULL AND t.deactivated_at IS NULL
ORDER BY t.registration_timestamp DESC"
);
let typed_query = query_as::<_, DbRegisteredApp>(&query);
Expand All @@ -56,7 +56,7 @@ impl Db {
admin_id: &String,
) -> Result<Vec<Team>, DbError> {
let query = format!(
"SELECT * FROM {TEAM_TABLE_NAME} WHERE team_admin_id = $1 AND personal = false AND active = true"
"SELECT * FROM {TEAM_TABLE_NAME} WHERE team_admin_id = $1 AND personal = false AND deactivated_at IS NULL"
);
let typed_query = query_as::<_, Team>(&query);

Expand All @@ -72,7 +72,7 @@ impl Db {
admin_id: &String,
) -> Result<Option<Team>, DbError> {
let query =
format!("SELECT * FROM {TEAM_TABLE_NAME} WHERE team_admin_id = $1 AND personal = true AND active = true");
format!("SELECT * FROM {TEAM_TABLE_NAME} WHERE team_admin_id = $1 AND personal = true AND deactivated_at IS NULL");
let typed_query = query_as::<_, Team>(&query);

return typed_query
Expand All @@ -88,7 +88,7 @@ impl Db {
admin_id: &String,
) -> Result<Option<Team>, DbError> {
let query =
format!("SELECT * FROM {TEAM_TABLE_NAME} WHERE team_name = $1 AND active = true AND team_admin_id = $2");
format!("SELECT * FROM {TEAM_TABLE_NAME} WHERE team_name = $1 AND deactivated_at IS NULL AND team_admin_id = $2");
let typed_query = query_as::<_, Team>(&query);

return typed_query
Expand All @@ -100,7 +100,7 @@ impl Db {
}

pub async fn get_team_by_admin_id(&self, admin_id: &String) -> Result<Option<Team>, DbError> {
let query = format!("SELECT * FROM {TEAM_TABLE_NAME} WHERE team_admin_id = $1 AND active = true");
let query = format!("SELECT * FROM {TEAM_TABLE_NAME} WHERE team_admin_id = $1 AND deactivated_at IS NULL");
let typed_query = query_as::<_, Team>(&query);

return typed_query
Expand Down
4 changes: 1 addition & 3 deletions database/src/tables/team/table_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sqlx::{

pub const TEAM_TABLE_NAME: &str = "team";
pub const TEAM_KEYS: &str =
"team_id, team_name, personal, subscription, team_admin_id, registration_timestamp, active, deactivated_at";
"team_id, team_name, personal, subscription, team_admin_id, registration_timestamp, deactivated_at";

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Team {
Expand All @@ -18,7 +18,6 @@ pub struct Team {
pub subscription: Option<Subscription>,
pub team_admin_id: String,
pub registration_timestamp: DateTime<Utc>,
pub active: bool,
pub deactivated_at: Option<DateTime<Utc>>,
}

Expand All @@ -31,7 +30,6 @@ impl FromRow<'_, PgRow> for Team {
subscription: row.get("subscription"),
registration_timestamp: row.get("registration_timestamp"),
team_admin_id: row.get("team_admin_id"),
active: row.get("active"),
deactivated_at: row.get("deactivated_at"),
})
}
Expand Down
10 changes: 4 additions & 6 deletions database/src/tables/team/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Db {
team: &Team,
) -> Result<(), DbError> {
let query_body = format!(
"INSERT INTO {TEAM_TABLE_NAME} ({TEAM_KEYS}) VALUES ($1, $2, $3, $4, $5, $6, $7, NULL)"
"INSERT INTO {TEAM_TABLE_NAME} ({TEAM_KEYS}) VALUES ($1, $2, $3, $4, $5, $6, NULL)"
);

let query_result = query(&query_body)
Expand All @@ -26,7 +26,6 @@ impl Db {
.bind(&team.subscription)
.bind(&team.team_admin_id)
.bind(&team.registration_timestamp)
.bind(&team.active)
.execute(&mut **tx)
.await;

Expand All @@ -38,7 +37,7 @@ impl Db {

pub async fn create_new_team(&self, team: &Team) -> Result<(), DbError> {
let query_body = format!(
"INSERT INTO {TEAM_TABLE_NAME} ({TEAM_KEYS}) VALUES ($1, $2, $3, $4, $5, $6, $7, NULL)"
"INSERT INTO {TEAM_TABLE_NAME} ({TEAM_KEYS}) VALUES ($1, $2, $3, $4, $5, $6, NULL)"
);

let query_result = query(&query_body)
Expand All @@ -48,7 +47,6 @@ impl Db {
.bind(&team.subscription)
.bind(&team.team_admin_id)
.bind(&team.registration_timestamp)
.bind(&team.active)
.execute(&self.connection_pool)
.await;

Expand All @@ -64,7 +62,7 @@ impl Db {
subscription: &Subscription,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {TEAM_TABLE_NAME} SET subscription = $1 WHERE team_id = $2 AND active = true"
"UPDATE {TEAM_TABLE_NAME} SET subscription = $1 WHERE team_id = $2 AND deactivated_at IS NULL"
);
let query_result = query(&query_body)
.bind(subscription)
Expand All @@ -84,7 +82,7 @@ impl Db {
team_id: &str,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {TEAM_TABLE_NAME} SET active = false, deactivated_at = $1 WHERE team_id = $2 AND active = true",
"UPDATE {TEAM_TABLE_NAME} SET deactivated_at = $1 WHERE team_id = $2 AND deactivated_at IS NULL",
);

let query_result = query(&query_body)
Expand Down
10 changes: 4 additions & 6 deletions database/src/tables/user_app_privileges/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Db {
"WITH RelevantTeams AS (
SELECT DISTINCT t.team_id, t.team_name, t.personal, t.subscription,
t.registration_timestamp, gu.email AS team_admin_email,
gu.user_id AS team_admin_id,
gu.user_id AS team_admin_id, t.deactivated_at,
CASE
WHEN t.team_admin_id = $1 THEN t.registration_timestamp
ELSE MAX(uap.creation_timestamp) OVER (PARTITION BY t.team_id)
Expand All @@ -124,17 +124,17 @@ impl Db {
LEFT JOIN {REGISTERED_APPS_TABLE_NAME} ra ON t.team_id = ra.team_id
LEFT JOIN {USER_APP_PRIVILEGES_TABLE_NAME} uap ON ra.app_id = uap.app_id AND uap.user_id = $1
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 ra.active = true
WHERE (t.team_admin_id = $1 OR uap.user_id = $1) AND ra.deactivated_at IS NULL
)
SELECT rt.team_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,
rt.user_joined_team_timestamp
rt.user_joined_team_timestamp, ra.deactivated_at
FROM RelevantTeams rt
LEFT JOIN {REGISTERED_APPS_TABLE_NAME} ra ON rt.team_id = ra.team_id
LEFT JOIN {USER_APP_PRIVILEGES_TABLE_NAME} uap ON ra.app_id = uap.app_id AND uap.user_id = $1
WHERE ra.active = true
WHERE ra.deactivated_at IS NULL
ORDER BY rt.team_id, ra.app_id"
);

Expand Down Expand Up @@ -168,7 +168,6 @@ impl Db {
subscription: row.get("subscription"),
registration_timestamp: row.get("registration_timestamp"),
team_admin_id: row.get("team_admin_id"),
active: row.get("active"),
deactivated_at: row.get("deactivated_at"),
};

Expand All @@ -190,7 +189,6 @@ impl Db {
whitelisted_domains: row.get("whitelisted_domains"),
ack_public_keys: row.get("ack_public_keys"),
registration_timestamp: row.get("app_registration_timestamp"),
active: row.get("active"),
deactivated_at: row.get("deactivated_at"),
};

Expand Down
10 changes: 7 additions & 3 deletions database/src/tables/user_app_privileges/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::tables::user_app_privileges::table_struct::{
USER_APP_PRIVILEGES_KEYS, USER_APP_PRIVILEGES_TABLE_NAME,
};
use crate::tables::utils::get_current_datetime;
use log::error;
use sqlx::query;
use sqlx::Transaction;

Expand Down Expand Up @@ -63,7 +64,7 @@ impl Db {
) -> Result<(), DbError> {
// Retrieve all apps associated with the team
let apps_query = format!(
"SELECT app_id FROM {REGISTERED_APPS_TABLE_NAME} WHERE team_id = $1 AND active = true"
"SELECT app_id FROM {REGISTERED_APPS_TABLE_NAME} WHERE team_id = $1 AND deactivated_at IS NULL"
);
let apps: Vec<String> = sqlx::query_as(&apps_query)
.bind(team_id)
Expand Down Expand Up @@ -112,7 +113,7 @@ impl Db {
) -> Result<(), DbError> {
// Retrieve all apps associated with the team
let apps_query = format!(
"SELECT app_id FROM {REGISTERED_APPS_TABLE_NAME} WHERE team_id = $1 AND active = true"
"SELECT app_id FROM {REGISTERED_APPS_TABLE_NAME} WHERE team_id = $1 AND deactivated_at IS NULL"
);
let apps: Vec<String> = sqlx::query_as(&apps_query)
.bind(team_id)
Expand Down Expand Up @@ -150,7 +151,10 @@ impl Db {
}

// Commit the transaction
tx.commit().await?;
if let Err(err) = tx.commit().await {
error!("Failed to commit transaction: {:?}", err);
return Err(err).map_err(|err| err.into());
}

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/http/cloud/cancel_team_user_invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub async fn cancel_team_user_invite(
));
}

if team.active == false {
if team.deactivated_at != None {
return Err((
StatusCode::BAD_REQUEST,
CloudApiErrors::TeamDoesNotExist.to_string(),
Expand Down
2 changes: 1 addition & 1 deletion server/src/http/cloud/change_user_privileges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub async fn change_user_privileges(
CloudApiErrors::InsufficientPermissions.to_string(),
));
}
if team.active == false {
if team.deactivated_at != None {
return Err((
StatusCode::BAD_REQUEST,
CloudApiErrors::TeamDoesNotExist.to_string(),
Expand Down
Loading

0 comments on commit 6e4cbf5

Please sign in to comment.