-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(organization): add organization table (#2669)
- Loading branch information
1 parent
23bd364
commit d682471
Showing
15 changed files
with
282 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
pub struct OrganizationNew { | ||
pub org_id: String, | ||
pub org_name: Option<String>, | ||
} | ||
|
||
impl OrganizationNew { | ||
pub fn new(org_name: Option<String>) -> Self { | ||
Self { | ||
org_id: common_utils::generate_id_with_default_len("org"), | ||
org_name, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use diesel::{AsChangeset, Identifiable, Insertable, Queryable}; | ||
|
||
use crate::schema::organization; | ||
|
||
#[derive(Clone, Debug, Identifiable, Queryable)] | ||
#[diesel(table_name = organization, primary_key(org_id))] | ||
pub struct Organization { | ||
pub org_id: String, | ||
pub org_name: Option<String>, | ||
} | ||
|
||
#[derive(Clone, Debug, Insertable)] | ||
#[diesel(table_name = organization, primary_key(org_id))] | ||
pub struct OrganizationNew { | ||
pub org_id: String, | ||
pub org_name: Option<String>, | ||
} | ||
|
||
#[derive(Clone, Debug, AsChangeset)] | ||
#[diesel(table_name = organization)] | ||
pub struct OrganizationUpdateInternal { | ||
org_name: Option<String>, | ||
} | ||
|
||
pub enum OrganizationUpdate { | ||
Update { org_name: Option<String> }, | ||
} | ||
|
||
impl From<OrganizationUpdate> for OrganizationUpdateInternal { | ||
fn from(value: OrganizationUpdate) -> Self { | ||
match value { | ||
OrganizationUpdate::Update { org_name } => Self { org_name }, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use diesel::{associations::HasTable, ExpressionMethods}; | ||
use router_env::tracing::{self, instrument}; | ||
|
||
use crate::{ | ||
organization::*, query::generics, schema::organization::dsl, PgPooledConn, StorageResult, | ||
}; | ||
|
||
impl OrganizationNew { | ||
#[instrument(skip(conn))] | ||
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<Organization> { | ||
generics::generic_insert(conn, self).await | ||
} | ||
} | ||
|
||
impl Organization { | ||
pub async fn find_by_org_id(conn: &PgPooledConn, org_id: String) -> StorageResult<Self> { | ||
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(conn, dsl::org_id.eq(org_id)) | ||
.await | ||
} | ||
|
||
pub async fn update_by_org_id( | ||
conn: &PgPooledConn, | ||
org_id: String, | ||
update: OrganizationUpdate, | ||
) -> StorageResult<Self> { | ||
generics::generic_update_with_unique_predicate_get_result::< | ||
<Self as HasTable>::Table, | ||
_, | ||
_, | ||
_, | ||
>( | ||
conn, | ||
dsl::org_id.eq(org_id), | ||
OrganizationUpdateInternal::from(update), | ||
) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use common_utils::errors::CustomResult; | ||
use diesel_models::organization as storage; | ||
use error_stack::IntoReport; | ||
|
||
use crate::{connection, core::errors, services::Store}; | ||
|
||
#[async_trait::async_trait] | ||
pub trait OrganizationInterface { | ||
async fn insert_organization( | ||
&self, | ||
organization: storage::OrganizationNew, | ||
) -> CustomResult<storage::Organization, errors::StorageError>; | ||
|
||
async fn find_organization_by_org_id( | ||
&self, | ||
org_id: &str, | ||
) -> CustomResult<storage::Organization, errors::StorageError>; | ||
|
||
async fn update_organization_by_org_id( | ||
&self, | ||
user_id: &str, | ||
update: storage::OrganizationUpdate, | ||
) -> CustomResult<storage::Organization, errors::StorageError>; | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl OrganizationInterface for Store { | ||
async fn insert_organization( | ||
&self, | ||
organization: storage::OrganizationNew, | ||
) -> CustomResult<storage::Organization, errors::StorageError> { | ||
let conn = connection::pg_connection_write(self).await?; | ||
organization | ||
.insert(&conn) | ||
.await | ||
.map_err(Into::into) | ||
.into_report() | ||
} | ||
|
||
async fn find_organization_by_org_id( | ||
&self, | ||
org_id: &str, | ||
) -> CustomResult<storage::Organization, errors::StorageError> { | ||
let conn = connection::pg_connection_read(self).await?; | ||
storage::Organization::find_by_org_id(&conn, org_id.to_string()) | ||
.await | ||
.map_err(Into::into) | ||
.into_report() | ||
} | ||
|
||
async fn update_organization_by_org_id( | ||
&self, | ||
org_id: &str, | ||
update: storage::OrganizationUpdate, | ||
) -> CustomResult<storage::Organization, errors::StorageError> { | ||
let conn = connection::pg_connection_write(self).await?; | ||
|
||
storage::Organization::update_by_org_id(&conn, org_id.to_string(), update) | ||
.await | ||
.map_err(Into::into) | ||
.into_report() | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl OrganizationInterface for super::MockDb { | ||
async fn insert_organization( | ||
&self, | ||
organization: storage::OrganizationNew, | ||
) -> CustomResult<storage::Organization, errors::StorageError> { | ||
let mut organizations = self.organizations.lock().await; | ||
|
||
if organizations | ||
.iter() | ||
.any(|org| org.org_id == organization.org_id) | ||
{ | ||
Err(errors::StorageError::DuplicateValue { | ||
entity: "org_id", | ||
key: None, | ||
})? | ||
} | ||
let org = storage::Organization { | ||
org_id: organization.org_id.clone(), | ||
org_name: organization.org_name, | ||
}; | ||
organizations.push(org.clone()); | ||
Ok(org) | ||
} | ||
|
||
async fn find_organization_by_org_id( | ||
&self, | ||
org_id: &str, | ||
) -> CustomResult<storage::Organization, errors::StorageError> { | ||
let organizations = self.organizations.lock().await; | ||
|
||
organizations | ||
.iter() | ||
.find(|org| org.org_id == org_id) | ||
.cloned() | ||
.ok_or( | ||
errors::StorageError::ValueNotFound(format!( | ||
"No organization available for org_id = {org_id}" | ||
)) | ||
.into(), | ||
) | ||
} | ||
|
||
async fn update_organization_by_org_id( | ||
&self, | ||
org_id: &str, | ||
update: storage::OrganizationUpdate, | ||
) -> CustomResult<storage::Organization, errors::StorageError> { | ||
let mut organizations = self.organizations.lock().await; | ||
|
||
organizations | ||
.iter_mut() | ||
.find(|org| org.org_id == org_id) | ||
.map(|org| match &update { | ||
storage::OrganizationUpdate::Update { org_name } => storage::Organization { | ||
org_name: org_name.clone(), | ||
..org.to_owned() | ||
}, | ||
}) | ||
.ok_or( | ||
errors::StorageError::ValueNotFound(format!( | ||
"No organization available for org_id = {org_id}" | ||
)) | ||
.into(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE IF EXISTS ORGANIZATION; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- Your SQL goes here | ||
CREATE TABLE IF NOT EXISTS ORGANIZATION ( | ||
org_id VARCHAR(32) PRIMARY KEY NOT NULL, | ||
org_name TEXT | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters