-
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(user): setup user tables (#2803)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Sahkal Poddar <[email protected]> Co-authored-by: Sahkal Poddar <[email protected]> Co-authored-by: Sai Harsha Vardhan <[email protected]> Co-authored-by: Venkatesh <[email protected]> Co-authored-by: venkatesh.devendran <[email protected]> Co-authored-by: Abhishek Marrivagu <[email protected]>
- Loading branch information
1 parent
966369b
commit 20c4226
Showing
19 changed files
with
919 additions
and
1 deletion.
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
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,62 @@ | ||
use diesel::{associations::HasTable, ExpressionMethods}; | ||
use error_stack::report; | ||
use router_env::tracing::{self, instrument}; | ||
|
||
use crate::{ | ||
errors::{self}, | ||
query::generics, | ||
schema::users::dsl, | ||
user::*, | ||
PgPooledConn, StorageResult, | ||
}; | ||
|
||
impl UserNew { | ||
#[instrument(skip(conn))] | ||
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<User> { | ||
generics::generic_insert(conn, self).await | ||
} | ||
} | ||
|
||
impl User { | ||
pub async fn find_by_user_email(conn: &PgPooledConn, user_email: &str) -> StorageResult<Self> { | ||
generics::generic_find_one::<<Self as HasTable>::Table, _, _>( | ||
conn, | ||
dsl::email.eq(user_email.to_owned()), | ||
) | ||
.await | ||
} | ||
|
||
pub async fn find_by_user_id(conn: &PgPooledConn, user_id: &str) -> StorageResult<Self> { | ||
generics::generic_find_one::<<Self as HasTable>::Table, _, _>( | ||
conn, | ||
dsl::user_id.eq(user_id.to_owned()), | ||
) | ||
.await | ||
} | ||
|
||
pub async fn update_by_user_id( | ||
conn: &PgPooledConn, | ||
user_id: &str, | ||
user: UserUpdate, | ||
) -> StorageResult<Self> { | ||
generics::generic_update_with_results::<<Self as HasTable>::Table, _, _, _>( | ||
conn, | ||
dsl::user_id.eq(user_id.to_owned()), | ||
UserUpdateInternal::from(user), | ||
) | ||
.await? | ||
.first() | ||
.cloned() | ||
.ok_or_else(|| { | ||
report!(errors::DatabaseError::NotFound).attach_printable("Error while updating user") | ||
}) | ||
} | ||
|
||
pub async fn delete_by_user_id(conn: &PgPooledConn, user_id: &str) -> StorageResult<bool> { | ||
generics::generic_delete::<<Self as HasTable>::Table, _>( | ||
conn, | ||
dsl::user_id.eq(user_id.to_owned()), | ||
) | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods}; | ||
use router_env::tracing::{self, instrument}; | ||
|
||
use crate::{query::generics, schema::user_roles::dsl, user_role::*, PgPooledConn, StorageResult}; | ||
|
||
impl UserRoleNew { | ||
#[instrument(skip(conn))] | ||
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<UserRole> { | ||
generics::generic_insert(conn, self).await | ||
} | ||
} | ||
|
||
impl UserRole { | ||
pub async fn find_by_user_id(conn: &PgPooledConn, user_id: String) -> StorageResult<Self> { | ||
generics::generic_find_one::<<Self as HasTable>::Table, _, _>( | ||
conn, | ||
dsl::user_id.eq(user_id), | ||
) | ||
.await | ||
} | ||
|
||
pub async fn update_by_user_id_merchant_id( | ||
conn: &PgPooledConn, | ||
user_id: String, | ||
merchant_id: String, | ||
update: UserRoleUpdate, | ||
) -> StorageResult<Self> { | ||
generics::generic_update_with_unique_predicate_get_result::< | ||
<Self as HasTable>::Table, | ||
_, | ||
_, | ||
_, | ||
>( | ||
conn, | ||
dsl::user_id | ||
.eq(user_id) | ||
.and(dsl::merchant_id.eq(merchant_id)), | ||
UserRoleUpdateInternal::from(update), | ||
) | ||
.await | ||
} | ||
|
||
pub async fn delete_by_user_id(conn: &PgPooledConn, user_id: String) -> StorageResult<bool> { | ||
generics::generic_delete::<<Self as HasTable>::Table, _>(conn, dsl::user_id.eq(user_id)) | ||
.await | ||
} | ||
|
||
pub async fn list_by_user_id(conn: &PgPooledConn, user_id: String) -> StorageResult<Vec<Self>> { | ||
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>( | ||
conn, | ||
dsl::user_id.eq(user_id), | ||
None, | ||
None, | ||
Some(dsl::created_at.asc()), | ||
) | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use common_utils::pii; | ||
use diesel::{AsChangeset, Identifiable, Insertable, Queryable}; | ||
use masking::Secret; | ||
use time::PrimitiveDateTime; | ||
|
||
use crate::schema::users; | ||
|
||
#[derive(Clone, Debug, Identifiable, Queryable)] | ||
#[diesel(table_name = users)] | ||
pub struct User { | ||
pub id: i32, | ||
pub user_id: String, | ||
pub email: pii::Email, | ||
pub name: Secret<String>, | ||
pub password: Secret<String>, | ||
pub is_verified: bool, | ||
pub created_at: PrimitiveDateTime, | ||
pub last_modified_at: PrimitiveDateTime, | ||
} | ||
|
||
#[derive( | ||
router_derive::Setter, Clone, Debug, Default, Insertable, router_derive::DebugAsDisplay, | ||
)] | ||
#[diesel(table_name = users)] | ||
pub struct UserNew { | ||
pub user_id: String, | ||
pub email: pii::Email, | ||
pub name: Secret<String>, | ||
pub password: Secret<String>, | ||
pub is_verified: bool, | ||
pub created_at: Option<PrimitiveDateTime>, | ||
pub last_modified_at: Option<PrimitiveDateTime>, | ||
} | ||
|
||
#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)] | ||
#[diesel(table_name = users)] | ||
pub struct UserUpdateInternal { | ||
name: Option<String>, | ||
password: Option<Secret<String>>, | ||
is_verified: Option<bool>, | ||
last_modified_at: PrimitiveDateTime, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum UserUpdate { | ||
VerifyUser, | ||
AccountUpdate { | ||
name: Option<String>, | ||
password: Option<Secret<String>>, | ||
is_verified: Option<bool>, | ||
}, | ||
} | ||
|
||
impl From<UserUpdate> for UserUpdateInternal { | ||
fn from(user_update: UserUpdate) -> Self { | ||
let last_modified_at = common_utils::date_time::now(); | ||
match user_update { | ||
UserUpdate::VerifyUser => Self { | ||
name: None, | ||
password: None, | ||
is_verified: Some(true), | ||
last_modified_at, | ||
}, | ||
UserUpdate::AccountUpdate { | ||
name, | ||
password, | ||
is_verified, | ||
} => Self { | ||
name, | ||
password, | ||
is_verified, | ||
last_modified_at, | ||
}, | ||
} | ||
} | ||
} |
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,79 @@ | ||
use diesel::{AsChangeset, Identifiable, Insertable, Queryable}; | ||
use time::PrimitiveDateTime; | ||
|
||
use crate::{enums, schema::user_roles}; | ||
|
||
#[derive(Clone, Debug, Identifiable, Queryable)] | ||
#[diesel(table_name = user_roles)] | ||
pub struct UserRole { | ||
pub id: i32, | ||
pub user_id: String, | ||
pub merchant_id: String, | ||
pub role_id: String, | ||
pub org_id: String, | ||
pub status: enums::UserStatus, | ||
pub created_by: String, | ||
pub last_modified_by: String, | ||
pub created_at: PrimitiveDateTime, | ||
pub last_modified_at: PrimitiveDateTime, | ||
} | ||
|
||
#[derive(router_derive::Setter, Clone, Debug, Insertable, router_derive::DebugAsDisplay)] | ||
#[diesel(table_name = user_roles)] | ||
pub struct UserRoleNew { | ||
pub user_id: String, | ||
pub merchant_id: String, | ||
pub role_id: String, | ||
pub org_id: String, | ||
pub status: enums::UserStatus, | ||
pub created_by: String, | ||
pub last_modified_by: String, | ||
pub created_at: PrimitiveDateTime, | ||
pub last_modified_at: PrimitiveDateTime, | ||
} | ||
|
||
#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)] | ||
#[diesel(table_name = user_roles)] | ||
pub struct UserRoleUpdateInternal { | ||
role_id: Option<String>, | ||
status: Option<enums::UserStatus>, | ||
last_modified_by: Option<String>, | ||
last_modified_at: PrimitiveDateTime, | ||
} | ||
|
||
pub enum UserRoleUpdate { | ||
UpdateStatus { | ||
status: enums::UserStatus, | ||
modified_by: String, | ||
}, | ||
UpdateRole { | ||
role_id: String, | ||
modified_by: String, | ||
}, | ||
} | ||
|
||
impl From<UserRoleUpdate> for UserRoleUpdateInternal { | ||
fn from(value: UserRoleUpdate) -> Self { | ||
let last_modified_at = common_utils::date_time::now(); | ||
match value { | ||
UserRoleUpdate::UpdateRole { | ||
role_id, | ||
modified_by, | ||
} => Self { | ||
role_id: Some(role_id), | ||
last_modified_by: Some(modified_by), | ||
status: None, | ||
last_modified_at, | ||
}, | ||
UserRoleUpdate::UpdateStatus { | ||
status, | ||
modified_by, | ||
} => Self { | ||
status: Some(status), | ||
last_modified_at, | ||
last_modified_by: Some(modified_by), | ||
role_id: None, | ||
}, | ||
} | ||
} | ||
} |
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
Oops, something went wrong.